wize-dev-kit 0.4.1 → 0.6.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.
Files changed (49) hide show
  1. package/CHANGELOG.md +49 -0
  2. package/package.json +1 -1
  3. package/src/core-skills/wize-customize/skill.md +114 -0
  4. package/src/core-skills/wize-editorial-review-prose/skill.md +92 -0
  5. package/src/core-skills/wize-editorial-review-structure/skill.md +97 -0
  6. package/src/core-skills/wize-index-docs/skill.md +117 -0
  7. package/src/core-skills/wize-review-edge-case-hunter/skill.md +112 -0
  8. package/src/method-skills/2-plan-workflows/wize-edit-prd/workflow.md +108 -0
  9. package/src/method-skills/3-solutioning/wize-project-context/workflow.md +118 -0
  10. package/src/method-skills/4-implementation/wize-checkpoint-preview/workflow.md +115 -0
  11. package/src/method-skills/4-implementation/wize-correct-course/workflow.md +89 -0
  12. package/src/method-skills/4-implementation/wize-investigate/workflow.md +121 -0
  13. package/src/method-skills/4-implementation/wize-sprint-planning/workflow.md +58 -71
  14. package/src/method-skills/4-implementation/wize-sprint-status/workflow.md +29 -82
  15. package/src/orchestrator-skills/wize-onboarding/workflow.md +76 -14
  16. package/src/security-overlay/_shared/allowlist.js +154 -0
  17. package/src/security-overlay/_shared/cli-runner.js +87 -0
  18. package/src/security-overlay/_shared/cvss.js +108 -0
  19. package/src/security-overlay/_shared/detect.js +125 -0
  20. package/src/security-overlay/_shared/install-script.js +205 -0
  21. package/src/security-overlay/_shared/invoke-phase.js +86 -0
  22. package/src/security-overlay/_shared/owasp.js +56 -0
  23. package/src/security-overlay/_shared/partial.js +225 -0
  24. package/src/security-overlay/_shared/preflight.js +175 -0
  25. package/src/security-overlay/_shared/scope-gate.js +172 -0
  26. package/src/security-overlay/_shared/scope-parser.js +120 -0
  27. package/src/security-overlay/agents/red-teamer/agent.yaml +51 -0
  28. package/src/security-overlay/agents/red-teamer/persona.md +43 -0
  29. package/src/security-overlay/data/common.txt +115 -0
  30. package/src/security-overlay/data/owasp-top10.json +15 -0
  31. package/src/security-overlay/data/tool-allowlist.json +31 -0
  32. package/src/security-overlay/skills/wize-sec-enumerate/scripts/run-enumerate.js +180 -0
  33. package/src/security-overlay/skills/wize-sec-enumerate/skill.md +32 -0
  34. package/src/security-overlay/skills/wize-sec-exploit/data/common.txt +117 -0
  35. package/src/security-overlay/skills/wize-sec-exploit/scripts/run-ffuf.js +147 -0
  36. package/src/security-overlay/skills/wize-sec-exploit/scripts/run-nikto.js +145 -0
  37. package/src/security-overlay/skills/wize-sec-exploit/scripts/run-nuclei.js +176 -0
  38. package/src/security-overlay/skills/wize-sec-exploit/scripts/run-sqlmap.js +139 -0
  39. package/src/security-overlay/skills/wize-sec-pentest/scripts/run-pipeline.js +157 -0
  40. package/src/security-overlay/skills/wize-sec-pentest/skill.md +52 -0
  41. package/src/security-overlay/skills/wize-sec-recon/scripts/run-gitleaks.js +139 -0
  42. package/src/security-overlay/skills/wize-sec-recon/scripts/run-osv.js +227 -0
  43. package/src/security-overlay/skills/wize-sec-recon/scripts/run-recon.js +162 -0
  44. package/src/security-overlay/skills/wize-sec-recon/skill.md +35 -0
  45. package/src/security-overlay/skills/wize-sec-report/scripts/render-report.js +999 -0
  46. package/src/tea-skills/wize-qa-generate-e2e-tests/workflow.md +119 -0
  47. package/tools/installer/onboarding.js +1 -0
  48. package/tools/installer/render-shared.js +50 -3
  49. package/tools/installer/wize-cli.js +72 -5
