Skip to content

Within a policy document, the most cautious matching rule wins - #113

Open
samuelcole wants to merge 4 commits into
mainfrom
sam/hbr-161-cautious-rule-wins
Open

samuelcole wants to merge 4 commits into
mainfrom
sam/hbr-161-cautious-rule-wins

Conversation

@samuelcole

Copy link
Copy Markdown
Collaborator

Fixes HBR-161

Problem

Within one policy document, Harbor's policy engine took the first rule that matched. The store tried the short-form arrays first (deny, ask, preview, allow), then the typed rules in the order written, so:

  • allow: ["Tool(*)"] beat a tool.call deny written in rules;
  • an earlier typed allow beat a later typed deny.

Only an organization's managed deny (Tier 1) was guaranteed to win. The permissions guide used to promise that a deny always wins; the maintainer decided that's the rule Harbor should follow.

Change

  • extension/src/policy/engine.ts. Tiers 6 and 7 now consider every rule in a source that matches the request, and take the most cautious effect: deny > preview > ask > allow. Ties keep the first rule in compiled order, so a decision's rule id is stable. Across sources nothing changes: the managed policy decides if any of its rules match, otherwise the user's, and origin grants stay at Tier 8. The decision still records the winning rule's own effect, so a page's origin grant still answers an allow rule that Tier 2 raised to a preview.
  • Docs. docs/PERMISSIONS.md's ladder, Tier 6/7 text and precedence paragraph say so. A narrow allow can no longer carve an exception out of a broad deny; that's the accepted trade.
  • Removed. findFirstMatchingRule in rules.ts, which described the old order and was used only by its own tests, and 'originGrant' as a rule source, which no rule was ever compiled with. Origin grants are resolved live at Tier 8.

Testing

  • New engine tests, committed red first (515cfa6, 3 of 5 failing before the fix):
    • a short-form allow against a typed deny → deny;
    • an earlier typed allow against a later typed deny → deny;
    • an allow and a preview both matching → preview;
    • ties keep the first rule's id;
    • a managed allow over a user deny, and a managed ask over a user allow (cross-source order unchanged).
  • npx vitest run in extension/: 144 files, 1611 tests pass.
  • npm run build and npm run build:chrome succeed. tsc --noEmit has the same errors as the base.
  • Firefox browser tests (tests/e2e): 21/21 pass.
  • An independent review of the diff found no problems; its one coverage note (the managed-ask case) is added.

Also fixed

  • policy/store.ts's header said origin grants were compiled into rules by an appendOriginGrantRules function that doesn't exist. It now describes what the code does.

What's left

  • Nothing for this ticket. Related open decisions are HBR-162 and HBR-164.

This PR was opened by an AI agent (Claude Code) working for the maintainer.

🤖 Generated with Claude Code

samuelcole and others added 4 commits November 23, 2026 13:49
Today compileDocument orders a document's rules deny -> ask -> preview
-> allow -> typed `rules`, and the engine's Tier 6/7 lookup takes the
first match across that whole ordering. That lets a short-form allow
array entry beat a typed deny rule, and an earlier typed allow beat a
later typed deny, instead of the more cautious rule winning.

Pins the cross-source precedence (managed decides before user, when it
has a matching rule) and the tie-break order (first rule wins on an
effect tie) that the implementation must preserve.
Co-Authored-By: Claude Sonnet 5 (Claude Code) <noreply@anthropic.com>
Tier 6/7 used to take the first matching rule across a document's
compiled rules — short-form deny/ask/preview/allow arrays, then typed
`rules`, in that written order — so a short-form allow could beat a
typed deny, and an earlier typed allow could beat a later typed deny.

The engine now looks at every rule that matches, per source (managed,
then user), and takes the most cautious effect: deny > preview > ask >
allow. The first source with any matching rule still decides, exactly
as before; only the within-document lookup changed. Ties between rules
of equal effect keep the first in the existing compiled order, so a
rule's id stays deterministic. Tiers 0-4, origin-grant semantics
(Tier 8), and the forced-minimum logic (Tier 2) are untouched; the
winning rule's own (pre-forced-minimum) effect is still what
`decision.rule.effect` reports, so upstream-consent's
`decision.rule.effect === 'allow'` check still sees an allow rule that
Tier 2 raised to a preview.

Updates docs/PERMISSIONS.md's precedence paragraph and the tier-ladder
description, and corrects two stale comments in store.ts about an
`appendOriginGrantRules` helper that was never implemented — origin
grants are resolved separately, at Tier 8, not compiled into
Policy.rules().
Co-Authored-By: Claude Sonnet 5 (Claude Code) <noreply@anthropic.com>
…ule source

findFirstMatchingRule described the old first-match order and only its
own tests called it. No rule is ever compiled with source 'originGrant'
(origin grants are resolved live at Tier 8), so the rule and compiler
types no longer offer it.

Co-Authored-By: Claude Opus 5.5 (Claude Code) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 (Claude Code) <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

1 participant