securenow 8.23.0 → 8.24.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/nextjs.js CHANGED
@@ -7,24 +7,32 @@
7
7
  *
8
8
  * 1. Add securenow to serverExternalPackages and standalone output tracing
9
9
  * includes in next.config.js:
10
- *
10
+ *
11
11
  * const nextConfig = {
12
12
  * serverExternalPackages: ["securenow"],
13
13
  * outputFileTracingIncludes: {
14
14
  * "/*": ["<securenow package glob>"],
15
15
  * },
16
16
  * };
17
- *
18
- * 2. Create instrumentation.ts (or .js) in your project root:
19
- *
17
+ *
18
+ * 2. Create instrumentation.ts (or .js) in your project root (`npx securenow init` writes it):
19
+ *
20
20
  * export async function register() {
21
- * if (process.env.NEXT_RUNTIME !== "nodejs") return;
22
- * const securenowNext = await import(/* webpackIgnore: true *\/ "securenow/nextjs");
23
- * const registerSecureNow = securenowNext.registerSecureNow || securenowNext.default?.registerSecureNow;
24
- * registerSecureNow({ captureBody: true });
25
- * await import(/* webpackIgnore: true *\/ "securenow/nextjs-auto-capture");
21
+ * if (process.env.NEXT_RUNTIME === "nodejs") {
22
+ * try {
23
+ * const securenowNext = await import("securenow/nextjs");
24
+ * const registerSecureNow = securenowNext.registerSecureNow || securenowNext.default?.registerSecureNow;
25
+ * registerSecureNow({ captureBody: true });
26
+ * await import("securenow/nextjs-auto-capture");
27
+ * } catch (error) {
28
+ * console.error("[securenow] SecureNow did not start; the app is running without it:", error);
29
+ * }
30
+ * }
26
31
  * }
27
- *
32
+ *
33
+ * The import stays inside the nodejs block, without webpackIgnore, so Next.js leaves it out
34
+ * of the Edge build and traces securenow and its dependencies into standalone output.
35
+ *
28
36
  * 3. Run `npx securenow login` and `npx securenow init`.
29
37
  * The SDK reads app identity, collector, firewall, capture, and
30
38
  * deploymentEnvironment from .securenow/credentials.json.
@@ -34,7 +42,6 @@ const { randomUUID } = require('crypto');
34
42
  const { nodeSdkDefaultTelemetryOptions } = require('./otel-defaults');
35
43
  const appConfig = require('./app-config');
36
44
  const { resolveClientIpWithDetails } = require('./resolve-ip');
37
- const otelResources = require('@opentelemetry/resources');
38
45
 
39
46
  const nodeSdkTelemetryOptions = nodeSdkDefaultTelemetryOptions();
40
47
 
@@ -50,6 +57,10 @@ function requireNodeBuiltin(name) {
50
57
  }
51
58
 
52
59
  function createResource(attributes) {
60
+ // Required here, not at the top of the file: a bundle that lacks the OpenTelemetry packages
61
+ // (an incomplete standalone image) must still load this module, so the firewall starts and
62
+ // the app keeps serving.
63
+ const otelResources = require('@opentelemetry/resources');
53
64
  if (typeof otelResources.resourceFromAttributes === 'function') {
54
65
  return otelResources.resourceFromAttributes(attributes);
55
66
  }
@@ -59,158 +70,12 @@ function createResource(attributes) {
59
70
  throw new Error('Unsupported @opentelemetry/resources version');
60
71
  }
61
72
 
62
- const { OTP_SENSITIVE_FIELDS, isOtpCodeField, matchesSensitiveField, redactOtpGraphQL, redactOtpQuery, withOtpRedactionExporter } = require('./otp-redaction');
63
-
64
- // Default sensitive fields to redact from request bodies.
65
- // OTP/code aliases use exact normalized matching; other fields are matched
66
- // substring-wise against lowercased keys (see redactSensitiveData),
67
- // so e.g. 'card' also catches 'creditCard' and 'account' also catches
68
- // 'accountNumber'. Over-redaction here is intentional: a falsely-redacted
69
- // telemetry value is always safer than a leaked secret. Entries are kept
70
- // specific enough to avoid nuking broad benign keys (e.g. we use
71
- // 'firstname'/'lastname'/'fullname', never bare 'name').
72
- const DEFAULT_SENSITIVE_FIELDS = [
73
- ...OTP_SENSITIVE_FIELDS,
74
- // credentials / auth
75
- 'password', 'passwd', 'pwd', 'secret', 'token', 'api_key', 'apikey',
76
- 'access_token', 'auth', 'authorization', 'bearer', 'credentials',
77
- 'mysql_pwd', 'otp', 'mfa', 'totp', 'sessionid', 'session_id',
78
- 'cookie', 'set-cookie',
79
- // financial
80
- 'stripeToken', 'card', 'cardnumber', 'ccv', 'cvc', 'cvv',
81
- 'iban', 'account', 'accountnumber', 'routing', 'sortcode', 'taxid',
82
- // PII
83
- 'ssn', 'pin', 'email', 'e_mail', 'phone', 'mobile', 'dob', 'birthdate',
84
- 'firstname', 'lastname', 'fullname', 'address', 'postcode', 'zip',
85
- 'passport', 'license',
86
- ];
87
-
88
- // Conservative value-shape redactors. Key-name matching misses secrets that
89
- // land in free-form string values (GraphQL bodies, message fields, etc.), so
90
- // as a second layer we scrub string VALUES that *look like* a secret/PII.
91
- // These are intentionally precise/bounded so they don't garble normal prose,
92
- // and they only ever transform captured telemetry strings (read-only) — never
93
- // the actual request/response stream. Compiled once at module load.
94
- const VALUE_REDACTORS = [
95
- // JWT: three base64url segments. Anchored to the eyJ header so it won't
96
- // match arbitrary dotted tokens.
97
- { name: 'jwt', re: /eyJ[A-Za-z0-9_-]{6,}\.[A-Za-z0-9_-]{4,}\.[A-Za-z0-9_-]{4,}/g },
98
- // Bearer/Basic auth header value embedded in a body string.
99
- { name: 'bearer', re: /\b(?:Bearer|Basic)\s+[A-Za-z0-9._~+/=-]{8,}/gi },
100
- // Stripe-style / SecureNow live+test API keys.
101
- { name: 'apikey', re: /\b(?:sk|pk|rk|snk)_(?:live|test)_[A-Za-z0-9]{8,}/g },
102
- // Email addresses (bounded local/domain parts).
103
- { name: 'email', re: /\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,24}\b/g },
104
- // Credit-card-like: 13–19 digit runs that pass a Luhn check, allowing
105
- // space/dash grouping. Luhn-gated to avoid clobbering ordinary long numbers.
106
- { name: 'card', re: /\b(?:\d[ -]?){13,19}\b/g, luhn: true },
107
- ];
108
-
109
- // Skip the value-shape scan on very large strings to bound per-body cost.
110
- const MAX_VALUE_SCAN_LENGTH = 16384;
111
-
112
- function luhnValid(digits) {
113
- let sum = 0;
114
- let alt = false;
115
- for (let i = digits.length - 1; i >= 0; i--) {
116
- let d = digits.charCodeAt(i) - 48;
117
- if (d < 0 || d > 9) return false;
118
- if (alt) { d *= 2; if (d > 9) d -= 9; }
119
- sum += d;
120
- alt = !alt;
121
- }
122
- return sum % 10 === 0;
123
- }
73
+ const { redactOtpQuery, withOtpRedactionExporter } = require('./otp-redaction');
74
+ const { createRequestCapture } = require('./request-capture');
124
75
 
125
- /**
126
- * Redact obvious secret/PII shapes inside a captured string VALUE.
127
- * Returns the input unchanged when nothing matches (cheap common case).
128
- */
129
- function redactSensitiveValue(value) {
130
- if (typeof value !== 'string' || value.length === 0) return value;
131
- // Credential assignments in GraphQL/query strings need field-aware
132
- // redaction even though a short numeric OTP has no unique value shape.
133
- value = redactOtpQuery(redactOtpGraphQL(value));
134
- if (value.length > MAX_VALUE_SCAN_LENGTH) return value; // bound shape scans
135
- let out = value;
136
- for (const r of VALUE_REDACTORS) {
137
- r.re.lastIndex = 0;
138
- if (r.luhn) {
139
- out = out.replace(r.re, (m) => {
140
- const digits = m.replace(/[ -]/g, '');
141
- if (digits.length < 13 || digits.length > 19) return m;
142
- return luhnValid(digits) ? '[REDACTED]' : m;
143
- });
144
- } else {
145
- out = out.replace(r.re, '[REDACTED]');
146
- }
147
- }
148
- return out;
149
- }
150
-
151
- /**
152
- * Redact sensitive fields from an object
153
- */
154
- function redactSensitiveData(obj, sensitiveFields = DEFAULT_SENSITIVE_FIELDS) {
155
- if (!obj || typeof obj !== 'object') return obj;
156
-
157
- const redacted = Array.isArray(obj) ? [...obj] : { ...obj };
158
-
159
- for (const key of Object.keys(redacted)) {
160
-
161
- if (matchesSensitiveField(key, sensitiveFields)) {
162
- redacted[key] = '[REDACTED]';
163
- } else if (typeof redacted[key] === 'object' && redacted[key] !== null) {
164
- // Recursively redact nested objects
165
- redacted[key] = redactSensitiveData(redacted[key], sensitiveFields);
166
- } else if (typeof redacted[key] === 'string') {
167
- // Second layer: even if the key name looks benign, scrub values that
168
- // *look like* a secret/PII (JWT, bearer token, API key, email, card).
169
- redacted[key] = redactSensitiveValue(redacted[key]);
170
- }
171
- }
172
-
173
- return redacted;
174
- }
175
-
176
- function escapeRegex(str) {
177
- return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
178
- }
179
-
180
- /**
181
- * Redact sensitive data from GraphQL query strings
182
- */
183
- function redactGraphQLQuery(query, sensitiveFields = DEFAULT_SENSITIVE_FIELDS) {
184
- if (!query || typeof query !== 'string') return query;
185
-
186
- let redacted = query;
187
-
188
- // Redact sensitive fields in GraphQL arguments and variables
189
- // Matches patterns like: password: "value" or password:"value" or password:'value'
190
- sensitiveFields.filter(field => field === 'otp' || !isOtpCodeField(field)).forEach(field => {
191
- const escaped = escapeRegex(field);
192
- const patterns = [
193
- new RegExp(`(${escaped}\\s*:\\s*["'])([^"']+)(["'])`, 'gi'),
194
- new RegExp(`(${escaped}\\s*:\\s*)([^\\s,})\n]+)`, 'gi'),
195
- ];
196
-
197
- patterns.forEach(pattern => {
198
- redacted = redacted.replace(pattern, (match, prefix, value, suffix) => {
199
- if (suffix) {
200
- return `${prefix}[REDACTED]${suffix}`;
201
- } else {
202
- return `${prefix}[REDACTED]`;
203
- }
204
- });
205
- });
206
- });
207
-
208
- // Second layer: scrub secret/PII *shapes* anywhere in the (free-form) query
209
- // body — catches values the key-name pass above can't see.
210
- redacted = redactOtpGraphQL(redactSensitiveValue(redacted));
211
-
212
- return redacted;
213
- }
76
+ // Set once the HTTP layer has captured a request in this process: nextjs-auto-capture then
77
+ // leaves bodies to it instead of attaching a second copy to Next.js's own span.
78
+ const HTTP_CAPTURE_ACTIVE = Symbol.for('securenow.nextjs.httpCaptureActive');
214
79
 
215
80
  /**
216
81
  * Register SecureNow OpenTelemetry for Next.js using @vercel/otel
@@ -280,10 +145,13 @@ function registerSecureNow(options = {}) {
280
145
 
281
146
  // -------- Body Capture Configuration --------
282
147
  // Opt-out default: set config.capture.body=false (or options.captureBody=false) to disable.
283
- const captureBody = options.captureBody ?? appConfig.boolConfig('capture.body', true);
284
- const maxBodySize = appConfig.numberConfig('capture.maxBodySize', 10240, 1024);
285
- const customSensitiveFields = appConfig.listConfig('capture.sensitiveFields');
286
- const allSensitiveFields = [...DEFAULT_SENSITIVE_FIELDS, ...customSensitiveFields];
148
+ const requestCapture = createRequestCapture({
149
+ captureBody: options.captureBody ?? appConfig.boolConfig('capture.body', true),
150
+ captureMultipart: appConfig.boolConfig('capture.multipart', true),
151
+ maxBodySize: appConfig.numberConfig('capture.maxBodySize', 10240, 1024),
152
+ customSensitiveFields: appConfig.listConfig('capture.sensitiveFields'),
153
+ });
154
+ const { captureBody } = requestCapture;
287
155
 
288
156
  // -------- Log environment detection --------
289
157
  if (!isVercel) {
@@ -432,11 +300,13 @@ function registerSecureNow(options = {}) {
432
300
  );
433
301
  }
434
302
 
435
- // -------- Request Body NOT captured at HTTP instrumentation level --------
436
- // IMPORTANT: Do NOT attempt to read request.body or listen to 'data' events
437
- // Next.js manages request streams internally and reading them here causes conflicts
438
- // Body capture must be done in Next.js middleware using request.clone()
439
-
303
+ // Headers (redacted) and body, the same attributes as every other SecureNow integration.
304
+ // The body is observed through the request's own emit('data') while Next.js reads it:
305
+ // no listener is added and nothing is read on Next.js's behalf.
306
+ requestCapture.captureHeaders(span, request);
307
+ requestCapture.observeBody(span, request);
308
+ globalThis[HTTP_CAPTURE_ACTIVE] = true;
309
+
440
310
  } catch (error) {
441
311
  // Silently fail to not break the request
442
312
  if (otelLogLevel === 'debug') {
@@ -519,7 +389,10 @@ function registerSecureNow(options = {}) {
519
389
  let _crashLoggerProvider = null;
520
390
  require('./crash-capture').installCrashCapture({
521
391
  getLogger: () => (_crashLoggerProvider ? _crashLoggerProvider.getLogger('securenow.crash', '1.0.0') : null),
522
- flush: () => (_crashLoggerProvider ? _crashLoggerProvider.forceFlush() : undefined),
392
+ flush: () => Promise.all([
393
+ _crashLoggerProvider ? _crashLoggerProvider.forceFlush() : undefined,
394
+ require('./crash-capture').flushTracerProvider(),
395
+ ]),
523
396
  isSdkError: (e) => _isOtlpTransientError(e) && (_looksLikeOtlpStack(e) || _looksLikeConfiguredOtlpEndpoint(e)),
524
397
  onSdkError: _reportSuppressedOtlpError,
525
398
  });
@@ -708,10 +581,7 @@ function registerSecureNow(options = {}) {
708
581
  console.log('[securenow] - Geographic data (Vercel/Cloudflare)');
709
582
  console.log('[securenow] - Request IDs, CSRF tokens, Auth presence');
710
583
  console.log('[securenow] - Response status, content-type, content-length');
711
- console.log('[securenow] Body capture DISABLED at HTTP instrumentation level (prevents Next.js conflicts)');
712
- if (captureBody) {
713
- console.log('[securenow] For body capture in Next.js, use: import "securenow/nextjs-auto-capture"');
714
- }
584
+ console.log('[securenow] - Request headers (redacted)%s', captureBody ? ' and JSON/form/GraphQL bodies (redacted)' : '');
715
585
 
716
586
  // Optional test span
717
587
  if (appConfig.boolConfig('runtime.testSpan', false)) {