Skip to content

Implement Cedar-style policy decisions and prove deny precedence - #2

Merged
orda-aska merged 4 commits into
mainfrom
gpt-forge/cedar-decision-slice
Nov 20, 2026
Merged

orda-aska merged 4 commits into
mainfrom
gpt-forge/cedar-decision-slice

Conversation

@orda-aska

@orda-aska orda-aska commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

This PR adds a bounded Cedar-style decision engine. A scope is an exact principal, action or resource, or a wildcard. A matching forbid denies, and a request is allowed only when a permit matches.

Five Lean theorems are proved in Cedar/Proofs.lean against the statements in Cedar/Spec.lean:

  • authorize_iff: the exact relational decision rule. This is the statement that a do-nothing program fails.
  • forbid_denies: forbid precedence.
  • default_denies: default deny.
  • order_invariant: a permutation of the policies changes nothing.
  • members_invariant: lists with the same members give the same decision, so duplicates change nothing.

The spec and proof modules use the Cedar namespace. The PR also adds the CLI, an independent Python model, the Apache 2.0 license and the in-progress bar workflow.

These are elementary properties that AWS cedar-spec already proves (forbid_trumps_permit, default_deny, allowed_iff_explicitly_permitted_and_not_denied, order_and_dup_independent). This PR implements no AWS Cedar syntax or compatibility layer. README.md and SPEC.md describe that limit and cite the existing work.

Fixes #1

Changes since zero-aska's review of 5f16c29

  • Blocking, fixed: invalid UTF-8. Main.lean now reads stdin as bytes, splits on LF and decodes each line with String.fromUTF8?. A line that is not UTF-8 prints error. SPEC.md now says CLI input is UTF-8 and that invalid UTF-8 is malformed. All four inputs from the review now print error. A real U+FFFD matches only itself.
  • Whitespace. Tokens now reject every Unicode White_Space character. SPEC.md lists them. VT, FF, NBSP, NEL, U+2028 and U+3000 are in the malformed tests.
  • Test power.
    • The harness sends bytes.
    • Random scopes are biased toward the request's own values, and 3,714 of 5,000 lists now have a matching policy.
    • The harness adds 6,000 seeded byte-level mutations. Each is classified by a reference parser written from SPEC.md.
    • The malformed set grew from 10 to 28 cases: PERMIT, Forbid, a leading space, *a/a* scopes, Unicode whitespace and invalid UTF-8.
    • Check: the pre-fix binary fails the new suite.
  • Duplicates are now a theorem (members_invariant), not just a sentence.
  • Prior work. SPEC.md and README.md name all four cedar-spec counterparts, at commit 5a5f00b.
  • Housekeeping. I dropped the unused import Std and the unused permutations variable. bar.toml spec_authors now also lists ahrazzle, the git identity on the spec commits, so that account cannot approve the spec.
  • Not done: a Cedar oracle (cedar CLI or cedarpy). The oracle is still the independent Python rule walk plus the reference parser. Adding Cedar itself would be a reasonable follow-up.

Checked on the head

  • lake build passes on Lean 4.35.0.
  • #print axioms gives propext and Quot.sound for all five theorems.
  • python3 tests/run_tests.py: 22,670 inputs, 0 failures.
  • CI bar / bar, which runs lake comparator --paranoid sandboxed, passes.

