Skip to content
Merged
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
13 changes: 13 additions & 0 deletions tests/test_hardening.py
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,19 @@ def test_default_policy_false_positives_fixed(self):
for tool, ti in bad:
self.assertEqual(policy.evaluate(pol, tool, ti, "/p")["decision"], "deny", ti)

def test_a_windows_path_is_read_with_its_backslashes_as_separators(self):
# a Cursor StrReplace of C:\w\.env was allowed on Windows: TK-D005 is written with `/`
pol = policy.load()[0]
for tool, ti in (("Edit", {"file_path": r"C:\w\.env"}), ("Write", {"file_path": r"C:\w\.env.production"}),
("Write", {"file_path": r"C:\Users\u\.ssh\authorized_keys"}),
("Write", {"file_path": r"C:\w\.aws\config"}),
("Edit", {"file_path": r"C:\w\app.py", "paths": [r"C:\w\app.py", r"C:\w\.env"]})):
with self.subTest(ti=ti):
d = policy.evaluate(pol, tool, ti, r"C:\w")
self.assertEqual((d["decision"], d["rule_ids"][:1]), ("deny", ["TK-D005"]))
for ti in ({"file_path": r"C:\w\app.py"}, {"file_path": r"C:\w\.env.example"}):
self.assertNotEqual(policy.evaluate(pol, "Edit", ti, r"C:\w")["decision"], "deny", ti)


class BoundedFeed(unittest.TestCase):
def test_feed_drops_oldest_and_keeps_absolute_positions(self):
Expand Down
9 changes: 6 additions & 3 deletions tracekit/policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,21 +254,24 @@ def evaluate(pol, tool, ti, cwd=None):
commands = ti.get("commands") if tool == "Bash" and "command" not in ti else None
if isinstance(commands, list) and all(isinstance(command, str) for command in commands):
calls.extend(dict(ti, command=command) for command in commands)
# a Windows path's separator is a backslash: the file rules, written with `/`, read it with `/` too
ruled = calls + [dict(c, **{k: c[k].replace("\\", "/")}) for c in calls for k in ("file_path", "notebook_path")
if isinstance(c.get(k), str) and "\\" in c[k]]
# Claude Code's Monitor runs its command in the shell Bash uses, and PowerShell is its shell on Windows: a rule
# applies under either name, so one written for Bash and one for the tool itself both match
names = [tool] + (["Bash"] if tool in ("Monitor", "PowerShell") else [])
rule_ids, reasons, flags = [], [], []
deny = ask = False
for r in pol.get("deny", []):
if any(_matches(r, t, c) for c in calls for t in names):
if any(_matches(r, t, c) for c in ruled for t in names):
deny = True
rule_ids.append(r["id"]); reasons.append(r.get("reason", r["pattern"]))
for r in pol.get("ask", []):
if any(ASKABLE.match(t or "") and _matches(r, t, c) for c in calls for t in names):
if any(ASKABLE.match(t or "") and _matches(r, t, c) for c in ruled for t in names):
ask = True
rule_ids.append(r["id"]); reasons.append(r.get("reason", r["pattern"]))
for r in pol.get("flag", []):
if any(_matches(r, t, c) for c in calls for t in names):
if any(_matches(r, t, c) for c in ruled for t in names):
if r["id"] not in rule_ids:
rule_ids.append(r["id"])
flags.append(r.get("label", "flag"))
Expand Down
Loading