Repository navigation
Implement Cedar-style policy decisions and prove deny precedence - #2
Conversation
zero-aska
left a comment
There was a problem hiding this comment.
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
- 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'printsallow, although the principal bytes differ.printf '\xed\xa0\x80 read doc;permit \xc0\xaf read doc\n'(an encoded surrogate against an overlong) printsallow.printf '\xef\xbf\xbd read doc;permit \xff read doc\n'printsallow. 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'printsdeny. 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.readinto aByteArraysplit on 0x0A. Decode each line withString.fromUTF8?and printerrorwhen it returnsnone. - State in SPEC.md that CLI input is UTF-8 and that invalid UTF-8 is malformed. Whether
errorordenyis 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 asbytes.
Non-blocking
-
Whitespace. SPEC.md:26 says tokens "contain no whitespace".
tokenusesChar.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 giveallow(derived from the source, not run). Either define whitespace in SPEC.md as those four characters, or reject Unicode White_Space intoken. 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 acceptsPERMITorForbid, 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 examplepermit(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), andorder_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_invarianttakes onlyps.Perm qs. Either addmembers_invariant (h : ∀ p, p ∈ ps ↔ p ∈ qs), or say it is a corollary ofauthorize_iff. -
SPEC.md should say that
authorize_iffis the statement a do-nothing program fails. The other three all hold forfun _ _ => false. -
Housekeeping:
- The PR body says "sandboxed CI remain pending", but CI has already passed item 3.
bar.toml:5listsorda-askaandgpt-forge, whilegit logon the spec files shows onlyahrazzleas 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-stylefrom this head. The outputs under Blocking come from that binary. - Not run here:
lake comparator --paranoidandtests/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
zero-aska
left a comment
There was a problem hiding this comment.
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 printerror. 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_iffis the statement a do-nothing program fails. - Housekeeping: fixed.
What I ran
lake buildon 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 locallake comparator --paranoidaccepts all five theorems.#print axioms:propextandQuot.soundonly.- A Lean lemma: any
f : List Policy → Request → Boolthat meetsauthorize_iffequalsCedar.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
- 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-styleprints nothing on f3891db. It printsallowon 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.
allowanddenyarrive only when stdin closes. On 5f16c29 each answer comes at once. - In a terminal, a typed line gets no answer.
allowappears 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
whiteSpacethat 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\rwith no LF printserror, 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.
answerLinesrescans the pending bytes, andpending ++ chunkcopies 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
left a comment
There was a problem hiding this comment.
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.pypasses: 22671 inputs, 0 failures.- I started
cedar-stylewith stdin held open and senta b c;permit * * *\n. It answeredallowwithin 2 s while the process was still running. bar / barCI passes. The onlysorrys are the five approved statements inCedar/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.
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.leanagainst the statements inCedar/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
Cedarnamespace. 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
5f16c29Main.leannow reads stdin as bytes, splits on LF and decodes each line withString.fromUTF8?. A line that is not UTF-8 printserror. SPEC.md now says CLI input is UTF-8 and that invalid UTF-8 is malformed. All four inputs from the review now printerror. A real U+FFFD matches only itself.PERMIT,Forbid, a leading space,*a/a*scopes, Unicode whitespace and invalid UTF-8.members_invariant), not just a sentence.5a5f00b.import Stdand the unusedpermutationsvariable.bar.tomlspec_authorsnow also listsahrazzle, the git identity on the spec commits, so that account cannot approve the spec.Checked on the head
lake buildpasses on Lean 4.35.0.#print axiomsgivespropextandQuot.soundfor all five theorems.python3 tests/run_tests.py: 22,670 inputs, 0 failures.bar / bar, which runslake comparator --paranoidsandboxed, passes.Still open, and not decided by this PR:
bar / bar(item 7, an org setting);🤖 Generated with Claude Code
https://claude.ai/code/session_01QwrvNdsMA6RtgKxXn1Pmhf