@@ -0,0 +1,157 @@
1
+ 'use strict';
2
+
3
+ // run-pipeline.js — the orchestrator for the security-overlay. Chains
4
+ // the four phase skills (recon -> enumerate -> exploit -> report) and
5
+ // propagates --active. This module exposes a `runPipeline(opts)` function
6
+ // so the test suite can drive it directly; the SKILL.md entrypoint
7
+ // (wize-sec-pentest) is a thin wrapper that just calls runPipeline with
8
+ // argv-derived options.
9
+
10
+ const path = require('node:path');
11
+ const fs = require('node:fs');
12
+
13
+ // Default kit root when running from the kit itself. Tests inject their own.
14
+ const DEFAULT_KIT_ROOT = path.resolve(__dirname, '..', '..', '..', '..', '..');
15
+
16
+ const PHASES = ['wize-sec-recon', 'wize-sec-enumerate', 'wize-sec-exploit', 'wize-sec-report'];
17
+
18
+ // Per-phase script manifests. Each phase can run one or more scripts.
19
+ // Scripts in the same phase run sequentially and any failure (or skip)
20
+ // marks the phase as incomplete, but does not abort the pipeline.
21
+ const PHASE_SCRIPTS = {
22
+ 'wize-sec-recon': ['recon', 'gitleaks', 'osv'],
23
+ 'wize-sec-enumerate': ['enumerate'],
24
+ 'wize-sec-exploit': ['nuclei', 'nikto', 'sqlmap', 'ffuf'],
25
+ 'wize-sec-report': ['report']
26
+ };
27
+
28
+ function parseArgv(argv) {
29
+ const out = { active: false, scopePath: null, kitRoot: DEFAULT_KIT_ROOT };
30
+ for (let i = 0; i < (argv || []).length; i++) {
31
+ const a = argv[i];
32
+ if (a === '--active') out.active = true;
33
+ else if (a === '--scope' && argv[i + 1]) { out.scopePath = argv[i + 1]; i++; }
34
+ else if (a.startsWith('--scope=')) out.scopePath = a.slice('--scope='.length);
35
+ }
36
+ if (!out.scopePath) out.scopePath = path.join(process.cwd(), '.wize', 'security', 'scope.md');
37
+ return out;
38
+ }
39
+
40
+ // runPipeline({ scopePath, active, kitRoot?, loadScopeFn?, invokePhase? })
41
+ // - loadScopeFn defaults to the real scope-gate loadScope.
42
+ // - invokePhase defaults to the real invokePhase (subprocess).
43
+ // Both are injectable for testing. We resolve the defaults lazily via a
44
+ // small indirection (a getter) so a test that mutates the real module's
45
+ // exports before loading this file sees the mocked value.
46
+ async function runPipeline(opts = {}) {
47
+ const scopePath = opts.scopePath;
48
+ const active = opts.active === true;
49
+ const kitRoot = opts.kitRoot || DEFAULT_KIT_ROOT;
50
+ const securityDir = opts.securityDir || path.join(process.cwd(), '.wize', 'security');
51
+
52
+ // Lazy default resolution — must read at call time, not destructured at
53
+ // module load, so a test that swaps the real module's exports is honored.
54
+ const loadScopeFn = opts.loadScopeFn
55
+ || require('../../../_shared/scope-gate.js').loadScope;
56
+ const invokePhase = opts.invokePhase
57
+ || require('../../../_shared/invoke-phase.js').invokePhase;
58
+
59
+ // 0. Preflight (permissive — always continues, but writes the install
60
+ // script if anything is missing). In test mode the env vars short-
61
+ // circuit the real probes so the result is deterministic.
62
+ const preflight = opts.preflight
63
+ || require('../../../_shared/preflight.js').runPreflight({ kitRoot });
64
+ if (preflight.missing.length > 0 && opts.writeInstallScript !== false) {
65
+ const { generateInstallScript } = require('../../../_shared/install-script.js');
66
+ const script = generateInstallScript(preflight);
67
+ try {
68
+ const scriptPath = path.join(securityDir, 'install-pentest-tools.sh');
69
+ // Don't clobber a user-edited script.
70
+ if (!fs.existsSync(scriptPath)) {
71
+ fs.mkdirSync(securityDir, { recursive: true });
72
+ fs.writeFileSync(scriptPath, script, 'utf8');
73
+ try { fs.chmodSync(scriptPath, 0o755); } catch (_) { /* best-effort */ }
74
+ }
75
+ } catch (_) { /* best-effort: a write failure here does not block the pipeline */ }
76
+ }
77
+
78
+ // 1. Gate first — if the scope is invalid, abort the whole pipeline.
79
+ const scope = loadScopeFn(scopePath);
80
+
81
+ const results = {};
82
+ const skipped = [];
83
+
84
+ for (const phase of PHASES) {
85
+ const scripts = PHASE_SCRIPTS[phase] || [phase.split('-').pop()];
86
+ const phaseResults = [];
87
+ for (const sub of scripts) {
88
+ // Naming convention: scripts in the skill dir are run-<sub>.js, except
89
+ // for the report phase which is render-report.js.
90
+ const baseName = phase === 'wize-sec-report' ? `render-${sub}` : `run-${sub}`;
91
+ const scriptName = baseName.endsWith('.js') ? baseName : baseName + '.js';
92
+ try {
93
+ const r = await invokePhase(phase, { kitRoot, active, securityDir, scopePath, scriptName });
94
+ phaseResults.push({ sub, ...r });
95
+ if (!r.ok) skipped.push(`${phase}/${sub}`);
96
+ } catch (_) {
97
+ phaseResults.push({ sub, ok: false, code: -1, stdout: '', stderr: '', error: 'invoke-threw' });
98
+ skipped.push(`${phase}/${sub}`);
99
+ }
100
+ }
101
+ // Phase is OK if at least one sub-script succeeded.
102
+ const anyOk = phaseResults.some(r => r && r.ok);
103
+ results[phase] = { ok: anyOk, subs: phaseResults };
104
+ }
105
+
106
+ const anyOk = Object.values(results).some(r => r && r.ok);
107
+ return {
108
+ ok: anyOk,
109
+ scopePath,
110
+ active,
111
+ preflight,
112
+ results,
113
+ skipped
114
+ };
115
+ }
116
+
117
+ // Entry point when invoked as a subprocess (the SKILL.md frontmatter
118
+ // declares commands: [run-pipeline.js]). The harness's adapter renders
119
+ // this file as the skill body and runs `node run-pipeline.js` when the
120
+ // user invokes `/wize-sec-pentest`.
121
+ async function main() {
122
+ const args = parseArgv(process.argv.slice(2));
123
+ let result;
124
+ try {
125
+ // Derive the securityDir from the scopePath's parent (the .wize/security
126
+ // directory) so the preflight's install script lands next to the scope.
127
+ const secDir = path.dirname(args.scopePath);
128
+ result = await runPipeline({
129
+ scopePath: args.scopePath,
130
+ active: args.active,
131
+ kitRoot: args.kitRoot,
132
+ securityDir: secDir
133
+ });
134
+ } catch (err) {
135
+ console.error('✖ wize-sec-pentest aborted:', err && err.message ? err.message : err);
136
+ process.exit(2);
137
+ }
138
+
139
+ // Minimal stdout surface — the partials already carry detail.
140
+ for (const phase of PHASES) {
141
+ const r = result.results[phase];
142
+ if (!r) { console.log(`= ${phase}: (no result)`); continue; }
143
+ const mark = r.ok ? '✓' : '✗';
144
+ const subs = (r.subs || []).map(s => `${s.sub}:${s.ok ? 'ok' : 'fail'}`).join(',');
145
+ console.log(`${mark} ${phase}: ${subs}`);
146
+ }
147
+ if (result.skipped.length) {
148
+ console.log(`! skipped: ${result.skipped.join(', ')}`);
149
+ }
150
+ process.exit(result.ok ? 0 : 1);
151
+ }
152
+
153
+ if (require.main === module) {
154
+ main();
155
+ }
156
+
157
+ module.exports = { runPipeline, parseArgv, PHASES };
@@ -0,0 +1,52 @@
1
+ ---
2
+ code: wize-sec-pentest
3
+ name: wize-sec-pentest
4
+ overlay: security
5
+ module: security-overlay
6
+ owner: red-teamer
7
+ status: ready
8
+ ---
9
+
10
+ # wize-sec-pentest — Orchestrator (Security Overlay)
11
+
12
+ End-to-end security pipeline. Chains **recon → enumerate → sast → dast → report** through the four phase skills (`wize-sec-recon`, `wize-sec-enumerate`, `wize-sec-exploit`, `wize-sec-report`). Loads `.wize/security/scope.md` first; aborts the whole pipeline if the scope is missing or invalid.
13
+
14
+ ## Usage
15
+
16
+ ```bash
17
+ # Default (passive — read-only checks only)
18
+ /wize-sec-pentest
19
+
20
+ # Active exploitation (sqlmap, ffuf active fuzzing) — requires scope acceptance
21
+ /wize-sec-pentest --active
22
+
23
+ # Custom scope path (default: .wize/security/scope.md)
24
+ /wize-sec-pentest --scope=/path/to/scope.md
25
+ ```
26
+
27
+ ## Phases (in order)
28
+
29
+ 1. **recon** — nmap (passive) → `recon.md`
30
+ 2. **enumerate** — nuclei passive, HTTP probing → `enumerate.md`
31
+ 3. **sast** — gitleaks (secrets) + osv-scanner/grype (deps) → `sast.md` (run inside `wize-sec-recon` per the architecture decision)
32
+ 4. **dast** — nuclei + nikto (passive) + sqlmap/ffuf (gated by `--active`) → `dast.md`
33
+ 5. **report** — render `report.md` + `report.html` from all partials
34
+
35
+ Each phase produces a partial. The report phase consumes them. A phase that fails is marked `partial_status: skipped` and the pipeline continues with the next one.
36
+
37
+ ## Gate
38
+
39
+ Before any phase, the orchestrator calls `loadScope()` (the shared gate). An invalid scope aborts the whole pipeline with the error code from `ScopeError`. The gate is the single source of truth — phases do not re-validate.
40
+
41
+ ## Propagation
42
+
43
+ `--active` is read from argv and passed to every phase invocation. Each phase that supports active checks (currently `wize-sec-exploit`) decides what changes in active mode.
44
+
45
+ ## Output
46
+
47
+ - `recon.md`, `enumerate.md`, `sast.md`, `dast.md` — partials (one per phase)
48
+ - `report.md`, `report.html` — final consolidated report
49
+ - `.refusals.log` — audit trail of out-of-scope refusals
50
+ - `.tools.json` — detection cache for the toolchain
51
+
52
+ Exits 0 if at least one phase ran; 1 if all phases failed; 2 if the scope gate aborted.
@@ -0,0 +1,139 @@
1
+ 'use strict';
2
+
3
+ // run-gitleaks.js — SAST secrets via gitleaks. Runs inside the
4
+ // wize-sec-recon skill (per the architecture decision that recon hosts
5
+ // the no-app-running checks). Writes a redaction into sast.md and stores
6
+ // the full gitleaks report in the security dir for the user to inspect.
7
+
8
+ const path = require('node:path');
9
+ const fs = require('node:fs');
10
+ const { execFileSync } = require('node:child_process');
11
+
12
+ const { filterArgs } = require('../../../_shared/allowlist.js');
13
+ const { writePartial, loadPartial } = require('../../../_shared/partial.js');
14
+
15
+ const REDACTED = '***REDACTED***';
16
+
17
+ // runGitleaks({ securityDir, scope, active, execFn?, detectFn?, reportFilename? })
18
+ // -> { ok, partialStatus, findingsCount }
19
+ async function runGitleaks(opts = {}) {
20
+ const sec = opts.securityDir;
21
+ const scope = opts.scope;
22
+ const active = opts.active === true;
23
+ const reportFilename = opts.reportFilename || 'gitleaks-report.json';
24
+ // The project to scan is the parent of `.wize/security/`. The scripts run
25
+ // with cwd = the kit, so we MUST point gitleaks at the target repo, not '.'.
26
+ const projectRoot = opts.projectRoot || (sec ? path.resolve(sec, '..', '..') : process.cwd());
27
+
28
+ const execFn = opts.execFn || ((bin, args) => {
29
+ return execFileSync(bin, args, { encoding: 'utf8', timeout: 5 * 60 * 1000 });
30
+ });
31
+ const detectFn = opts.detectFn || require('../../../_shared/detect.js').detectTools;
32
+
33
+ const tools = detectFn(['gitleaks'], { cacheDir: sec });
34
+ const present = !!(tools.gitleaks && tools.gitleaks.present);
35
+
36
+ if (!present) {
37
+ // Update or create sast.md with the degraded_checks entry.
38
+ mergeSast(sec, scope, active, tools, {
39
+ degraded: '- secrets: gitleaks ausente — instale gitleaks e re-rode para scan completo.'
40
+ });
41
+ return { ok: true, partialStatus: 'incomplete', findingsCount: 0 };
42
+ }
43
+
44
+ const reportPath = path.join(sec, reportFilename);
45
+ // gitleaks 8.18: `-s/--source` is the scan path, `-f/--report-format` is
46
+ // the output format (json), `-r/--report-path` is the file. (Earlier
47
+ // versions used `-f` for the path — do NOT use that here.)
48
+ const args = filterArgs('gitleaks', [
49
+ 'detect', '--no-banner', '-s', projectRoot, '-f', 'json', '-r', reportPath, '--exit-code', '0'
50
+ ]);
51
+ // gitleaks exits non-zero when it FINDS leaks (default exit-code 1). That
52
+ // is the success path for us, not an error — so we pass --exit-code 0 and
53
+ // also swallow any non-zero status defensively (we read the JSON report
54
+ // regardless).
55
+ try {
56
+ execFn('gitleaks', args, { timeout: 5 * 60 * 1000 });
57
+ } catch (_) {
58
+ // Leaks found (or gitleaks returned non-zero) — the report file is still
59
+ // written; we parse it below.
60
+ }
61
+
62
+ // Read the JSON report gitleaks wrote. It is an array of findings.
63
+ let findings = [];
64
+ if (fs.existsSync(reportPath)) {
65
+ try {
66
+ findings = JSON.parse(fs.readFileSync(reportPath, 'utf8'));
67
+ } catch (_) {
68
+ findings = [];
69
+ }
70
+ }
71
+
72
+ // Build the secrets section from findings — ONLY redacted values.
73
+ const secretLines = findings.map(f => {
74
+ const file = f.File || f.file || '<unknown>';
75
+ const line = f.StartLine || f.startLine || '?';
76
+ const rule = f.RuleID || f.ruleID || 'unknown';
77
+ return `- **${file}** line ${line} rule \`${rule}\` — redacted_value: \`${REDACTED}\``;
78
+ });
79
+
80
+ // Merge into existing sast.md (preserve other sections like deps from E05-S02).
81
+ mergeSast(sec, scope, active, tools, {
82
+ secrets: secretLines.length ? secretLines.join('\n') : '_(nenhum secret encontrado)_'
83
+ });
84
+ return { ok: true, partialStatus: secretLines.length ? 'complete' : 'incomplete', findingsCount: secretLines.length };
85
+ }
86
+
87
+ // Helper: load existing sast.md, update the relevant section, and write back.
88
+ // Preserves sections that other SAST scripts (e.g. run-osv) added.
89
+ function mergeSast(sec, scope, active, tools, update) {
90
+ const existing = loadPartial({ securityDir: sec, phase: 'sast' });
91
+ const sections = {};
92
+ let mergedTools = Object.assign({}, tools);
93
+ if (existing) {
94
+ if (existing.body) {
95
+ // Extract known section bodies from the existing partial.
96
+ const re = /## ([a-z_]+)\n\n([\s\S]*?)(?=\n## |$)/g;
97
+ let m;
98
+ while ((m = re.exec(existing.body)) !== null) {
99
+ sections[m[1]] = m[2].trim();
100
+ }
101
+ }
102
+ // Preserve tools detected by sibling SAST scripts (gitleaks + osv each
103
+ // run separately; neither should clobber the other's tools block).
104
+ if (existing.frontmatter && existing.frontmatter.tools) {
105
+ mergedTools = Object.assign({}, existing.frontmatter.tools, tools);
106
+ }
107
+ }
108
+ // Apply updates.
109
+ if (update.degraded) {
110
+ // Append to degraded_checks if present, else create.
111
+ if (sections.degraded_checks) sections.degraded_checks += '\n' + update.degraded;
112
+ else sections.degraded_checks = update.degraded;
113
+ }
114
+ if (update.secrets !== undefined) sections.secrets = update.secrets;
115
+
116
+ const status = sections.degraded_checks ? 'incomplete' : 'complete';
117
+ writePartial({
118
+ securityDir: sec,
119
+ phase: 'sast',
120
+ mode: active ? 'active' : 'passive',
121
+ scope,
122
+ status,
123
+ tools: mergedTools,
124
+ sections
125
+ });
126
+ }
127
+
128
+ module.exports = { runGitleaks, mergeSast, REDACTED };
129
+
130
+ if (require.main === module) {
131
+ require('../../../_shared/cli-runner.js').runFromArgv({
132
+ fn: ({ securityDir, scopePath, active, reportFilename } = {}) => {
133
+ const { loadScope } = require('../../../_shared/scope-gate.js');
134
+ const scope = loadScope(scopePath);
135
+ return runGitleaks({ securityDir, scope, active, reportFilename });
136
+ },
137
+ argMap: { 'securityDir': 'securityDir', 'scope': 'scopePath', 'active': 'active', 'reportFilename': 'reportFilename' }
138
+ });
139
+ }
@@ -0,0 +1,227 @@
1
+ 'use strict';
2
+
3
+ // run-osv.js — SAST dependencies via osv-scanner (primary) or grype (fallback).
4
+ // Auto-detects common manifest files in the project root and emits findings
5
+ // into sast.md (composing with secrets from run-gitleaks.js).
6
+
7
+ const path = require('node:path');
8
+ const fs = require('node:fs');
9
+ const { execFileSync } = require('node:child_process');
10
+
11
+ const { filterArgs } = require('../../../_shared/allowlist.js');
12
+ const { writePartial, loadPartial } = require('../../../_shared/partial.js');
13
+
14
+ const MANIFEST_FILES = [
15
+ 'package.json', 'package-lock.json', 'yarn.lock', 'pnpm-lock.yaml',
16
+ 'requirements.txt', 'Pipfile', 'Pipfile.lock', 'pyproject.toml', 'poetry.lock',
17
+ 'go.mod', 'go.sum',
18
+ 'Cargo.toml', 'Cargo.lock',
19
+ 'composer.json', 'composer.lock',
20
+ 'Gemfile', 'Gemfile.lock'
21
+ ];
22
+
23
+ function detectManifests(root) {
24
+ const found = [];
25
+ for (const m of MANIFEST_FILES) {
26
+ if (fs.existsSync(path.join(root, m))) found.push(m);
27
+ }
28
+ return found;
29
+ }
30
+
31
+ function parseOsvReport(report) {
32
+ // osv-scanner JSON shape (simplified): { results: [{ packages: [{ package: {name, version}, vulnerabilities: [{id, severity, cvss: {score}}] }] }] }
33
+ const out = [];
34
+ const results = report && report.results ? report.results : [];
35
+ // Map a CVSS base score to a coarse severity label.
36
+ const sevFromScore = s => {
37
+ const n = parseFloat(s);
38
+ if (isNaN(n)) return null;
39
+ if (n === 0) return 'None';
40
+ if (n < 4) return 'Low';
41
+ if (n < 7) return 'Medium';
42
+ if (n < 9) return 'High';
43
+ return 'Critical';
44
+ };
45
+ for (const r of results) {
46
+ for (const p of (r.packages || [])) {
47
+ const name = p.package && p.package.name;
48
+ const version = p.package && p.package.version;
49
+ // osv-scanner v2 groups vulnerabilities and exposes a max_severity
50
+ // (CVSS) + aliases (CVE ids) per group. Prefer that; fall back to
51
+ // the raw vulnerabilities array for older shapes.
52
+ const groups = Array.isArray(p.groups) ? p.groups : [];
53
+ if (groups.length) {
54
+ for (const g of groups) {
55
+ const ids = g.aliases && g.aliases.length ? g.aliases : g.ids || [];
56
+ const cve = ids.find(x => /^CVE-/.test(x)) || ids[0] || '?';
57
+ const cvss = g.max_severity ? parseFloat(g.max_severity) : null;
58
+ out.push({ package: name, version, cve, severity: sevFromScore(g.max_severity) || 'UNKNOWN', cvss });
59
+ }
60
+ } else {
61
+ for (const v of (p.vulnerabilities || [])) {
62
+ const cve = v.id || '?';
63
+ const cvss = v.cvss && (typeof v.cvss === 'number' ? v.cvss : v.cvss.score);
64
+ out.push({ package: name, version, cve, severity: sevFromScore(cvss) || 'UNKNOWN', cvss });
65
+ }
66
+ }
67
+ }
68
+ }
69
+ return out;
70
+ }
71
+
72
+ function parseGrypeReport(report) {
73
+ // grype JSON shape: { matches: [{ artifact: {name, version}, vulnerability: {id, severity, cvss:[{metrics:{baseScore}}]} }] }
74
+ const out = [];
75
+ for (const m of (report && report.matches ? report.matches : [])) {
76
+ const name = m.artifact && m.artifact.name;
77
+ const version = m.artifact && m.artifact.version;
78
+ const v = m.vulnerability || {};
79
+ const cve = v.id || '?';
80
+ const severity = v.severity || 'UNKNOWN';
81
+ let cvss = null;
82
+ if (Array.isArray(v.cvss) && v.cvss[0] && v.cvss[0].metrics) {
83
+ cvss = v.cvss[0].metrics.baseScore;
84
+ } else if (typeof v.cvss === 'number') {
85
+ cvss = v.cvss;
86
+ }
87
+ out.push({ package: name, version, cve, severity, cvss });
88
+ }
89
+ return out;
90
+ }
91
+
92
+ function renderDepsSection(findings) {
93
+ if (!findings.length) return '_(nenhuma dep vulnerável encontrada)_';
94
+ return findings.map(f => {
95
+ const cvss = f.cvss != null ? ` cvss=${f.cvss}` : '';
96
+ return `- **${f.package}@${f.version || '?'}** \`${f.cve}\` severity=${f.severity}${cvss}`;
97
+ }).join('\n');
98
+ }
99
+
100
+ // runOsv({ securityDir, scope, active, execFn?, detectFn?, manifestRoot?, reportFilename? })
101
+ async function runOsv(opts = {}) {
102
+ const sec = opts.securityDir;
103
+ const scope = opts.scope;
104
+ const active = opts.active === true;
105
+ // The project to scan is the parent of `.wize/security/`. Scripts run with
106
+ // cwd = the kit, so default to the target repo, not process.cwd().
107
+ const manifestRoot = opts.manifestRoot
108
+ || (sec ? path.resolve(sec, '..', '..') : process.cwd());
109
+ const osvReportName = 'osv-report.json';
110
+ const grypeReportName = 'grype-report.json';
111
+
112
+ const execFn = opts.execFn || ((bin, args) => {
113
+ return execFileSync(bin, args, { encoding: 'utf8', timeout: 5 * 60 * 1000 });
114
+ });
115
+ const detectFn = opts.detectFn || require('../../../_shared/detect.js').detectTools;
116
+
117
+ const tools = detectFn(['osv-scanner', 'grype'], { cacheDir: sec });
118
+ const osvPresent = !!(tools['osv-scanner'] && tools['osv-scanner'].present);
119
+ const grypePresent = !!(tools.grype && tools.grype.present);
120
+
121
+ // No tools at all -> degraded.
122
+ if (!osvPresent && !grypePresent) {
123
+ mergeSast(sec, scope, active, tools, {
124
+ degraded: '- deps: osv-scanner e grype ausentes — instale um dos dois e re-rode.'
125
+ });
126
+ return { ok: true, partialStatus: 'incomplete', tool: null, findings: [] };
127
+ }
128
+
129
+ // Manifest detection: warn if the project root has no known manifest.
130
+ const manifests = detectManifests(manifestRoot);
131
+ if (manifests.length === 0) {
132
+ mergeSast(sec, scope, active, tools, {
133
+ degraded: `- deps: nenhum manifesto encontrado em ${manifestRoot} (procurando package.json, requirements.txt, go.mod, Cargo.toml, pyproject.toml, composer.json, Gemfile).`
134
+ });
135
+ return { ok: true, partialStatus: 'incomplete', tool: null, findings: [] };
136
+ }
137
+
138
+ // Pick tool. Prefer osv-scanner; fallback to grype.
139
+ let findings = [];
140
+ let tool = null;
141
+ if (osvPresent) {
142
+ tool = 'osv-scanner';
143
+ const reportPath = path.join(sec, osvReportName);
144
+ // osv-scanner v2 needs explicit lockfiles (`-L <path>`); recursive
145
+ // directory scan misses composer.lock/package-lock.json. We pass each
146
+ // detected lockfile we found. Non-zero exit = vulns found (success).
147
+ const LOCKFILES = ['composer.lock', 'package-lock.json', 'yarn.lock', 'pnpm-lock.yaml',
148
+ 'requirements.txt', 'Pipfile.lock', 'poetry.lock', 'go.sum', 'Cargo.lock', 'Gemfile.lock'];
149
+ const lockArgs = [];
150
+ for (const lf of LOCKFILES) {
151
+ if (fs.existsSync(path.join(manifestRoot, lf))) {
152
+ lockArgs.push('-L', path.join(manifestRoot, lf));
153
+ }
154
+ }
155
+ const args = lockArgs.length
156
+ ? filterArgs('osv-scanner', ['scan', 'source', ...lockArgs, '--format', 'json', '--output-file', reportPath])
157
+ : filterArgs('osv-scanner', ['scan', 'source', '-r', '--format', 'json', '--output-file', reportPath, manifestRoot]);
158
+ try {
159
+ execFn('osv-scanner', args, { timeout: 5 * 60 * 1000 });
160
+ } catch (_) { /* vulns found -> non-zero exit; report still written */ }
161
+ if (fs.existsSync(reportPath)) {
162
+ try { findings = parseOsvReport(JSON.parse(fs.readFileSync(reportPath, 'utf8'))); }
163
+ catch (_) { findings = []; }
164
+ }
165
+ } else if (grypePresent) {
166
+ tool = 'grype';
167
+ const reportPath = path.join(sec, grypeReportName);
168
+ const args = filterArgs('grype', ['dir:' + manifestRoot, '-o', 'json']);
169
+ execFn('grype', args, { timeout: 5 * 60 * 1000 });
170
+ if (fs.existsSync(reportPath)) {
171
+ try { findings = parseGrypeReport(JSON.parse(fs.readFileSync(reportPath, 'utf8'))); }
172
+ catch (_) { findings = []; }
173
+ }
174
+ }
175
+
176
+ mergeSast(sec, scope, active, tools, {
177
+ deps: renderDepsSection(findings)
178
+ });
179
+ return { ok: true, partialStatus: 'complete', tool, findings };
180
+ }
181
+
182
+ function mergeSast(sec, scope, active, tools, update) {
183
+ const existing = loadPartial({ securityDir: sec, phase: 'sast' });
184
+ const sections = {};
185
+ let mergedTools = Object.assign({}, tools);
186
+ if (existing) {
187
+ if (existing.body) {
188
+ const re = /## ([a-z_]+)\n\n([\s\S]*?)(?=\n## |$)/g;
189
+ let m;
190
+ while ((m = re.exec(existing.body)) !== null) {
191
+ sections[m[1]] = m[2].trim();
192
+ }
193
+ }
194
+ if (existing.frontmatter && existing.frontmatter.tools) {
195
+ mergedTools = Object.assign({}, existing.frontmatter.tools, tools);
196
+ }
197
+ }
198
+ if (update.degraded) {
199
+ sections.degraded_checks = sections.degraded_checks
200
+ ? sections.degraded_checks + '\n' + update.degraded
201
+ : update.degraded;
202
+ }
203
+ if (update.deps !== undefined) sections.deps = update.deps;
204
+ const status = sections.degraded_checks ? 'incomplete' : 'complete';
205
+ writePartial({
206
+ securityDir: sec,
207
+ phase: 'sast',
208
+ mode: active ? 'active' : 'passive',
209
+ scope,
210
+ status,
211
+ tools: mergedTools,
212
+ sections
213
+ });
214
+ }
215
+
216
+ module.exports = { runOsv, parseOsvReport, parseGrypeReport, detectManifests, MANIFEST_FILES };
217
+
218
+ if (require.main === module) {
219
+ require('../../../_shared/cli-runner.js').runFromArgv({
220
+ fn: ({ securityDir, scopePath, active, manifestRoot, reportFilename } = {}) => {
221
+ const { loadScope } = require('../../../_shared/scope-gate.js');
222
+ const scope = loadScope(scopePath);
223
+ return runOsv({ securityDir, scope, active, manifestRoot, reportFilename });
224
+ },
225
+ argMap: { 'securityDir': 'securityDir', 'scope': 'scopePath', 'active': 'active', 'manifestRoot': 'manifestRoot', 'reportFilename': 'reportFilename' }
226
+ });
227
+ }