securenow 4.0.2 → 4.0.5

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.
@@ -0,0 +1,368 @@
1
+ # Next.js Body Capture - Self-Sufficient Solution
2
+
3
+ ## 🚀 Enable Body Capture in Next.js (2 Steps)
4
+
5
+ ### Step 1: Enable in Environment
6
+
7
+ ```bash
8
+ # .env.local
9
+ SECURENOW_CAPTURE_BODY=1
10
+ ```
11
+
12
+ ### Step 2: Create middleware.ts
13
+
14
+ **Just ONE line!** Create `middleware.ts` in your project root:
15
+
16
+ ```typescript
17
+ export { middleware } from 'securenow/nextjs-middleware';
18
+
19
+ export const config = {
20
+ matcher: '/api/:path*', // Apply to API routes
21
+ };
22
+ ```
23
+
24
+ **That's it!** 🎉 Bodies are now captured with sensitive data automatically redacted.
25
+
26
+ ---
27
+
28
+ ## ✨ Why This Works
29
+
30
+ **Self-sufficient design:**
31
+ - ✅ **One-line import** - No code to write
32
+ - ✅ **All logic in package** - Redaction, parsing, size limits
33
+ - ✅ **Zero configuration** - Works with defaults
34
+ - ✅ **Doesn't lock request stream** - Uses `request.clone()`
35
+ - ✅ **Safe by default** - 20+ sensitive fields redacted
36
+
37
+ **No customer code changes needed!** Just import and configure where to apply.
38
+
39
+ ---
40
+
41
+ ## 📊 What Gets Captured
42
+
43
+ ### ✅ JSON Requests
44
+
45
+ ```json
46
+ POST /api/users
47
+ Content-Type: application/json
48
+
49
+ {
50
+ "username": "john",
51
+ "password": "secret123" ← Automatically redacted
52
+ }
53
+ ```
54
+
55
+ **Captured:**
56
+ ```json
57
+ {
58
+ "username": "john",
59
+ "password": "[REDACTED]"
60
+ }
61
+ ```
62
+
63
+ ### ✅ GraphQL Requests
64
+
65
+ ```graphql
66
+ POST /api/graphql
67
+ Content-Type: application/graphql
68
+
69
+ mutation Login {
70
+ login(email: "john@example.com", password: "secret") ← Redacted
71
+ }
72
+ ```
73
+
74
+ **Captured:**
75
+ ```graphql
76
+ mutation Login {
77
+ login(email: "john@example.com", password: "[REDACTED]")
78
+ }
79
+ ```
80
+
81
+ ### ✅ Form Data
82
+
83
+ ```http
84
+ POST /api/contact
85
+ Content-Type: application/x-www-form-urlencoded
86
+
87
+ name=John&email=john@example.com&message=Hello
88
+ ```
89
+
90
+ **Captured:**
91
+ ```json
92
+ {
93
+ "name": "John",
94
+ "email": "john@example.com",
95
+ "message": "Hello"
96
+ }
97
+ ```
98
+
99
+ ---
100
+
101
+ ## ⚙️ Configuration
102
+
103
+ ### Matcher Patterns
104
+
105
+ **Apply to all API routes:**
106
+ ```typescript
107
+ export const config = {
108
+ matcher: '/api/:path*',
109
+ };
110
+ ```
111
+
112
+ **Apply to specific routes:**
113
+ ```typescript
114
+ export const config = {
115
+ matcher: ['/api/login', '/api/register', '/api/graphql'],
116
+ };
117
+ ```
118
+
119
+ **Apply to everything except static files:**
120
+ ```typescript
121
+ export const config = {
122
+ matcher: '/((?!_next/static|_next/image|favicon.ico).*)',
123
+ };
124
+ ```
125
+
126
+ **Apply to multiple patterns:**
127
+ ```typescript
128
+ export const config = {
129
+ matcher: ['/api/:path*', '/graphql'],
130
+ };
131
+ ```
132
+
133
+ ### Environment Variables
134
+
135
+ ```bash
136
+ # Max body size (default: 10KB)
137
+ SECURENOW_MAX_BODY_SIZE=20480
138
+
139
+ # Custom sensitive fields to redact
140
+ SECURENOW_SENSITIVE_FIELDS=email,phone,address
141
+
142
+ # Enable debug logging
143
+ OTEL_LOG_LEVEL=debug
144
+ ```
145
+
146
+ ---
147
+
148
+ ## 🔒 Security Features
149
+
150
+ ### Automatic Redaction (20+ Fields)
151
+
152
+ These are **always redacted**:
153
+ ```
154
+ password, passwd, pwd, secret, token, api_key, apikey,
155
+ access_token, auth, credentials, mysql_pwd, stripeToken,
156
+ card, cardnumber, cvv, cvc, ccv, ssn, pin
157
+ ```
158
+
159
+ ### Custom Sensitive Fields
160
+
161
+ Add your own:
162
+ ```bash
163
+ SECURENOW_SENSITIVE_FIELDS=credit_card,phone_number,dob
164
+ ```
165
+
166
+ Now these are also redacted automatically!
167
+
168
+ ### Size Protection
169
+
170
+ Bodies larger than `SECURENOW_MAX_BODY_SIZE` show:
171
+ ```
172
+ [TOO LARGE: 50000 bytes]
173
+ ```
174
+
175
+ ---
176
+
177
+ ## 🎯 File Structure
178
+
179
+ ```
180
+ your-nextjs-app/
181
+ ├── middleware.ts ← Create this (1 line import!)
182
+ ├── instrumentation.ts ← Already created by installer
183
+ ├── .env.local ← Set SECURENOW_CAPTURE_BODY=1
184
+ ├── app/
185
+ │ └── api/
186
+ │ ├── login/route.ts ← Bodies auto-captured
187
+ │ ├── users/route.ts ← Bodies auto-captured
188
+ │ └── graphql/route.ts ← Bodies auto-captured
189
+ └── ...
190
+ ```
191
+
192
+ ---
193
+
194
+ ## 🧪 Testing
195
+
196
+ ### Test Body Capture
197
+
198
+ 1. **Create middleware.ts:**
199
+ ```typescript
200
+ export { middleware } from 'securenow/nextjs-middleware';
201
+ export const config = { matcher: '/api/:path*' };
202
+ ```
203
+
204
+ 2. **Enable in .env.local:**
205
+ ```bash
206
+ SECURENOW_CAPTURE_BODY=1
207
+ ```
208
+
209
+ 3. **Make a test request:**
210
+ ```bash
211
+ curl -X POST http://localhost:3000/api/test \
212
+ -H "Content-Type: application/json" \
213
+ -d '{"username":"test","password":"secret123"}'
214
+ ```
215
+
216
+ 4. **Check SigNoz:**
217
+ - Find the `/api/test` trace
218
+ - Look for `http.request.body` attribute
219
+ - Verify password shows `[REDACTED]`
220
+
221
+ ### Debug Mode
222
+
223
+ ```bash
224
+ OTEL_LOG_LEVEL=debug npm run dev
225
+ ```
226
+
227
+ You'll see:
228
+ ```
229
+ [securenow] 📝 Body capture enabled
230
+ [securenow] 📚 Import middleware from securenow/nextjs-middleware
231
+ ```
232
+
233
+ ---
234
+
235
+ ## 🎓 Complete Setup Example
236
+
237
+ ### 1. instrumentation.ts (auto-created by installer)
238
+
239
+ ```typescript
240
+ import { registerSecureNow } from 'securenow/nextjs';
241
+
242
+ export function register() {
243
+ registerSecureNow();
244
+ }
245
+ ```
246
+
247
+ ### 2. middleware.ts (create this for body capture)
248
+
249
+ ```typescript
250
+ export { middleware } from 'securenow/nextjs-middleware';
251
+
252
+ export const config = {
253
+ matcher: '/api/:path*',
254
+ };
255
+ ```
256
+
257
+ ### 3. .env.local
258
+
259
+ ```bash
260
+ # Required
261
+ SECURENOW_APPID=my-nextjs-app
262
+ SECURENOW_INSTANCE=http://your-signoz:4318
263
+
264
+ # Optional: Enable body capture
265
+ SECURENOW_CAPTURE_BODY=1
266
+ SECURENOW_MAX_BODY_SIZE=20480
267
+ SECURENOW_SENSITIVE_FIELDS=email,phone
268
+ ```
269
+
270
+ ### 4. Done! ✅
271
+
272
+ ```bash
273
+ npm run dev
274
+ ```
275
+
276
+ ---
277
+
278
+ ## 💡 Why Two Files?
279
+
280
+ **instrumentation.ts** - OpenTelemetry setup (required)
281
+ - Sets up tracing infrastructure
282
+ - Auto-captures IP, headers, geo data
283
+ - Runs for all requests
284
+
285
+ **middleware.ts** - Body capture (optional)
286
+ - Intercepts requests to clone and read body
287
+ - Only needed if you want body capture
288
+ - Can be applied selectively to routes
289
+
290
+ **Separation = flexibility!** You can have tracing without body capture.
291
+
292
+ ---
293
+
294
+ ## ⚡ Performance
295
+
296
+ **Overhead per request:**
297
+ - **Without middleware:** 0ms (just tracing)
298
+ - **With middleware:** < 1ms (clone + parse + redact)
299
+
300
+ **The middleware:**
301
+ - Uses `request.clone()` - doesn't lock original
302
+ - Runs async - doesn't block request
303
+ - Fails gracefully - errors don't break app
304
+
305
+ ---
306
+
307
+ ## ❓ FAQ
308
+
309
+ ### Q: Is this truly self-sufficient?
310
+
311
+ **A:** Yes! Just export the middleware:
312
+ ```typescript
313
+ export { middleware } from 'securenow/nextjs-middleware';
314
+ ```
315
+
316
+ All logic (parsing, redaction, size limits) is in the package.
317
+
318
+ ### Q: Do I need to write any redaction code?
319
+
320
+ **A:** No! 20+ sensitive fields are redacted automatically. Just add custom ones if needed via env vars.
321
+
322
+ ### Q: Can I customize the redaction?
323
+
324
+ **A:** Yes, via environment variables:
325
+ ```bash
326
+ SECURENOW_SENSITIVE_FIELDS=my_custom_field,another_field
327
+ ```
328
+
329
+ ### Q: Will this slow down my app?
330
+
331
+ **A:** No! < 1ms overhead. Uses `request.clone()` so original is unaffected.
332
+
333
+ ### Q: Can I apply to specific routes only?
334
+
335
+ **A:** Yes! Use the matcher config:
336
+ ```typescript
337
+ export const config = {
338
+ matcher: ['/api/login', '/api/graphql'],
339
+ };
340
+ ```
341
+
342
+ ### Q: What if I don't want body capture?
343
+
344
+ **A:** Don't create `middleware.ts`! Just use `instrumentation.ts` for tracing without bodies.
345
+
346
+ ---
347
+
348
+ ## 🎉 Summary
349
+
350
+ **Enable body capture:**
351
+
352
+ 1. ```bash
353
+ SECURENOW_CAPTURE_BODY=1 # in .env.local
354
+ ```
355
+
356
+ 2. ```typescript
357
+ export { middleware } from 'securenow/nextjs-middleware'; # in middleware.ts
358
+ ```
359
+
360
+ **Result:**
361
+ - ✅ All request bodies captured
362
+ - ✅ Sensitive data redacted
363
+ - ✅ JSON, GraphQL, Form supported
364
+ - ✅ No customer code changes
365
+ - ✅ Package handles everything
366
+
367
+ **Self-sufficient design** - customers just import and configure! 🚀
368
+
package/NEXTJS-GUIDE.md CHANGED
@@ -113,6 +113,16 @@ SecureNow automatically captures comprehensive request data:
113
113
 
