Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions LifeOS/install/settings.permissions.test.ts
Original file line number Diff line number Diff line change
@@ -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 <<EOF\nprint(1)\nEOF`)).toBe(true);
});

test("a piped program on stdin is denied like -c", () => {
expect(denied(`echo "open('f','w').write('x')" | python3`)).toBe(true);
expect(denied(`cat script.py | python`)).toBe(true);
});
});
6 changes: 6 additions & 0 deletions LifeOS/install/settings.system.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 *)",
Expand Down
6 changes: 6 additions & 0 deletions LifeOS/install/skills/LifeOS/install/settings.system.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 *)",
Expand Down