securenow 7.5.1 → 7.6.1

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.
Files changed (50) hide show
  1. package/CONSUMING-APPS-GUIDE.md +2 -0
  2. package/NPM_README.md +201 -237
  3. package/README.md +73 -26
  4. package/SKILL-API.md +205 -205
  5. package/SKILL-CLI.md +71 -64
  6. package/app-config.js +479 -83
  7. package/cli/apiKey.js +1 -1
  8. package/cli/apps.js +1 -1
  9. package/cli/config.js +31 -12
  10. package/cli/credentials.js +88 -0
  11. package/cli/diagnostics.js +68 -104
  12. package/cli/firewall.js +29 -14
  13. package/cli/init.js +211 -212
  14. package/cli/monitor.js +107 -43
  15. package/cli/security.js +24 -12
  16. package/cli/utils.js +2 -1
  17. package/cli.js +72 -40
  18. package/console-instrumentation.js +1 -1
  19. package/docs/ENVIRONMENT-VARIABLES.md +137 -863
  20. package/docs/ENVIRONMENTS.md +60 -0
  21. package/docs/EXPRESS-SETUP-GUIDE.md +3 -0
  22. package/docs/FIREWALL-GUIDE.md +3 -0
  23. package/docs/INDEX.md +6 -8
  24. package/docs/LOGGING-GUIDE.md +3 -0
  25. package/docs/MCP-GUIDE.md +8 -0
  26. package/docs/NEXTJS-GUIDE.md +3 -0
  27. package/docs/NEXTJS-QUICKSTART.md +22 -16
  28. package/docs/NUXT-GUIDE.md +3 -0
  29. package/docs/QUICKSTART-BODY-CAPTURE.md +3 -0
  30. package/docs/REQUEST-BODY-CAPTURE.md +3 -0
  31. package/firewall-cloud.js +10 -10
  32. package/firewall-only.js +25 -23
  33. package/firewall.js +47 -29
  34. package/free-trial-banner.js +1 -1
  35. package/mcp/catalog.js +104 -17
  36. package/nextjs-auto-capture.d.ts +7 -4
  37. package/nextjs-auto-capture.js +7 -7
  38. package/nextjs-middleware.js +4 -3
  39. package/nextjs-wrapper.js +6 -6
  40. package/nextjs.d.ts +36 -25
  41. package/nextjs.js +48 -55
  42. package/nuxt-server-plugin.mjs +35 -51
  43. package/nuxt.d.ts +29 -23
  44. package/package.json +1 -1
  45. package/postinstall.js +27 -61
  46. package/register.d.ts +19 -33
  47. package/register.js +8 -8
  48. package/resolve-ip.js +4 -5
  49. package/tracing.d.ts +21 -19
  50. package/tracing.js +34 -42
package/cli/init.js CHANGED
@@ -1,244 +1,243 @@
1
- 'use strict';
2
-
1
+ 'use strict';
2
+
3
3
  const fs = require('fs');
4
4
  const path = require('path');
5
5
  const ui = require('./ui');
6
6
  const config = require('./config');
7
7
 
8
- const DEFAULT_ENV = {
9
- SECURENOW_LOGGING_ENABLED: '1',
10
- SECURENOW_CAPTURE_BODY: '1',
11
- SECURENOW_CAPTURE_MULTIPART: '1',
12
- SECURENOW_FIREWALL_ENABLED: '1',
13
- };
14
-
15
- const INSTRUMENTATION_JS = `import { createRequire } from 'node:module';
16
-
17
- const require = createRequire(import.meta.url);
18
-
19
- export async function register() {
8
+ const INSTRUMENTATION = `export async function register() {
20
9
  if (process.env.NEXT_RUNTIME !== 'nodejs') return;
21
10
 
22
- process.env.SECURENOW_LOGGING_ENABLED ??= '1';
23
- process.env.SECURENOW_CAPTURE_BODY ??= '1';
24
- process.env.SECURENOW_CAPTURE_MULTIPART ??= '1';
25
- process.env.SECURENOW_FIREWALL_ENABLED ??= '1';
26
-
27
- const { registerSecureNow } = require('securenow/nextjs');
11
+ const securenowNext = await import(/* webpackIgnore: true */ 'securenow/nextjs');
12
+ const registerSecureNow = securenowNext.registerSecureNow || securenowNext.default?.registerSecureNow;
28
13
  registerSecureNow({ captureBody: true });
29
- require('securenow/nextjs-auto-capture');
14
+ await import(/* webpackIgnore: true */ 'securenow/nextjs-auto-capture');
30
15
  }
