securenow 6.0.2 → 7.0.0-anas

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. package/CONSUMING-APPS-GUIDE.md +455 -0
  2. package/NPM_README.md +2029 -0
  3. package/README.md +297 -40
  4. package/SKILL-API.md +634 -0
  5. package/SKILL-CLI.md +454 -0
  6. package/app-config.js +130 -0
  7. package/cidr.js +83 -0
  8. package/cli/apps.js +608 -0
  9. package/cli/auth.js +298 -0
  10. package/cli/client.js +115 -0
  11. package/cli/config.js +202 -0
  12. package/cli/diagnostics.js +387 -0
  13. package/cli/firewall.js +100 -0
  14. package/cli/fp.js +638 -0
  15. package/cli/init.js +201 -0
  16. package/cli/monitor.js +440 -0
  17. package/cli/run.js +148 -0
  18. package/cli/security.js +980 -0
  19. package/cli/ui.js +386 -0
  20. package/cli/utils.js +127 -0
  21. package/cli.js +469 -455
  22. package/console-instrumentation.js +147 -136
  23. package/docs/ALL-FRAMEWORKS-QUICKSTART.md +1377 -455
  24. package/docs/API-KEYS-GUIDE.md +233 -0
  25. package/docs/ARCHITECTURE.md +3 -3
  26. package/docs/AUTO-BODY-CAPTURE.md +1 -1
  27. package/docs/AUTO-SETUP-SUMMARY.md +331 -0
  28. package/docs/AUTO-SETUP.md +4 -4
  29. package/docs/AUTOMATIC-IP-CAPTURE.md +5 -5
  30. package/docs/BODY-CAPTURE-FIX.md +261 -0
  31. package/docs/BODY-CAPTURE-QUICKSTART.md +2 -2
  32. package/docs/CHANGELOG-NEXTJS.md +1 -35
  33. package/docs/COMPLETION-REPORT.md +408 -0
  34. package/docs/CUSTOMER-GUIDE.md +16 -16
  35. package/docs/EASIEST-SETUP.md +5 -5
  36. package/docs/ENVIRONMENT-VARIABLES.md +880 -652
  37. package/docs/EXPRESS-BODY-CAPTURE.md +13 -12
  38. package/docs/EXPRESS-SETUP-GUIDE.md +719 -720
  39. package/docs/FINAL-SOLUTION.md +335 -0
  40. package/docs/FIREWALL-GUIDE.md +426 -0
  41. package/docs/IMPLEMENTATION-SUMMARY.md +410 -0
  42. package/docs/INDEX.md +22 -4
  43. package/docs/LOGGING-GUIDE.md +701 -708
  44. package/docs/LOGGING-QUICKSTART.md +234 -255
  45. package/docs/NEXTJS-BODY-CAPTURE-COMPARISON.md +323 -0
  46. package/docs/NEXTJS-BODY-CAPTURE.md +2 -2
  47. package/docs/NEXTJS-GUIDE.md +14 -14
  48. package/docs/NEXTJS-QUICKSTART.md +1 -1
  49. package/docs/NEXTJS-SETUP-COMPLETE.md +795 -0
  50. package/docs/NEXTJS-WRAPPER-APPROACH.md +1 -1
  51. package/docs/NUXT-GUIDE.md +166 -0
  52. package/docs/QUICKSTART-BODY-CAPTURE.md +2 -2
  53. package/docs/REDACTION-EXAMPLES.md +1 -1
  54. package/docs/REQUEST-BODY-CAPTURE.md +19 -10
  55. package/docs/SOLUTION-SUMMARY.md +312 -0
  56. package/docs/VERCEL-OTEL-MIGRATION.md +3 -3
  57. package/examples/README.md +6 -6
  58. package/examples/instrumentation-with-auto-capture.ts +1 -1
  59. package/examples/nextjs-env-example.txt +2 -2
  60. package/examples/nextjs-instrumentation.js +1 -1
  61. package/examples/nextjs-instrumentation.ts +1 -1
  62. package/examples/nextjs-with-logging-example.md +6 -6
  63. package/examples/nextjs-with-options.ts +1 -1
  64. package/examples/test-nextjs-setup.js +1 -1
  65. package/firewall-cloud.js +212 -0
  66. package/firewall-iptables.js +139 -0
  67. package/firewall-only.js +38 -0
  68. package/firewall-tcp.js +74 -0
  69. package/firewall.js +720 -0
  70. package/free-trial-banner.js +174 -0
  71. package/nextjs-auto-capture.js +198 -207
  72. package/nextjs-middleware.js +186 -181
  73. package/nextjs-webpack-config.js +88 -53
  74. package/nextjs-wrapper.js +158 -158
  75. package/nextjs.d.ts +1 -1
  76. package/nextjs.js +638 -647
  77. package/nuxt-server-plugin.mjs +425 -0
  78. package/nuxt.d.ts +60 -0
  79. package/nuxt.mjs +75 -0
  80. package/package.json +172 -164
  81. package/postinstall.js +42 -14
  82. package/register.d.ts +1 -1
  83. package/register.js +39 -4
  84. package/resolve-ip.js +77 -0
  85. package/tracing.d.ts +2 -1
  86. package/tracing.js +318 -45
  87. package/web-vite.mjs +239 -156
  88. package/LICENSE +0 -15
