Skip to content

fix: make policy add place rules where they take effect - #20

Merged
Asdivyansh merged 1 commit into
SilosLabs:mainfrom
TheDreamSaver:fix/policy-add-rule-placement
Nov 16, 2026
Merged

Asdivyansh merged 1 commit into
SilosLabs:mainfrom
TheDreamSaver:fix/policy-add-rule-placement

Conversation

@TheDreamSaver

Copy link
Copy Markdown
Member

Fixes #19

Problem

Policy rules are first-match, but silo policy add appended the new rule. When an earlier rule with the opposite action matched the same host, the new rule was never reached. silo policy add evil.npmjs.org --action deny printed "Added rule → deny", but the host stayed allowed, because allow *.npmjs.org came first.

Change

  • placeRule() (src/policy/engine.ts): returns where a rule must go to take effect. That's just ahead of the first earlier rule that matches the new host with the opposite action, or the end of the list if none does. The new host is tested as a literal against existing patterns, so *.evil.npmjs.org also lands ahead of *.npmjs.org. Matching is case-insensitive, like evaluation.
  • silo policy add inserts the rule at that index and tells the user when it had to move ahead of a rule: Placed before "*.npmjs.org" (allow)…. Deny now prints in red.
  • Bootstrap is visible: with no policy file, the command still starts from the standard preset, but now says so, with a pointer to silo policy show.
  • Negated patterns are rejected (!evil.com, exit 1). In picomatch, ! negates, so the rule would match every host except the one named.
  • Docs: docs/policy-engine.md and docs/cli-reference.md now describe the placement. The bootstrap description also now matches the code: it's always the standard preset, not the onboard mode.

Rules already in a policy file are left alone. Only the position of newly added rules changes.

Before and after

The same commands in a throwaway HOME with a fresh config (rule order from ~/.silo/policy.yaml; verdicts from evaluate()):

main this branch
policy add evil.npmjs.org --action deny "Added rule → deny"; rule appended last rule placed before *.npmjs.org, with a message saying so
evil.npmjs.org allow deny
cdn.npmjs.org / registry.npmjs.org allow / allow allow / allow
policy add '!evil.com' accepted as an allow rule, exit 0 rejected, exit 1
bootstrap from standard silent announced (10 allow rules)

Tests

7 new cases for placeRule in tests/policy/engine.test.ts:

  • a deny goes ahead of a shadowing allow wildcard, and the host then evaluates to deny while siblings stay allowed;
  • a pinned case showing that appending left the host allowed;
  • a wildcard deny goes ahead of a broader allow;
  • no conflict means append;
  • a same-action match means append;
  • case-insensitive matching;
  • an allow goes ahead of a blocking deny under default: allow.

npm run typecheck, npm test (352 passing) and npm run build are green. The branch merges cleanly with #13.

Not changed here: parsePolicyFile still accepts ! patterns in hand-written policy files. Rejecting them in the schema would make an existing file fail to load, so I've left that decision to you.

Rules are first-match, but policy add appended the new rule, so an earlier
rule with the opposite action (allow *.npmjs.org) shadowed it and a deny for
evil.npmjs.org was never reached, while the CLI reported success.

- Add placeRule(), which returns the index just ahead of the first earlier
  rule that matches the new host with the opposite action (else the end).
- policy add inserts there and says which rule it was placed before.
- Say so when policy add bootstraps a policy file from the standard preset.
- Reject negated patterns (!host): they match every other host.
- Update policy-engine.md and cli-reference.md to describe the behaviour.

Fixes SilosLabs#19
@Asdivyansh

Copy link
Copy Markdown
Member

Critical, thanks for the fix.

@Asdivyansh
Asdivyansh merged commit 1c87c61 into SilosLabs:main Nov 16, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

silo policy add: new rule can be shadowed by an earlier rule and never apply

2 participants