114
114
  See [AUTOMATIC-IP-CAPTURE.md](./AUTOMATIC-IP-CAPTURE.md) for full details.
115
115
 
116
+ ### 📝 Request Body Capture (Optional!)
117
+ - **JSON Bodies** - API payloads with sensitive fields redacted
118
+ - **GraphQL Queries** - Full query capture
119
+ - **Form Data** - Form submissions
120
+ - **Auto-Redaction** - Passwords, tokens, cards automatically hidden
121
+
122
+ Enable with: `SECURENOW_CAPTURE_BODY=1`
123
+
124
+ See [REQUEST-BODY-CAPTURE.md](./REQUEST-BODY-CAPTURE.md) for full details.
125
+
116
126
  ### Next.js Built-in Spans
117
127
  - ✅ HTTP requests (`[http.method] [next.route]`)
118
128
  - ✅ API routes execution
@@ -35,7 +35,7 @@ export function register() { registerSecureNow(); }
35
35
 
36
36
  ```bash
37
37
  SECURENOW_APPID=my-nextjs-app
38
- SECURENOW_INSTANCE=http://your-signoz:4318
38
+ SECURENOW_INSTANCE=http://your-securenow:4318
39
39
  ```
40
40
 
41
41
  ### 3. (Next.js 14 only) Update `next.config.js`: