securenow 8.5.0 → 8.7.0

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.
@@ -1,185 +1,268 @@
1
- /**
2
- * SecureNow Next.js Middleware for Body Capture
3
- *
4
- * OPTIONAL: Import this in your Next.js app to enable automatic body capture
5
- *
6
- * Usage:
7
- *
8
- * Create middleware.ts in your Next.js app root:
9
- *
10
- * export { middleware } from 'securenow/nextjs-middleware';
11
- * export const config = {
12
- * matcher: '/api/:path*', // Apply to API routes only
13
- * };
14
- *
15
- * That's it! Bodies are now captured with sensitive data redacted.
16
- */
17
-
18
- const { trace, context, SpanStatusCode } = require('@opentelemetry/api');
19
- const appConfig = require('./app-config');
20
-
21
- // Default sensitive fields to redact
22
- const DEFAULT_SENSITIVE_FIELDS = [
23
- 'password', 'passwd', 'pwd', 'secret', 'token', 'api_key', 'apikey',
24
- 'access_token', 'auth', 'credentials', 'mysql_pwd', 'stripeToken',
25
- 'card', 'cardnumber', 'ccv', 'cvc', 'cvv', 'ssn', 'pin',
26
- ];
27
-
28
- /**
29
- * Redact sensitive fields from an object
30
- */
31
- function escapeRegex(str) {
32
- return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
33
- }
34
-
35
- function redactSensitiveData(obj, sensitiveFields = DEFAULT_SENSITIVE_FIELDS) {
36
- if (!obj || typeof obj !== 'object') return obj;
37
-
38
- const redacted = Array.isArray(obj) ? [...obj] : { ...obj };
39
-
40
- for (const key of Object.keys(redacted)) {
41
- const lowerKey = key.toLowerCase();
42
-
43
- if (sensitiveFields.some(field => lowerKey.includes(field.toLowerCase()))) {
44
- redacted[key] = '[REDACTED]';
45
- } else if (typeof redacted[key] === 'object' && redacted[key] !== null) {
46
- redacted[key] = redactSensitiveData(redacted[key], sensitiveFields);
47
- }
48
- }
49
-
50
- return redacted;
51
- }
52
-
53
- /**
54
- * Redact sensitive data from GraphQL query strings
55
- */
56
- function redactGraphQLQuery(query, sensitiveFields = DEFAULT_SENSITIVE_FIELDS) {
57
- if (!query || typeof query !== 'string') return query;
58
-
59
- let redacted = query;
60
-
61
- sensitiveFields.forEach(field => {
62
- const escaped = escapeRegex(field);
63
- const patterns = [
64
- new RegExp(`(${escaped}\\s*:\\s*["'])([^"']+)(["'])`, 'gi'),
65
- new RegExp(`(${escaped}\\s*:\\s*)([^\\s,})\n]+)`, 'gi'),
66
- ];
67
-
68
- patterns.forEach(pattern => {
69
- redacted = redacted.replace(pattern, (match, prefix, value, suffix) => {
70
- return suffix ? `${prefix}[REDACTED]${suffix}` : `${prefix}[REDACTED]`;
71
- });
72
- });
73
- });
74
-
75
- return redacted;
76
- }
77
-
78
- /**
79
- * Next.js Middleware for Body Capture
80
- */
81
- async function middleware(request) {
82
- const { NextResponse } = require('next/server');
83
-
84
- // Only capture for POST/PUT/PATCH
85
- if (!['POST', 'PUT', 'PATCH'].includes(request.method)) {
86
- return NextResponse.next();
87
- }
88
-
89
- // Get or create a tracer
90
- const tracer = trace.getTracer('securenow-middleware');
91
- let span = trace.getActiveSpan();
92
- let createdSpan = false;
93
-
94
- // If no active span, create one for this middleware
95
- if (!span) {
96
- const url = new URL(request.url);
97
- span = tracer.startSpan(`middleware ${request.method} ${url.pathname}`);
98
- createdSpan = true;
99
- }
100
-
101
- try {
102
- const contentType = request.headers.get('content-type') || '';
103
- const maxBodySize = appConfig.numberConfig('capture.maxBodySize', 10240, 1024);
104
- const customSensitiveFields = appConfig.listConfig('capture.sensitiveFields');
105
- const allSensitiveFields = [...DEFAULT_SENSITIVE_FIELDS, ...customSensitiveFields];
106
-
107
- // Only capture supported types
108
- if (contentType.includes('application/json') ||
109
- contentType.includes('application/graphql')) {
110
-
111
- // Clone the request to read body without consuming the original
112
- const clonedRequest = request.clone();
113
- const bodyText = await clonedRequest.text();
114
-
115
- if (bodyText.length <= maxBodySize) {
116
- let redactedBody;
117
-
118
- if (contentType.includes('application/graphql')) {
119
- // GraphQL: redact query string
120
- redactedBody = redactGraphQLQuery(bodyText, allSensitiveFields);
121
- } else {
122
- // JSON: parse and redact
123
- try {
124
- const parsed = JSON.parse(bodyText);
125
- const redacted = redactSensitiveData(parsed, allSensitiveFields);
126
- redactedBody = JSON.stringify(redacted);
127
- } catch (e) {
128
- redactedBody = '[UNPARSEABLE - REDACTED FOR SAFETY]';
129
- }
130
- }
131
-
132
- span.setAttributes({
133
- 'http.request.body': redactedBody.substring(0, maxBodySize),
134
- 'http.request.body.type': contentType.includes('graphql') ? 'graphql' : 'json',
135
- 'http.request.body.size': bodyText.length,
136
- });
137
- } else {
138
- span.setAttribute('http.request.body', `[TOO LARGE: ${bodyText.length} bytes]`);
139
- }
140
- } else if (contentType.includes('application/x-www-form-urlencoded')) {
141
- const clonedRequest = request.clone();
142
- const formData = await clonedRequest.formData();
143
- const parsed = Object.fromEntries(formData);
144
- const redacted = redactSensitiveData(parsed, allSensitiveFields);
145
-
146
- span.setAttributes({
147
- 'http.request.body': JSON.stringify(redacted).substring(0, maxBodySize),
148
- 'http.request.body.type': 'form',
149
- 'http.request.body.size': JSON.stringify(parsed).length,
150
- });
151
- } else if (contentType.includes('multipart/form-data')) {
152
- span.setAttribute('http.request.body', '[MULTIPART - NOT CAPTURED]');
153
- span.setAttribute('http.request.body.type', 'multipart');
154
- }
155
-
156
- // End span if we created it
157
- if (createdSpan) {
158
- span.setStatus({ code: SpanStatusCode.OK });
159
- span.end();
160
- }
161
- } catch (error) {
162
- // Silently fail - don't break the request
163
- console.debug('[securenow] Body capture failed:', error.message);
164
-
165
- // End span with error if we created it
166
- if (createdSpan && span) {
167
- span.setStatus({
168
- code: SpanStatusCode.ERROR,
169
- message: error.message
170
- });
171
- span.end();
172
- }
173
- }
174
-
175
- return NextResponse.next();
176
- }
177
-
178
- module.exports = {
179
- middleware,
180
- redactSensitiveData,
181
- redactGraphQLQuery,
182
- DEFAULT_SENSITIVE_FIELDS,
183
- };
184
-
185
-
1
+ /**
2
+ * SecureNow Next.js Middleware for Body Capture
3
+ *
4
+ * OPTIONAL: Import this in your Next.js app to enable automatic body capture
5
+ *
6
+ * Usage:
7
+ *
8
+ * Create middleware.ts in your Next.js app root:
9
+ *
10
+ * export { middleware } from 'securenow/nextjs-middleware';
11
+ * export const config = {
12
+ * matcher: '/api/:path*', // Apply to API routes only
13
+ * };
14
+ *
15
+ * That's it! Bodies are now captured with sensitive data redacted.
16
+ */
17
+
18
+ const { trace, context, SpanStatusCode } = require('@opentelemetry/api');
19
+ const appConfig = require('./app-config');
20
+
21
+ // Default sensitive fields to redact from request bodies.
22
+ // Matched substring-wise against lowercased keys (see redactSensitiveData),
23
+ // so e.g. 'card' also catches 'creditCard' and 'account' also catches
24
+ // 'accountNumber'. Over-redaction here is intentional: a falsely-redacted
25
+ // telemetry value is always safer than a leaked secret. Entries are kept
26
+ // specific enough to avoid nuking broad benign keys (e.g. we use
27
+ // 'firstname'/'lastname'/'fullname', never bare 'name').
28
+ const DEFAULT_SENSITIVE_FIELDS = [
29
+ // credentials / auth
30
+ 'password', 'passwd', 'pwd', 'secret', 'token', 'api_key', 'apikey',
31
+ 'access_token', 'auth', 'authorization', 'bearer', 'credentials',
32
+ 'mysql_pwd', 'otp', 'mfa', 'totp', 'sessionid', 'session_id',
33
+ 'cookie', 'set-cookie',
34
+ // financial
35
+ 'stripeToken', 'card', 'cardnumber', 'ccv', 'cvc', 'cvv',
36
+ 'iban', 'account', 'accountnumber', 'routing', 'sortcode', 'taxid',
37
+ // PII
38
+ 'ssn', 'pin', 'email', 'e_mail', 'phone', 'mobile', 'dob', 'birthdate',
39
+ 'firstname', 'lastname', 'fullname', 'address', 'postcode', 'zip',
40
+ 'passport', 'license',
41
+ ];
42
+
43
+ // Conservative value-shape redactors. Key-name matching misses secrets that
44
+ // land in free-form string values (GraphQL bodies, message fields, etc.), so
45
+ // as a second layer we scrub string VALUES that *look like* a secret/PII.
46
+ // These are intentionally precise/bounded so they don't garble normal prose,
47
+ // and they only ever transform captured telemetry strings (read-only) — never
48
+ // the actual request/response stream. Compiled once at module load.
49
+ const VALUE_REDACTORS = [
50
+ // JWT: three base64url segments. Anchored to the eyJ header so it won't
51
+ // match arbitrary dotted tokens.
52
+ { name: 'jwt', re: /eyJ[A-Za-z0-9_-]{6,}\.[A-Za-z0-9_-]{4,}\.[A-Za-z0-9_-]{4,}/g },
53
+ // Bearer/Basic auth header value embedded in a body string.
54
+ { name: 'bearer', re: /\b(?:Bearer|Basic)\s+[A-Za-z0-9._~+/=-]{8,}/gi },
55
+ // Stripe-style / SecureNow live+test API keys.
56
+ { name: 'apikey', re: /\b(?:sk|pk|rk|snk)_(?:live|test)_[A-Za-z0-9]{8,}/g },
57
+ // Email addresses (bounded local/domain parts).
58
+ { name: 'email', re: /\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,24}\b/g },
59
+ // Credit-card-like: 13–19 digit runs that pass a Luhn check, allowing
60
+ // space/dash grouping. Luhn-gated to avoid clobbering ordinary long numbers.
61
+ { name: 'card', re: /\b(?:\d[ -]?){13,19}\b/g, luhn: true },
62
+ ];
63
+
64
+ // Skip the value-shape scan on very large strings to bound per-body cost.
65
+ const MAX_VALUE_SCAN_LENGTH = 16384;
66
+
67
+ function luhnValid(digits) {
68
+ let sum = 0;
69
+ let alt = false;
70
+ for (let i = digits.length - 1; i >= 0; i--) {
71
+ let d = digits.charCodeAt(i) - 48;
72
+ if (d < 0 || d > 9) return false;
73
+ if (alt) { d *= 2; if (d > 9) d -= 9; }
74
+ sum += d;
75
+ alt = !alt;
76
+ }
77
+ return sum % 10 === 0;
78
+ }
79
+
80
+ /**
81
+ * Redact obvious secret/PII shapes inside a captured string VALUE.
82
+ * Returns the input unchanged when nothing matches (cheap common case).
83
+ */
84
+ function redactSensitiveValue(value) {
85
+ if (typeof value !== 'string' || value.length === 0) return value;
86
+ if (value.length > MAX_VALUE_SCAN_LENGTH) return value; // bound the work
87
+ let out = value;
88
+ for (const r of VALUE_REDACTORS) {
89
+ r.re.lastIndex = 0;
90
+ if (r.luhn) {
91
+ out = out.replace(r.re, (m) => {
92
+ const digits = m.replace(/[ -]/g, '');
93
+ if (digits.length < 13 || digits.length > 19) return m;
94
+ return luhnValid(digits) ? '[REDACTED]' : m;
95
+ });
96
+ } else {
97
+ out = out.replace(r.re, '[REDACTED]');
98
+ }
99
+ }
100
+ return out;
101
+ }
102
+
103
+ /**
104
+ * Redact sensitive fields from an object
105
+ */
106
+ function escapeRegex(str) {
107
+ return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
108
+ }
109
+
110
+ function redactSensitiveData(obj, sensitiveFields = DEFAULT_SENSITIVE_FIELDS) {
111
+ if (!obj || typeof obj !== 'object') return obj;
112
+
113
+ const redacted = Array.isArray(obj) ? [...obj] : { ...obj };
114
+
115
+ for (const key of Object.keys(redacted)) {
116
+ const lowerKey = key.toLowerCase();
117
+
118
+ if (sensitiveFields.some(field => lowerKey.includes(field.toLowerCase()))) {
119
+ redacted[key] = '[REDACTED]';
120
+ } else if (typeof redacted[key] === 'object' && redacted[key] !== null) {
121
+ redacted[key] = redactSensitiveData(redacted[key], sensitiveFields);
122
+ } else if (typeof redacted[key] === 'string') {
123
+ // Second layer: even if the key name looks benign, scrub values that
124
+ // *look like* a secret/PII (JWT, bearer token, API key, email, card).
125
+ redacted[key] = redactSensitiveValue(redacted[key]);
126
+ }
127
+ }
128
+
129
+ return redacted;
130
+ }
131
+
132
+ /**
133
+ * Redact sensitive data from GraphQL query strings
134
+ */
135
+ function redactGraphQLQuery(query, sensitiveFields = DEFAULT_SENSITIVE_FIELDS) {
136
+ if (!query || typeof query !== 'string') return query;
137
+
138
+ let redacted = query;
139
+
140
+ sensitiveFields.forEach(field => {
141
+ const escaped = escapeRegex(field);
142
+ const patterns = [
143
+ new RegExp(`(${escaped}\\s*:\\s*["'])([^"']+)(["'])`, 'gi'),
144
+ new RegExp(`(${escaped}\\s*:\\s*)([^\\s,})\n]+)`, 'gi'),
145
+ ];
146
+
147
+ patterns.forEach(pattern => {
148
+ redacted = redacted.replace(pattern, (match, prefix, value, suffix) => {
149
+ return suffix ? `${prefix}[REDACTED]${suffix}` : `${prefix}[REDACTED]`;
150
+ });
151
+ });
152
+ });
153
+
154
+ // Second layer: scrub secret/PII *shapes* anywhere in the (free-form) query
155
+ // body — catches values the key-name pass above can't see.
156
+ redacted = redactSensitiveValue(redacted);
157
+
158
+ return redacted;
159
+ }
160
+
161
+ /**
162
+ * Next.js Middleware for Body Capture
163
+ */
164
+ async function middleware(request) {
165
+ const { NextResponse } = require('next/server');
166
+
167
+ // Only capture for POST/PUT/PATCH
168
+ if (!['POST', 'PUT', 'PATCH'].includes(request.method)) {
169
+ return NextResponse.next();
170
+ }
171
+
172
+ // Get or create a tracer
173
+ const tracer = trace.getTracer('securenow-middleware');
174
+ let span = trace.getActiveSpan();
175
+ let createdSpan = false;
176
+
177
+ // If no active span, create one for this middleware
178
+ if (!span) {
179
+ const url = new URL(request.url);
180
+ span = tracer.startSpan(`middleware ${request.method} ${url.pathname}`);
181
+ createdSpan = true;
182
+ }
183
+
184
+ try {
185
+ const contentType = request.headers.get('content-type') || '';
186
+ const maxBodySize = appConfig.numberConfig('capture.maxBodySize', 10240, 1024);
187
+ const customSensitiveFields = appConfig.listConfig('capture.sensitiveFields');
188
+ const allSensitiveFields = [...DEFAULT_SENSITIVE_FIELDS, ...customSensitiveFields];
189
+
190
+ // Only capture supported types
191
+ if (contentType.includes('application/json') ||
192
+ contentType.includes('application/graphql')) {
193
+
194
+ // Clone the request to read body without consuming the original
195
+ const clonedRequest = request.clone();
196
+ const bodyText = await clonedRequest.text();
197
+
198
+ if (bodyText.length <= maxBodySize) {
199
+ let redactedBody;
200
+
201
+ if (contentType.includes('application/graphql')) {
202
+ // GraphQL: redact query string
203
+ redactedBody = redactGraphQLQuery(bodyText, allSensitiveFields);
204
+ } else {
205
+ // JSON: parse and redact
206
+ try {
207
+ const parsed = JSON.parse(bodyText);
208
+ const redacted = redactSensitiveData(parsed, allSensitiveFields);
209
+ redactedBody = JSON.stringify(redacted);
210
+ } catch (e) {
211
+ redactedBody = '[UNPARSEABLE - REDACTED FOR SAFETY]';
212
+ }
213
+ }
214
+
215
+ span.setAttributes({
216
+ 'http.request.body': redactedBody.substring(0, maxBodySize),
217
+ 'http.request.body.type': contentType.includes('graphql') ? 'graphql' : 'json',
218
+ 'http.request.body.size': bodyText.length,
219
+ });
220
+ } else {
221
+ span.setAttribute('http.request.body', `[TOO LARGE: ${bodyText.length} bytes]`);
222
+ }
223
+ } else if (contentType.includes('application/x-www-form-urlencoded')) {
224
+ const clonedRequest = request.clone();
225
+ const formData = await clonedRequest.formData();
226
+ const parsed = Object.fromEntries(formData);
227
+ const redacted = redactSensitiveData(parsed, allSensitiveFields);
228
+
229
+ span.setAttributes({
230
+ 'http.request.body': JSON.stringify(redacted).substring(0, maxBodySize),
231
+ 'http.request.body.type': 'form',
232
+ 'http.request.body.size': JSON.stringify(parsed).length,
233
+ });
234
+ } else if (contentType.includes('multipart/form-data')) {
235
+ span.setAttribute('http.request.body', '[MULTIPART - NOT CAPTURED]');
236
+ span.setAttribute('http.request.body.type', 'multipart');
237
+ }
238
+
239
+ // End span if we created it
240
+ if (createdSpan) {
241
+ span.setStatus({ code: SpanStatusCode.OK });
242
+ span.end();
243
+ }
244
+ } catch (error) {
245
+ // Silently fail - don't break the request
246
+ console.debug('[securenow] Body capture failed:', error.message);
247
+
248
+ // End span with error if we created it
249
+ if (createdSpan && span) {
250
+ span.setStatus({
251
+ code: SpanStatusCode.ERROR,
252
+ message: error.message
253
+ });
254
+ span.end();
255
+ }
256
+ }
257
+
258
+ return NextResponse.next();
259
+ }
260
+
261
+ module.exports = {
262
+ middleware,
263
+ redactSensitiveData,
264
+ redactGraphQLQuery,
265
+ DEFAULT_SENSITIVE_FIELDS,
266
+ };
267
+
268
+