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.
- package/challenge.js +273 -0
- package/cli/challenges.js +253 -0
- package/cli/security.js +80 -8
- package/cli.js +42 -4
- package/firewall.js +952 -702
- package/nextjs-auto-capture.js +274 -195
- package/nextjs-middleware.js +268 -185
- package/nextjs-wrapper.js +234 -155
- package/nextjs.js +768 -685
- package/nuxt-server-plugin.mjs +506 -426
- package/package.json +5 -1
- package/tracing.js +844 -758
package/nextjs-wrapper.js
CHANGED
|
@@ -1,155 +1,234 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* SecureNow Next.js API Route Wrapper for Body Capture
|
|
3
|
-
*
|
|
4
|
-
* This approach is NON-INVASIVE and runs INSIDE your handler,
|
|
5
|
-
* so it never blocks or interferes with middleware or routing.
|
|
6
|
-
*
|
|
7
|
-
* Usage:
|
|
8
|
-
*
|
|
9
|
-
* import { withSecureNow } from 'securenow/nextjs-wrapper';
|
|
10
|
-
*
|
|
11
|
-
* export const POST = withSecureNow(async (request) => {
|
|
12
|
-
* // Your handler code - request.body is available as parsed JSON
|
|
13
|
-
* const data = await request.json();
|
|
14
|
-
* return Response.json({ success: true });
|
|
15
|
-
* });
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
const { trace } = require('@opentelemetry/api');
|
|
19
|
-
const appConfig = require('./app-config');
|
|
20
|
-
|
|
21
|
-
// Default sensitive fields to redact
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
1
|
+
/**
|
|
2
|
+
* SecureNow Next.js API Route Wrapper for Body Capture
|
|
3
|
+
*
|
|
4
|
+
* This approach is NON-INVASIVE and runs INSIDE your handler,
|
|
5
|
+
* so it never blocks or interferes with middleware or routing.
|
|
6
|
+
*
|
|
7
|
+
* Usage:
|
|
8
|
+
*
|
|
9
|
+
* import { withSecureNow } from 'securenow/nextjs-wrapper';
|
|
10
|
+
*
|
|
11
|
+
* export const POST = withSecureNow(async (request) => {
|
|
12
|
+
* // Your handler code - request.body is available as parsed JSON
|
|
13
|
+
* const data = await request.json();
|
|
14
|
+
* return Response.json({ success: true });
|
|
15
|
+
* });
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
const { trace } = 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 redactSensitiveData(obj, sensitiveFields = DEFAULT_SENSITIVE_FIELDS) {
|
|
107
|
+
if (!obj || typeof obj !== 'object') return obj;
|
|
108
|
+
|
|
109
|
+
const redacted = Array.isArray(obj) ? [...obj] : { ...obj };
|
|
110
|
+
|
|
111
|
+
for (const key of Object.keys(redacted)) {
|
|
112
|
+
const lowerKey = key.toLowerCase();
|
|
113
|
+
|
|
114
|
+
if (sensitiveFields.some(field => lowerKey.includes(field.toLowerCase()))) {
|
|
115
|
+
redacted[key] = '[REDACTED]';
|
|
116
|
+
} else if (typeof redacted[key] === 'object' && redacted[key] !== null) {
|
|
117
|
+
redacted[key] = redactSensitiveData(redacted[key], sensitiveFields);
|
|
118
|
+
} else if (typeof redacted[key] === 'string') {
|
|
119
|
+
// Second layer: even if the key name looks benign, scrub values that
|
|
120
|
+
// *look like* a secret/PII (JWT, bearer token, API key, email, card).
|
|
121
|
+
redacted[key] = redactSensitiveValue(redacted[key]);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return redacted;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Capture body from Request object (clone to avoid consuming)
|
|
130
|
+
*/
|
|
131
|
+
async function captureRequestBody(request) {
|
|
132
|
+
const captureBody = appConfig.boolConfig('capture.body', true);
|
|
133
|
+
|
|
134
|
+
if (!captureBody) return;
|
|
135
|
+
if (!['POST', 'PUT', 'PATCH'].includes(request.method)) return;
|
|
136
|
+
|
|
137
|
+
const span = trace.getActiveSpan();
|
|
138
|
+
if (!span) return;
|
|
139
|
+
|
|
140
|
+
try {
|
|
141
|
+
const contentType = request.headers.get('content-type') || '';
|
|
142
|
+
const maxBodySize = appConfig.numberConfig('capture.maxBodySize', 10240, 1024);
|
|
143
|
+
const customSensitiveFields = appConfig.listConfig('capture.sensitiveFields');
|
|
144
|
+
const allSensitiveFields = [...DEFAULT_SENSITIVE_FIELDS, ...customSensitiveFields];
|
|
145
|
+
|
|
146
|
+
// Only for supported types
|
|
147
|
+
if (!contentType.includes('application/json') &&
|
|
148
|
+
!contentType.includes('application/graphql') &&
|
|
149
|
+
!contentType.includes('application/x-www-form-urlencoded')) {
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// Clone to avoid consuming the original
|
|
154
|
+
const cloned = request.clone();
|
|
155
|
+
const bodyText = await cloned.text();
|
|
156
|
+
|
|
157
|
+
if (bodyText.length > maxBodySize) {
|
|
158
|
+
span.setAttribute('http.request.body', `[TOO LARGE: ${bodyText.length} bytes]`);
|
|
159
|
+
span.setAttribute('http.request.body.size', bodyText.length);
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// Parse and redact based on type
|
|
164
|
+
let redacted;
|
|
165
|
+
if (contentType.includes('application/json') || contentType.includes('application/graphql')) {
|
|
166
|
+
try {
|
|
167
|
+
const parsed = JSON.parse(bodyText);
|
|
168
|
+
redacted = redactSensitiveData(parsed, allSensitiveFields);
|
|
169
|
+
span.setAttributes({
|
|
170
|
+
'http.request.body': JSON.stringify(redacted).substring(0, maxBodySize),
|
|
171
|
+
'http.request.body.type': contentType.includes('graphql') ? 'graphql' : 'json',
|
|
172
|
+
'http.request.body.size': bodyText.length,
|
|
173
|
+
});
|
|
174
|
+
} catch (e) {
|
|
175
|
+
span.setAttribute('http.request.body', '[INVALID JSON]');
|
|
176
|
+
}
|
|
177
|
+
} else if (contentType.includes('application/x-www-form-urlencoded')) {
|
|
178
|
+
const params = new URLSearchParams(bodyText);
|
|
179
|
+
const parsed = Object.fromEntries(params);
|
|
180
|
+
redacted = redactSensitiveData(parsed, allSensitiveFields);
|
|
181
|
+
span.setAttributes({
|
|
182
|
+
'http.request.body': JSON.stringify(redacted).substring(0, maxBodySize),
|
|
183
|
+
'http.request.body.type': 'form',
|
|
184
|
+
'http.request.body.size': bodyText.length,
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
} catch (error) {
|
|
188
|
+
// Silently fail - never block the request
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Wrap a Next.js API route handler to capture body
|
|
194
|
+
* This is OPTIONAL and NON-INVASIVE - only use on routes where you want body capture
|
|
195
|
+
*/
|
|
196
|
+
function withSecureNow(handler) {
|
|
197
|
+
return async function wrappedHandler(request, context) {
|
|
198
|
+
// Capture body asynchronously (doesn't block handler)
|
|
199
|
+
captureRequestBody(request).catch(() => {
|
|
200
|
+
// Ignore errors silently
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
// Call original handler immediately - no blocking!
|
|
204
|
+
return handler(request, context);
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Alternative: Auto-capture wrapper that tries to capture AFTER handler runs
|
|
210
|
+
* This is even safer as it never interferes with the handler logic
|
|
211
|
+
*/
|
|
212
|
+
function withSecureNowAsync(handler) {
|
|
213
|
+
return async function wrappedHandler(request, context) {
|
|
214
|
+
// Try to capture body in background (non-blocking)
|
|
215
|
+
const capturePromise = captureRequestBody(request);
|
|
216
|
+
|
|
217
|
+
// Run handler
|
|
218
|
+
const response = await handler(request, context);
|
|
219
|
+
|
|
220
|
+
// Wait for capture to finish (but don't fail if it doesn't)
|
|
221
|
+
await capturePromise.catch(() => {});
|
|
222
|
+
|
|
223
|
+
return response;
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
module.exports = {
|
|
228
|
+
withSecureNow,
|
|
229
|
+
withSecureNowAsync,
|
|
230
|
+
captureRequestBody,
|
|
231
|
+
redactSensitiveData,
|
|
232
|
+
DEFAULT_SENSITIVE_FIELDS,
|
|
233
|
+
};
|
|
234
|
+
|