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,426 +1,506 @@
1
- /**
2
- * SecureNow Nitro Server Plugin
3
- *
4
- * Initialises the OpenTelemetry SDK and hooks into Nitro's request lifecycle
5
- * to create spans, capture metadata, and forward logs.
6
- *
7
- * This file is registered by the Nuxt module (nuxt.mjs) via addServerPlugin.
8
- */
9
-
10
- import * as otelResources from '@opentelemetry/resources';
11
- import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
12
- import {
13
- context as otelContext,
14
- trace as otelTrace,
15
- SpanStatusCode,
16
- } from '@opentelemetry/api';
17
- import { createRequire } from 'node:module';
18
- import { randomUUID } from 'node:crypto';
19
-
20
- const nodeRequire = createRequire(import.meta.url);
21
- const appConfig = nodeRequire('./app-config');
22
- const { resolveClientIpWithDetails } = nodeRequire('./resolve-ip');
23
- const { nodeSdkDefaultTelemetryOptions } = nodeRequire('./otel-defaults');
24
- const nodeSdkTelemetryOptions = nodeSdkDefaultTelemetryOptions();
25
-
26
- const { NodeSDK } = nodeRequire('@opentelemetry/sdk-node');
27
- const { OTLPTraceExporter } = nodeRequire('@opentelemetry/exporter-trace-otlp-http');
28
- const { HttpInstrumentation } = nodeRequire('@opentelemetry/instrumentation-http');
29
-
30
- // ── Helpers ──
31
-
32
- function createResource(attributes) {
33
- if (typeof otelResources.resourceFromAttributes === 'function') {
34
- return otelResources.resourceFromAttributes(attributes);
35
- }
36
- if (typeof otelResources.Resource === 'function') {
37
- return new otelResources.Resource(attributes);
38
- }
39
- throw new Error('Unsupported @opentelemetry/resources version');
40
- }
41
-
42
- const DEFAULT_SENSITIVE_FIELDS = [
43
- 'password', 'passwd', 'pwd', 'secret', 'token', 'api_key', 'apikey',
44
- 'access_token', 'auth', 'credentials', 'mysql_pwd', 'stripeToken',
45
- 'card', 'cardnumber', 'ccv', 'cvc', 'cvv', 'ssn', 'pin',
46
- ];
47
-
48
- function redactSensitiveData(obj, fields = DEFAULT_SENSITIVE_FIELDS) {
49
- if (!obj || typeof obj !== 'object') return obj;
50
- const redacted = Array.isArray(obj) ? [...obj] : { ...obj };
51
- for (const key of Object.keys(redacted)) {
52
- const lower = key.toLowerCase();
53
- if (fields.some((f) => lower.includes(f.toLowerCase()))) {
54
- redacted[key] = '[REDACTED]';
55
- } else if (typeof redacted[key] === 'object' && redacted[key] !== null) {
56
- redacted[key] = redactSensitiveData(redacted[key], fields);
57
- }
58
- }
59
- return redacted;
60
- }
61
-
62
- // ── Runtime config helpers ──
63
-
64
- function getRuntimeOptions() {
65
- try {
66
- const cfg = useRuntimeConfig();
67
- return cfg.securenow || {};
68
- } catch {
69
- return {};
70
- }
71
- }
72
-
73
- // ── Plugin ──
74
-
75
- export default defineNitroPlugin(async (nitroApp) => {
76
- const opts = getRuntimeOptions();
77
-
78
- // Resolution order: opts -> .securenow/credentials.json -> package.json#name
79
- const resolvedApp = appConfig.resolveAll();
80
-
81
- // ── Naming ──
82
- const rawBase = (opts.serviceName || resolvedApp.appId || '').trim().replace(/^['"]|['"]$/g, '');
83
- const baseName = rawBase || null;
84
- // Default: auto-disable per-worker suffix when logged in (appId is the
85
- // routing UUID and the dashboard does exact match). opts.noUuid or
86
- // config.runtime.noUuid or opts.noUuid override.
87
- const noUuid = appConfig.resolveNoUuid({ noUuid: opts.noUuid });
88
- const deploymentEnvironment = appConfig.normalizeDeploymentEnvironment(
89
- opts.environment || resolvedApp.deploymentEnvironment
90
- );
91
-
92
- let serviceName;
93
- if (baseName) {
94
- serviceName = noUuid ? baseName : `${baseName}-${randomUUID()}`;
95
- } else {
96
- serviceName = `nuxt-app-${randomUUID()}`;
97
- console.warn(
98
- '[securenow] ⚠️ No app identity resolved. Using fallback: %s',
99
- serviceName,
100
- );
101
- console.warn(
102
- '[securenow] Run `npx securenow login` or set app.key in .securenow/credentials.json',
103
- );
104
- }
105
-
106
- const serviceInstanceId = `${baseName || 'securenow'}-${randomUUID()}`;
107
-
108
- // ── Endpoints ──
109
- const resolvedEndpoints = appConfig.resolveEndpoints({ endpoint: opts.endpoint || resolvedApp.instance });
110
- const endpointBase = resolvedEndpoints.endpointBase;
111
- const tracesUrl = resolvedEndpoints.tracesUrl;
112
- const logsUrl = resolvedEndpoints.logsUrl;
113
- const headers = resolvedEndpoints.headers;
114
-
115
- // ── Resource ──
116
- const resource = createResource({
117
- [SemanticResourceAttributes.SERVICE_NAME]: serviceName,
118
- [SemanticResourceAttributes.SERVICE_INSTANCE_ID]: serviceInstanceId,
119
- [SemanticResourceAttributes.DEPLOYMENT_ENVIRONMENT]: deploymentEnvironment,
120
- [SemanticResourceAttributes.SERVICE_VERSION]:
121
- process.env.npm_package_version || undefined,
122
- 'framework': 'nuxt',
123
- });
124
-
125
- // ── Body capture config ──
126
- // Opt-out default: set config.capture.body=false (or opts.captureBody=false) to disable.
127
- const captureBody =
128
- opts.captureBody ??
129
- appConfig.boolConfig('capture.body', true);
130
- const maxBodySize = appConfig.numberConfig('capture.maxBodySize', 10240, 1024);
131
- const customSensitiveFields = appConfig.listConfig('capture.sensitiveFields');
132
- const allSensitiveFields = [...DEFAULT_SENSITIVE_FIELDS, ...customSensitiveFields];
133
-
134
- // ── HTTP instrumentation ──
135
- const httpInstrumentation = new HttpInstrumentation({
136
- requestHook: (span, request) => {
137
- try {
138
- const hdrs = request.headers || {};
139
- const ipDetails = resolveClientIpWithDetails(request);
140
- const clientIp = ipDetails.ip || 'unknown';
141
-
142
- span.setAttributes({
143
- 'http.client_ip': clientIp,
144
- 'http.client_ip.source': ipDetails.source,
145
- 'http.socket_ip': ipDetails.socketIp || '',
146
- 'http.forwarded_for': ipDetails.forwardedFor || '',
147
- 'http.real_ip': ipDetails.realIp || '',
148
- 'http.proxy.trusted': String(!!ipDetails.trustedProxy),
149
- 'http.request.header.x_forwarded_for': ipDetails.forwardedFor || '',
150
- 'http.request.header.x_real_ip': ipDetails.realIp || '',
151
- 'http.request.header.cf_connecting_ip': ipDetails.cfConnectingIp || '',
152
- 'http.request.header.true_client_ip': ipDetails.trueClientIp || '',
153
- 'http.request.header.x_client_ip': ipDetails.clientIp || '',
154
- 'http.user_agent': hdrs['user-agent'] || '',
155
- 'http.host': hdrs['x-forwarded-host'] || hdrs['host'] || '',
156
- 'http.scheme':
157
- hdrs['x-forwarded-proto'] ||
158
- (request.socket?.encrypted ? 'https' : 'http'),
159
- 'http.referer': hdrs['referer'] || '',
160
- 'http.origin': hdrs['origin'] || '',
161
- 'http.request_id':
162
- hdrs['x-request-id'] || hdrs['x-trace-id'] || '',
163
- });
164
-
165
- if (hdrs['authorization']) {
166
- span.setAttribute('http.security.auth_present', 'true');
167
- }
168
- if (hdrs['cookie']) {
169
- span.setAttribute('http.security.cookies_present', 'true');
170
- }
171
-
172
- // Body capture via stream listener (same approach as tracing.js)
173
- if (
174
- captureBody &&
175
- request.method &&
176
- ['POST', 'PUT', 'PATCH'].includes(request.method)
177
- ) {
178
- const ct = hdrs['content-type'] || '';
179
- if (
180
- ct.includes('application/json') ||
181
- ct.includes('application/graphql') ||
182
- ct.includes('application/x-www-form-urlencoded')
183
- ) {
184
- const chunks = [];
185
- let size = 0;
186
- request.on('data', (chunk) => {
187
- size += chunk.length;
188
- if (size <= maxBodySize) chunks.push(chunk);
189
- });
190
- request.on('end', () => {
191
- if (size > maxBodySize) {
192
- span.setAttribute('http.request.body', `[TOO LARGE: ${size} bytes]`);
193
- return;
194
- }
195
- if (chunks.length === 0) return;
196
- const raw = Buffer.concat(chunks).toString('utf8');
197
- try {
198
- const parsed = JSON.parse(raw);
199
- const redacted = redactSensitiveData(parsed, allSensitiveFields);
200
- span.setAttributes({
201
- 'http.request.body': JSON.stringify(redacted).substring(0, maxBodySize),
202
- 'http.request.body.type': ct.includes('graphql') ? 'graphql' : 'json',
203
- 'http.request.body.size': size,
204
- });
205
- } catch {
206
- span.setAttribute('http.request.body', '[UNPARSEABLE - REDACTED FOR SAFETY]');
207
- span.setAttribute('http.request.body.parse_error', true);
208
- }
209
- });
210
- }
211
- }
212
- } catch {
213
- // never break the request
214
- }
215
- },
216
- });
217
-
218
- // ── Trace exporter + SDK ──
219
- const traceExporter = new OTLPTraceExporter({ url: tracesUrl, headers });
220
-
221
- const sdk = new NodeSDK({
222
- ...nodeSdkTelemetryOptions,
223
- traceExporter,
224
- resource,
225
- instrumentations: [httpInstrumentation],
226
- });
227
-
228
- sdk.start();
229
- console.log('[securenow] 🚀 Nuxt OTel SDK started → %s', tracesUrl);
230
- console.log(
231
- '[securenow] service.name=%s instance.id=%s',
232
- serviceName,
233
- serviceInstanceId,
234
- );
235
-
236
- // ── Logging ──
237
- // Opt-out default: set config.logging.enabled=false (or opts.logging=false) to disable.
238
- const loggingEnabled =
239
- opts.logging ??
240
- appConfig.boolConfig('logging.enabled', true);
241
-
242
- let loggerProvider = null;
243
-
244
- if (loggingEnabled) {
245
- try {
246
- const { OTLPLogExporter } = await import(
247
- '@opentelemetry/exporter-logs-otlp-http'
248
- );
249
- const { LoggerProvider, BatchLogRecordProcessor } = await import(
250
- '@opentelemetry/sdk-logs'
251
- );
252
-
253
- const logExporter = new OTLPLogExporter({ url: logsUrl, headers });
254
- loggerProvider = new LoggerProvider({
255
- resource,
256
- processors: [new BatchLogRecordProcessor(logExporter)],
257
- });
258
-
259
- const logger = loggerProvider.getLogger('console', '1.0.0');
260
- const SEV = { DEBUG: 5, INFO: 9, WARN: 13, ERROR: 17 };
261
- const orig = {
262
- log: console.log,
263
- info: console.info,
264
- warn: console.warn,
265
- error: console.error,
266
- debug: console.debug,
267
- };
268
-
269
- function emitLog(sn, st, args) {
270
- try {
271
- const ctx = otelContext.active();
272
- const spanCtx = otelTrace.getSpanContext(ctx);
273
- logger.emit({
274
- severityNumber: sn,
275
- severityText: st,
276
- body: args
277
- .map((a) =>
278
- typeof a === 'object' && a !== null ? JSON.stringify(a) : String(a),
279
- )
280
- .join(' '),
281
- attributes: { 'log.source': 'console', 'log.method': st.toLowerCase() },
282
- ...(spanCtx && { context: ctx }),
283
- });
284
- } catch {
285
- // swallow
286
- }
287
- }
288
-
289
- console.log = (...a) => { emitLog(SEV.INFO, 'INFO', a); orig.log.apply(console, a); };
290
- console.info = (...a) => { emitLog(SEV.INFO, 'INFO', a); orig.info.apply(console, a); };
291
- console.warn = (...a) => { emitLog(SEV.WARN, 'WARN', a); orig.warn.apply(console, a); };
292
- console.error = (...a) => { emitLog(SEV.ERROR, 'ERROR', a); orig.error.apply(console, a); };
293
- console.debug = (...a) => { emitLog(SEV.DEBUG, 'DEBUG', a); orig.debug.apply(console, a); };
294
-
295
- console.log('[securenow] 📋 Logging: ENABLED → %s', logsUrl);
296
- } catch (e) {
297
- console.warn('[securenow] ⚠️ Logging setup failed:', e.message);
298
- }
299
- } else {
300
- console.log(
301
- '[securenow] Logging: DISABLED (config.logging.enabled=false)',
302
- );
303
- }
304
-
305
- if (captureBody) {
306
- console.log(
307
- '[securenow] 📝 Body capture: ENABLED (max %d bytes, redacting %d fields)',
308
- maxBodySize,
309
- allSensitiveFields.length,
310
- );
311
- }
312
-
313
- // ── Free trial banner ──
314
- try {
315
- const { isFreeTrial, patchHttpForBanner } = await import('./free-trial-banner.js');
316
- if (isFreeTrial(endpointBase) && !appConfig.boolConfig('runtime.hideBanner', false)) {
317
- patchHttpForBanner();
318
- }
319
- } catch {
320
- // not critical
321
- }
322
-
323
- // ── Firewall — runs independently from OTel ──
324
- const firewallOptions = appConfig.resolveFirewallOptions();
325
- if (firewallOptions.apiKey) {
326
- try {
327
- const { init: fwInit } = await import('./firewall.js');
328
- fwInit({
329
- apiKey: firewallOptions.apiKey,
330
- appKey: firewallOptions.appKey,
331
- environment: deploymentEnvironment || firewallOptions.environment,
332
- apiUrl: firewallOptions.apiUrl,
333
- apiUrlFallbacks: firewallOptions.apiUrlFallbacks,
334
- versionCheckInterval: firewallOptions.versionCheckInterval,
335
- syncInterval: firewallOptions.syncInterval,
336
- failMode: firewallOptions.failMode,
337
- statusCode: firewallOptions.statusCode,
338
- log: firewallOptions.log,
339
- tcp: firewallOptions.tcp,
340
- iptables: firewallOptions.iptables,
341
- cloud: firewallOptions.cloud,
342
- cloudDryRun: firewallOptions.cloudDryRun,
343
- cloudflare: firewallOptions.cloudflare,
344
- aws: firewallOptions.aws,
345
- gcp: firewallOptions.gcp,
346
- });
347
- } catch (e) {
348
- console.warn('[securenow] Firewall init failed:', e.message);
349
- }
350
- }
351
-
352
- // ── Graceful shutdown ──
353
- const shutdown = async (sig) => {
354
- try {
355
- await sdk.shutdown?.();
356
- if (loggerProvider) await loggerProvider.shutdown?.();
357
- try { const fw = await import('./firewall.js'); fw.shutdown?.(); } catch {}
358
- console.log(`[securenow] Shut down on ${sig}`);
359
- } catch {
360
- // swallow
361
- }
362
- };
363
- process.on('SIGINT', () => shutdown('SIGINT'));
364
- process.on('SIGTERM', () => shutdown('SIGTERM'));
365
-
366
- // ── Nitro request hooks for span enrichment ──
367
- const tracer = otelTrace.getTracer('securenow-nuxt', '1.0.0');
368
- const spanMap = new WeakMap();
369
-
370
- nitroApp.hooks.hook('request', (event) => {
371
- try {
372
- const req = event.node.req;
373
- const method = event.method || req.method || 'GET';
374
- const path = event.path || req.url || '/';
375
-
376
- const span = tracer.startSpan(`${method} ${path}`, {
377
- attributes: {
378
- 'http.method': method,
379
- 'http.target': path,
380
- 'http.url': `${req.headers?.['x-forwarded-proto'] || 'http'}://${req.headers?.host || 'localhost'}${path}`,
381
- 'component': 'nuxt-nitro',
382
- },
383
- });
384
-
385
- spanMap.set(event, span);
386
- } catch {
387
- // never break the request
388
- }
389
- });
390
-
391
- nitroApp.hooks.hook('afterResponse', (event) => {
392
- try {
393
- const span = spanMap.get(event);
394
- if (!span) return;
395
-
396
- const status = event.node.res.statusCode || 200;
397
- span.setAttribute('http.status_code', status);
398
-
399
- if (status >= 500) {
400
- span.setStatus({ code: SpanStatusCode.ERROR, message: `HTTP ${status}` });
401
- }
402
-
403
- span.end();
404
- spanMap.delete(event);
405
- } catch {
406
- // swallow
407
- }
408
- });
409
-
410
- nitroApp.hooks.hook('error', (error, { event }) => {
411
- try {
412
- const span = event ? spanMap.get(event) : null;
413
- if (span) {
414
- span.recordException(error);
415
- span.setStatus({
416
- code: SpanStatusCode.ERROR,
417
- message: error.message || 'Internal Server Error',
418
- });
419
- span.end();
420
- spanMap.delete(event);
421
- }
422
- } catch {
423
- // swallow
424
- }
425
- });
426
- });
1
+ /**
2
+ * SecureNow Nitro Server Plugin
3
+ *
4
+ * Initialises the OpenTelemetry SDK and hooks into Nitro's request lifecycle
5
+ * to create spans, capture metadata, and forward logs.
6
+ *
7
+ * This file is registered by the Nuxt module (nuxt.mjs) via addServerPlugin.
8
+ */
9
+
10
+ import * as otelResources from '@opentelemetry/resources';
11
+ import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
12
+ import {
13
+ context as otelContext,
14
+ trace as otelTrace,
15
+ SpanStatusCode,
16
+ } from '@opentelemetry/api';
17
+ import { createRequire } from 'node:module';
18
+ import { randomUUID } from 'node:crypto';
19
+
20
+ const nodeRequire = createRequire(import.meta.url);
21
+ const appConfig = nodeRequire('./app-config');
22
+ const { resolveClientIpWithDetails } = nodeRequire('./resolve-ip');
23
+ const { nodeSdkDefaultTelemetryOptions } = nodeRequire('./otel-defaults');
24
+ const nodeSdkTelemetryOptions = nodeSdkDefaultTelemetryOptions();
25
+
26
+ const { NodeSDK } = nodeRequire('@opentelemetry/sdk-node');
27
+ const { OTLPTraceExporter } = nodeRequire('@opentelemetry/exporter-trace-otlp-http');
28
+ const { HttpInstrumentation } = nodeRequire('@opentelemetry/instrumentation-http');
29
+
30
+ // ── Helpers ──
31
+
32
+ function createResource(attributes) {
33
+ if (typeof otelResources.resourceFromAttributes === 'function') {
34
+ return otelResources.resourceFromAttributes(attributes);
35
+ }
36
+ if (typeof otelResources.Resource === 'function') {
37
+ return new otelResources.Resource(attributes);
38
+ }
39
+ throw new Error('Unsupported @opentelemetry/resources version');
40
+ }
41
+
42
+ // Default sensitive fields to redact from request bodies.
43
+ // Matched substring-wise against lowercased keys (see redactSensitiveData),
44
+ // so e.g. 'card' also catches 'creditCard' and 'account' also catches
45
+ // 'accountNumber'. Over-redaction here is intentional: a falsely-redacted
46
+ // telemetry value is always safer than a leaked secret. Entries are kept
47
+ // specific enough to avoid nuking broad benign keys (e.g. we use
48
+ // 'firstname'/'lastname'/'fullname', never bare 'name').
49
+ const DEFAULT_SENSITIVE_FIELDS = [
50
+ // credentials / auth
51
+ 'password', 'passwd', 'pwd', 'secret', 'token', 'api_key', 'apikey',
52
+ 'access_token', 'auth', 'authorization', 'bearer', 'credentials',
53
+ 'mysql_pwd', 'otp', 'mfa', 'totp', 'sessionid', 'session_id',
54
+ 'cookie', 'set-cookie',
55
+ // financial
56
+ 'stripeToken', 'card', 'cardnumber', 'ccv', 'cvc', 'cvv',
57
+ 'iban', 'account', 'accountnumber', 'routing', 'sortcode', 'taxid',
58
+ // PII
59
+ 'ssn', 'pin', 'email', 'e_mail', 'phone', 'mobile', 'dob', 'birthdate',
60
+ 'firstname', 'lastname', 'fullname', 'address', 'postcode', 'zip',
61
+ 'passport', 'license',
62
+ ];
63
+
64
+ // Conservative value-shape redactors. Key-name matching misses secrets that
65
+ // land in free-form string values (GraphQL bodies, message fields, etc.), so
66
+ // as a second layer we scrub string VALUES that *look like* a secret/PII.
67
+ // These are intentionally precise/bounded so they don't garble normal prose,
68
+ // and they only ever transform captured telemetry strings (read-only) — never
69
+ // the actual request/response stream. Compiled once at module load.
70
+ const VALUE_REDACTORS = [
71
+ // JWT: three base64url segments. Anchored to the eyJ header so it won't
72
+ // match arbitrary dotted tokens.
73
+ { name: 'jwt', re: /eyJ[A-Za-z0-9_-]{6,}\.[A-Za-z0-9_-]{4,}\.[A-Za-z0-9_-]{4,}/g },
74
+ // Bearer/Basic auth header value embedded in a body string.
75
+ { name: 'bearer', re: /\b(?:Bearer|Basic)\s+[A-Za-z0-9._~+/=-]{8,}/gi },
76
+ // Stripe-style / SecureNow live+test API keys.
77
+ { name: 'apikey', re: /\b(?:sk|pk|rk|snk)_(?:live|test)_[A-Za-z0-9]{8,}/g },
78
+ // Email addresses (bounded local/domain parts).
79
+ { name: 'email', re: /\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,24}\b/g },
80
+ // Credit-card-like: 13–19 digit runs that pass a Luhn check, allowing
81
+ // space/dash grouping. Luhn-gated to avoid clobbering ordinary long numbers.
82
+ { name: 'card', re: /\b(?:\d[ -]?){13,19}\b/g, luhn: true },
83
+ ];
84
+
85
+ // Skip the value-shape scan on very large strings to bound per-body cost.
86
+ const MAX_VALUE_SCAN_LENGTH = 16384;
87
+
88
+ function luhnValid(digits) {
89
+ let sum = 0;
90
+ let alt = false;
91
+ for (let i = digits.length - 1; i >= 0; i--) {
92
+ let d = digits.charCodeAt(i) - 48;
93
+ if (d < 0 || d > 9) return false;
94
+ if (alt) { d *= 2; if (d > 9) d -= 9; }
95
+ sum += d;
96
+ alt = !alt;
97
+ }
98
+ return sum % 10 === 0;
99
+ }
100
+
101
+ /**
102
+ * Redact obvious secret/PII shapes inside a captured string VALUE.
103
+ * Returns the input unchanged when nothing matches (cheap common case).
104
+ */
105
+ function redactSensitiveValue(value) {
106
+ if (typeof value !== 'string' || value.length === 0) return value;
107
+ if (value.length > MAX_VALUE_SCAN_LENGTH) return value; // bound the work
108
+ let out = value;
109
+ for (const r of VALUE_REDACTORS) {
110
+ r.re.lastIndex = 0;
111
+ if (r.luhn) {
112
+ out = out.replace(r.re, (m) => {
113
+ const digits = m.replace(/[ -]/g, '');
114
+ if (digits.length < 13 || digits.length > 19) return m;
115
+ return luhnValid(digits) ? '[REDACTED]' : m;
116
+ });
117
+ } else {
118
+ out = out.replace(r.re, '[REDACTED]');
119
+ }
120
+ }
121
+ return out;
122
+ }
123
+
124
+ function redactSensitiveData(obj, fields = DEFAULT_SENSITIVE_FIELDS) {
125
+ if (!obj || typeof obj !== 'object') return obj;
126
+ const redacted = Array.isArray(obj) ? [...obj] : { ...obj };
127
+ for (const key of Object.keys(redacted)) {
128
+ const lower = key.toLowerCase();
129
+ if (fields.some((f) => lower.includes(f.toLowerCase()))) {
130
+ redacted[key] = '[REDACTED]';
131
+ } else if (typeof redacted[key] === 'object' && redacted[key] !== null) {
132
+ redacted[key] = redactSensitiveData(redacted[key], fields);
133
+ } else if (typeof redacted[key] === 'string') {
134
+ // Second layer: even if the key name looks benign, scrub values that
135
+ // *look like* a secret/PII (JWT, bearer token, API key, email, card).
136
+ redacted[key] = redactSensitiveValue(redacted[key]);
137
+ }
138
+ }
139
+ return redacted;
140
+ }
141
+
142
+ // ── Runtime config helpers ──
143
+
144
+ function getRuntimeOptions() {
145
+ try {
146
+ const cfg = useRuntimeConfig();
147
+ return cfg.securenow || {};
148
+ } catch {
149
+ return {};
150
+ }
151
+ }
152
+
153
+ // ── Plugin ──
154
+
155
+ export default defineNitroPlugin(async (nitroApp) => {
156
+ const opts = getRuntimeOptions();
157
+
158
+ // Resolution order: opts -> .securenow/credentials.json -> package.json#name
159
+ const resolvedApp = appConfig.resolveAll();
160
+
161
+ // ── Naming ──
162
+ const rawBase = (opts.serviceName || resolvedApp.appId || '').trim().replace(/^['"]|['"]$/g, '');
163
+ const baseName = rawBase || null;
164
+ // Default: auto-disable per-worker suffix when logged in (appId is the
165
+ // routing UUID and the dashboard does exact match). opts.noUuid or
166
+ // config.runtime.noUuid or opts.noUuid override.
167
+ const noUuid = appConfig.resolveNoUuid({ noUuid: opts.noUuid });
168
+ const deploymentEnvironment = appConfig.normalizeDeploymentEnvironment(
169
+ opts.environment || resolvedApp.deploymentEnvironment
170
+ );
171
+
172
+ let serviceName;
173
+ if (baseName) {
174
+ serviceName = noUuid ? baseName : `${baseName}-${randomUUID()}`;
175
+ } else {
176
+ serviceName = `nuxt-app-${randomUUID()}`;
177
+ console.warn(
178
+ '[securenow] ⚠️ No app identity resolved. Using fallback: %s',
179
+ serviceName,
180
+ );
181
+ console.warn(
182
+ '[securenow] Run `npx securenow login` or set app.key in .securenow/credentials.json',
183
+ );
184
+ }
185
+
186
+ const serviceInstanceId = `${baseName || 'securenow'}-${randomUUID()}`;
187
+
188
+ // ── Endpoints ──
189
+ const resolvedEndpoints = appConfig.resolveEndpoints({ endpoint: opts.endpoint || resolvedApp.instance });
190
+ const endpointBase = resolvedEndpoints.endpointBase;
191
+ const tracesUrl = resolvedEndpoints.tracesUrl;
192
+ const logsUrl = resolvedEndpoints.logsUrl;
193
+ const headers = resolvedEndpoints.headers;
194
+
195
+ // ── Resource ──
196
+ const resource = createResource({
197
+ [SemanticResourceAttributes.SERVICE_NAME]: serviceName,
198
+ [SemanticResourceAttributes.SERVICE_INSTANCE_ID]: serviceInstanceId,
199
+ [SemanticResourceAttributes.DEPLOYMENT_ENVIRONMENT]: deploymentEnvironment,
200
+ [SemanticResourceAttributes.SERVICE_VERSION]:
201
+ process.env.npm_package_version || undefined,
202
+ 'framework': 'nuxt',
203
+ });
204
+
205
+ // ── Body capture config ──
206
+ // Opt-out default: set config.capture.body=false (or opts.captureBody=false) to disable.
207
+ const captureBody =
208
+ opts.captureBody ??
209
+ appConfig.boolConfig('capture.body', true);
210
+ const maxBodySize = appConfig.numberConfig('capture.maxBodySize', 10240, 1024);
211
+ const customSensitiveFields = appConfig.listConfig('capture.sensitiveFields');
212
+ const allSensitiveFields = [...DEFAULT_SENSITIVE_FIELDS, ...customSensitiveFields];
213
+
214
+ // ── HTTP instrumentation ──
215
+ const httpInstrumentation = new HttpInstrumentation({
216
+ requestHook: (span, request) => {
217
+ try {
218
+ const hdrs = request.headers || {};
219
+ const ipDetails = resolveClientIpWithDetails(request);
220
+ const clientIp = ipDetails.ip || 'unknown';
221
+
222
+ span.setAttributes({
223
+ 'http.client_ip': clientIp,
224
+ 'http.client_ip.source': ipDetails.source,
225
+ 'http.socket_ip': ipDetails.socketIp || '',
226
+ 'http.forwarded_for': ipDetails.forwardedFor || '',
227
+ 'http.real_ip': ipDetails.realIp || '',
228
+ 'http.proxy.trusted': String(!!ipDetails.trustedProxy),
229
+ 'http.request.header.x_forwarded_for': ipDetails.forwardedFor || '',
230
+ 'http.request.header.x_real_ip': ipDetails.realIp || '',
231
+ 'http.request.header.cf_connecting_ip': ipDetails.cfConnectingIp || '',
232
+ 'http.request.header.true_client_ip': ipDetails.trueClientIp || '',
233
+ 'http.request.header.x_client_ip': ipDetails.clientIp || '',
234
+ 'http.user_agent': hdrs['user-agent'] || '',
235
+ 'http.host': hdrs['x-forwarded-host'] || hdrs['host'] || '',
236
+ 'http.scheme':
237
+ hdrs['x-forwarded-proto'] ||
238
+ (request.socket?.encrypted ? 'https' : 'http'),
239
+ 'http.referer': hdrs['referer'] || '',
240
+ 'http.origin': hdrs['origin'] || '',
241
+ 'http.request_id':
242
+ hdrs['x-request-id'] || hdrs['x-trace-id'] || '',
243
+ });
244
+
245
+ if (hdrs['authorization']) {
246
+ span.setAttribute('http.security.auth_present', 'true');
247
+ }
248
+ if (hdrs['cookie']) {
249
+ span.setAttribute('http.security.cookies_present', 'true');
250
+ }
251
+
252
+ // Body capture via stream listener (same approach as tracing.js)
253
+ if (
254
+ captureBody &&
255
+ request.method &&
256
+ ['POST', 'PUT', 'PATCH'].includes(request.method)
257
+ ) {
258
+ const ct = hdrs['content-type'] || '';
259
+ if (
260
+ ct.includes('application/json') ||
261
+ ct.includes('application/graphql') ||
262
+ ct.includes('application/x-www-form-urlencoded')
263
+ ) {
264
+ const chunks = [];
265
+ let size = 0;
266
+ request.on('data', (chunk) => {
267
+ size += chunk.length;
268
+ if (size <= maxBodySize) chunks.push(chunk);
269
+ });
270
+ request.on('end', () => {
271
+ if (size > maxBodySize) {
272
+ span.setAttribute('http.request.body', `[TOO LARGE: ${size} bytes]`);
273
+ return;
274
+ }
275
+ if (chunks.length === 0) return;
276
+ const raw = Buffer.concat(chunks).toString('utf8');
277
+ try {
278
+ const parsed = JSON.parse(raw);
279
+ const redacted = redactSensitiveData(parsed, allSensitiveFields);
280
+ span.setAttributes({
281
+ 'http.request.body': JSON.stringify(redacted).substring(0, maxBodySize),
282
+ 'http.request.body.type': ct.includes('graphql') ? 'graphql' : 'json',
283
+ 'http.request.body.size': size,
284
+ });
285
+ } catch {
286
+ span.setAttribute('http.request.body', '[UNPARSEABLE - REDACTED FOR SAFETY]');
287
+ span.setAttribute('http.request.body.parse_error', true);
288
+ }
289
+ });
290
+ }
291
+ }
292
+ } catch {
293
+ // never break the request
294
+ }
295
+ },
296
+ });
297
+
298
+ // ── Trace exporter + SDK ──
299
+ const traceExporter = new OTLPTraceExporter({ url: tracesUrl, headers });
300
+
301
+ const sdk = new NodeSDK({
302
+ ...nodeSdkTelemetryOptions,
303
+ traceExporter,
304
+ resource,
305
+ instrumentations: [httpInstrumentation],
306
+ });
307
+
308
+ sdk.start();
309
+ console.log('[securenow] 🚀 Nuxt OTel SDK started → %s', tracesUrl);
310
+ console.log(
311
+ '[securenow] service.name=%s instance.id=%s',
312
+ serviceName,
313
+ serviceInstanceId,
314
+ );
315
+
316
+ // ── Logging ──
317
+ // Opt-out default: set config.logging.enabled=false (or opts.logging=false) to disable.
318
+ const loggingEnabled =
319
+ opts.logging ??
320
+ appConfig.boolConfig('logging.enabled', true);
321
+
322
+ let loggerProvider = null;
323
+
324
+ if (loggingEnabled) {
325
+ try {
326
+ const { OTLPLogExporter } = await import(
327
+ '@opentelemetry/exporter-logs-otlp-http'
328
+ );
329
+ const { LoggerProvider, BatchLogRecordProcessor } = await import(
330
+ '@opentelemetry/sdk-logs'
331
+ );
332
+
333
+ const logExporter = new OTLPLogExporter({ url: logsUrl, headers });
334
+ loggerProvider = new LoggerProvider({
335
+ resource,
336
+ processors: [new BatchLogRecordProcessor(logExporter)],
337
+ });
338
+
339
+ const logger = loggerProvider.getLogger('console', '1.0.0');
340
+ const SEV = { DEBUG: 5, INFO: 9, WARN: 13, ERROR: 17 };
341
+ const orig = {
342
+ log: console.log,
343
+ info: console.info,
344
+ warn: console.warn,
345
+ error: console.error,
346
+ debug: console.debug,
347
+ };
348
+
349
+ function emitLog(sn, st, args) {
350
+ try {
351
+ const ctx = otelContext.active();
352
+ const spanCtx = otelTrace.getSpanContext(ctx);
353
+ logger.emit({
354
+ severityNumber: sn,
355
+ severityText: st,
356
+ body: args
357
+ .map((a) =>
358
+ typeof a === 'object' && a !== null ? JSON.stringify(a) : String(a),
359
+ )
360
+ .join(' '),
361
+ attributes: { 'log.source': 'console', 'log.method': st.toLowerCase() },
362
+ ...(spanCtx && { context: ctx }),
363
+ });
364
+ } catch {
365
+ // swallow
366
+ }
367
+ }
368
+
369
+ console.log = (...a) => { emitLog(SEV.INFO, 'INFO', a); orig.log.apply(console, a); };
370
+ console.info = (...a) => { emitLog(SEV.INFO, 'INFO', a); orig.info.apply(console, a); };
371
+ console.warn = (...a) => { emitLog(SEV.WARN, 'WARN', a); orig.warn.apply(console, a); };
372
+ console.error = (...a) => { emitLog(SEV.ERROR, 'ERROR', a); orig.error.apply(console, a); };
373
+ console.debug = (...a) => { emitLog(SEV.DEBUG, 'DEBUG', a); orig.debug.apply(console, a); };
374
+
375
+ console.log('[securenow] 📋 Logging: ENABLED → %s', logsUrl);
376
+ } catch (e) {
377
+ console.warn('[securenow] ⚠️ Logging setup failed:', e.message);
378
+ }
379
+ } else {
380
+ console.log(
381
+ '[securenow] Logging: DISABLED (config.logging.enabled=false)',
382
+ );
383
+ }
384
+
385
+ if (captureBody) {
386
+ console.log(
387
+ '[securenow] 📝 Body capture: ENABLED (max %d bytes, redacting %d fields)',
388
+ maxBodySize,
389
+ allSensitiveFields.length,
390
+ );
391
+ }
392
+
393
+ // ── Free trial banner ──
394
+ try {
395
+ const { isFreeTrial, patchHttpForBanner } = await import('./free-trial-banner.js');
396
+ if (isFreeTrial(endpointBase) && !appConfig.boolConfig('runtime.hideBanner', false)) {
397
+ patchHttpForBanner();
398
+ }
399
+ } catch {
400
+ // not critical
401
+ }
402
+
403
+ // ── Firewall — runs independently from OTel ──
404
+ const firewallOptions = appConfig.resolveFirewallOptions();
405
+ if (firewallOptions.apiKey) {
406
+ try {
407
+ const { init: fwInit } = await import('./firewall.js');
408
+ fwInit({
409
+ apiKey: firewallOptions.apiKey,
410
+ appKey: firewallOptions.appKey,
411
+ environment: deploymentEnvironment || firewallOptions.environment,
412
+ apiUrl: firewallOptions.apiUrl,
413
+ apiUrlFallbacks: firewallOptions.apiUrlFallbacks,
414
+ versionCheckInterval: firewallOptions.versionCheckInterval,
415
+ syncInterval: firewallOptions.syncInterval,
416
+ failMode: firewallOptions.failMode,
417
+ statusCode: firewallOptions.statusCode,
418
+ log: firewallOptions.log,
419
+ tcp: firewallOptions.tcp,
420
+ iptables: firewallOptions.iptables,
421
+ cloud: firewallOptions.cloud,
422
+ cloudDryRun: firewallOptions.cloudDryRun,
423
+ cloudflare: firewallOptions.cloudflare,
424
+ aws: firewallOptions.aws,
425
+ gcp: firewallOptions.gcp,
426
+ });
427
+ } catch (e) {
428
+ console.warn('[securenow] Firewall init failed:', e.message);
429
+ }
430
+ }
431
+
432
+ // ── Graceful shutdown ──
433
+ const shutdown = async (sig) => {
434
+ try {
435
+ await sdk.shutdown?.();
436
+ if (loggerProvider) await loggerProvider.shutdown?.();
437
+ try { const fw = await import('./firewall.js'); fw.shutdown?.(); } catch {}
438
+ console.log(`[securenow] Shut down on ${sig}`);
439
+ } catch {
440
+ // swallow
441
+ }
442
+ };
443
+ process.on('SIGINT', () => shutdown('SIGINT'));
444
+ process.on('SIGTERM', () => shutdown('SIGTERM'));
445
+
446
+ // ── Nitro request hooks for span enrichment ──
447
+ const tracer = otelTrace.getTracer('securenow-nuxt', '1.0.0');
448
+ const spanMap = new WeakMap();
449
+
450
+ nitroApp.hooks.hook('request', (event) => {
451
+ try {
452
+ const req = event.node.req;
453
+ const method = event.method || req.method || 'GET';
454
+ const path = event.path || req.url || '/';
455
+
456
+ const span = tracer.startSpan(`${method} ${path}`, {
457
+ attributes: {
458
+ 'http.method': method,
459
+ 'http.target': path,
460
+ 'http.url': `${req.headers?.['x-forwarded-proto'] || 'http'}://${req.headers?.host || 'localhost'}${path}`,
461
+ 'component': 'nuxt-nitro',
462
+ },
463
+ });
464
+
465
+ spanMap.set(event, span);
466
+ } catch {
467
+ // never break the request
468
+ }
469
+ });
470
+
471
+ nitroApp.hooks.hook('afterResponse', (event) => {
472
+ try {
473
+ const span = spanMap.get(event);
474
+ if (!span) return;
475
+
476
+ const status = event.node.res.statusCode || 200;
477
+ span.setAttribute('http.status_code', status);
478
+
479
+ if (status >= 500) {
480
+ span.setStatus({ code: SpanStatusCode.ERROR, message: `HTTP ${status}` });
481
+ }
482
+
483
+ span.end();
484
+ spanMap.delete(event);
485
+ } catch {
486
+ // swallow
487
+ }
488
+ });
489
+
490
+ nitroApp.hooks.hook('error', (error, { event }) => {
491
+ try {
492
+ const span = event ? spanMap.get(event) : null;
493
+ if (span) {
494
+ span.recordException(error);
495
+ span.setStatus({
496
+ code: SpanStatusCode.ERROR,
497
+ message: error.message || 'Internal Server Error',
498
+ });
499
+ span.end();
500
+ spanMap.delete(event);
501
+ }
502
+ } catch {
503
+ // swallow
504
+ }
505
+ });
506
+ });