diff --git a/LifeOS/install/settings.permissions.test.ts b/LifeOS/install/settings.permissions.test.ts new file mode 100644 index 0000000000..47d0354c6d --- /dev/null +++ b/LifeOS/install/settings.permissions.test.ts @@ -0,0 +1,67 @@ +/** + * Test for shipped Python deny rules in settings.system.json. + * Validates that inline Python via -c, stdin dash, heredoc, and pipeline are denied, + * while executing existing script files remains permitted. + * + * Issue #2797 + */ +import { describe, expect, test } from "bun:test"; +import { readFileSync } from "node:fs"; +import { join } from "node:path"; + +describe("Python deny rules in settings.system.json", () => { + const settingsPath = join(import.meta.dir, "settings.system.json"); + const settings = JSON.parse(readFileSync(settingsPath, "utf8")); + const deny: string[] = settings.permissions?.deny ?? []; + const globs = deny + .filter((r) => r.startsWith("Bash(") && /python/.test(r)) + .map((r) => r.slice(5, -1)); + + const deniedOne = (cmd: string) => + globs.some((g) => + new RegExp( + "^" + + g + .split("*") + .map((s) => s.replace(/[.+?^${}()|[\]\\]/g, "\\$&")) + .join(".*") + + "$", + "s" + ).test(cmd) + ); + + const denied = (cmd: string) => + cmd.split(/\s*(?:\|\||&&|\||;)\s*/).some((part) => deniedOne(part.trim())); + + test("shipped python deny rules are present", () => { + expect(globs.length).toBeGreaterThan(0); + }); + + test("control: inline -c is denied", () => { + expect(denied(`python3 -c "open('f','w').write('x')"`)).toBe(true); + expect(denied(`python -c "print(1)"`)).toBe(true); + }); + + test("control: stdin dash is denied", () => { + expect(denied(`python3 - < script.py`)).toBe(true); + expect(denied(`python -`)).toBe(true); + expect(denied(`python3 -`)).toBe(true); + }); + + test("control: running an existing script file stays allowed", () => { + expect(denied("python3 tool.py --input data.csv")).toBe(false); + expect(denied("python script.py")).toBe(false); + expect(denied("python3 -u tool.py")).toBe(false); + }); + + test("a heredoc program on stdin is denied like -c", () => { + expect(denied(`python3 <<'PY'\nopen('f','w').write('x')\nPY`)).toBe(true); + expect(denied(`python <<'PY'\nprint(1)\nPY`)).toBe(true); + expect(denied(`python3 < { + expect(denied(`echo "open('f','w').write('x')" | python3`)).toBe(true); + expect(denied(`cat script.py | python`)).toBe(true); + }); +}); diff --git a/LifeOS/install/settings.system.json b/LifeOS/install/settings.system.json index 03f328e6d1..b4164f322f 100644 --- a/LifeOS/install/settings.system.json +++ b/LifeOS/install/settings.system.json @@ -241,6 +241,12 @@ "Bash(python3 -c *)", "Bash(python - *)", "Bash(python3 - *)", + "Bash(python -)", + "Bash(python3 -)", + "Bash(python <<*)", + "Bash(python3 <<*)", + "Bash(python)", + "Bash(python3)", "Bash(perl -e *)", "Bash(perl -pe *)", "Bash(perl -ne *)", diff --git a/LifeOS/install/skills/LifeOS/install/settings.system.json b/LifeOS/install/skills/LifeOS/install/settings.system.json index 03f328e6d1..b4164f322f 100644 --- a/LifeOS/install/skills/LifeOS/install/settings.system.json +++ b/LifeOS/install/skills/LifeOS/install/settings.system.json @@ -241,6 +241,12 @@ "Bash(python3 -c *)", "Bash(python - *)", "Bash(python3 - *)", + "Bash(python -)", + "Bash(python3 -)", + "Bash(python <<*)", + "Bash(python3 <<*)", + "Bash(python)", + "Bash(python3)", "Bash(perl -e *)", "Bash(perl -pe *)", "Bash(perl -ne *)",