@yagyeshvyas/vibeguard 0.1.0 → 1.1.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/CHANGELOG.md +126 -0
- package/LICENSE +21 -0
- package/README.md +499 -599
- package/bin/cli.js +2083 -1640
- package/ci/vibeguard.yml +42 -41
- package/package.json +14 -11
- package/src/ast.js +4 -0
- package/src/auto.js +427 -0
- package/src/crosstaint.js +154 -9
- package/src/daemon-run.js +12 -0
- package/src/daemon.js +342 -0
- package/src/engine.js +294 -234
- package/src/guard.js +80 -0
- package/src/mcp-server.js +1814 -1804
- package/src/report.js +325 -312
- package/src/rules-pack.js +121 -14
- package/src/rules.js +1506 -1446
- package/src/scanner.js +805 -651
- package/src/shell-guard.js +292 -0
- package/src/shell-hook.ps1 +135 -0
- package/src/shell-hook.sh +90 -0
- package/src/suppress.js +78 -8
- package/src/taint-ast.js +1004 -0
- package/src/taint.js +52 -13
package/ci/vibeguard.yml
CHANGED
|
@@ -1,41 +1,42 @@
|
|
|
1
|
-
name: VibeGuard
|
|
2
|
-
|
|
3
|
-
# Runs VibeGuard on every push and pull request. Fails the job when a CRITICAL
|
|
4
|
-
# issue is found, and uploads a SARIF report to GitHub code scanning so findings
|
|
5
|
-
# show up inline on the PR.
|
|
6
|
-
#
|
|
7
|
-
# Scope reminder: VibeGuard catches high-frequency, well-defined issues (secrets,
|
|
8
|
-
# injection, misconfig). It is not a full security review.
|
|
9
|
-
|
|
10
|
-
on:
|
|
11
|
-
push:
|
|
12
|
-
branches: [main, master]
|
|
13
|
-
pull_request:
|
|
14
|
-
|
|
15
|
-
permissions:
|
|
16
|
-
contents: read
|
|
17
|
-
security-events: write # required to upload SARIF
|
|
18
|
-
|
|
19
|
-
jobs:
|
|
20
|
-
vibeguard:
|
|
21
|
-
runs-on: ubuntu-latest
|
|
22
|
-
steps:
|
|
23
|
-
- uses: actions/checkout@v4
|
|
24
|
-
|
|
25
|
-
- uses: actions/setup-node@v4
|
|
26
|
-
with:
|
|
27
|
-
node-version: 20
|
|
28
|
-
|
|
29
|
-
# Produce a SARIF report for GitHub code scanning (does not fail the job).
|
|
30
|
-
- name: VibeGuard scan (SARIF)
|
|
31
|
-
run: npx -y vibeguard scan . --sarif --no-deps > vibeguard.sarif || true
|
|
32
|
-
|
|
33
|
-
- name: Upload SARIF
|
|
34
|
-
if: always()
|
|
35
|
-
uses: github/codeql-action/upload-sarif@v3
|
|
36
|
-
with:
|
|
37
|
-
sarif_file: vibeguard.sarif
|
|
38
|
-
|
|
39
|
-
# Gate the build: fail
|
|
40
|
-
-
|
|
41
|
-
|
|
1
|
+
name: VibeGuard
|
|
2
|
+
|
|
3
|
+
# Runs VibeGuard on every push and pull request. Fails the job when a CRITICAL
|
|
4
|
+
# issue is found, and uploads a SARIF report to GitHub code scanning so findings
|
|
5
|
+
# show up inline on the PR.
|
|
6
|
+
#
|
|
7
|
+
# Scope reminder: VibeGuard catches high-frequency, well-defined issues (secrets,
|
|
8
|
+
# injection, misconfig). It is not a full security review.
|
|
9
|
+
|
|
10
|
+
on:
|
|
11
|
+
push:
|
|
12
|
+
branches: [main, master]
|
|
13
|
+
pull_request:
|
|
14
|
+
|
|
15
|
+
permissions:
|
|
16
|
+
contents: read
|
|
17
|
+
security-events: write # required to upload SARIF
|
|
18
|
+
|
|
19
|
+
jobs:
|
|
20
|
+
vibeguard:
|
|
21
|
+
runs-on: ubuntu-latest
|
|
22
|
+
steps:
|
|
23
|
+
- uses: actions/checkout@v4
|
|
24
|
+
|
|
25
|
+
- uses: actions/setup-node@v4
|
|
26
|
+
with:
|
|
27
|
+
node-version: 20
|
|
28
|
+
|
|
29
|
+
# Produce a SARIF report for GitHub code scanning (does not fail the job).
|
|
30
|
+
- name: VibeGuard scan (SARIF)
|
|
31
|
+
run: npx -y vibeguard scan . --sarif --no-deps > vibeguard.sarif || true
|
|
32
|
+
|
|
33
|
+
- name: Upload SARIF
|
|
34
|
+
if: always()
|
|
35
|
+
uses: github/codeql-action/upload-sarif@v3
|
|
36
|
+
with:
|
|
37
|
+
sarif_file: vibeguard.sarif
|
|
38
|
+
|
|
39
|
+
# Gate the build: fail on CRITICAL findings, and on a degraded scan
|
|
40
|
+
# (--strict) so an incomplete/parse-failed scan can't pass as clean.
|
|
41
|
+
- name: VibeGuard gate (block on CRITICAL)
|
|
42
|
+
run: npx -y vibeguard scan . --precommit --strict
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yagyeshvyas/vibeguard",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "Offline security scanner for AI-generated code. 699 rules, 75 MCP tools, AST taint analysis, shell guard. Zero telemetry, zero dependencies.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "commonjs",
|
|
7
7
|
"author": "Yagyesh Vyas <https://www.linkedin.com/in/yagyeshvyas/>",
|
|
@@ -23,44 +23,47 @@
|
|
|
23
23
|
"hooks",
|
|
24
24
|
"ci",
|
|
25
25
|
"ci-templates",
|
|
26
|
-
"README.md"
|
|
26
|
+
"README.md",
|
|
27
|
+
"CHANGELOG.md"
|
|
27
28
|
],
|
|
28
29
|
"scripts": {
|
|
29
30
|
"test": "node test/run.js",
|
|
30
31
|
"scan": "node bin/cli.js scan",
|
|
31
32
|
"lint": "eslint src/ bin/ --ignore-pattern node_modules",
|
|
32
33
|
"coverage": "c8 node test/run.js",
|
|
33
|
-
"
|
|
34
|
+
"counts": "node scripts/counts.js",
|
|
35
|
+
"benchmark": "node test/benchmark/run.js",
|
|
36
|
+
"postinstall": "node scripts/postinstall.js",
|
|
37
|
+
"prepublishOnly": "node test/run.js && npm run lint"
|
|
34
38
|
},
|
|
35
39
|
"keywords": [
|
|
36
40
|
"security",
|
|
37
|
-
"sast",
|
|
38
41
|
"scanner",
|
|
42
|
+
"sast",
|
|
43
|
+
"vibe-coding",
|
|
44
|
+
"ai-code",
|
|
45
|
+
"mcp",
|
|
39
46
|
"secrets",
|
|
47
|
+
"prompt-injection",
|
|
48
|
+
"static-analysis",
|
|
40
49
|
"sarif",
|
|
41
|
-
"mcp",
|
|
42
50
|
"claude",
|
|
43
51
|
"cursor",
|
|
44
52
|
"windsurf",
|
|
45
53
|
"codex",
|
|
46
|
-
"vibe-coding",
|
|
47
54
|
"vibecoding",
|
|
48
55
|
"ai-security",
|
|
49
|
-
"prompt-injection",
|
|
50
56
|
"jailbreak",
|
|
51
57
|
"privacy",
|
|
52
58
|
"pii",
|
|
53
59
|
"firewall",
|
|
54
60
|
"sandbox",
|
|
55
|
-
"zero-trust",
|
|
56
61
|
"supply-chain",
|
|
57
62
|
"compliance",
|
|
58
63
|
"gdpr",
|
|
59
64
|
"soc2",
|
|
60
|
-
"hipaa",
|
|
61
65
|
"pre-commit",
|
|
62
66
|
"linter",
|
|
63
|
-
"static-analysis",
|
|
64
67
|
"vulnerability",
|
|
65
68
|
"xss",
|
|
66
69
|
"sql-injection",
|
package/src/ast.js
CHANGED
|
@@ -180,6 +180,10 @@ function analyzeAst(content, relPath, preparsed) {
|
|
|
180
180
|
if (QUERY_METHODS.has(prop) && isReqDerived(a0)) {
|
|
181
181
|
push(node, { ruleId: 'ast.nosql-injection', severity: 'high', title: 'NoSQL query from request (AST-confirmed)', message: `${prop}() uses the request as a query object — operator injection ({"$gt":""}).`, fix: 'Build the query from validated scalar fields only.', snippet: `${prop}(req.*)` });
|
|
182
182
|
}
|
|
183
|
+
// Model.find({ name: req.body.name }) — request data nested in query object
|
|
184
|
+
if (QUERY_METHODS.has(prop) && a0 && a0.type === 'ObjectExpression' && a0.properties.some(p => p.value && isReqDerived(p.value))) {
|
|
185
|
+
push(node, { ruleId: 'ast.nosql-injection', severity: 'high', title: 'NoSQL query with request data (AST-confirmed)', message: `${prop}() builds a query from request fields — operator injection if attacker sends {"$gt":""}.`, fix: 'Validate each field type before building the query; reject objects with $ operators.', snippet: `${prop}({...req.*})` });
|
|
186
|
+
}
|
|
183
187
|
// any call whose object arg contains $where
|
|
184
188
|
if (args.some(objectHasWhere)) {
|
|
185
189
|
push(node, { ruleId: 'ast.nosql-injection', severity: 'high', title: 'NoSQL $where operator (AST-confirmed)', message: 'A query uses the $where operator, which executes JavaScript on the DB server — injection risk.', fix: 'Avoid $where; use structured query operators on validated fields.', snippet: '$where' });
|
package/src/auto.js
ADDED
|
@@ -0,0 +1,427 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* src/auto.js — One-command autonomous protection orchestrator.
|
|
5
|
+
*
|
|
6
|
+
* `vibeguard auto` → scan + daemon + hooks + shell guard (idempotent)
|
|
7
|
+
* `vibeguard auto --stop` → reverse everything, restore backups
|
|
8
|
+
* `vibeguard auto --status` → show what's active
|
|
9
|
+
*
|
|
10
|
+
* Composes existing modules: scanner, daemon, hook, shell-guard.
|
|
11
|
+
* All state under .vibeguard/auto.json.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
const fs = require('fs');
|
|
15
|
+
const path = require('path');
|
|
16
|
+
const os = require('os');
|
|
17
|
+
const { spawn, execSync } = require('child_process');
|
|
18
|
+
|
|
19
|
+
const STATE_DIR = '.vibeguard';
|
|
20
|
+
const STATE_FILE = path.join(STATE_DIR, 'auto.json');
|
|
21
|
+
const REPORT_FILE = path.join(STATE_DIR, 'report.json');
|
|
22
|
+
const SARIF_FILE = path.join(STATE_DIR, 'report.sarif');
|
|
23
|
+
const EVENTS_LOG = path.join(STATE_DIR, 'events.log');
|
|
24
|
+
const BACKUP_DIR = path.join(STATE_DIR, 'backups');
|
|
25
|
+
|
|
26
|
+
// ─── Helpers ──────────────────────────────────────────────────────────────
|
|
27
|
+
|
|
28
|
+
function findProjectRoot(startDir) {
|
|
29
|
+
let dir = path.resolve(startDir);
|
|
30
|
+
for (let i = 0; i < 20; i++) {
|
|
31
|
+
if (fs.existsSync(path.join(dir, 'package.json')) || fs.existsSync(path.join(dir, '.git'))) {
|
|
32
|
+
return dir;
|
|
33
|
+
}
|
|
34
|
+
const parent = path.dirname(dir);
|
|
35
|
+
if (parent === dir) break;
|
|
36
|
+
dir = parent;
|
|
37
|
+
}
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function ensureStateDir(root) {
|
|
42
|
+
const dir = path.join(root, STATE_DIR);
|
|
43
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
44
|
+
fs.mkdirSync(path.join(root, BACKUP_DIR), { recursive: true });
|
|
45
|
+
return dir;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function readState(root) {
|
|
49
|
+
const p = path.join(root, STATE_FILE);
|
|
50
|
+
if (!fs.existsSync(p)) return null;
|
|
51
|
+
try { return JSON.parse(fs.readFileSync(p, 'utf8')); } catch { return null; }
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function writeState(root, state) {
|
|
55
|
+
ensureStateDir(root);
|
|
56
|
+
fs.writeFileSync(path.join(root, STATE_FILE), JSON.stringify(state, null, 2));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function removeState(root) {
|
|
60
|
+
try { fs.unlinkSync(path.join(root, STATE_FILE)); } catch {}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function logEvent(root, msg) {
|
|
64
|
+
const logPath = path.join(root, EVENTS_LOG);
|
|
65
|
+
const ts = new Date().toISOString();
|
|
66
|
+
fs.appendFileSync(logPath, `[${ts}] ${msg}\n`);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function backupFile(src, dst) {
|
|
70
|
+
if (fs.existsSync(src)) {
|
|
71
|
+
fs.copyFileSync(src, dst);
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function restoreFile(dst, src) {
|
|
78
|
+
if (fs.existsSync(src)) {
|
|
79
|
+
fs.copyFileSync(src, dst);
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function isProcessAlive(pid) {
|
|
86
|
+
try { process.kill(pid, 0); return true; } catch { return false; }
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// ─── Auto Start ────────────────────────────────────────────────────────────
|
|
90
|
+
|
|
91
|
+
function autoStart(rootDir, opts) {
|
|
92
|
+
opts = opts || {};
|
|
93
|
+
const root = findProjectRoot(rootDir);
|
|
94
|
+
if (!root) {
|
|
95
|
+
return { ok: false, reason: 'No project root found (no package.json or .git). Run from a project directory.' };
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Idempotent: if already running, report status
|
|
99
|
+
const existing = readState(root);
|
|
100
|
+
if (existing && existing.pid && isProcessAlive(existing.pid)) {
|
|
101
|
+
return { ok: true, alreadyRunning: true, state: existing };
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
ensureStateDir(root);
|
|
105
|
+
const state = {
|
|
106
|
+
pid: process.pid,
|
|
107
|
+
root,
|
|
108
|
+
startedAt: new Date().toISOString(),
|
|
109
|
+
features: {},
|
|
110
|
+
backups: {},
|
|
111
|
+
lastScan: null,
|
|
112
|
+
findingsSinceStart: 0,
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
const steps = [];
|
|
116
|
+
|
|
117
|
+
// 1. Initial full scan (always run — CI mode needs it too)
|
|
118
|
+
const { scan } = require('./scanner');
|
|
119
|
+
const result = scan(root, { deps: true, deep: !!opts.deep });
|
|
120
|
+
state.lastScan = new Date().toISOString();
|
|
121
|
+
state.findingsSinceStart = result.findings.length;
|
|
122
|
+
|
|
123
|
+
// Write report + SARIF
|
|
124
|
+
ensureStateDir(root);
|
|
125
|
+
fs.writeFileSync(path.join(root, REPORT_FILE), JSON.stringify(result, null, 2));
|
|
126
|
+
try {
|
|
127
|
+
const { renderSarif } = require('./report');
|
|
128
|
+
fs.writeFileSync(path.join(root, SARIF_FILE), renderSarif(result));
|
|
129
|
+
} catch {}
|
|
130
|
+
|
|
131
|
+
steps.push({
|
|
132
|
+
step: 'scan',
|
|
133
|
+
grade: result.grade,
|
|
134
|
+
counts: result.counts,
|
|
135
|
+
findings: result.findings.length,
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
// CI mode: exit non-zero on critical, no daemon
|
|
139
|
+
if (opts.ci) {
|
|
140
|
+
writeState(root, state);
|
|
141
|
+
return { ok: true, ci: true, steps, exitCode: result.counts.critical > 0 ? 1 : 0 };
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// --fix: apply safe auto-fixes
|
|
145
|
+
if (opts.fix) {
|
|
146
|
+
try {
|
|
147
|
+
const { computeAutoFixes } = require('./autofix');
|
|
148
|
+
const fixes = computeAutoFixes(result.findings, result.root);
|
|
149
|
+
if (fixes && fixes.length > 0) {
|
|
150
|
+
steps.push({ step: 'fix', fixes: fixes.length, reversible: true });
|
|
151
|
+
}
|
|
152
|
+
} catch {}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// 2. Start daemon (file watcher)
|
|
156
|
+
if (!opts.ci) {
|
|
157
|
+
try {
|
|
158
|
+
const { startDaemon, isDaemonRunning } = require('./daemon');
|
|
159
|
+
if (!isDaemonRunning(root)) {
|
|
160
|
+
startDaemon(root, { verbose: opts.verbose }).then((r) => {
|
|
161
|
+
if (r.started) {
|
|
162
|
+
state.features.daemon = true;
|
|
163
|
+
state.daemonPid = r.pid;
|
|
164
|
+
logEvent(root, `daemon started (PID ${r.pid})`);
|
|
165
|
+
}
|
|
166
|
+
}).catch(() => {});
|
|
167
|
+
} else {
|
|
168
|
+
state.features.daemon = true;
|
|
169
|
+
}
|
|
170
|
+
steps.push({ step: 'daemon', started: state.features.daemon });
|
|
171
|
+
} catch (e) {
|
|
172
|
+
steps.push({ step: 'daemon', error: e.message });
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// 3. Install git pre-commit hook (if .git exists)
|
|
177
|
+
const gitDir = path.join(root, '.git');
|
|
178
|
+
if (fs.existsSync(gitDir) && !opts.ci) {
|
|
179
|
+
const hooksDir = path.join(gitDir, 'hooks');
|
|
180
|
+
fs.mkdirSync(hooksDir, { recursive: true });
|
|
181
|
+
const preCommit = path.join(hooksDir, 'pre-commit');
|
|
182
|
+
const backupPath = path.join(root, BACKUP_DIR, 'pre-commit.bak');
|
|
183
|
+
|
|
184
|
+
// Backup existing hook if present
|
|
185
|
+
if (fs.existsSync(preCommit)) {
|
|
186
|
+
backupFile(preCommit, backupPath);
|
|
187
|
+
state.backups.preCommit = backupPath;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// Write the hook
|
|
191
|
+
const nodeBin = process.execPath.replace(/\\/g, '/');
|
|
192
|
+
const cliPath = path.join(__dirname, '..', 'bin', 'cli.js').replace(/\\/g, '/');
|
|
193
|
+
const strictFlag = opts.strict ? ' --strict' : '';
|
|
194
|
+
const hook = `#!/usr/bin/env sh\n# VibeGuard pre-commit hook (auto-installed).\nNODE="${nodeBin}"\nCLI="${cliPath}"\nif [ ! -f "$CLI" ]; then exit 0; fi\n"$NODE" "$CLI" scan . --precommit${strictFlag}\nSTATUS=$?\nif [ "$STATUS" -eq 2 ]; then\n echo "VibeGuard: commit blocked — CRITICAL issue(s). Fix or use --no-verify."\n exit 1\nfi\nexit 0\n`;
|
|
195
|
+
fs.writeFileSync(preCommit, hook);
|
|
196
|
+
try { fs.chmodSync(preCommit, 0o755); } catch {}
|
|
197
|
+
state.features.preCommitHook = true;
|
|
198
|
+
steps.push({ step: 'pre-commit-hook', installed: true, backedUp: state.backups.preCommit ? true : false });
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// 4. Install PostToolUse hook (for AI agent edit scanning)
|
|
202
|
+
if (!opts.ci) {
|
|
203
|
+
try {
|
|
204
|
+
const hookMod = require('./hook');
|
|
205
|
+
const r = hookMod.installPostEditHook(root);
|
|
206
|
+
state.features.postEditHook = r.installed;
|
|
207
|
+
steps.push({ step: 'post-edit-hook', installed: r.installed, file: r.file });
|
|
208
|
+
} catch (e) {
|
|
209
|
+
steps.push({ step: 'post-edit-hook', error: e.message });
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// 5. Arm shell guard (install shell hook) — requires consent
|
|
214
|
+
if (!opts.noShell && !opts.ci) {
|
|
215
|
+
// Skip shell guard in auto unless --yes is passed (consent).
|
|
216
|
+
if (!opts.yes) {
|
|
217
|
+
steps.push({ step: 'shell-guard', installed: false, reason: 'skipped (use --yes to enable)' });
|
|
218
|
+
} else {
|
|
219
|
+
try {
|
|
220
|
+
const srcDir = path.join(__dirname);
|
|
221
|
+
const home = os.homedir();
|
|
222
|
+
const marker = '# >>> VibeGuard shell hook >>>';
|
|
223
|
+
const endMarker = '# <<< VibeGuard shell hook <<<';
|
|
224
|
+
const psMarker = '### >>> VibeGuard shell hook >>>';
|
|
225
|
+
const psEndMarker = '### <<< VibeGuard shell hook <<<';
|
|
226
|
+
const hookContent = `${marker}\nVG_SCRIPT_DIR="${srcDir.replace(/\\/g, '\\\\')}"\nsource "${path.join(srcDir, 'shell-hook.sh').replace(/\\/g, '\\\\')}"\n${endMarker}`;
|
|
227
|
+
|
|
228
|
+
// bash
|
|
229
|
+
const bashrc = path.join(home, '.bashrc');
|
|
230
|
+
if (fs.existsSync(bashrc) || true) {
|
|
231
|
+
let content = fs.existsSync(bashrc) ? fs.readFileSync(bashrc, 'utf8') : '';
|
|
232
|
+
if (!content.includes(marker)) {
|
|
233
|
+
const backupPath = path.join(root, BACKUP_DIR, 'bashrc.bak');
|
|
234
|
+
if (fs.existsSync(bashrc)) backupFile(bashrc, backupPath);
|
|
235
|
+
state.backups.bashrc = backupPath;
|
|
236
|
+
content = content.trimEnd() + '\n' + hookContent + '\n';
|
|
237
|
+
fs.writeFileSync(bashrc, content);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// zsh
|
|
242
|
+
const zshrc = path.join(home, '.zshrc');
|
|
243
|
+
if (fs.existsSync(zshrc)) {
|
|
244
|
+
let content = fs.readFileSync(zshrc, 'utf8');
|
|
245
|
+
if (!content.includes(marker)) {
|
|
246
|
+
const backupPath = path.join(root, BACKUP_DIR, 'zshrc.bak');
|
|
247
|
+
backupFile(zshrc, backupPath);
|
|
248
|
+
state.backups.zshrc = backupPath;
|
|
249
|
+
content = content.trimEnd() + '\n' + hookContent + '\n';
|
|
250
|
+
fs.writeFileSync(zshrc, content);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// PowerShell (Windows)
|
|
255
|
+
if (process.platform === 'win32') {
|
|
256
|
+
const psDir = path.join(home, 'Documents', 'WindowsPowerShell');
|
|
257
|
+
fs.mkdirSync(psDir, { recursive: true });
|
|
258
|
+
const psProfile = path.join(psDir, 'Microsoft.PowerShell_profile.ps1');
|
|
259
|
+
let content = fs.existsSync(psProfile) ? fs.readFileSync(psProfile, 'utf8') : '';
|
|
260
|
+
if (!content.includes(psMarker)) {
|
|
261
|
+
const backupPath = path.join(root, BACKUP_DIR, 'ps-profile.bak');
|
|
262
|
+
if (fs.existsSync(psProfile)) backupFile(psProfile, backupPath);
|
|
263
|
+
state.backups.psProfile = backupPath;
|
|
264
|
+
const psHook = `${psMarker}\n$VG_SCRIPT_DIR = "${srcDir.replace(/\\/g, '\\\\')}"\n. "${path.join(srcDir, 'shell-hook.ps1').replace(/\\/g, '\\\\')}"\n${psEndMarker}`;
|
|
265
|
+
content = content.trimEnd() + '\n' + psHook + '\n';
|
|
266
|
+
fs.writeFileSync(psProfile, content);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
state.features.shellGuard = true;
|
|
271
|
+
steps.push({ step: 'shell-guard', installed: true });
|
|
272
|
+
} catch (e) {
|
|
273
|
+
steps.push({ step: 'shell-guard', error: e.message });
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// Add .vibeguard/ to .gitignore
|
|
279
|
+
const gitignore = path.join(root, '.gitignore');
|
|
280
|
+
try {
|
|
281
|
+
let content = fs.existsSync(gitignore) ? fs.readFileSync(gitignore, 'utf8') : '';
|
|
282
|
+
if (!content.includes('.vibeguard/')) {
|
|
283
|
+
content = content.trimEnd() + '\n.vibeguard/\n';
|
|
284
|
+
fs.writeFileSync(gitignore, content);
|
|
285
|
+
}
|
|
286
|
+
} catch {}
|
|
287
|
+
|
|
288
|
+
writeState(root, state);
|
|
289
|
+
logEvent(root, 'auto started');
|
|
290
|
+
|
|
291
|
+
return { ok: true, steps, state };
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// ─── Auto Stop ─────────────────────────────────────────────────────────────
|
|
295
|
+
|
|
296
|
+
function autoStop(rootDir) {
|
|
297
|
+
const root = findProjectRoot(rootDir) || path.resolve(rootDir);
|
|
298
|
+
const state = readState(root);
|
|
299
|
+
if (!state) {
|
|
300
|
+
return { ok: false, reason: 'No auto state found. Was `vibeguard auto` run?' };
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
const reversed = [];
|
|
304
|
+
|
|
305
|
+
// 1. Kill daemon
|
|
306
|
+
if (state.daemonPid) {
|
|
307
|
+
try { process.kill(state.daemonPid, 'SIGTERM'); } catch {}
|
|
308
|
+
reversed.push('daemon stopped');
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
// 2. Remove pre-commit hook (restore backup if exists)
|
|
312
|
+
const preCommit = path.join(root, '.git', 'hooks', 'pre-commit');
|
|
313
|
+
if (fs.existsSync(preCommit) && state.backups && state.backups.preCommit) {
|
|
314
|
+
restoreFile(preCommit, state.backups.preCommit);
|
|
315
|
+
reversed.push('pre-commit hook restored from backup');
|
|
316
|
+
} else if (fs.existsSync(preCommit)) {
|
|
317
|
+
// Only remove if we installed it
|
|
318
|
+
const content = fs.readFileSync(preCommit, 'utf8');
|
|
319
|
+
if (content.includes('VibeGuard')) {
|
|
320
|
+
fs.unlinkSync(preCommit);
|
|
321
|
+
reversed.push('pre-commit hook removed');
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// 3. Remove PostToolUse hook
|
|
326
|
+
try {
|
|
327
|
+
const hookMod = require('./hook');
|
|
328
|
+
const r = hookMod.uninstallPostEditHook(root);
|
|
329
|
+
if (r.uninstalled) reversed.push('post-edit hook removed');
|
|
330
|
+
} catch {}
|
|
331
|
+
|
|
332
|
+
// 4. Disarm shell guard (remove from shell profiles)
|
|
333
|
+
const marker = '# >>> VibeGuard shell hook >>>';
|
|
334
|
+
const endMarker = '# <<< VibeGuard shell hook <<<';
|
|
335
|
+
const psMarker = '### >>> VibeGuard shell hook >>>';
|
|
336
|
+
const psEndMarker = '### <<< VibeGuard shell hook <<<';
|
|
337
|
+
const home = os.homedir();
|
|
338
|
+
|
|
339
|
+
for (const profile of ['.bashrc', '.zshrc']) {
|
|
340
|
+
const p = path.join(home, profile);
|
|
341
|
+
try {
|
|
342
|
+
if (fs.existsSync(p)) {
|
|
343
|
+
let content = fs.readFileSync(p, 'utf8');
|
|
344
|
+
// Restore from backup if available
|
|
345
|
+
const backupKey = profile === '.bashrc' ? 'bashrc' : 'zshrc';
|
|
346
|
+
if (state.backups && state.backups[backupKey] && fs.existsSync(state.backups[backupKey])) {
|
|
347
|
+
content = fs.readFileSync(state.backups[backupKey], 'utf8');
|
|
348
|
+
fs.writeFileSync(p, content);
|
|
349
|
+
reversed.push(`${profile} restored from backup`);
|
|
350
|
+
} else {
|
|
351
|
+
// Just remove our block
|
|
352
|
+
const before = content;
|
|
353
|
+
content = content.replace(new RegExp(marker + '[\\s\\S]*?' + endMarker + '\\n?', 'g'), '');
|
|
354
|
+
if (content !== before) {
|
|
355
|
+
fs.writeFileSync(p, content);
|
|
356
|
+
reversed.push(`${profile} hook removed`);
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
} catch {}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
if (process.platform === 'win32') {
|
|
364
|
+
try {
|
|
365
|
+
const psProfile = path.join(home, 'Documents', 'WindowsPowerShell', 'Microsoft.PowerShell_profile.ps1');
|
|
366
|
+
if (fs.existsSync(psProfile)) {
|
|
367
|
+
let content = fs.readFileSync(psProfile, 'utf8');
|
|
368
|
+
if (state.backups && state.backups.psProfile && fs.existsSync(state.backups.psProfile)) {
|
|
369
|
+
content = fs.readFileSync(state.backups.psProfile, 'utf8');
|
|
370
|
+
fs.writeFileSync(psProfile, content);
|
|
371
|
+
reversed.push('PowerShell profile restored from backup');
|
|
372
|
+
} else {
|
|
373
|
+
const before = content;
|
|
374
|
+
content = content.replace(new RegExp(psMarker + '[\\s\\S]*?' + psEndMarker + '\\n?', 'g'), '');
|
|
375
|
+
if (content !== before) {
|
|
376
|
+
fs.writeFileSync(psProfile, content);
|
|
377
|
+
reversed.push('PowerShell profile hook removed');
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
} catch {}
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
// 5. Remove state file
|
|
385
|
+
removeState(root);
|
|
386
|
+
reversed.push('state file removed');
|
|
387
|
+
|
|
388
|
+
logEvent(root, 'auto stopped');
|
|
389
|
+
|
|
390
|
+
return { ok: true, reversed };
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
// ─── Auto Status ──────────────────────────────────────────────────────────
|
|
394
|
+
|
|
395
|
+
function autoStatus(rootDir) {
|
|
396
|
+
const root = findProjectRoot(rootDir) || path.resolve(rootDir);
|
|
397
|
+
const state = readState(root);
|
|
398
|
+
if (!state) {
|
|
399
|
+
return { running: false, reason: 'No auto state found' };
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
const daemonAlive = state.daemonPid ? isProcessAlive(state.daemonPid) : false;
|
|
403
|
+
const uptime = state.startedAt ? Date.now() - new Date(state.startedAt).getTime() : 0;
|
|
404
|
+
|
|
405
|
+
return {
|
|
406
|
+
running: true,
|
|
407
|
+
pid: state.pid,
|
|
408
|
+
daemonPid: state.daemonPid,
|
|
409
|
+
daemonAlive,
|
|
410
|
+
startedAt: state.startedAt,
|
|
411
|
+
uptimeMs: uptime,
|
|
412
|
+
features: state.features || {},
|
|
413
|
+
findingsSinceStart: state.findingsSinceStart || 0,
|
|
414
|
+
lastScan: state.lastScan,
|
|
415
|
+
root: state.root,
|
|
416
|
+
};
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
// ─── CI Mode ────────────────────────────────────────────────────────────────
|
|
420
|
+
|
|
421
|
+
function autoCI(rootDir, opts) {
|
|
422
|
+
opts = opts || {};
|
|
423
|
+
opts.ci = true;
|
|
424
|
+
return autoStart(rootDir, opts);
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
module.exports = { autoStart, autoStop, autoStatus, autoCI, findProjectRoot };
|