Skip to content

Commit c5279a1

Browse files
author
Peter Steinberger
committed
fix: preserve fail-closed validation process and command guards
1 parent d9bec4f commit c5279a1

4 files changed

Lines changed: 25 additions & 7 deletions

File tree

‎src/repair/process-tree-containment.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,7 @@ def reap_exited_children(primary_pid, background_pids):
710710
background_pids.add(pid)
711711
712712
713-
def reap_adopted_children(primary_pid, background_pids):
713+
def reap_adopted_children(primary_pid, background_pids, primary_process=None):
714714
for pid, parent_pid in process_rows():
715715
if parent_pid != os.getpid() or pid == primary_pid:
716716
continue
@@ -721,6 +721,10 @@ def reap_adopted_children(primary_pid, background_pids):
721721
continue
722722
if reaped_pid == 0:
723723
background_pids.add(pid)
724+
elif primary_process is not None and primary_process.poll() is not None:
725+
# The helper survived the primary command even if both exited
726+
# between supervisor polls; preserve that fail-closed evidence.
727+
background_pids.add(pid)
724728
else:
725729
# Short-lived detached package-manager helpers are already gone while
726730
# the primary command is still running; they were not left behind.
@@ -794,7 +798,7 @@ def main():
794798
child = subprocess.Popen(command, close_fds=True)
795799
background_pids = set()
796800
while True:
797-
reap_adopted_children(child.pid, background_pids)
801+
reap_adopted_children(child.pid, background_pids, child)
798802
return_code = child.poll()
799803
if return_code is not None:
800804
break

‎src/repair/target-validation.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1689,8 +1689,8 @@ export function requiredValidationCommands(
16891689

16901690
function isMutatingFormatterValidationHint(command: LooseRecord): boolean {
16911691
if (typeof command !== "string") return false;
1692-
return /^(?:pnpm(?:\s+run)?|npm\s+run|bun\s+run)\s+format(?:\s+(?!-)[A-Za-z0-9@_./-]+)+$/.test(
1693-
command.trim(),
1692+
return /^(?:pnpm(?: +run)?|npm +run|bun +run) +format(?: +(?!-)[A-Za-z0-9@_./-]+)+$/.test(
1693+
command.replace(/^ +| +$/g, ""),
16941694
);
16951695
}
16961696

‎test/repair/process-tree-containment.test.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ test("Linux validation containment uses an externally owned PID namespace and su
1919
assert.match(containment, /if pid != primary_pid:\s+background_pids\.add\(pid\)/);
2020
assert.match(containment, /if pid != os\.getpid\(\)/);
2121
assert.match(containment, /background_pids\.update\(pid for pid in remaining_pids/);
22-
assert.match(containment, /reap_adopted_children\(child\.pid, background_pids\)/);
22+
assert.match(containment, /reap_adopted_children\(child\.pid, background_pids, child\)/);
2323
assert.match(containment, /return_code = child\.poll\(\)/);
2424
assert.match(containment, /except ChildProcessError/);
2525
assert.match(containment, /struct\.pack\("=Qi", allowed_access, path_fd\)/);
@@ -123,6 +123,10 @@ test("containment does not classify reaped transient helpers as surviving backgr
123123
tracked: [],
124124
status: "ok",
125125
});
126+
assert.deepEqual(runLandlockScenario("adopted_child_primary_exited"), {
127+
tracked: [25],
128+
status: "ok",
129+
});
126130
});
127131

128132
test("Landlock capability probe selects fallback only for unsupported syscalls", () => {
@@ -301,7 +305,10 @@ if scenario.startswith("adopted_child_"):
301305
return (0, 0) if scenario == "adopted_child_alive" else (25, 0)
302306
module.os.waitpid = fake_waitpid
303307
tracked = {25}
304-
module.reap_adopted_children(20, tracked)
308+
class PrimaryProcess:
309+
def poll(self):
310+
return 0 if scenario == "adopted_child_primary_exited" else None
311+
module.reap_adopted_children(20, tracked, PrimaryProcess())
305312
print(json.dumps({"tracked": sorted(tracked), "status": "ok"}, separators=(",", ":")))
306313
raise SystemExit(0)
307314

‎test/repair/target-validation.test.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,14 @@ test("formatter-hint filtering does not hide shell injection or write flags", ()
102102
const cwd = packageFixture({ "check:changed": "node check.js", format: "node format.js" });
103103
const options = validationOptions("openclaw/openclaw");
104104

105-
for (const command of ["pnpm format --write", "pnpm format src/index.ts; touch escaped"]) {
105+
for (const command of [
106+
"pnpm format --write",
107+
"pnpm format src/index.ts; touch escaped",
108+
"pnpm format src/index.ts\ntouch escaped",
109+
"pnpm format src/index.ts\r\ntouch escaped",
110+
"pnpm format src/index.ts\r touch escaped",
111+
"pnpm format src/index.ts\t touch escaped",
112+
]) {
106113
assert.throws(
107114
() =>
108115
preflightTargetValidationPlan(

0 commit comments

Comments
 (0)