31
16
  `;
32
17
 
33
- const INSTRUMENTATION_TS = `import { createRequire } from 'node:module';
18
+ function detectProject(dir) {
19
+ const pkgPath = path.join(dir, 'package.json');
20
+ if (!fs.existsSync(pkgPath)) return { framework: 'unknown' };
34
21
 
35
- const require = createRequire(import.meta.url);
22
+ const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8').replace(/^\uFEFF/, ''));
23
+ const allDeps = { ...pkg.dependencies, ...pkg.devDependencies };
36
24
 
37
- export async function register() {
38
- if (process.env.NEXT_RUNTIME !== 'nodejs') return;
25
+ if (allDeps.next) return { framework: 'nextjs', pkg, pkgPath, nextVersion: allDeps.next };
26
+ if (allDeps.nuxt) return { framework: 'nuxt', pkg, pkgPath };
27
+ if (allDeps.express) return { framework: 'express', pkg, pkgPath };
28
+ if (allDeps.fastify) return { framework: 'fastify', pkg, pkgPath };
29
+ if (allDeps.koa) return { framework: 'koa', pkg, pkgPath };
30
+ if (allDeps.hapi || allDeps['@hapi/hapi']) return { framework: 'hapi', pkg, pkgPath };
31
+ return { framework: 'node', pkg, pkgPath };
32
+ }
39
33
 
40
- process.env.SECURENOW_LOGGING_ENABLED ??= '1';
41
- process.env.SECURENOW_CAPTURE_BODY ??= '1';
42
- process.env.SECURENOW_CAPTURE_MULTIPART ??= '1';
43
- process.env.SECURENOW_FIREWALL_ENABLED ??= '1';
34
+ function findInstrumentationFile(dir) {
35
+ for (const name of ['instrumentation.ts', 'instrumentation.js', 'src/instrumentation.ts', 'src/instrumentation.js']) {
36
+ const p = path.join(dir, name);
37
+ if (fs.existsSync(p)) return p;
38
+ }
39
+ return null;
40
+ }
44
41
 
45
- const { registerSecureNow } = require('securenow/nextjs');
46
- registerSecureNow({ captureBody: true });
47
- require('securenow/nextjs-auto-capture');
42
+ function findNextConfig(dir) {
43
+ for (const name of ['next.config.js', 'next.config.mjs', 'next.config.ts']) {
44
+ const p = path.join(dir, name);
45
+ if (fs.existsSync(p)) return p;
46
+ }
47
+ return null;
48
48
  }
49
- `;
50
-
51
- function detectProject(dir) {
52
- const pkgPath = path.join(dir, 'package.json');
53
- if (!fs.existsSync(pkgPath)) return { framework: 'unknown' };
54
-
55
- const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
56
- const allDeps = { ...pkg.dependencies, ...pkg.devDependencies };
57
-
58
- if (allDeps.next) return { framework: 'nextjs', pkg, pkgPath, nextVersion: allDeps.next };
59
- if (allDeps.nuxt) return { framework: 'nuxt', pkg, pkgPath };
60
- if (allDeps.express) return { framework: 'express', pkg, pkgPath };
61
- if (allDeps.fastify) return { framework: 'fastify', pkg, pkgPath };
62
- if (allDeps.koa) return { framework: 'koa', pkg, pkgPath };
63
- if (allDeps.hapi || allDeps['@hapi/hapi']) return { framework: 'hapi', pkg, pkgPath };
64
- return { framework: 'node', pkg, pkgPath };
65
- }
66
-
67
- function findInstrumentationFile(dir) {
68
- for (const name of ['instrumentation.ts', 'instrumentation.js', 'src/instrumentation.ts', 'src/instrumentation.js']) {
69
- const p = path.join(dir, name);
70
- if (fs.existsSync(p)) return p;
71
- }
72
- return null;
73
- }
74
-
75
- function findNextConfig(dir) {
76
- for (const name of ['next.config.js', 'next.config.mjs', 'next.config.ts']) {
77
- const p = path.join(dir, name);
78
- if (fs.existsSync(p)) return p;
79
- }
80
- return null;
81
- }
82
-
83
- function hasTypeScript(dir) {
84
- return fs.existsSync(path.join(dir, 'tsconfig.json'));
85
- }
86
-
87
- async function init(_args, flags) {
88
- const dir = process.cwd();
89
- const project = detectProject(dir);
90
-
91
- ui.header('SecureNow Project Setup');
92
-
93
- if (project.framework === 'unknown') {
94
- ui.error('No package.json found. Run this command in your project root.');
95
- process.exit(1);
96
- }
97
-
98
- ui.info(`Detected framework: ${ui.bold(project.framework)}`);
99
-
100
- if (project.framework === 'nextjs') {
101
- await initNextJs(dir, project, flags);
102
- } else if (project.framework === 'nuxt') {
103
- initNuxt(dir, project);
104
- } else {
105
- initNode(dir, project);
106
- }
107
-
108
- initEnv(dir, flags);
109
-
110
- console.log('');
111
- ui.success('Setup complete! Run your app to verify.');
112
- }
113
-
114
- async function initNextJs(dir, project, flags) {
115
- const useTs = hasTypeScript(dir);
116
- const ext = useTs ? 'ts' : 'js';
117
-
118
- const existing = findInstrumentationFile(dir);
119
- if (existing) {
120
- ui.info(`instrumentation file already exists: ${path.relative(dir, existing)}`);
121
- } else {
122
- const filePath = path.join(dir, `instrumentation.${ext}`);
123
- const content = useTs ? INSTRUMENTATION_TS : INSTRUMENTATION_JS;
124
- fs.writeFileSync(filePath, content, 'utf8');
125
- ui.success(`Created instrumentation.${ext}`);
126
- }
127
-
128
- const configPath = findNextConfig(dir);
129
- if (configPath) {
130
- const content = fs.readFileSync(configPath, 'utf8');
131
- if (content.includes('withSecureNow')) {
132
- ui.info('next.config already uses withSecureNow — skipping');
133
- } else if (content.includes('serverExternalPackages') && content.includes('securenow')) {
134
- ui.info('next.config already externalizes securenow — skipping');
135
- } else if (content.includes('serverComponentsExternalPackages') && content.includes('securenow')) {
136
- ui.info('next.config already externalizes securenow — skipping');
137
- } else {
138
- ui.warn(`Update your ${path.basename(configPath)} to use withSecureNow():`);
139
- console.log('');
140
- console.log(` const { withSecureNow } = require('securenow/nextjs-webpack-config');`);
141
- console.log(` module.exports = withSecureNow({ /* your config */ });`);
142
- console.log('');
143
- }
144
- } else {
145
- const newConfigPath = path.join(dir, 'next.config.js');
146
- fs.writeFileSync(newConfigPath, `const { withSecureNow } = require('securenow/nextjs-webpack-config');\n\nmodule.exports = withSecureNow({\n reactStrictMode: true,\n});\n`, 'utf8');
147
- ui.success('Created next.config.js with withSecureNow()');
148
- }
149
- }
150
-
151
- function initNuxt(dir, project) {
152
- const configPath = path.join(dir, 'nuxt.config.ts');
153
- if (fs.existsSync(configPath)) {
154
- const content = fs.readFileSync(configPath, 'utf8');
155
- if (content.includes('securenow/nuxt')) {
156
- ui.info('nuxt.config already references securenow/nuxt — skipping');
157
- } else {
158
- ui.warn('Add securenow/nuxt to your nuxt.config modules:');
159
- console.log('');
160
- console.log(" modules: ['securenow/nuxt'],");
161
- console.log('');
162
- }
163
- } else {
164
- ui.warn('Add securenow/nuxt to your nuxt.config modules array.');
165
- }
166
- }
167
-
168
- function initNode(dir, project) {
169
- const pkg = project.pkg;
170
- const scripts = pkg.scripts || {};
171
-
172
- const startScript = scripts.start || '';
173
- if (startScript.includes('securenow/register')) {
174
- ui.info('start script already uses securenow/register — skipping');
175
- return;
176
- }
177
-
178
- if (startScript) {
179
- ui.warn('Update your start script to include the securenow preload:');
180
- console.log('');
181
- if (startScript.includes('node ')) {
182
- const updated = startScript.replace('node ', 'node -r securenow/register ');
183
- console.log(` "start": "${updated}"`);
184
- } else {
185
- console.log(` "start": "node -r securenow/register ${startScript.replace(/^node\s+/, '')}"`);
186
- }
187
- console.log('');
188
- } else {
189
- ui.warn('Add a start script with the securenow preload:');
190
- console.log('');
191
- console.log(' "start": "node -r securenow/register src/index.js"');
192
- console.log('');
193
- }
194
- }
195
-
196
- function initEnv(dir, flags) {
197
- const envFiles = ['.env', '.env.local'];
198
- let envPath = null;
199
-
200
- for (const f of envFiles) {
201
- const p = path.join(dir, f);
202
- if (fs.existsSync(p)) {
203
- envPath = p;
204
- break;
205
- }
49
+
50
+ function hasTypeScript(dir) {
51
+ return fs.existsSync(path.join(dir, 'tsconfig.json'));
52
+ }
53
+
54
+ function nextMajor(project) {
55
+ const raw = String(project.nextVersion || '').replace(/^[^\d]*/, '');
56
+ const major = parseInt(raw, 10);
57
+ return Number.isFinite(major) ? major : 15;
58
+ }
59
+
60
+ async function init(_args, flags) {
61
+ const dir = process.cwd();
62
+ const project = detectProject(dir);
63
+
64
+ ui.header('SecureNow Project Setup');
65
+
66
+ if (project.framework === 'unknown') {
67
+ ui.error('No package.json found. Run this command in your project root.');
68
+ process.exit(1);
206
69
  }
207
70
 
208
- if (!envPath) envPath = path.join(dir, '.env.local');
71
+ ui.info(`Detected framework: ${ui.bold(project.framework)}`);
72
+ initCredentials(flags);
209
73
 
210
- const existing = fs.existsSync(envPath) ? fs.readFileSync(envPath, 'utf8') : '';
211
- const additions = [];
212
- for (const [key, value] of Object.entries(DEFAULT_ENV)) {
213
- if (!hasEnvKey(existing, key)) additions.push(`${key}=${value}`);
74
+ if (project.framework === 'nextjs') {
75
+ await initNextJs(dir, project, flags);
76
+ } else if (project.framework === 'nuxt') {
77
+ initNuxt(dir);
78
+ } else {
79
+ initNode(project);
214
80
  }
215
81
 
82
+ console.log('');
83
+ ui.success('Setup complete.');
84
+ ui.info('Run `npx securenow login` if this project is not linked to an app yet.');
85
+ ui.info('Then verify with `npx securenow test-span` and `npx securenow status`.');
86
+ }
87
+
88
+ function initCredentials(flags) {
89
+ config.ensureCredentialDefaults({ local: true });
90
+ config.ensureLocalGitignore();
91
+ const creds = config.loadCredentials();
92
+ creds.config = creds.config || {};
93
+ creds.config.runtime = creds.config.runtime || {};
94
+ creds.config.runtime.deploymentEnvironment = flags.env || flags.environment || 'local';
95
+ config.saveCredentials(creds, { local: true });
96
+
216
97
  const explicitApiKey = flags.key || flags['api-key'] || '';
217
- const hasApiKey = hasEnvKey(existing, 'SECURENOW_API_KEY');
218
- if (explicitApiKey && !hasApiKey) additions.push(`SECURENOW_API_KEY=${explicitApiKey}`);
98
+ if (explicitApiKey) {
99
+ if (!String(explicitApiKey).startsWith('snk_live_')) {
100
+ ui.error('--key must start with snk_live_');
101
+ process.exit(1);
102
+ }
103
+ config.setApiKey(explicitApiKey, { local: true });
104
+ ui.success('Stored firewall API key in .securenow/credentials.json');
105
+ }
219
106
 
220
- if (additions.length > 0) {
221
- const sep = existing && !existing.endsWith('\n') ? '\n' : '';
222
- fs.appendFileSync(envPath, `${sep}${additions.join('\n')}\n`, 'utf8');
223
- ui.success(`Updated ${path.basename(envPath)} with SecureNow defaults`);
107
+ ui.success('Ensured .securenow/credentials.json has secure defaults and explanations');
108
+ }
109
+
110
+ async function initNextJs(dir, project, flags) {
111
+ const useTs = flags.javascript ? false : flags.typescript ? true : hasTypeScript(dir);
112
+ const ext = useTs ? 'ts' : 'js';
113
+
114
+ const existing = findInstrumentationFile(dir);
115
+ if (existing) {
116
+ ui.info(`instrumentation file already exists: ${path.relative(dir, existing)}`);
117
+ printAgentPrompt('instrumentation', path.basename(existing), nextMajor(project));
224
118
  } else {
225
- ui.info(`${path.basename(envPath)} already contains SecureNow defaults`);
119
+ const filePath = path.join(dir, `instrumentation.${ext}`);
120
+ fs.writeFileSync(filePath, INSTRUMENTATION, 'utf8');
121
+ ui.success(`Created instrumentation.${ext}`);
226
122
  }
227
123
 
228
- if (hasApiKey || explicitApiKey) {
229
- ui.info(`SECURENOW_API_KEY is set in ${path.basename(envPath)}`);
230
- } else if (config.getApiKey()) {
231
- ui.info('Using firewall API key from project .securenow/credentials.json');
124
+ const configPath = findNextConfig(dir);
125
+ if (configPath) {
126
+ const patched = patchNextConfig(configPath, nextMajor(project));
127
+ if (patched === 'already') {
128
+ ui.info(`${path.basename(configPath)} already externalizes securenow`);
129
+ } else if (patched === 'patched') {
130
+ ui.success(`Updated ${path.basename(configPath)} with SecureNow server externalization`);
131
+ } else {
132
+ ui.warn(`Could not safely edit ${path.basename(configPath)} automatically.`);
133
+ printAgentPrompt('next-config', path.basename(configPath), nextMajor(project));
134
+ }
232
135
  } else {
233
- ui.warn(`Add your API key to ${path.basename(envPath)}:`);
136
+ const newConfigPath = path.join(dir, 'next.config.mjs');
137
+ fs.writeFileSync(newConfigPath, `/** @type {import('next').NextConfig} */
138
+ const nextConfig = {
139
+ serverExternalPackages: ['securenow'],
140
+ };
141
+
142
+ export default nextConfig;
143
+ `, 'utf8');
144
+ ui.success('Created next.config.mjs');
145
+ }
146
+ }
147
+
148
+ function patchNextConfig(configPath, major) {
149
+ const content = fs.readFileSync(configPath, 'utf8');
150
+ const serverExternalWithSecureNow = /serverExternalPackages\s*:\s*\[[\s\S]*?['"]securenow['"][\s\S]*?\]/m.test(content);
151
+ const serverComponentsWithSecureNow = /serverComponentsExternalPackages\s*:\s*\[[\s\S]*?['"]securenow['"][\s\S]*?\]/m.test(content);
152
+ if (serverExternalWithSecureNow || serverComponentsWithSecureNow || content.includes('withSecureNow(')) {
153
+ return 'already';
154
+ }
155
+
156
+ if (major < 15) return 'manual';
157
+
158
+ const existingServerExternal = content.match(/serverExternalPackages\s*:\s*\[([\s\S]*?)\]/m);
159
+ if (existingServerExternal) {
160
+ const current = existingServerExternal[1].trim().replace(/,\s*$/, '');
161
+ const replacement = `serverExternalPackages: [${current ? `${current}, ` : ''}'securenow']`;
162
+ fs.writeFileSync(configPath, content.replace(existingServerExternal[0], replacement), 'utf8');
163
+ return 'patched';
164
+ }
165
+
166
+ const insert = ` serverExternalPackages: ['securenow'],\n`;
167
+ const patterns = [
168
+ /(const\s+nextConfig\s*=\s*{\s*\r?\n)/,
169
+ /(export\s+default\s+{\s*\r?\n)/,
170
+ /(module\.exports\s*=\s*{\s*\r?\n)/,
171
+ ];
172
+
173
+ for (const pattern of patterns) {
174
+ if (pattern.test(content)) {
175
+ fs.writeFileSync(configPath, content.replace(pattern, `$1${insert}`), 'utf8');
176
+ return 'patched';
177
+ }
178
+ }
179
+
180
+ return 'manual';
181
+ }
182
+
183
+ function printAgentPrompt(kind, filename, major) {
184
+ console.log('');
185
+ ui.heading('Codex/Claude prompt');
186
+ console.log([
187
+ 'Set up SecureNow in this existing Next.js project without using .env files.',
188
+ 'Use .securenow/credentials.json for local and production configuration; do not add .env files.',
189
+ kind === 'instrumentation'
190
+ ? `Merge this into ${filename}: in register(), return unless process.env.NEXT_RUNTIME === "nodejs"; then dynamically import "securenow/nextjs" and "securenow/nextjs-auto-capture" with /* webpackIgnore: true */ so Next does not bundle OpenTelemetry internals. Preserve all existing instrumentation.`
191
+ : null,
192
+ kind === 'next-config' && major >= 15
193
+ ? `Update ${filename} while preserving existing config: add securenow to serverExternalPackages, e.g. serverExternalPackages: [...(existing || []), "securenow"].`
194
+ : null,
195
+ kind === 'next-config' && major < 15
196
+ ? `Update ${filename} while preserving existing config: enable experimental.instrumentationHook and add securenow to experimental.serverComponentsExternalPackages.`
197
+ : null,
198
+ 'Verify with: npx securenow env, npx securenow test-span, npx securenow status, and the project build command.',
199
+ ].filter(Boolean).join('\n'));
200
+ console.log('');
201
+ }
202
+
203
+ function initNuxt(dir) {
204
+ const configPath = path.join(dir, 'nuxt.config.ts');
205
+ if (fs.existsSync(configPath)) {
206
+ const content = fs.readFileSync(configPath, 'utf8');
207
+ if (content.includes('securenow/nuxt')) {
208
+ ui.info('nuxt.config already references securenow/nuxt');
209
+ } else {
210
+ ui.warn('Add securenow/nuxt to your nuxt.config modules array.');
211
+ }
212
+ } else {
213
+ ui.warn('Add securenow/nuxt to your nuxt.config modules array.');
214
+ }
215
+ }
216
+
217
+ function initNode(project) {
218
+ const scripts = project.pkg.scripts || {};
219
+ const startScript = scripts.start || '';
220
+ if (startScript.includes('securenow/register')) {
221
+ ui.info('start script already uses securenow/register');
222
+ return;
223
+ }
224
+
225
+ if (startScript) {
226
+ ui.warn('Update your start script to include the securenow preload:');
234
227
  console.log('');
235
- console.log(' SECURENOW_API_KEY=snk_live_...');
228
+ if (startScript.includes('node ')) {
229
+ const updated = startScript.replace('node ', 'node -r securenow/register ');
230
+ console.log(` "start": "${updated}"`);
231
+ } else {
232
+ console.log(` "start": "node -r securenow/register ${startScript.replace(/^node\s+/, '')}"`);
233
+ }
234
+ console.log('');
235
+ } else {
236
+ ui.warn('Add a start script with the securenow preload:');
237
+ console.log('');
238
+ console.log(' "start": "node -r securenow/register src/index.js"');
236
239
  console.log('');
237
240
  }
238
241
  }
239
242
 
240
- function hasEnvKey(content, key) {
241
- return new RegExp(`^\\s*${key}\\s*=`, 'm').test(content || '');
242
- }
243
-
244
- module.exports = { init };
243
+ module.exports = { init };