From 50c607395585ba6ed4c4c28f9709893b7e877f79 Mon Sep 17 00:00:00 2001 From: isihussain <35667459+isihussain@users.noreply.github.com> Date: Sat, 14 Nov 2026 11:48:27 +0300 Subject: [PATCH] fix(settings): deny inline Python fed via heredoc and stdin pipelines (#2797) The native deny rules in settings.system.json previously covered program strings passed on the command line ('python -c *', 'python3 -c *') and stdin flags with trailing arguments ('python - *', 'python3 - *'), but missed stdin programs fed via heredoc (e.g. python3 <<'PY' ... PY) or via pipes (e.g. ... | python3), as well as bare interpreter invocations ('python -', 'python3 -'). Add deny rules for: - 'Bash(python -)' and 'Bash(python3 -)' - 'Bash(python <<*)' and 'Bash(python3 <<*)' - 'Bash(python)' and 'Bash(python3)' Keep script execution ('python3 tool.py --input data.csv') allowed. Add settings.permissions.test.ts verifying rejection of -c, -, heredocs, and pipeline stdin while preserving script execution. Closes #2797 Signed-off-by: isihussain --- LifeOS/install/settings.permissions.test.ts | 67 +++++++++++++++++++ LifeOS/install/settings.system.json | 6 ++ .../LifeOS/install/settings.system.json | 6 ++ 3 files changed, 79 insertions(+) create mode 100644 LifeOS/install/settings.permissions.test.ts 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 *)",