taskforce-loop-engineering 0.16.2 → 0.16.4
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 +16 -0
- package/README.md +4 -0
- package/docs/isolated-verification.md +81 -0
- package/lib/context-budget.mjs +9 -1
- package/lib/core.mjs +298 -40
- package/lib/isolated-verification.mjs +47 -0
- package/package.json +6 -4
- package/scripts/async-acceptance-refresh-self-test.mjs +3 -0
- package/scripts/context-budget-self-test.mjs +25 -7
- package/scripts/final-judgement-self-test.mjs +568 -1
- package/scripts/isolated-verification-self-test.mjs +201 -0
- package/scripts/isolated-verify-self-test.py +133 -0
- package/scripts/isolated-verify.py +418 -0
- package/scripts/project-gate-reconciliation-self-test.mjs +36 -1
- package/scripts/revision-coverage-self-test.mjs +105 -0
- package/skills/taskforce-loop-engineering/SKILL.md +26 -1
- package/skills/taskforce-loop-engineering/references/context-budget.md +1 -1
- package/skills/taskforce-loop-engineering/references/frozen-input-verification.md +40 -0
- package/skills/taskforce-loop-engineering/references/scope-task-acceptance.md +29 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import assert from 'node:assert/strict';
|
|
2
|
+
import { chmod, mkdtemp, mkdir, readFile, symlink, writeFile } from 'node:fs/promises';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { tmpdir } from 'node:os';
|
|
5
|
+
import { realpathSync } from 'node:fs';
|
|
6
|
+
import { execFileSync } from 'node:child_process';
|
|
7
|
+
import { enqueueTask, runCommand, runQueueOnce } from '../lib/core.mjs';
|
|
8
|
+
import { runIsolatedVerification, validateIsolatedVerification } from '../lib/isolated-verification.mjs';
|
|
9
|
+
|
|
10
|
+
const base = await mkdtemp(path.join(tmpdir(), 'loop-isolation-self-test-'));
|
|
11
|
+
const quote = (value) => "'" + String(value).replaceAll("'", "'\"'\"'") + "'";
|
|
12
|
+
const run = async (project, command, inputs, label, runtimeFiles = []) => {
|
|
13
|
+
const evidenceDir = path.join(base, label);
|
|
14
|
+
const result = await runIsolatedVerification({ cwd: project, command, inputs, runtimeFiles,
|
|
15
|
+
evidenceDir, timeoutMs: 15000, runCommand, shellQuote: quote });
|
|
16
|
+
let terminal;
|
|
17
|
+
try { terminal = JSON.parse(await readFile(path.join(evidenceDir, 'terminal.json'), 'utf8')); }
|
|
18
|
+
catch { throw new Error(`missing isolation terminal: ${JSON.stringify(result)}`); }
|
|
19
|
+
return { result, terminal };
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
assert.throws(() => validateIsolatedVerification({ inputs: ['../private'] }), /safe relative/);
|
|
23
|
+
assert.throws(() => validateIsolatedVerification({ inputs: ['input.txt'], runtimeFiles: ['/etc/passwd'] }), /runtimeFiles/);
|
|
24
|
+
|
|
25
|
+
const pythonProject = path.join(base, 'python-project');
|
|
26
|
+
await mkdir(pythonProject);
|
|
27
|
+
await writeFile(path.join(pythonProject, 'input.txt'), 'hello\n');
|
|
28
|
+
await writeFile(path.join(pythonProject, 'hidden.txt'), 'private\n');
|
|
29
|
+
const python = await run(pythonProject, 'cat input.txt > /out/result.txt', ['input.txt'], 'python-pass');
|
|
30
|
+
assert.equal(python.result.exitCode, 0, JSON.stringify(python.terminal) + '\n' +
|
|
31
|
+
(await readFile(path.join(base, 'python-pass', 'stderr.log'), 'utf8')).slice(0, 500));
|
|
32
|
+
assert.equal(python.terminal.status, 'passed');
|
|
33
|
+
assert.equal((await readFile(path.join(base, 'python-pass', 'output', 'result.txt'), 'utf8')).trim(), 'hello');
|
|
34
|
+
|
|
35
|
+
const hidden = await run(pythonProject, 'cat hidden.txt >/dev/null 2>&1 || true', ['input.txt'], 'undeclared');
|
|
36
|
+
assert.equal(hidden.terminal.exit_code, 0);
|
|
37
|
+
assert.equal(hidden.result.exitCode, 1);
|
|
38
|
+
assert.match(hidden.terminal.read_violations.join(' '), /undeclared available project input/);
|
|
39
|
+
|
|
40
|
+
// Root/nested reads must produce the same rejection, even after swallowing errors.
|
|
41
|
+
await mkdir(path.join(pythonProject, 'sub'));
|
|
42
|
+
await writeFile(path.join(pythonProject, 'sub', 'input.txt'), 'nested\n');
|
|
43
|
+
await writeFile(path.join(pythonProject, 'sub', 'hidden.txt'), 'private\n');
|
|
44
|
+
for (const [label, command] of [
|
|
45
|
+
['nested', 'cd sub; cat hidden.txt >/dev/null 2>&1 || true'],
|
|
46
|
+
['parent', 'cd sub; cat ../hidden.txt >/dev/null 2>&1 || true'],
|
|
47
|
+
]) {
|
|
48
|
+
const probe = await run(pythonProject, command, ['input.txt', 'sub/input.txt'], label);
|
|
49
|
+
assert.equal(probe.terminal.exit_code, 0);
|
|
50
|
+
assert.equal(probe.result.exitCode, 1);
|
|
51
|
+
assert.match(probe.terminal.read_violations.join(' '), /undeclared available project input/);
|
|
52
|
+
}
|
|
53
|
+
// Optional probes are allowed only when the source path is genuinely absent.
|
|
54
|
+
for (const [label, command] of [
|
|
55
|
+
['missing-root-parent', 'cat /nonexistent_r4/../work/hidden.txt >/dev/null 2>&1 || true'],
|
|
56
|
+
['missing-root-reentry', 'cat /work/../nonexistent_r4/../work/hidden.txt >/dev/null 2>&1 || true'],
|
|
57
|
+
['missing-alias', 'cat /work/../work/missing.txt >/dev/null 2>&1 || true'],
|
|
58
|
+
['missing-alias-parent', 'cat //work/absent/../hidden.txt >/dev/null 2>&1 || true'],
|
|
59
|
+
['file-alias-parent', 'cat ../work/input.txt/../hidden.txt >/dev/null 2>&1 || true'],
|
|
60
|
+
['missing-parent', 'cat absent/../hidden.txt >/dev/null 2>&1 || true'],
|
|
61
|
+
['nondirectory-parent', 'cat input.txt/../hidden.txt >/dev/null 2>&1 || true'],
|
|
62
|
+
['missing-trailing-slash', 'cat hidden.txt/ >/dev/null 2>&1 || true'],
|
|
63
|
+
['optional-exec', './optional.sh >/dev/null 2>&1 || true'],
|
|
64
|
+
['optional-access', `python3 -c "import os; assert not os.access('absent.txt', os.R_OK)"`],
|
|
65
|
+
['optional-nested', `cd sub; python3 -c "import os; assert not os.access('absent.txt', os.R_OK)"`],
|
|
66
|
+
['optional-unicode', "cat '不存在.txt' >/dev/null 2>&1 || true"],
|
|
67
|
+
['optional-fchdir', `python3 -c "import os; fd=os.open('sub', os.O_RDONLY); os.fchdir(fd); assert not os.access('absent.txt', os.R_OK)"`],
|
|
68
|
+
]) {
|
|
69
|
+
const probe = await run(pythonProject, command, ['input.txt', 'sub/input.txt'], label);
|
|
70
|
+
assert.equal(probe.result.exitCode, 0, label + JSON.stringify(probe.terminal));
|
|
71
|
+
}
|
|
72
|
+
for (const [label, command] of [
|
|
73
|
+
['relative-alias', 'cat ../work/hidden.txt >/dev/null 2>&1 || true'],
|
|
74
|
+
['absolute-alias', 'cat /work/../work/hidden.txt >/dev/null 2>&1 || true'],
|
|
75
|
+
['double-slash', 'cat //work/hidden.txt >/dev/null 2>&1 || true'],
|
|
76
|
+
['multi-reentry', 'cat /work/../../work/sub/../../work/hidden.txt >/dev/null 2>&1 || true'],
|
|
77
|
+
['existing-parent', 'cat sub/../hidden.txt >/dev/null 2>&1 || true'],
|
|
78
|
+
['hidden-access', `python3 -c "import os; os.access('hidden.txt', os.R_OK)"`],
|
|
79
|
+
['hidden-nested-access', `cd sub; python3 -c "import os; os.access('hidden.txt', os.R_OK)"`],
|
|
80
|
+
['hidden-exec', './hidden.txt >/dev/null 2>&1 || true'],
|
|
81
|
+
['hidden-fchdir', `python3 -c "import os; fd=os.open('sub', os.O_RDONLY); os.fchdir(fd); os.access('hidden.txt', os.R_OK)"`],
|
|
82
|
+
]) {
|
|
83
|
+
const probe = await run(pythonProject, command, ['input.txt', 'sub/input.txt'], label);
|
|
84
|
+
assert.equal(probe.terminal.exit_code, 0, label);
|
|
85
|
+
assert.equal(probe.result.exitCode, 1, label);
|
|
86
|
+
assert.match(probe.terminal.read_violations.join(' '), /undeclared available project input/, label);
|
|
87
|
+
}
|
|
88
|
+
for (const [index, name] of ['中文.txt', 'quote".txt', 'back\\slash.txt'].entries()) {
|
|
89
|
+
await writeFile(path.join(pythonProject, name), 'content');
|
|
90
|
+
const allowed = await run(pythonProject, `cat ${quote(name)} >/out/result`, [name], `filename-${index}`);
|
|
91
|
+
assert.equal(allowed.result.exitCode, 0, JSON.stringify(allowed.terminal));
|
|
92
|
+
const denied = await run(pythonProject, `cat ${quote(name)} >/dev/null 2>&1 || true`, ['input.txt'], `filename-hidden-${index}`);
|
|
93
|
+
assert.equal(denied.result.exitCode, 1, JSON.stringify(denied.terminal));
|
|
94
|
+
assert.match(denied.terminal.read_violations.join(' '), /undeclared available project input/);
|
|
95
|
+
}
|
|
96
|
+
const nestedPass = await run(pythonProject, 'cd sub; cat input.txt > /out/result',
|
|
97
|
+
['sub/input.txt'], 'nested-pass');
|
|
98
|
+
assert.equal(nestedPass.result.exitCode, 0, JSON.stringify(nestedPass.terminal));
|
|
99
|
+
await writeFile(path.join(pythonProject, 'test.sh'), '#!/bin/sh\ncat input.txt > /out/executed\n');
|
|
100
|
+
await chmod(path.join(pythonProject, 'test.sh'), 0o755);
|
|
101
|
+
const executable = await run(pythonProject, './test.sh', ['test.sh', 'input.txt'], 'exec-pass');
|
|
102
|
+
assert.equal(executable.result.exitCode, 0, JSON.stringify(executable.terminal));
|
|
103
|
+
await chmod(path.join(pythonProject, 'test.sh'), 0o744);
|
|
104
|
+
const changedMode = await run(pythonProject, './test.sh', ['test.sh', 'input.txt'], 'mode-change');
|
|
105
|
+
assert.equal(changedMode.result.exitCode, 0);
|
|
106
|
+
assert.notEqual(executable.terminal.input_digest, changedMode.terminal.input_digest);
|
|
107
|
+
|
|
108
|
+
const forged = await run(pythonProject, 'printf "fake" > /tmp/trace.log; cat hidden.txt >/dev/null 2>&1 || true',
|
|
109
|
+
['input.txt'], 'forged-trace');
|
|
110
|
+
assert.equal(forged.terminal.exit_code, 0);
|
|
111
|
+
assert.equal(forged.result.exitCode, 1);
|
|
112
|
+
assert.equal(forged.terminal.status, 'failed_or_unknown');
|
|
113
|
+
await symlink(base, path.join(pythonProject, 'outside'));
|
|
114
|
+
const linked = await run(pythonProject, 'true', ['outside/python-project/input.txt'], 'symlink-ancestor');
|
|
115
|
+
assert.equal(linked.result.exitCode, 1);
|
|
116
|
+
assert.match(linked.terminal.error, /symlink input ancestor/);
|
|
117
|
+
|
|
118
|
+
const nodeProject = path.join(base, 'node-project');
|
|
119
|
+
await mkdir(nodeProject);
|
|
120
|
+
await writeFile(path.join(nodeProject, 'input.txt'), 'node\n');
|
|
121
|
+
await writeFile(path.join(nodeProject, 'app.js'), 'require("fs").readFileSync("input.txt");\n');
|
|
122
|
+
const nodeBinary = realpathSync(process.execPath);
|
|
123
|
+
const node = await run(nodeProject, `${nodeBinary} app.js`,
|
|
124
|
+
['input.txt', 'app.js'], 'node-pass', nodeBinary.startsWith('/usr/') ? [] : [nodeBinary]);
|
|
125
|
+
assert.equal(node.result.exitCode, 0);
|
|
126
|
+
assert.equal(node.terminal.status, 'passed');
|
|
127
|
+
assert.equal(node.terminal.read_trace_verified, true);
|
|
128
|
+
|
|
129
|
+
const queueProject = path.join(base, 'queue-project');
|
|
130
|
+
await mkdir(queueProject);
|
|
131
|
+
await writeFile(path.join(queueProject, '.gitignore'), 'runtime/\n');
|
|
132
|
+
await writeFile(path.join(queueProject, 'input.txt'), 'queue\n');
|
|
133
|
+
await writeFile(path.join(queueProject, 'hidden.txt'), 'not declared\n');
|
|
134
|
+
await mkdir(path.join(queueProject, 'sub'));
|
|
135
|
+
await writeFile(path.join(queueProject, 'sub/input.txt'), 'nested\n');
|
|
136
|
+
await writeFile(path.join(queueProject, 'test.sh'), '#!/bin/sh\ncat input.txt > /out/result.txt\n');
|
|
137
|
+
await chmod(path.join(queueProject, 'test.sh'), 0o755);
|
|
138
|
+
const git = (...args) => execFileSync('git', args, { cwd: queueProject, stdio: 'pipe' });
|
|
139
|
+
git('init', '-q');
|
|
140
|
+
git('config', 'user.email', 'loop@example.invalid');
|
|
141
|
+
git('config', 'user.name', 'loop');
|
|
142
|
+
git('add', '-A');
|
|
143
|
+
git('commit', '-qm', 'fixture');
|
|
144
|
+
await enqueueTask(queueProject, { queue: 'isolated', title: 'verify fixture', task: 'Run bounded verification' });
|
|
145
|
+
const queued = await runQueueOnce(queueProject, { queue: 'isolated', dispatcher: 'true',
|
|
146
|
+
worktree: { enabled: true, baseDir: 'runtime/loops/isolated/worktrees', branchPrefix: 'loop/isolated',
|
|
147
|
+
verifyCommands: ['./test.sh'], isolatedVerification: { inputs: ['input.txt', 'test.sh'] } } });
|
|
148
|
+
const queueVerification = queued.run?.verification ?? queued.verification;
|
|
149
|
+
assert.equal(queueVerification?.[0]?.result?.isolation?.status, 'passed', JSON.stringify(queued).slice(0, 2000));
|
|
150
|
+
assert.match(queueVerification[0].result.isolation.inputDigest, /^[0-9a-f]{64}$/);
|
|
151
|
+
await enqueueTask(queueProject, { queue: 'isolated', title: 'reject hidden input', task: 'Prove fail closed' });
|
|
152
|
+
const rejected = await runQueueOnce(queueProject, { queue: 'isolated', dispatcher: 'true',
|
|
153
|
+
worktree: { enabled: true, baseDir: 'runtime/loops/isolated/worktrees', branchPrefix: 'loop/isolated',
|
|
154
|
+
verifyCommands: ['cd sub; cat ../hidden.txt >/dev/null 2>&1 || true'], isolatedVerification: { inputs: ['input.txt', 'sub/input.txt'] } } });
|
|
155
|
+
assert.equal(rejected.status, 'verify_failed');
|
|
156
|
+
assert.equal(rejected.run.verification[0].result.isolation.status, 'failed_or_unknown');
|
|
157
|
+
|
|
158
|
+
// Exercise path semantics through the real disposable queue, not only the parser.
|
|
159
|
+
for (const [label, command, expected] of [
|
|
160
|
+
['bin-missing', 'cat /bin/../work/missing.txt 2>/dev/null || true', 'passed'],
|
|
161
|
+
['bin-hidden', 'cat /bin/../work/hidden.txt 2>/dev/null || true', 'failed_or_unknown'],
|
|
162
|
+
['lib-missing', 'cat /lib/../work/missing.txt 2>/dev/null || true', 'passed'],
|
|
163
|
+
['lib-hidden', 'cat /lib/../work/hidden.txt 2>/dev/null || true', 'failed_or_unknown'],
|
|
164
|
+
['usr-missing', 'cat /usr/../work/missing.txt 2>/dev/null || true', 'passed'],
|
|
165
|
+
['usr-hidden', 'cat /usr/../work/hidden.txt 2>/dev/null || true', 'failed_or_unknown'],
|
|
166
|
+
['etc-missing', 'cat /etc/../work/missing.txt 2>/dev/null || true', 'passed'],
|
|
167
|
+
['etc-hidden', 'cat /etc/../work/hidden.txt 2>/dev/null || true', 'failed_or_unknown'],
|
|
168
|
+
['lib64-missing', 'cat /lib64/../work/missing.txt 2>/dev/null || true', 'passed'],
|
|
169
|
+
['lib64-hidden', 'cat /lib64/../work/hidden.txt 2>/dev/null || true', 'failed_or_unknown'],
|
|
170
|
+
['bin-kind', 'test -d /bin && ! test -L /bin', 'passed'],
|
|
171
|
+
['-proc-self-cwd-hidden.txt', 'cat /proc/self/cwd/hidden.txt 2>/dev/null || true', 'failed_or_unknown'],
|
|
172
|
+
['-proc-self-cwd-missing.txt', 'cat /proc/self/cwd/missing.txt 2>/dev/null || true', 'passed'],
|
|
173
|
+
['-proc-self-cwd-absent-dotdot-hidden.txt', 'cat /proc/self/cwd/absent/../hidden.txt 2>/dev/null || true', 'passed'],
|
|
174
|
+
['-proc-self-cwd-hidden.txt-', 'cat /proc/self/cwd/hidden.txt/ 2>/dev/null || true', 'passed'],
|
|
175
|
+
['-proc-thread-self-cwd-hidden.txt', 'cat /proc/thread-self/cwd/hidden.txt 2>/dev/null || true', 'failed_or_unknown'],
|
|
176
|
+
['-proc-thread-self-cwd-missing.txt', 'cat /proc/thread-self/cwd/missing.txt 2>/dev/null || true', 'passed'],
|
|
177
|
+
['-proc-thread-self-cwd-absent-dotdot-hidden.txt', 'cat /proc/thread-self/cwd/absent/../hidden.txt 2>/dev/null || true', 'passed'],
|
|
178
|
+
['-proc-thread-self-cwd-hidden.txt-', 'cat /proc/thread-self/cwd/hidden.txt/ 2>/dev/null || true', 'passed'],
|
|
179
|
+
['-proc-self-root-work-hidden.txt', 'cat /proc/self/root/work/hidden.txt 2>/dev/null || true', 'failed_or_unknown'],
|
|
180
|
+
['-proc-self-root-work-missing.txt', 'cat /proc/self/root/work/missing.txt 2>/dev/null || true', 'passed'],
|
|
181
|
+
['-proc-self-root-work-absent-dotdot-hidden.txt', 'cat /proc/self/root/work/absent/../hidden.txt 2>/dev/null || true', 'passed'],
|
|
182
|
+
['-proc-self-root-work-hidden.txt-', 'cat /proc/self/root/work/hidden.txt/ 2>/dev/null || true', 'passed'],
|
|
183
|
+
['dynamic-fd', 'cat /dev/fd/9/hidden.txt 2>/dev/null || true', 'failed_or_unknown'],
|
|
184
|
+
['missing-parent', 'cat absent/../hidden.txt 2>/dev/null || true', 'passed'],
|
|
185
|
+
['existing-parent', 'cat sub/../hidden.txt 2>/dev/null || true', 'failed_or_unknown'],
|
|
186
|
+
['relative-alias', 'cat ../work/hidden.txt 2>/dev/null || true', 'failed_or_unknown'],
|
|
187
|
+
['absolute-alias', 'cat /work/../work/hidden.txt 2>/dev/null || true', 'failed_or_unknown'],
|
|
188
|
+
['double-slash', 'cat //work/hidden.txt 2>/dev/null || true', 'failed_or_unknown'],
|
|
189
|
+
['missing-alias', 'cat //work/../work/missing.txt 2>/dev/null || true', 'passed'],
|
|
190
|
+
['file-alias', 'cat ../work/input.txt/../hidden.txt 2>/dev/null || true', 'passed'],
|
|
191
|
+
]) {
|
|
192
|
+
await enqueueTask(queueProject, { queue: 'path-' + label, title: label, task: 'Verify path semantics' });
|
|
193
|
+
const outcome = await runQueueOnce(queueProject, { queue: 'path-' + label, dispatcher: 'true',
|
|
194
|
+
worktree: { enabled: true, baseDir: 'runtime/loops/path-' + label + '/worktrees',
|
|
195
|
+
branchPrefix: 'loop/path-' + label, verifyCommands: [command],
|
|
196
|
+
isolatedVerification: { inputs: ['input.txt', 'sub/input.txt'] } } });
|
|
197
|
+
assert.equal(outcome.run.verification[0]?.result?.isolation?.status, expected, JSON.stringify(outcome));
|
|
198
|
+
if (expected !== 'passed') assert.equal(outcome.status, 'verify_failed');
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
console.log('isolated verification: two projects passed; undeclared input and forged trace rejected');
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
"""Parser and mode-identity regressions independent of sandbox availability."""
|
|
2
|
+
import importlib.util
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
import tempfile
|
|
5
|
+
import sys
|
|
6
|
+
sys.dont_write_bytecode = True
|
|
7
|
+
|
|
8
|
+
spec = importlib.util.spec_from_file_location('isolation', Path(__file__).with_name('isolated-verify.py'))
|
|
9
|
+
module = importlib.util.module_from_spec(spec)
|
|
10
|
+
spec.loader.exec_module(module)
|
|
11
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
12
|
+
root = Path(tmp)
|
|
13
|
+
(root / 'sub').mkdir()
|
|
14
|
+
(root / 'hidden.txt').write_text('hidden')
|
|
15
|
+
(root / 'sub/hidden.txt').write_text('hidden')
|
|
16
|
+
trace = root / 'trace'
|
|
17
|
+
for call in [
|
|
18
|
+
'openat(AT_FDCWD</work>, "hidden.txt", O_RDONLY)',
|
|
19
|
+
'openat(AT_FDCWD</work/sub>, "hidden.txt", O_RDONLY)',
|
|
20
|
+
'openat(AT_FDCWD</work/sub>, "../hidden.txt", O_RDONLY)',
|
|
21
|
+
'openat(3</work/sub>, "hidden.txt", O_RDONLY)',
|
|
22
|
+
'statx(AT_FDCWD</work/sub>, "hidden.txt", 0, STATX_ALL, 0x123)',
|
|
23
|
+
'openat(AT_FDCWD</work/sub>, "hidden.txt", O_RDONLY <unfinished ...>\n1 <... openat resumed>)',
|
|
24
|
+
]:
|
|
25
|
+
trace.write_text('1 ' + call + ' = -1 ENOENT (No such file or directory)\n')
|
|
26
|
+
ok, errors = module.trace_verdict(trace, {}, root, {})
|
|
27
|
+
assert not ok and any('undeclared available project input' in e for e in errors), (call, errors)
|
|
28
|
+
trace.write_text('1 openat(AT_FDCWD</work/sub>, "absent.txt", O_RDONLY) = -1 ENOENT (No such file or directory)\n')
|
|
29
|
+
assert module.trace_verdict(trace, {}, root, {}) == (True, [])
|
|
30
|
+
script = root / 'test.sh'
|
|
31
|
+
script.write_text('#!/bin/sh\ntrue\n')
|
|
32
|
+
script.chmod(0o755)
|
|
33
|
+
before = module.inventory(root, ['test.sh'])
|
|
34
|
+
script.chmod(0o644)
|
|
35
|
+
after = module.inventory(root, ['test.sh'])
|
|
36
|
+
assert before != after
|
|
37
|
+
assert before['test.sh']['sha256'] == after['test.sh']['sha256']
|
|
38
|
+
assert before['test.sh']['mode'] == 0o755
|
|
39
|
+
print('isolation parser and permission identity regressions passed')
|
|
40
|
+
|
|
41
|
+
# Byte escapes, malformed/truncated paths, process cwd inheritance and fchdir.
|
|
42
|
+
assert module.decode_trace_string(r'\344\270\255\346\226\207') == '中文'
|
|
43
|
+
assert module.decode_trace_string(r'a\"b\\c') == 'a"b\\c'
|
|
44
|
+
for value in [r'\q', r'\xZZ', r'\000', r'\777']:
|
|
45
|
+
try:
|
|
46
|
+
module.decode_trace_string(value)
|
|
47
|
+
raise AssertionError(value)
|
|
48
|
+
except ValueError:
|
|
49
|
+
pass
|
|
50
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
51
|
+
root = Path(tmp)
|
|
52
|
+
(root / 'sub').mkdir()
|
|
53
|
+
(root / 'sub/hidden.txt').write_text('secret')
|
|
54
|
+
trace = root / 'trace'
|
|
55
|
+
for transition in ['1 chdir("/work/sub") = 0', '1 fchdir(3</work/sub>) = 0']:
|
|
56
|
+
for filename, expected in [('hidden.txt', False), ('absent.txt', True)]:
|
|
57
|
+
trace.write_text(transition + '\n1 clone(child_stack=NULL, flags=SIGCHLD <unfinished ...>\n'
|
|
58
|
+
+ f'2 access("{filename}", R_OK) = -1 ENOENT (missing)\n'
|
|
59
|
+
+ '1 <... clone resumed>) = 2\n')
|
|
60
|
+
assert module.trace_verdict(trace, {}, root, {})[0] == expected
|
|
61
|
+
for path in ['"hidden"...', r'"\q"']:
|
|
62
|
+
trace.write_text(f'1 access({path}, R_OK) = -1 ENOENT (missing)\n')
|
|
63
|
+
assert not module.trace_verdict(trace, {}, root, {})[0]
|
|
64
|
+
print('C byte escapes, truncated paths and inherited cwd regressions passed')
|
|
65
|
+
|
|
66
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
67
|
+
root = Path(tmp)
|
|
68
|
+
(root / 'hidden.txt').write_text('secret')
|
|
69
|
+
trace = root / 'trace'
|
|
70
|
+
trace.write_text('1 chdir("/work") = 0\n'
|
|
71
|
+
"1 clone(child_stack=NULL, flags=SIGCHLD) = 2 /* 12345 in strace's PID NS */\n"
|
|
72
|
+
'12345 access("hidden.txt", R_OK) = -1 ENOENT (missing)\n')
|
|
73
|
+
assert not module.trace_verdict(trace, {}, root, {})[0]
|
|
74
|
+
trace.write_text('1 openat(4, "absent.txt", O_RDONLY) = -1 ENOENT (missing)\n')
|
|
75
|
+
assert not module.trace_verdict(trace, {}, root, {})[0]
|
|
76
|
+
print('PID namespace mapping and unknown directory fd regressions passed')
|
|
77
|
+
|
|
78
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
79
|
+
root = Path(tmp)
|
|
80
|
+
(root / 'sub').mkdir()
|
|
81
|
+
(root / 'hidden.txt').write_text('private')
|
|
82
|
+
(root / 'input.txt').write_text('declared')
|
|
83
|
+
trace = root / 'trace'
|
|
84
|
+
for name, allowed in [
|
|
85
|
+
('../work/hidden.txt', False), ('/work/../work/hidden.txt', False),
|
|
86
|
+
('//work/hidden.txt', False), ('///work//hidden.txt', False),
|
|
87
|
+
('/work/../../work/sub/../../work/hidden.txt', False),
|
|
88
|
+
('../work/missing.txt', True), ('//work/absent/../hidden.txt', True),
|
|
89
|
+
('../work/input.txt/../hidden.txt', True),
|
|
90
|
+
('/work/../work/hidden.txt/', True), ('/work/../work/hidden.txt/.', True),
|
|
91
|
+
('/nonexistent_r4/../work/hidden.txt', True),
|
|
92
|
+
('/work/../nonexistent_r4/../work/hidden.txt', True),
|
|
93
|
+
('/home/../work/hidden.txt', False),
|
|
94
|
+
]:
|
|
95
|
+
trace.write_text(f'1 openat(AT_FDCWD</work>, "{name}", O_RDONLY) = -1 ENOENT (missing)\n')
|
|
96
|
+
ok, errors = module.trace_verdict(trace, {'input.txt': {}}, root, {})
|
|
97
|
+
assert ok == allowed, (name, errors)
|
|
98
|
+
print('namespace re-entry, slash aliases and missing component regressions passed')
|
|
99
|
+
|
|
100
|
+
# Explicit ro-bind roots differ from internal system symlinks.
|
|
101
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
102
|
+
root = Path(tmp)
|
|
103
|
+
(root / 'hidden.txt').write_text('private')
|
|
104
|
+
for mount in ['bin', 'lib', 'lib64', 'usr', 'etc']:
|
|
105
|
+
if not Path('/' + mount).is_dir():
|
|
106
|
+
continue
|
|
107
|
+
assert not module.available_project_attempt('/' + mount + '/../work/missing.txt', root, {})
|
|
108
|
+
assert module.available_project_attempt('/' + mount + '/../work/hidden.txt', root, {})
|
|
109
|
+
# Use a mocked descendant type: no changes to the host system directories.
|
|
110
|
+
from unittest.mock import patch
|
|
111
|
+
original = Path.is_symlink
|
|
112
|
+
with patch.object(Path, 'is_symlink', lambda p: str(p) == '/usr/internal-link' or original(p)):
|
|
113
|
+
try:
|
|
114
|
+
module.available_project_attempt('/usr/internal-link/../../work/hidden.txt', root, {})
|
|
115
|
+
raise AssertionError('unknown internal symlink must fail closed')
|
|
116
|
+
except ValueError as error:
|
|
117
|
+
assert 'unresolved system symlink' in str(error)
|
|
118
|
+
print('ro-bind roots and unknown descendant symlinks passed')
|
|
119
|
+
|
|
120
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
121
|
+
root = Path(tmp)
|
|
122
|
+
(root / 'hidden.txt').write_text('hidden')
|
|
123
|
+
(root / 'sub').mkdir()
|
|
124
|
+
trace = root / 'trace'
|
|
125
|
+
for prefix in ['/proc/self/cwd/', '/proc/thread-self/cwd/', '/proc/self/root/work/']:
|
|
126
|
+
for suffix, allowed in [('hidden.txt', False), ('missing.txt', True), ('absent/../hidden.txt', True), ('hidden.txt/', True)]:
|
|
127
|
+
trace.write_text('1 chdir("/work") = 0\n' + f'1 access("{prefix}{suffix}", R_OK) = -1 ENOENT (missing)\n')
|
|
128
|
+
ok, errors = module.trace_verdict(trace, {}, root, {})
|
|
129
|
+
assert ok == allowed, (prefix, suffix, errors)
|
|
130
|
+
for name in ['/dev/fd/9/hidden.txt', '/proc/self/fd/9/hidden.txt', '/proc/9/cwd/hidden.txt']:
|
|
131
|
+
trace.write_text(f'1 access("{name}", R_OK) = -1 ENOENT (missing)\n')
|
|
132
|
+
assert not module.trace_verdict(trace, {}, root, {})[0]
|
|
133
|
+
print('self cwd/root aliases and unknown fd/process alias regressions passed')
|