@@ -0,0 +1,455 @@
1
+ # How to Use SecureNow Logging in Your App
2
+
3
+ This guide is for developers who want to add the `securenow` package to their applications to enable logging to SecureNow or any OTLP-compatible backend.
4
+
5
+ **Since v5.6.0:** When `SECURENOW_LOGGING_ENABLED=1`, `console.log`, `console.warn`, `console.error`, `console.info`, and `console.debug` are **automatically** forwarded as OTLP log records after you load `securenow/register`. A separate `require('securenow/console-instrumentation')` is no longer required (the module remains for backward compatibility).
6
+
7
+ ---
8
+
9
+ ## Installation
10
+
11
+ ```bash
12
+ npm install securenow
13
+ ```
14
+
15
+ ---
16
+
17
+ ## Setup Steps
18
+
19
+ ### Step 1: Configure Environment Variables
20
+
21
+ Add these to your `.env` file or export them:
22
+
23
+ ```bash
24
+ # Required: Enable logging
25
+ SECURENOW_LOGGING_ENABLED=1
26
+
27
+ # Required: Your app name
28
+ SECURENOW_APPID=my-app-name
29
+
30
+ # Required: Your OTLP endpoint
31
+ SECURENOW_INSTANCE=http://your-otlp-collector:4318
32
+
33
+ # For managed OTLP / authentication (optional):
34
+ # OTEL_EXPORTER_OTLP_HEADERS="x-api-key=your-key"
35
+ ```
36
+
37
+ ---
38
+
39
+ ### Step 2: Choose Your Integration Method
40
+
41
+ You have **three options** to integrate logging:
42
+
43
+ #### **Option A: Automatic Console Instrumentation** (Recommended - Easiest)
44
+
45
+ Since **v5.6.0**, when `SECURENOW_LOGGING_ENABLED=1`, all `console.log()`, `console.info()`, `console.warn()`, `console.error()`, and `console.debug()` calls are **automatically** captured and sent as OTLP log records. No extra require is needed.
46
+
47
+ **Add to your main application file:**
48
+
49
+ ```javascript
50
+ // At the very top of your app.js, index.js, server.js, or main.ts
51
+ require('securenow/register');
52
+
53
+ // That's it! With SECURENOW_LOGGING_ENABLED=1, all console calls become OTLP logs.
54
+ console.log('Application started');
55
+ console.error('An error occurred', { userId: 123, details: 'Something went wrong' });
56
+ console.warn('Warning message');
57
+ ```
58
+
59
+ **Or using NODE_OPTIONS (no code changes needed):**
60
+
61
+ ```bash
62
+ NODE_OPTIONS="-r securenow/register" node app.js
63
+ ```
64
+
65
+ ---
66
+
67
+ #### **Option B: Direct Logger API**
68
+
69
+ For more control over logging, use the OpenTelemetry logger API directly:
70
+
71
+ ```javascript
72
+ require('securenow/register');
73
+ const { getLogger } = require('securenow/tracing');
74
+
75
+ // Get a logger instance
76
+ const logger = getLogger('my-service', '1.0.0');
77
+
78
+ // Emit structured logs
79
+ logger.emit({
80
+ severityNumber: 9, // INFO level
81
+ severityText: 'INFO',
82
+ body: 'User logged in',
83
+ attributes: {
84
+ userId: 123,
85
+ username: 'john',
86
+ ip: '192.168.1.1',
87
+ },
88
+ });
89
+ ```
90
+
91
+ **Severity Levels:**
92
+ - `5` = DEBUG
93
+ - `9` = INFO
94
+ - `13` = WARN
95
+ - `17` = ERROR
96
+
97
+ ---
98
+
99
+ #### **Option C: Custom Logger Wrapper**
100
+
101
+ Create your own logger wrapper for cleaner API:
102
+
103
+ ```javascript
104
+ // logger.js
105
+ require('securenow/register');
106
+ const { getLogger } = require('securenow/tracing');
107
+
108
+ const logger = getLogger('app-logger', '1.0.0');
109
+
110
+ const SeverityNumber = {
111
+ DEBUG: 5,
112
+ INFO: 9,
113
+ WARN: 13,
114
+ ERROR: 17,
115
+ };
116
+
117
+ function log(level, message, attributes = {}) {
118
+ if (logger) {
119
+ logger.emit({
120
+ severityNumber: SeverityNumber[level],
121
+ severityText: level,
122
+ body: message,
123
+ attributes,
124
+ });
125
+ }
126
+ console.log(`[${level}] ${message}`, attributes);
127
+ }
128
+
129
+ module.exports = {
130
+ debug: (msg, attrs) => log('DEBUG', msg, attrs),
131
+ info: (msg, attrs) => log('INFO', msg, attrs),
132
+ warn: (msg, attrs) => log('WARN', msg, attrs),
133
+ error: (msg, attrs) => log('ERROR', msg, attrs),
134
+ };
135
+ ```
136
+
137
+ **Usage:**
138
+
139
+ ```javascript
140
+ const logger = require('./logger');
141
+
142
+ logger.info('User signed up', { userId: 456, email: 'user@example.com' });
143
+ logger.error('Payment failed', { orderId: 789, amount: 99.99 });
144
+ ```
145
+
146
+ ---
147
+
148
+ ## Framework-Specific Setup
149
+
150
+ ### Express.js
151
+
152
+ ```javascript
153
+ // app.js
154
+ require('securenow/register');
155
+
156
+ const express = require('express');
157
+ const app = express();
158
+
159
+ app.get('/', (req, res) => {
160
+ console.log('Home page accessed');
161
+ res.send('Hello World');
162
+ });
163
+
164
+ app.listen(3000, () => {
165
+ console.log('Server running on port 3000');
166
+ });
167
+ ```
168
+
169
+ **Run:**
170
+ ```bash
171
+ SECURENOW_LOGGING_ENABLED=1 SECURENOW_APPID=express-app node app.js
172
+ ```
173
+
174
+ ---
175
+
176
+ ### Next.js (App Router)
177
+
178
+ **1. Run `npx securenow init`** or create `instrumentation.ts` in your project root:
179
+
180
+ ```typescript
181
+ // instrumentation.ts
182
+ export async function register() {
183
+ if (process.env.NEXT_RUNTIME === 'nodejs') {
184
+ const { registerSecureNow } = require('securenow/nextjs');
185
+ registerSecureNow();
186
+ }
187
+ }
188
+ ```
189
+
190
+ **2. Update `next.config.js` with `withSecureNow()`:**
191
+
192
+ ```javascript
193
+ const { withSecureNow } = require('securenow/nextjs-webpack-config');
194
+
195
+ module.exports = withSecureNow({
196
+ // your existing config
197
+ });
198
+ ```
199
+
200
+ This auto-detects Next.js 14 vs 15 and sets the correct `serverExternalPackages` / `experimental.serverComponentsExternalPackages` config.
201
+
202
+ **3. Add to `.env.local`:**
203
+
204
+ ```bash
205
+ SECURENOW_LOGGING_ENABLED=1
206
+ SECURENOW_APPID=my-nextjs-app
207
+ SECURENOW_INSTANCE=http://localhost:4318
208
+ SECURENOW_API_KEY=snk_live_abc123...
209
+ ```
210
+
211
+ **4. Use in API routes:**
212
+
213
+ ```typescript
214
+ // app/api/users/route.ts
215
+ export async function GET() {
216
+ console.log('GET /api/users called');
217
+ const users = await fetchUsers();
218
+ console.info('Users fetched', { count: users.length });
219
+ return Response.json(users);
220
+ }
221
+ ```
222
+
223
+ ---
224
+
225
+ ### Fastify
226
+
227
+ ```javascript
228
+ // server.js
229
+ require('securenow/register');
230
+
231
+ const fastify = require('fastify')();
232
+
233
+ fastify.get('/', async () => {
234
+ console.log('Root endpoint called');
235
+ return { hello: 'world' };
236
+ });
237
+
238
+ fastify.listen({ port: 3000 });
239
+ ```
240
+
241
+ ---
242
+
243
+ ### NestJS
244
+
245
+ ```typescript
246
+ // main.ts
247
+ require('securenow/register');
248
+
249
+ import { NestFactory } from '@nestjs/core';
250
+ import { AppModule } from './app.module';
251
+
252
+ async function bootstrap() {
253
+ const app = await NestFactory.create(AppModule);
254
+ console.log('NestJS application starting');
255
+ await app.listen(3000);
256
+ }
257
+
258
+ bootstrap();
259
+ ```
260
+
261
+ ---
262
+
263
+ ## Verification
264
+
265
+ After starting your app, you should see:
266
+
267
+ ```
268
+ [securenow] OTel SDK started → http://your-otlp-collector:4318/v1/traces
269
+ [securenow] 📋 Logging: ENABLED → http://your-otlp-collector:4318/v1/logs
270
+ [securenow] Console instrumentation installed
271
+ ```
272
+
273
+ ---
274
+
275
+ ## View Logs in SecureNow
276
+
277
+ 1. Open your SecureNow dashboard
278
+ 2. Go to **Logs** section
279
+ 3. Filter by `service.name = my-app-name`
280
+ 4. See all your logs with:
281
+ - Automatic severity levels
282
+ - Structured attributes
283
+ - Trace correlation
284
+
285
+ ---
286
+
287
+ ## Common Issues
288
+
289
+ ### Logs Not Appearing
290
+
291
+ **Check 1:** Verify `SECURENOW_LOGGING_ENABLED=1` is set
292
+
293
+ ```bash
294
+ echo $SECURENOW_LOGGING_ENABLED # Should output: 1
295
+ ```
296
+
297
+ **Check 2:** Verify endpoint is correct
298
+
299
+ ```bash
300
+ # Self-hosted OTLP collector
301
+ export SECURENOW_INSTANCE=http://localhost:4318
302
+
303
+ # Managed OTLP (example)
304
+ export SECURENOW_INSTANCE=https://ingest.<region>.securenow.ai:443
305
+ export OTEL_EXPORTER_OTLP_HEADERS="x-api-key=<your-key>"
306
+ ```
307
+
308
+ **Check 3:** Enable debug logging
309
+
310
+ ```bash
311
+ export OTEL_LOG_LEVEL=debug
312
+ node app.js
313
+ ```
314
+
315
+ ---
316
+
317
+ ### Console Instrumentation Not Working
318
+
319
+ Make sure the load order is correct:
320
+
321
+ ```javascript
322
+ // ✅ Correct — register must be first
323
+ require('securenow/register');
324
+ const express = require('express');
325
+
326
+ // ❌ Wrong
327
+ const express = require('express');
328
+ require('securenow/register');
329
+ ```
330
+
331
+ ---
332
+
333
+ ### Logger Returns Null
334
+
335
+ This happens when logging is disabled:
336
+
337
+ ```javascript
338
+ const { getLogger } = require('securenow/tracing');
339
+ const logger = getLogger('test');
340
+
341
+ if (!logger) {
342
+ console.log('Set SECURENOW_LOGGING_ENABLED=1 to enable logging');
343
+ }
344
+ ```
345
+
346
+ ---
347
+
348
+ ## Environment Variables Reference
349
+
350
+ ```bash
351
+ # Logging
352
+ SECURENOW_LOGGING_ENABLED=1 # Enable/disable logging (default: 1)
353
+
354
+ # Connection
355
+ SECURENOW_INSTANCE=http://localhost:4318 # OTLP endpoint
356
+ OTEL_EXPORTER_OTLP_LOGS_ENDPOINT=... # Override logs endpoint
357
+
358
+ # Authentication
359
+ OTEL_EXPORTER_OTLP_HEADERS="x-api-key=KEY"
360
+
361
+ # Service Info
362
+ SECURENOW_APPID=my-app # Your app name
363
+ OTEL_SERVICE_NAME=my-app # Alternative
364
+
365
+ # Request Body Capture
366
+ SECURENOW_CAPTURE_BODY=1 # Capture JSON/form/GraphQL request bodies
367
+ SECURENOW_MAX_BODY_SIZE=10240 # Max body size in bytes (default: 10KB)
368
+ SECURENOW_SENSITIVE_FIELDS="field1,field2" # Additional fields to redact
369
+
370
+ # Multipart Body Capture (v5.8.0+)
371
+ SECURENOW_CAPTURE_MULTIPART=1 # Capture multipart field values & file metadata (streaming)
372
+
373
+ # Debugging
374
+ OTEL_LOG_LEVEL=debug # Enable debug output
375
+ ```
376
+
377
+ ---
378
+
379
+ ## Best Practices
380
+
381
+ 1. **Use Structured Logging** - Pass objects with meaningful attributes
382
+ ```javascript
383
+ console.log('User action', { userId: 123, action: 'login' });
384
+ ```
385
+
386
+ 2. **Choose Appropriate Severity Levels**
387
+ - `console.log()` / `console.info()` - Normal operations
388
+ - `console.warn()` - Warnings, deprecations
389
+ - `console.error()` - Errors, exceptions
390
+
391
+ 3. **Include Context** - Add userId, requestId, etc. to log attributes
392
+
393
+ 4. **Don't Log Sensitive Data** - SecureNow automatically redacts passwords, tokens, etc.
394
+
395
+ 5. **Use Different Loggers for Different Modules**
396
+ ```javascript
397
+ const authLogger = getLogger('auth-service');
398
+ const dbLogger = getLogger('database');
399
+ ```
400
+
401
+ ---
402
+
403
+ ---
404
+
405
+ ## Firewall — Automatic IP Blocking
406
+
407
+ If you use the SecureNow blocklist to block malicious IPs, the firewall module can enforce that blocklist directly in your app with zero code changes.
408
+
409
+ ### Enable It
410
+
411
+ Add your API key to `.env`:
412
+
413
+ ```bash
414
+ SECURENOW_API_KEY=snk_live_abc123...
415
+ ```
416
+
417
+ The firewall activates automatically on startup and syncs the blocklist using a version-based protocol:
418
+
419
+ ```
420
+ [securenow] Firewall: ENABLED
421
+ [securenow] Firewall: Layer 1 (HTTP 403) active
422
+ [securenow] Firewall: synced 142 blocked IPs (138 exact + 4 CIDR ranges)
423
+ ```
424
+
425
+ Blocked IPs get a 403 Forbidden with a full security alert page. Changes propagate in 10-15 seconds. No code changes needed -- works with Express, Next.js, Nuxt, Fastify, and all Node.js frameworks.
426
+
427
+ **Firewall-only mode** (no tracing overhead):
428
+
429
+ ```bash
430
+ node -r securenow/firewall-only app.js
431
+ ```
432
+
433
+ See the [Firewall Guide](./docs/FIREWALL-GUIDE.md) for advanced layers (TCP blocking, iptables, Cloud WAF).
434
+
435
+ ---
436
+
437
+ ## Complete Documentation
438
+
439
+ - [Firewall Guide](./docs/FIREWALL-GUIDE.md)
440
+ - [API Keys Guide](./docs/API-KEYS-GUIDE.md)
441
+ - [Logging Quick Start](./docs/LOGGING-QUICKSTART.md)
442
+ - [Logging Complete Guide](./docs/LOGGING-GUIDE.md)
443
+ - [All Examples](./examples/)
444
+
445
+ ---
446
+
447
+ ## Support
448
+
449
+ - **Documentation**: [Full Docs](./docs/INDEX.md)
450
+ - **Website**: [securenow.ai](http://securenow.ai/)
451
+ - **Issues**: GitHub Issues
452
+
453
+ ---
454
+
455
+ **That's it!** Your app is now sending logs to your OTLP backend (for example SecureNow). 🎉