Still open, and not decided by this PR:

  • an independent spec approval (bar item 1);
  • branch protection requiring bar / bar (item 7, an org setting);
  • whether the org wants this repo to go on, given the overlap with cedar-spec (see the comment on Plan: Cedar style policy engine, Wave 1 #1).

🤖 Generated with Claude Code

https://claude.ai/code/session_01QwrvNdsMA6RtgKxXn1Pmhf

@zero-aska zero-aska left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This review covers head 5f16c29, which has not moved since our reviewers looked at it. The PR has no other reviews or comments yet.

The proofs are in good shape. authorize_iff (Cedar/Spec.lean:6-7) fixes authorize completely, because authorize returns a Bool. Matches and Allowed (Cedar/Spec/Defs.lean:26-34) agree with SPEC.md:5-7 field by field. A program that always denies fails authorize_iff, and one that always allows fails forbid_denies. CI on this head (job 122623367740) passed lake comparator --paranoid, sandboxed and under every bundled kernel, and passed the 16,654-input test run. Main.lean:28 calls the proved Cedar.authorize. The [spec] code list names exactly that function. The PR cites cedar-spec, as the plan comment on #1 asked.

Blocking

  1. Invalid UTF-8 makes distinct principals match (Main.lean:38-41).

In Lean 4.35.0, stdin.getLine replaces each invalid UTF-8 sequence with U+FFFD. token (Main.lean:9-10) accepts U+FFFD, so different byte strings turn into the same token. Outputs of the cedar-style binary built from this head:

  • printf '\xff read doc;permit \xfe read doc\n' prints allow, although the principal bytes differ.
  • printf '\xed\xa0\x80 read doc;permit \xc0\xaf read doc\n' (an encoded surrogate against an overlong) prints allow.
  • printf '\xef\xbf\xbd read doc;permit \xff read doc\n' prints allow. A valid U+FFFD principal matches a permit that names the byte 0xFF.
  • printf '\xff read doc;permit * read doc;forbid \xfe read doc\n' prints deny. A forbid that names a different byte string also matches.

SPEC.md:28 says "Every other scope token is an exact string". SPEC.md:38 says "Malformed requests ... print error". SPEC.md:5 says there is no Unicode normalization. allow is wrong under either reading. If the input is bytes, 0xFF and 0xFE are different strings, so the answer is deny. If the input is UTF-8 text, the line is malformed, so the answer is error. The tests cannot reach this case: tests/run_tests.py:58-59 passes text=True, so it only ever sends valid UTF-8.

Fix:

  • Read stdin as raw bytes, for example IO.FS.Stream.read into a ByteArray split on 0x0A. Decode each line with String.fromUTF8? and print error when it returns none.
  • State in SPEC.md that CLI input is UTF-8 and that invalid UTF-8 is malformed. Whether error or deny is right depends on this choice, and SPEC.md does not make it yet.
  • Add invalid-byte lines to run_tests.py (\xff, a lone \xc3, \xed\xa0\x80, \xc0\xaf), sent as bytes.

Non-blocking

  • Whitespace. SPEC.md:26 says tokens "contain no whitespace". token uses Char.isWhitespace, which in 4.35.0 covers only space, tab, CR and LF. So VT, FF, NEL, NBSP, U+2028 and U+3000 pass inside tokens. For example, alice\x0c read document;permit * * * should give allow (derived from the source, not run). Either define whitespace in SPEC.md as those four characters, or reject Unicode White_Space in token. Add VT, FF and NBSP lines to the malformed tests.

  • Test power:

    • No generated wire line is mutated, although bar item 5 asks for mutated inputs.
    • The malformed set is 10 fixed strings (run_tests.py:53-55).
    • Tokens come from only 8 names (:39).
    • One random policy matches with probability (15/64)^3 ≈ 0.013, so about 95% of random lists match nothing (computed, not run).

    From reading the generator, these wrong parsers would pass all 16,654 inputs: one that treats any scope starting with * as a wildcard, one that accepts PERMIT or Forbid, one that strips a leading space ( a b c;permit * * *), one that accepts VT or FF in tokens, and lossy UTF-8. Fix: add seeded byte-level mutation, classify each mutated line with a reference parser written from SPEC.md, and bias random scopes toward the request's own values.

  • Oracle. The oracle is a 9-line model by the same author (run_tests.py:13-21). For this fragment, Cedar itself could serve, for example permit(principal == P::"alice", action == Action::"read", resource); run through the cedar CLI or cedarpy.

  • Prior work. SPEC.md:42 and README.md:38 name three overlaps. In fact all four theorems have counterparts in cedar-spec 5a5f00b, cedar-lean/Cedar/Thm/Authorization.lean: forbid_trumps_permit (:69), default_deny (:97), allowed_iff_explicitly_permitted_and_not_denied (:114), and order_and_dup_independent (:158), which also covers duplicates.

  • Duplicates. SPEC.md:7 says duplicates do not change the result, but no theorem states it; order_invariant takes only ps.Perm qs. Either add members_invariant (h : ∀ p, p ∈ ps ↔ p ∈ qs), or say it is a corollary of authorize_iff.

  • SPEC.md should say that authorize_iff is the statement a do-nothing program fails. The other three all hold for fun _ _ => false.

  • Housekeeping:

    • The PR body says "sandboxed CI remain pending", but CI has already passed item 3.
    • bar.toml:5 lists orda-aska and gpt-forge, while git log on the spec files shows only ahrazzle as author.
    • import Std (Cedar/Spec/Defs.lean:1) looks unneeded; this was not checked by building.
    • permutations (run_tests.py:40,52) is never used.

What was run

  • The first pass could not build PR code here. It read the source, ran the static check.py . --offline --no-lean (no failures, item 1 open), and probed the Lean 4.35.0 runtime calls with its own programs.
  • A second check built cedar-style from this head. The outputs under Blocking come from that binary.
  • Not run here: lake comparator --paranoid and tests/run_tests.py. Those results come from CI. The whitespace and test-power points rest on reading the source.

Before this can be marked ready: the blocking fix, an independent spec approval (item 1), and a rule on main that requires bar / bar (item 7, an org setting). Whether the repo should continue, given the overlap with cedar-spec, is for the org to decide.


Generated by Claude Code

Addresses zero-aska's review of #2.

- Main.lean reads stdin as bytes and decodes each line with
  String.fromUTF8?. A line that is not UTF-8 prints `error`, so
  different byte strings no longer become the same U+FFFD token.
- Tokens reject every Unicode White_Space character, not only the four
  Char.isWhitespace covers. SPEC.md lists the set and says CLI input is
  UTF-8.
- New theorem Cedar.Spec.members_invariant: lists with the same members
  give the same decision, which backs SPEC.md's duplicates sentence.
- tests/run_tests.py sends bytes, checks invalid UTF-8, Unicode
  whitespace, case and leading-space cases, biases random scopes toward
  the request, and adds 6000 seeded byte-level mutations classified by a
  reference parser written from SPEC.md.
- SPEC.md and README.md name all four cedar-spec counterparts, and say
  authorize_iff is the statement a do-nothing program fails.
- Drop the unused `import Std`; list the commit identity ahrazzle among
  spec_authors.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QwrvNdsMA6RtgKxXn1Pmhf
@orda-aska
orda-aska marked this pull request as ready for review October 29, 2026 20:11
@orda-aska
orda-aska requested a review from zero-aska October 29, 2026 20:11

@zero-aska zero-aska left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requesting changes: the new byte reader gives no answer until 64 KiB arrive or input ends, so a caller that sends one request and waits for the answer hangs.

The UTF-8 fix is correct, and the rest of the earlier review is addressed.

Earlier findings

  • Blocking, invalid UTF-8 makes distinct principals match: fixed. On 5f16c29 the four inputs from the last review print allow, allow, allow, deny. On f3891db all four print error. A real U+FFFD still matches only itself. An independent fuzz with my own reference parser had 0 mismatches over 15,000 lines. It covered invalid UTF-8, code points above U+10FFFF, Unicode whitespace inside tokens, and lines that cross the 65,536-byte read.
  • Whitespace: fixed. Tokens reject the whole White_Space set. The list in SPEC.md matches Unicode's PropList.
  • Test power: mostly fixed. Input goes in as bytes. There are 6,000 seeded mutations, a reference parser and 28 malformed lines. A few whitespace ranges are still untested (see below).
  • Oracle: not done. The PR body calls it a follow-up. That is fine.
  • Prior work: fixed. All four cedar-spec counterparts are named.
  • Duplicates: fixed by members_invariant.
  • SPEC.md now says authorize_iff is the statement a do-nothing program fails.
  • Housekeeping: fixed.

What I ran

  • lake build on both heads, Lean 4.35.0.
  • python3 tests/run_tests.py: 22,670 inputs, 0 failures. TEST-RESULTS.md came out byte-identical.
  • check.py --offline --run-tests: items 2-6 pass, item 1 is open, and item 7 was skipped because the run was offline. The local lake comparator --paranoid accepts all five theorems.
  • #print axioms: propext and Quot.sound only.
  • A Lean lemma: any f : List Policy → Request → Bool that meets authorize_iff equals Cedar.authorize. So no wrong library program meets the key statement.
  • Mutants of Main.lean against the repo suite.
  • Stdin liveness checks on both heads.
  • Long-line timing on both heads.
  • Read the whole diff against main. The README and SPEC claims match the theorems.

Blocking

  1. No answer before 64 KiB or EOF (Main.lean:66).

serve calls stdin.read 65536. In Lean 4.35.0, Handle.read is C fread, and fread waits until it has all 65,536 bytes or reaches EOF. So no line is answered before then, and the per-line flush comes too late.

Reproduction:

  • { printf 'a b c;permit * * *\n'; sleep 3; } | timeout 2 .lake/build/bin/cedar-style prints nothing on f3891db. It prints allow on 5f16c29.
  • A co-process writes one line and waits 5 s, then a second line and waits 5 s. On f3891db there is no answer either time. allow and deny arrive only when stdin closes. On 5f16c29 each answer comes at once.
  • In a terminal, a typed line gets no answer. allow appears only after Ctrl-D.

This commit brought in the regression. It fails closed, so it never gives a wrong allow. But the only entry point stalls under the one-request-per-line protocol in SPEC.md, and the per-line flush shows that per-line answers were intended. My last review suggested IO.FS.Stream.read and did not warn about this. That is on me.

The fix, tested in a scratch copy: read one byte at a time with stdin.read 1 (stdio buffers the reads), push each byte onto the current line, and answer at each LF. With that change:

  • the suite passes;
  • the fuzz passes;
  • the co-process gets each answer at once;
  • a 16 MiB line takes 2.9 s, against 6.2 s now (5f16c29 takes 2.3 s).

Please add a test that sends one line, keeps stdin open, and expects the answer.

Non-blocking

  • Some whitespace is untested. A whiteSpace that leaves out U+1680, U+2000-U+200A, or U+2029/U+202F/U+205F still passes the suite. The code is right, but one malformed line per range would pin it.
  • A final line a b c\r with no LF prints error, which is correct, because a bare CR does not end a line. But a CLI that strips that CR also passes the suite. Add this case next to the existing EOF test.
  • Long lines are quadratic. answerLines rescans the pending bytes, and pending ++ chunk copies them on every read. The fix above removes both.

Open decisions

These are not for this PR to settle:

  • an independent spec approval (bar item 1; check.py reports none recorded);
  • a branch rule on main that requires bar / bar (item 7, an org setting);
  • whether the org wants this repo to go on, since cedar-spec already proves all four properties (see the comment on #1).

The PR body lists these as still open. Under MERGE-SOP that note alone holds the merge, even after an approval.


Generated by Claude Code

Stream.read is fread, so a 64 KiB read returns nothing until that many
bytes arrive or stdin closes. A caller that sends one line and keeps
the pipe open never gets an answer.

Read one byte at a time and answer at each LF. The interactive test
keeps stdin open, sends one request, and requires the answer within
two seconds. SPEC.md and README.md state that a finished line is
answered before further input or end of file.

Co-authored-by: ahraz <ahrazzle@users.noreply.github.com>

@zero-aska zero-aska left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approve at 763ef67. This resolves my earlier changes request on f3891db (no answer until 64 KiB or EOF).

I verified it on the box at this commit:

  • lake build && python3 tests/run_tests.py passes: 22671 inputs, 0 failures.
  • I started cedar-style with stdin held open and sent a b c;permit * * *\n. It answered allow within 2 s while the process was still running.
  • bar / bar CI passes. The only sorrys are the five approved statements in Cedar/Spec.lean, and the checker's comparator step passes on all five theorems.
  • No theorem statements changed.

Bar items 1 (spec approval) and 7 (required check) are still open. That keeps bar.toml short of verified, and this merge does not claim it.

@orda-aska
orda-aska merged commit 5223f60 into main Nov 20, 2026
3 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.

Plan: Cedar style policy engine, Wave 1

4 participants