@haposoft/cafekit 0.18.1 → 0.18.2
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/README.md +12 -1
- package/bin/__tests__/claude-settings.test.js +123 -0
- package/bin/__tests__/codex-ask-guard.test.js +123 -0
- package/bin/__tests__/codex-ask-projection.test.js +51 -0
- package/bin/__tests__/codex-hooks-ownership.test.js +80 -1
- package/bin/__tests__/codex-hooks.test.js +774 -6
- package/bin/__tests__/codex-native-hook-boundaries.test.js +136 -0
- package/bin/__tests__/codex-native.test.js +192 -59
- package/bin/__tests__/codex-projection-parity.test.js +314 -0
- package/bin/__tests__/codex-quiet-hooks.test.js +170 -0
- package/bin/__tests__/codex-research-projection.test.js +70 -0
- package/bin/__tests__/codex-specs-projection.test.js +76 -0
- package/bin/__tests__/develop-contract.test.js +1 -1
- package/bin/__tests__/docs-sync-on-demand.test.js +25 -20
- package/bin/__tests__/inspect-paths.test.js +183 -0
- package/bin/__tests__/installer-safety.test.js +335 -0
- package/bin/__tests__/legacy-hooks-retired.test.js +18 -8
- package/bin/__tests__/omp-bridge.test.js +66 -12
- package/bin/__tests__/omp-hooks.test.js +11 -5
- package/bin/__tests__/orca-session.test.js +7 -0
- package/bin/__tests__/privacy-paths.test.js +317 -0
- package/bin/__tests__/usage-hook-retired.test.js +5 -3
- package/bin/install.js +25 -0
- package/bin/lib/codex-install.js +134 -12
- package/bin/lib/context.js +1 -1
- package/bin/phases/claude-runtime.js +91 -25
- package/bin/phases/claude-settings.js +22 -1
- package/bin/phases/codex-hooks.js +96 -33
- package/bin/phases/codex-runtime.js +45 -52
- package/bin/phases/copy-payload.js +1 -3
- package/package.json +1 -1
- package/src/claude/CLAUDE.md +4 -0
- package/src/claude/gitignore +3 -0
- package/src/claude/hooks/__tests__/installed-root.test.js +111 -0
- package/src/claude/hooks/__tests__/precompact.test.js +206 -10
- package/src/claude/hooks/__tests__/privacy-block.test.js +27 -0
- package/src/claude/hooks/__tests__/session-writeenv.test.js +25 -0
- package/src/claude/hooks/__tests__/spec-gate.test.js +141 -10
- package/src/claude/hooks/__tests__/spec-state-touched.test.js +204 -8
- package/src/claude/hooks/__tests__/state.test.js +207 -7
- package/src/claude/hooks/__tests__/update-check.test.js +88 -0
- package/src/claude/hooks/agent.cjs +7 -6
- package/src/claude/hooks/inspect-block.cjs +15 -63
- package/src/claude/hooks/lib/inspect-paths.cjs +304 -0
- package/src/claude/hooks/lib/parser.cjs +1 -1
- package/src/claude/hooks/lib/privacy-command-analysis.cjs +191 -17
- package/src/claude/hooks/lib/privacy-paths.cjs +313 -0
- package/src/claude/hooks/lib/project-root.cjs +15 -0
- package/src/claude/hooks/lib/rules-reservation.cjs +36 -0
- package/src/claude/hooks/lib/spec-touch.cjs +67 -0
- package/src/claude/hooks/lib/update-check.cjs +77 -0
- package/src/claude/hooks/precompact.cjs +8 -15
- package/src/claude/hooks/privacy-block.cjs +23 -205
- package/src/claude/hooks/rules.cjs +6 -28
- package/src/claude/hooks/session.cjs +34 -117
- package/src/claude/hooks/spec-gate.cjs +9 -26
- package/src/claude/hooks/spec-state.cjs +43 -59
- package/src/claude/hooks/state.cjs +96 -51
- package/src/claude/migration-manifest.json +8 -1
- package/src/claude/rules/hook-protocols.md +1 -1
- package/src/claude/runtime.json +0 -3
- package/src/claude/scripts/generate-skill-catalog.cjs +2 -2
- package/src/claude/scripts/provenance.cjs +14 -0
- package/src/claude/settings/settings.json +1 -1
- package/src/claude/skills/ai-multimodal/scripts/check_setup.py +8 -68
- package/src/claude/skills/ai-multimodal/scripts/gemini_batch_process.py +6 -28
- package/src/claude/skills/git/SKILL.md +1 -1
- package/src/claude/skills/git/references/worktree-blueprint.md +1 -1
- package/src/codex/AGENTS.md +8 -0
- package/src/codex/hooks/ask-guard.cjs +54 -0
- package/src/codex/hooks/inspect-block.cjs +11 -7
- package/src/codex/hooks/lib/inspect-paths.cjs +1 -199
- package/src/codex/hooks/lib/privacy-paths.cjs +1 -0
- package/src/codex/hooks/lib/privacy-state.cjs +98 -16
- package/src/codex/hooks/lib/rules-reservation.cjs +38 -0
- package/src/codex/hooks/lib/secret-keywords.cjs +1 -0
- package/src/codex/hooks/lib/spec-session-touch.cjs +100 -0
- package/src/codex/hooks/lib/spec-touch.cjs +3 -0
- package/src/codex/hooks/lib/update-check.cjs +1 -0
- package/src/codex/hooks/precompact.cjs +39 -0
- package/src/codex/hooks/privacy-approval.cjs +28 -17
- package/src/codex/hooks/privacy-block.cjs +9 -215
- package/src/codex/hooks/rules.cjs +4 -30
- package/src/codex/hooks/secret-output-guardrail.cjs +1 -1
- package/src/codex/hooks/session.cjs +47 -11
- package/src/codex/hooks/spec-gate.cjs +21 -6
- package/src/codex/hooks/spec-state.cjs +32 -6
- package/src/codex/hooks/state.cjs +25 -4
- package/src/codex/hooks.json +39 -7
- package/src/codex/rules/hook-protocols.md +13 -6
- package/src/omp/extensions/cafekit-bridge.mjs +12 -4
- package/src/omp/hooks/privacy-block.cjs +24 -206
- package/src/codex/rules/state-sync.md +0 -60
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const crypto = require('crypto');
|
|
4
|
+
const fs = require('fs');
|
|
5
|
+
const path = require('path');
|
|
6
|
+
const { packetAt, namedPackets, explicitFailure } = require('./spec-touch.cjs');
|
|
7
|
+
const { atomicWrite, assertConfiguredSpecsPath, hookStateDir } = require('./hook-context.cjs');
|
|
8
|
+
|
|
9
|
+
function sessionIdentity(payload) {
|
|
10
|
+
return [payload.session_id, payload.sessionId, payload.sessionID, payload.session?.id]
|
|
11
|
+
.find((value) => typeof value === 'string' && value.trim()) || null;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function patchPaths(command) {
|
|
15
|
+
if (typeof command !== 'string') return [];
|
|
16
|
+
const lines = command.trim().split(/\r?\n/);
|
|
17
|
+
if (lines.shift() !== '*** Begin Patch' || lines.pop() !== '*** End Patch') return [];
|
|
18
|
+
const paths = [];
|
|
19
|
+
let operation = null;
|
|
20
|
+
let body = false;
|
|
21
|
+
for (const line of lines) {
|
|
22
|
+
const header = line.match(/^\*\*\* (Add|Update|Delete) File: (.+)$/);
|
|
23
|
+
if (header) {
|
|
24
|
+
if (operation && operation !== 'Delete' && !body) return [];
|
|
25
|
+
operation = header[1]; body = operation === 'Delete';
|
|
26
|
+
paths.push(header[2]);
|
|
27
|
+
} else if (/^\*\*\* Move to: .+/.test(line) && operation === 'Update' && !body) {
|
|
28
|
+
paths.push(line.slice('*** Move to: '.length));
|
|
29
|
+
} else if (operation === 'Add' && /^\+/.test(line)) {
|
|
30
|
+
body = true;
|
|
31
|
+
} else if (operation === 'Update' && /^(?:@@|[ +\-]|\*\*\* End of File$)/.test(line)) {
|
|
32
|
+
body = true;
|
|
33
|
+
} else {
|
|
34
|
+
return [];
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return operation && body ? paths : [];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function responseText(response) {
|
|
41
|
+
if (typeof response === 'string') return response;
|
|
42
|
+
if (!response || typeof response !== 'object') return '';
|
|
43
|
+
const value = response.output ?? response.result ?? response.message ?? response.content;
|
|
44
|
+
if (typeof value === 'string') return value;
|
|
45
|
+
return Array.isArray(value) ? value.map((item) => item?.text || '').join('\n') : '';
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function successfulMutation(payload) {
|
|
49
|
+
const response = payload.tool_response;
|
|
50
|
+
if (payload.hook_event_name !== 'PostToolUse' || explicitFailure(payload) || response == null) return false;
|
|
51
|
+
const text = responseText(response).trim();
|
|
52
|
+
if (/^(?:error\b|failed\b|failure\b|apply_patch verification failed)/im.test(text)) return false;
|
|
53
|
+
// Contract: github.com/openai/codex/blob/main/codex-rs/core/src/tools/context.rs
|
|
54
|
+
// Native ApplyPatchToolOutput emits a string summary to PostToolUse, whereas its
|
|
55
|
+
// code-mode return is {}. Unknown/empty output is not observed successful mutation.
|
|
56
|
+
const nativeSuccess = /^Success\. Updated the following files:\r?\n[AMD] .+/m.test(text);
|
|
57
|
+
if (payload.tool_name === 'apply_patch') return nativeSuccess;
|
|
58
|
+
return nativeSuccess || response?.ok === true || response?.success === true
|
|
59
|
+
|| /^(?:success\b|.*\bupdated successfully[.!]?\s*$)/i.test(text);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function mutationPaths(payload) {
|
|
63
|
+
if (!successfulMutation(payload)) return [];
|
|
64
|
+
const input = payload.tool_input || {};
|
|
65
|
+
if (payload.tool_name === 'apply_patch') return patchPaths(input.command);
|
|
66
|
+
if (!['Edit', 'Write', 'MultiEdit'].includes(payload.tool_name)) return [];
|
|
67
|
+
return [input.file_path, input.path, ...(Array.isArray(input.edits) ? input.edits.map((edit) => edit?.file_path || edit?.path) : [])]
|
|
68
|
+
.filter((value) => typeof value === 'string');
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function touchSession(payload, context) {
|
|
72
|
+
const sessionId = sessionIdentity(payload);
|
|
73
|
+
const specsRoot = assertConfiguredSpecsPath(context.projectRoot, context.runtime);
|
|
74
|
+
if (!sessionId) return { sessionId, names: [], touched: [], specsRoot };
|
|
75
|
+
const key = crypto.createHash('sha256').update(`${context.projectRoot}\0${sessionId}`).digest('hex');
|
|
76
|
+
const file = path.join(hookStateDir(context.projectRoot), `spec-touched-${key}.json`);
|
|
77
|
+
let previous = [];
|
|
78
|
+
try { previous = JSON.parse(fs.readFileSync(file, 'utf8')); } catch { /* first touch */ }
|
|
79
|
+
const touched = new Set(Array.isArray(previous) ? previous.filter((name) => typeof name === 'string') : []);
|
|
80
|
+
let names;
|
|
81
|
+
if (payload.hook_event_name === 'PostToolUse') {
|
|
82
|
+
names = mutationPaths(payload).map((value) => packetAt(value, specsRoot, context.sessionCwd)).filter(Boolean);
|
|
83
|
+
} else {
|
|
84
|
+
let packets = [];
|
|
85
|
+
try { packets = fs.readdirSync(specsRoot, { withFileTypes: true }).filter((entry) => entry.isDirectory()).map((entry) => entry.name); } catch { /* absent root */ }
|
|
86
|
+
names = namedPackets(payload.prompt || payload.user_prompt, packets, specsRoot, context.projectRoot);
|
|
87
|
+
const feature = payload.explicitFeature ?? payload.featureName ?? payload.feature;
|
|
88
|
+
if (typeof feature === 'string' && feature.trim() && !/[\\/]/.test(feature) && !['.', '..', '_shared'].includes(feature)) names.push(feature);
|
|
89
|
+
const explicitPath = payload.explicitPath ?? payload.specPath ?? payload.spec_path ?? payload.featurePath;
|
|
90
|
+
const fromPath = packetAt(explicitPath, specsRoot, context.projectRoot);
|
|
91
|
+
if (fromPath) names.push(fromPath);
|
|
92
|
+
}
|
|
93
|
+
for (const name of names) touched.add(name);
|
|
94
|
+
if (names.length) {
|
|
95
|
+
try { atomicWrite(file, `${JSON.stringify([...touched].sort())}\n`); } catch { /* no shared fallback */ }
|
|
96
|
+
}
|
|
97
|
+
return { sessionId, names: [...new Set(names)], touched: [...touched], specsRoot };
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
module.exports = { touchSession, sessionIdentity };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('../../../claude/hooks/lib/update-check.cjs');
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
const crypto = require('crypto');
|
|
5
|
+
const path = require('path');
|
|
6
|
+
const { execFileSync } = require('child_process');
|
|
7
|
+
const { PROJECT_ROOT, atomicWrite, hookStateDir, logCrash, readPayload } = require('./lib/hook-context.cjs');
|
|
8
|
+
|
|
9
|
+
function gitLine(args) {
|
|
10
|
+
try {
|
|
11
|
+
return execFileSync('git', args, {
|
|
12
|
+
cwd: PROJECT_ROOT, encoding: 'utf8', timeout: 3000, stdio: ['ignore', 'pipe', 'ignore'],
|
|
13
|
+
}).trim() || null;
|
|
14
|
+
} catch { return null; }
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
try {
|
|
18
|
+
const payload = readPayload();
|
|
19
|
+
if (!payload) process.exit(0);
|
|
20
|
+
const sessionId = [payload.session_id, payload.sessionId, payload.sessionID, payload.session?.id]
|
|
21
|
+
.find(value => typeof value === 'string' && value.trim());
|
|
22
|
+
if (!sessionId) process.exit(0);
|
|
23
|
+
const status = gitLine(['status', '--porcelain']);
|
|
24
|
+
// These anchors are re-derivable facts, never proof or authorization.
|
|
25
|
+
const record = {
|
|
26
|
+
captured_at: new Date().toISOString(),
|
|
27
|
+
session_id: sessionId,
|
|
28
|
+
trigger: typeof payload.trigger === 'string' ? payload.trigger : null,
|
|
29
|
+
project_root: PROJECT_ROOT,
|
|
30
|
+
branch: gitLine(['rev-parse', '--abbrev-ref', 'HEAD']),
|
|
31
|
+
head: gitLine(['rev-parse', '--short', 'HEAD']),
|
|
32
|
+
dirty_count: status === null ? null : status.split('\n').filter(line => line.trim()).length,
|
|
33
|
+
};
|
|
34
|
+
const key = crypto.createHash('sha256').update(sessionId).digest('hex');
|
|
35
|
+
atomicWrite(path.join(hookStateDir(PROJECT_ROOT), `compact-recovery-${key}.json`), `${JSON.stringify(record)}\n`);
|
|
36
|
+
} catch (error) {
|
|
37
|
+
if (error.message === 'hook payload is empty') process.exit(0);
|
|
38
|
+
logCrash('precompact', error);
|
|
39
|
+
}
|
|
@@ -1,16 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
|
-
const {
|
|
5
|
-
APPROVAL_PREFIX,
|
|
6
|
-
approvePending,
|
|
7
|
-
cleanup
|
|
8
|
-
} = require('./lib/privacy-state.cjs');
|
|
9
|
-
const {
|
|
10
|
-
getHookContext,
|
|
11
|
-
readPayload
|
|
12
|
-
} = require('./lib/hook-context.cjs');
|
|
13
|
-
|
|
14
4
|
function context(message) {
|
|
15
5
|
process.stdout.write(`${JSON.stringify({
|
|
16
6
|
hookSpecificOutput: {
|
|
@@ -20,10 +10,20 @@ function context(message) {
|
|
|
20
10
|
})}\n`);
|
|
21
11
|
}
|
|
22
12
|
|
|
13
|
+
let projectRoot, sessionId, state;
|
|
23
14
|
try {
|
|
15
|
+
const { PROJECT_ROOT, readPayload } = require('./lib/hook-context.cjs');
|
|
24
16
|
const data = readPayload();
|
|
25
17
|
if (!data) process.exit(0);
|
|
26
|
-
|
|
18
|
+
projectRoot = PROJECT_ROOT;
|
|
19
|
+
sessionId = data.session_id;
|
|
20
|
+
let claimed = { ok: false, requestIds: [] };
|
|
21
|
+
try {
|
|
22
|
+
state = require('./lib/privacy-state.cjs');
|
|
23
|
+
claimed = state.claimShortMarkers(projectRoot, sessionId);
|
|
24
|
+
} catch { /* the outer catch retries invalidation, never approval */ }
|
|
25
|
+
if (!claimed.ok) throw new Error('Short approval markers could not be claimed safely');
|
|
26
|
+
const { APPROVAL_PREFIX, approvePending, cleanup } = state;
|
|
27
27
|
// A pasted prompt arrives with whatever whitespace the terminal added around
|
|
28
28
|
// it. Rejecting on a trailing newline made the approval look broken while the
|
|
29
29
|
// id it named was still perfectly valid; the exactness that matters is the
|
|
@@ -31,20 +31,25 @@ try {
|
|
|
31
31
|
const prompt = String(data.prompt || '').trim();
|
|
32
32
|
cleanup(projectRoot);
|
|
33
33
|
|
|
34
|
-
|
|
35
|
-
if (!
|
|
34
|
+
const shortApproval = new Set(['y', 'yes', 'có']).has(prompt.normalize('NFC').toLowerCase());
|
|
35
|
+
if (!shortApproval && !prompt.startsWith(APPROVAL_PREFIX)) process.exit(0);
|
|
36
|
+
if (typeof sessionId !== 'string' || !sessionId.trim()) {
|
|
36
37
|
context('CafeKit privacy approval rejected: this hook invocation had no Codex session ID.');
|
|
37
38
|
process.exit(0);
|
|
38
39
|
}
|
|
39
|
-
|
|
40
|
-
if (
|
|
40
|
+
let requestId;
|
|
41
|
+
if (shortApproval) {
|
|
42
|
+
if (claimed.requestIds.length !== 1) process.exit(0);
|
|
43
|
+
requestId = claimed.requestIds[0];
|
|
44
|
+
} else requestId = prompt.slice(APPROVAL_PREFIX.length);
|
|
45
|
+
if (!/^[a-f0-9]{24}$/.test(requestId)) {
|
|
41
46
|
context('CafeKit privacy approval rejected: the prompt did not exactly match a pending request.');
|
|
42
47
|
process.exit(0);
|
|
43
48
|
}
|
|
44
49
|
|
|
45
50
|
const result = approvePending({
|
|
46
51
|
projectRoot,
|
|
47
|
-
sessionId
|
|
52
|
+
sessionId,
|
|
48
53
|
requestId
|
|
49
54
|
});
|
|
50
55
|
if (!result.ok) {
|
|
@@ -54,10 +59,16 @@ try {
|
|
|
54
59
|
|
|
55
60
|
context(
|
|
56
61
|
`User approved the pending sensitive access (${result.displayName}). ` +
|
|
57
|
-
`The grant is session-bound, bound to exactly those paths, and expires in one hour
|
|
62
|
+
`The grant is session-bound, bound to exactly those paths, and expires in one hour. ` +
|
|
63
|
+
`Tell the user which paths were just granted (${result.displayName}) before reading them.`
|
|
58
64
|
);
|
|
59
65
|
process.exit(0);
|
|
60
66
|
} catch {
|
|
67
|
+
try {
|
|
68
|
+
if (projectRoot && typeof sessionId === 'string' && sessionId.trim()) {
|
|
69
|
+
(state || require('./lib/privacy-state.cjs')).claimShortMarkers(projectRoot, sessionId);
|
|
70
|
+
}
|
|
71
|
+
} catch { /* no approval follows an invalidation failure */ }
|
|
61
72
|
context('CafeKit privacy approval failed safely; no token was issued.');
|
|
62
73
|
process.exit(0);
|
|
63
74
|
}
|
|
@@ -1,218 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
|
-
const fs = require('fs');
|
|
5
|
-
const path = require('path');
|
|
6
|
-
const {
|
|
7
|
-
APPROVAL_PREFIX,
|
|
8
|
-
createPending,
|
|
9
|
-
hasGrant
|
|
10
|
-
} = require('./lib/privacy-state.cjs');
|
|
11
|
-
const {
|
|
12
|
-
getHookContext,
|
|
13
|
-
readPayload
|
|
14
|
-
} = require('./lib/hook-context.cjs');
|
|
15
|
-
const { commandAccess } = require('./lib/privacy-command-analysis.cjs');
|
|
16
|
-
|
|
17
|
-
const RESTRICTED = [
|
|
18
|
-
/^\.env(?:[.\[*?{]|$)/i,
|
|
19
|
-
/^credentials/i,
|
|
20
|
-
/secrets?\.(ya?ml|json)$/i,
|
|
21
|
-
/\.pem$/i,
|
|
22
|
-
/\.key$/i,
|
|
23
|
-
/\.p12$/i,
|
|
24
|
-
/\.pfx$/i,
|
|
25
|
-
/^id_(rsa|ed25519|ecdsa|dsa)$/i,
|
|
26
|
-
/\.netrc$/i,
|
|
27
|
-
/\.pgpass$/i,
|
|
28
|
-
/kubeconfig/i,
|
|
29
|
-
/\.keystore$/i,
|
|
30
|
-
/\.jks$/i,
|
|
31
|
-
/auth\.json$/i,
|
|
32
|
-
/token(s)?\.json$/i
|
|
33
|
-
];
|
|
34
|
-
|
|
35
|
-
const EXEMPT = [/\.env\.(example|sample|template|test)$/i];
|
|
36
|
-
|
|
37
|
-
function isPathKey(key) {
|
|
38
|
-
return /^(?:file_?paths?|paths?|search_?paths?|director(?:y|ies)|cwd|root|locations?)$/i
|
|
39
|
-
.test(key);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
function matchesAny(filePath, rules) {
|
|
43
|
-
const base = path.basename(filePath);
|
|
44
|
-
return rules.some((rule) => rule.test(base) || rule.test(filePath));
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function isSafe(filePath) {
|
|
48
|
-
return matchesAny(filePath, EXEMPT);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function isSensitive(filePath) {
|
|
52
|
-
return matchesAny(filePath, RESTRICTED);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
function resolveTarget(filePath, cwd) {
|
|
56
|
-
try {
|
|
57
|
-
const requested = path.isAbsolute(filePath) ? filePath : path.resolve(cwd, filePath);
|
|
58
|
-
return fs.realpathSync(requested);
|
|
59
|
-
} catch {
|
|
60
|
-
return null;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* When realpath fails, check whether the requested path is a symlink (or chain)
|
|
66
|
-
* whose lexical target is sensitive. Dangling link to protected target -> true (fail-closed deny);
|
|
67
|
-
* dangling benign link or link to allowed example/template -> false (allow).
|
|
68
|
-
* Limit hops to avoid loops; malformed/looping -> fail closed.
|
|
69
|
-
* TOCTOU note: check and use are not atomic; target could be swapped between this check and the actual read.
|
|
70
|
-
* This is a best-effort guardrail, not an arbitrary-shell security boundary. For strong isolation use OS sandbox.
|
|
71
|
-
*/
|
|
72
|
-
function isSymlinkToSensitive(filePath, cwd) {
|
|
73
|
-
try {
|
|
74
|
-
const requested = path.isAbsolute(filePath) ? filePath : path.resolve(cwd, filePath);
|
|
75
|
-
let current = requested;
|
|
76
|
-
let hops = 0;
|
|
77
|
-
const MAX_HOPS = 8;
|
|
78
|
-
while (hops < MAX_HOPS) {
|
|
79
|
-
let stat;
|
|
80
|
-
try {
|
|
81
|
-
stat = fs.lstatSync(current);
|
|
82
|
-
} catch {
|
|
83
|
-
return false;
|
|
84
|
-
}
|
|
85
|
-
if (!stat.isSymbolicLink()) return false;
|
|
86
|
-
let linkTarget;
|
|
87
|
-
try {
|
|
88
|
-
linkTarget = fs.readlinkSync(current);
|
|
89
|
-
} catch {
|
|
90
|
-
return true;
|
|
91
|
-
}
|
|
92
|
-
if (isSensitive(linkTarget) && !isSafe(linkTarget)) return true;
|
|
93
|
-
if (isSafe(linkTarget)) return false;
|
|
94
|
-
const next = path.isAbsolute(linkTarget) ? linkTarget : path.resolve(path.dirname(current), linkTarget);
|
|
95
|
-
if (next === current) return true;
|
|
96
|
-
try {
|
|
97
|
-
const real = fs.realpathSync(next);
|
|
98
|
-
if (isSensitive(real) && !isSafe(real)) return true;
|
|
99
|
-
if (isSafe(real)) return false;
|
|
100
|
-
return false;
|
|
101
|
-
} catch {
|
|
102
|
-
current = next;
|
|
103
|
-
hops += 1;
|
|
104
|
-
continue;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
return true;
|
|
108
|
-
} catch {
|
|
109
|
-
return true;
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
function extractCommandPaths(command) {
|
|
114
|
-
// The analyzer parses the command and returns the operands of every reading
|
|
115
|
-
// command it recognises, nested shells and `find -exec` included. Treating
|
|
116
|
-
// every unresolved expansion as sensitive, as this did before, denied
|
|
117
|
-
// `cat $FILE`, `wc -l *.cjs` and `cat ~/.zshrc` alike -- and every denial here
|
|
118
|
-
// costs a round trip through a one-shot approval prompt.
|
|
119
|
-
// A heredoc whose delimiter is quoted (<<'BODY') is inert: the shell expands
|
|
120
|
-
// nothing inside it, so no word in the body is ever read. The analyzer has no
|
|
121
|
-
// notion of heredocs and parses the body as shell, so prose that quotes a
|
|
122
|
-
// command -- a PR body carrying `cat .env` in backticks -- came back as a real
|
|
123
|
-
// command substitution. Blank those bodies out first. An unquoted delimiter
|
|
124
|
-
// (<<BODY) does expand, so it is left to the analyzer.
|
|
125
|
-
const scrubbed = String(command).replace(
|
|
126
|
-
/(<<-?[ \t]*)(['"])([A-Za-z_]\w*)\2[^\n]*\n[\s\S]*?\n[ \t]*\3\b/g,
|
|
127
|
-
'$1$2$3$2'
|
|
128
|
-
);
|
|
129
|
-
|
|
130
|
-
const paths = new Set(commandAccess(scrubbed).paths);
|
|
131
|
-
|
|
132
|
-
// Two shapes the analyzer cannot return. It drops any word carrying an
|
|
133
|
-
// expansion, so `~/.ssh/id_rsa` and `certs/*.pem` still have to be matched by
|
|
134
|
-
// name. And it only walks commands it recognises, so an unknown tool handed a
|
|
135
|
-
// secret through a file-shaped option, such as
|
|
136
|
-
// `docker compose --env-file .env`, is invisible to it.
|
|
137
|
-
const FILE_OPTION = /^--?[\w-]*(?:file|config|key|cert|identity)$/i;
|
|
138
|
-
const tokens = scrubbed.split(/[\s|;&<>"'()`]+/);
|
|
139
|
-
for (let index = 0; index < tokens.length; index += 1) {
|
|
140
|
-
const token = tokens[index];
|
|
141
|
-
const shaped = /^~|[*?[{]/.test(token);
|
|
142
|
-
const optionFed = index > 0 && FILE_OPTION.test(tokens[index - 1]);
|
|
143
|
-
if (!shaped && !optionFed) continue;
|
|
144
|
-
const value = token.replace(/^~/, '').replace(/^["'()`]+|["'(),]+$/g, '');
|
|
145
|
-
if (value && isSensitive(value) && !isSafe(value)) paths.add(value);
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
return [...paths];
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
function extractPatchPaths(patch) {
|
|
152
|
-
return [...String(patch).matchAll(
|
|
153
|
-
/^\*\*\* (?:(?:Add|Update|Delete) File:|Move to:)\s+(.+)$/gm
|
|
154
|
-
)]
|
|
155
|
-
.map((match) => match[1].trim())
|
|
156
|
-
.filter(Boolean);
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
function shellInput(input) {
|
|
160
|
-
if (typeof input?.command === 'string') return input.command;
|
|
161
|
-
if (typeof input?.cmd === 'string') return input.cmd;
|
|
162
|
-
return '';
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
function patchInput(input) {
|
|
166
|
-
if (typeof input === 'string') return input;
|
|
167
|
-
for (const key of ['command', 'patch', 'input']) {
|
|
168
|
-
if (typeof input?.[key] === 'string') return input[key];
|
|
169
|
-
}
|
|
170
|
-
return '';
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
function collectPathValues(value, key, out) {
|
|
174
|
-
if (typeof value === 'string' && isPathKey(key)) {
|
|
175
|
-
out.push(value);
|
|
176
|
-
return;
|
|
177
|
-
}
|
|
178
|
-
if (Array.isArray(value)) {
|
|
179
|
-
if (isPathKey(key)) out.push(...value.filter((item) => typeof item === 'string'));
|
|
180
|
-
return;
|
|
181
|
-
}
|
|
182
|
-
if (!value || typeof value !== 'object') return;
|
|
183
|
-
for (const [childKey, childValue] of Object.entries(value)) {
|
|
184
|
-
collectPathValues(childValue, childKey, out);
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
function extractPaths(toolName, input) {
|
|
189
|
-
const paths = [];
|
|
190
|
-
collectPathValues(input, '', paths);
|
|
191
|
-
const normalizedTool = String(toolName).toLowerCase();
|
|
192
|
-
if (['bash', 'exec_command', 'shell'].includes(normalizedTool)) {
|
|
193
|
-
paths.push(...extractCommandPaths(shellInput(input)));
|
|
194
|
-
} else if (['apply_patch', 'edit', 'write'].includes(normalizedTool)) {
|
|
195
|
-
paths.push(...extractPatchPaths(patchInput(input)));
|
|
196
|
-
}
|
|
197
|
-
return [...new Set(paths.filter(Boolean))];
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
function sensitiveRequest(filePath, cwd) {
|
|
201
|
-
const target = resolveTarget(filePath, cwd);
|
|
202
|
-
if (target) {
|
|
203
|
-
if (isSensitive(target) && !isSafe(target)) return true;
|
|
204
|
-
if (isSafe(target)) return false;
|
|
205
|
-
} else {
|
|
206
|
-
if (isSymlinkToSensitive(filePath, cwd)) return true;
|
|
207
|
-
}
|
|
208
|
-
return !isSafe(filePath) && isSensitive(filePath);
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
function sensitivePaths(toolName, input, cwd) {
|
|
212
|
-
return extractPaths(toolName, input)
|
|
213
|
-
.filter((filePath) => sensitiveRequest(filePath, cwd));
|
|
214
|
-
}
|
|
215
|
-
|
|
216
4
|
function deny(reason) {
|
|
217
5
|
process.stdout.write(`${JSON.stringify({
|
|
218
6
|
hookSpecificOutput: {
|
|
@@ -224,9 +12,14 @@ function deny(reason) {
|
|
|
224
12
|
}
|
|
225
13
|
|
|
226
14
|
try {
|
|
15
|
+
const { APPROVAL_PREFIX, createPending, hasGrant } = require('./lib/privacy-state.cjs');
|
|
16
|
+
const { PROJECT_ROOT, readRuntime, resolveSessionCwd, readPayload } = require('./lib/hook-context.cjs');
|
|
17
|
+
const { sensitivePaths } = require('./lib/privacy-paths.cjs');
|
|
227
18
|
const data = readPayload();
|
|
228
19
|
if (!data) process.exit(0);
|
|
229
|
-
const
|
|
20
|
+
const projectRoot = PROJECT_ROOT;
|
|
21
|
+
const runtime = readRuntime(PROJECT_ROOT);
|
|
22
|
+
const sessionCwd = resolveSessionCwd(data);
|
|
230
23
|
if (runtime.privacyBlock === false) process.exit(0);
|
|
231
24
|
|
|
232
25
|
const filePaths = sensitivePaths(data.tool_name || '', data.tool_input || {}, sessionCwd);
|
|
@@ -250,8 +43,9 @@ try {
|
|
|
250
43
|
const pending = createPending(grantInput);
|
|
251
44
|
deny(
|
|
252
45
|
`Sensitive file access denied: ${pending.displayName}. ` +
|
|
253
|
-
`To
|
|
254
|
-
`
|
|
46
|
+
`To allow exactly these paths for one hour of this Codex session, the user replies ` +
|
|
47
|
+
`y (or yes / có) as their very next message; any other reply cancels that shortcut, ` +
|
|
48
|
+
`and it only works while this is the only pending request. Fallback exact prompt: ` +
|
|
255
49
|
`${APPROVAL_PREFIX}${pending.requestId}`
|
|
256
50
|
);
|
|
257
51
|
process.exit(0);
|
|
@@ -1,40 +1,14 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const crypto = require('crypto');
|
|
4
3
|
const fs = require('fs');
|
|
5
4
|
const path = require('path');
|
|
6
5
|
const {
|
|
7
|
-
|
|
8
|
-
hookStateDir,
|
|
6
|
+
PROJECT_ROOT,
|
|
9
7
|
logCrash,
|
|
10
8
|
readPayload,
|
|
11
9
|
resolveProjectPath
|
|
12
10
|
} = require('./lib/hook-context.cjs');
|
|
13
|
-
|
|
14
|
-
function reservationFile(projectRoot, sessionId) {
|
|
15
|
-
const key = crypto.createHash('sha256')
|
|
16
|
-
.update(String(sessionId || 'unknown'))
|
|
17
|
-
.digest('hex')
|
|
18
|
-
.slice(0, 16);
|
|
19
|
-
return path.join(hookStateDir(projectRoot), `rules-${key}.json`);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/** Reserve one injection slot per session. Returns null on reservation errors. */
|
|
23
|
-
function reserveSession(projectRoot, sessionId) {
|
|
24
|
-
if (!sessionId) return null;
|
|
25
|
-
try {
|
|
26
|
-
const file = reservationFile(projectRoot, sessionId);
|
|
27
|
-
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
28
|
-
fs.writeFileSync(file, JSON.stringify({ sessionId: String(sessionId) }), {
|
|
29
|
-
encoding: 'utf8',
|
|
30
|
-
flag: 'wx',
|
|
31
|
-
mode: 0o600
|
|
32
|
-
});
|
|
33
|
-
return true;
|
|
34
|
-
} catch (error) {
|
|
35
|
-
return error.code === 'EEXIST' ? false : null;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
11
|
+
const { reserveSession } = require('./lib/rules-reservation.cjs');
|
|
38
12
|
|
|
39
13
|
/**
|
|
40
14
|
* Body of the output style for `codingLevel`, or null.
|
|
@@ -70,10 +44,10 @@ function readRuntime(projectRoot) {
|
|
|
70
44
|
try {
|
|
71
45
|
const payload = readPayload();
|
|
72
46
|
if (!payload) process.exit(0);
|
|
73
|
-
const
|
|
74
|
-
if (reserveSession(projectRoot, payload.session_id) !== true) process.exit(0);
|
|
47
|
+
const projectRoot = PROJECT_ROOT;
|
|
75
48
|
const runtime = readRuntime(projectRoot);
|
|
76
49
|
if (runtime === null) process.exit(0);
|
|
50
|
+
if (reserveSession(projectRoot, payload.session_id) !== true) process.exit(0);
|
|
77
51
|
|
|
78
52
|
const respondLang = runtime.locale?.responseLanguage || '';
|
|
79
53
|
const thinkLang = runtime.locale?.thinkingLanguage || (respondLang ? 'en' : '');
|
|
@@ -15,9 +15,9 @@
|
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
17
|
const { logCrash, readPayload } = require('./lib/hook-context.cjs');
|
|
18
|
-
const { REMINDER, containsSecretKeyword } = require('./lib/secret-keywords.cjs');
|
|
19
18
|
|
|
20
19
|
try {
|
|
20
|
+
const { REMINDER, containsSecretKeyword } = require('./lib/secret-keywords.cjs');
|
|
21
21
|
const payload = readPayload();
|
|
22
22
|
if (!payload) process.exit(0);
|
|
23
23
|
|
|
@@ -3,13 +3,18 @@
|
|
|
3
3
|
|
|
4
4
|
const fs = require('fs');
|
|
5
5
|
const path = require('path');
|
|
6
|
+
const crypto = require('crypto');
|
|
6
7
|
const { execFileSync } = require('child_process');
|
|
7
8
|
const {
|
|
8
|
-
|
|
9
|
+
PROJECT_ROOT,
|
|
10
|
+
hookStateDir,
|
|
11
|
+
readRuntime,
|
|
12
|
+
resolveSessionCwd,
|
|
9
13
|
logCrash,
|
|
10
14
|
readPayload
|
|
11
15
|
} = require('./lib/hook-context.cjs');
|
|
12
16
|
const { clearState } = require('./lib/privacy-state.cjs');
|
|
17
|
+
const { clearSessionReservation } = require('./lib/rules-reservation.cjs');
|
|
13
18
|
|
|
14
19
|
function readPackage(projectRoot) {
|
|
15
20
|
try {
|
|
@@ -69,10 +74,48 @@ function gitBranch(projectRoot) {
|
|
|
69
74
|
}
|
|
70
75
|
}
|
|
71
76
|
|
|
77
|
+
function printCompactAnchors(projectRoot, sessionId) {
|
|
78
|
+
if (!sessionId) return;
|
|
79
|
+
try {
|
|
80
|
+
const key = crypto.createHash('sha256').update(sessionId).digest('hex');
|
|
81
|
+
const file = path.join(hookStateDir(projectRoot), `compact-recovery-${key}.json`);
|
|
82
|
+
const record = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
83
|
+
if (record.session_id !== sessionId) return;
|
|
84
|
+
const parts = [];
|
|
85
|
+
if (record.branch) parts.push(`branch ${record.branch}`);
|
|
86
|
+
if (record.head) parts.push(`HEAD ${record.head}`);
|
|
87
|
+
if (typeof record.dirty_count === 'number') parts.push(`${record.dirty_count} uncommitted path(s)`);
|
|
88
|
+
process.stdout.write('\n=== State before compaction ===\n');
|
|
89
|
+
if (record.captured_at) process.stdout.write(`Captured: ${record.captured_at}\n`);
|
|
90
|
+
if (parts.length) process.stdout.write(`Repository: ${parts.join(' | ')}\n`);
|
|
91
|
+
process.stdout.write('Re-read these from disk before acting; they describe the moment before compaction.\n');
|
|
92
|
+
process.stdout.write('=== End of pre-compaction state ===\n');
|
|
93
|
+
} catch { /* no matching capture available */ }
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
(async () => {
|
|
72
97
|
try {
|
|
73
98
|
const payload = readPayload();
|
|
74
99
|
if (!payload) process.exit(0);
|
|
75
|
-
|
|
100
|
+
// Invalidate this session before runtime configuration can interrupt startup.
|
|
101
|
+
const sessionId = [payload.session_id, payload.sessionId, payload.sessionID, payload.session?.id]
|
|
102
|
+
.find((value) => typeof value === 'string' && value.trim());
|
|
103
|
+
clearState(PROJECT_ROOT, sessionId);
|
|
104
|
+
let updateNotice = '';
|
|
105
|
+
try {
|
|
106
|
+
const { checkCafeKitUpdate } = require('./lib/update-check.cjs');
|
|
107
|
+
updateNotice = await checkCafeKitUpdate(path.join(PROJECT_ROOT, '.codex'));
|
|
108
|
+
} catch { /* an update failure must not interrupt session startup */ }
|
|
109
|
+
if (payload.source === 'compact') {
|
|
110
|
+
clearSessionReservation(PROJECT_ROOT, sessionId);
|
|
111
|
+
process.stdout.write(
|
|
112
|
+
'Session compacted. Pending confirmations may have been lost. Pending CafeKit privacy approvals were invalidated; request fresh user approval before continuing.\n'
|
|
113
|
+
);
|
|
114
|
+
printCompactAnchors(PROJECT_ROOT, sessionId);
|
|
115
|
+
}
|
|
116
|
+
const projectRoot = PROJECT_ROOT;
|
|
117
|
+
const runtime = readRuntime(PROJECT_ROOT);
|
|
118
|
+
const sessionCwd = resolveSessionCwd(payload);
|
|
76
119
|
const pkg = readPackage(projectRoot);
|
|
77
120
|
const configured = runtime.project || {};
|
|
78
121
|
const projectType = configured.type && configured.type !== 'auto'
|
|
@@ -86,9 +129,6 @@ try {
|
|
|
86
129
|
: detectFramework(pkg);
|
|
87
130
|
const branch = gitBranch(projectRoot);
|
|
88
131
|
|
|
89
|
-
// Approval tokens never survive a session boundary or compaction.
|
|
90
|
-
clearState(projectRoot);
|
|
91
|
-
|
|
92
132
|
// Orca (onorca.dev) sets ORCA_PANE_KEY in every pane; it is a presence marker, not a
|
|
93
133
|
// secret. ORCA_AGENT_HOOK_TOKEN and ORCA_AGENT_LAUNCH_TOKEN live in the same
|
|
94
134
|
// environment and must never be read or printed here.
|
|
@@ -101,7 +141,7 @@ try {
|
|
|
101
141
|
insideOrcaPane && 'Orca: pane'
|
|
102
142
|
].filter(Boolean);
|
|
103
143
|
process.stdout.write(
|
|
104
|
-
`Session ${payload.source || 'unknown'}. ${parts.join(' | ') || 'No project info detected.'}\n`
|
|
144
|
+
`Session ${payload.source || 'unknown'}. ${parts.join(' | ') || 'No project info detected.'}${updateNotice}\n`
|
|
105
145
|
);
|
|
106
146
|
|
|
107
147
|
if (sessionCwd !== projectRoot) {
|
|
@@ -109,11 +149,7 @@ try {
|
|
|
109
149
|
`CafeKit project root: ${projectRoot} (session working directory: ${sessionCwd}).\n`
|
|
110
150
|
);
|
|
111
151
|
}
|
|
112
|
-
if (payload.source === 'compact') {
|
|
113
|
-
process.stdout.write(
|
|
114
|
-
'Session compacted. Pending CafeKit privacy approvals were invalidated; request fresh user approval if needed.\n'
|
|
115
|
-
);
|
|
116
|
-
}
|
|
117
152
|
} catch (error) {
|
|
118
153
|
logCrash('session', error);
|
|
119
154
|
}
|
|
155
|
+
})();
|