Repository navigation
feat(policy): split the ip attribute into ip4 and ip6 - #244
abhisheksp wants to merge 4 commits into
Conversation
## Problem Policy gets one spelling of a pinned destination address, so an IPv6 encoding of an IPv4 address reads as the IPv4 literal an operator writes. The fold covered the IPv4-mapped, IPv4-compatible, 6to4, and Teredo encodings only. It missed the NAT64 well-known prefix `64:ff9b::/96` and the IPv4-translated prefix `::ffff:0:0:0/96`. An address in one of those two prefixes reached no fold arm, so `context.input.ip` carried an IPv6 string. A `forbid` on `context.input.ip like "169.254.*"` did not match it. The gateway then permitted the destination and opened the connection. The design guide says that an IPv6 spelling of an IPv4 address becomes that IPv4 address, so the code did less than the guide states. The cost falls on the instance metadata address. Policy is the only check on a metadata endpoint, and the gateway holds no built-in block, so an authored `forbid` is the only control. A translator on the path is also necessary to reach a metadata service. This bounds the reach, but it does not close the gap in authorization. ## Solution `embedded_v4` gets one arm for each prefix. Both encodings hold the IPv4 address in the last 32 bits, so each arm matches the first six segments and uses the extraction that was already there. The six arms are disjoint, so their order has no effect. Two forms stay outside the fold, because no fixed prefix finds them. A network-specific prefix is not well known, so no arm can know it. The local-use NAT64 prefix `64:ff9b:1::/48` is reserved, but it holds the address in a different layout, and that layout permits four more prefix lengths. An address under that prefix, such as `64:ff9b:1:a9fe:a9:fe00::`, is still an IPv6 string to a rule. This is residual risk, and this commit does not change it. Two more costs are now larger, and neither is new. The load does not hold an `ip` literal to its canonical spelling, as it holds a host literal, so a rule that names an IPv6 encoding of an IPv4 address is inert, with no refusal and no warning. And the fold feeds the live decision and both history legs, so older events keep the IPv6 spelling. A temporal rule on `ip` that spans this change counts two keys for one destination. History has no rewind. No interface moves. `address` is a private module, and the action vocabulary, the attributes a rule reads, and what `when temporal` observes are unchanged. ## Tests `cargo test -p strands-box-policy --all-features`: 31 suites, 464 passed, 0 failed, 7 ignored. With default features: 298 passed, 0 failed, 5 ignored. `cargo clippy -p strands-box-policy --all-targets`, with and without `--all-features`: no warning in `crates/policy`. `cargo fmt --all --check` and `RUSTDOCFLAGS="-D warnings" cargo doc`: clean. `address.rs::every_ipv6_encoding_of_an_ipv4_address_is_that_address` gets four entries: the dotted and the hex spelling of each new prefix. Each new arm was removed in turn, and the test failed on that arm's entry. `address.rs::ipv6_is_compressed_and_lowercase` gets five addresses that keep the IPv6 form, although their last 32 bits spell an IPv4 address. They hold each new arm to its `/96`. The NAT64 arm was widened to `/32` and the translated arm to `/80`, and the test failed each time. `adapters/egress.rs::an_address_forbid_refuses_only_its_addresses` gets the two spellings. This shows that an authored `forbid` on `context.input.ip` refuses them through the interceptor. With both arms removed, the test failed on the NAT64 spelling. `test-integ/tests/policy/po_14.rs` gets `LIT_NAT64` and `LIT_V4_TRANSLATED`, which keeps its one literal for each fold arm, and its comment names the two encodings. Not run: `po_14` did not execute. Linux containment supports ARM64 only, and this host is x86_64, so the trampoline refuses before any case starts. Continuous integration must prove that case. `test-integ` type-checks and formats clean, and its two clippy warnings are also present without this change.
## Problem A decision reads one spelling of a pinned destination address. An IPv6 form that carries an IPv4 address reads as that IPv4 address, and any other IPv6 address reads compressed and in lowercase. The load held a host literal to its spelling, and it held an `ip` literal to none. A rule that named any other spelling loaded, raised no finding, and matched nothing. A `forbid` that matches nothing is a fail-open that passes review. The commit before this one made the cost worse. It added the NAT64 well-known prefix and the IPv4-translated prefix to the fold. On an IPv6-only host with DNS64, each IPv4 destination arrives on `64:ff9b::/96`, so an operator reads `ip` in that form from the decision log and writes a `forbid` against it. That `forbid` refused the connection before the fold held those two prefixes. After it, the policy loads, `ip` carries the dotted quad, and the gateway opens the connection. There is no refusal and no warning. The decision guide states that the load refuses a rule that cannot fire, so the code did less than the page states, for this rule and also for the four older encodings. ## Solution `spelling::judge_address` holds an `ip` literal to `address::canonical`, which is the one function that writes `context.input.ip`. The judge and the decision share one rule, as `judge_host_literal` shares the gateway's own `normalize_host`. `Scope::checks` admits `ip` beside `host` under the network family, and `input_attribute` admits the name, which is why no `ip` literal was judged before. The judge gives `Finding::Inert` in two cases. A literal that parses as an address and is not its canonical form is refused, and the refusal names the form to write. A literal that parses as no address is refused too: each string that `address::canonical` returns parses as an address and is its own canonical form, so no pinned address equals such a literal. Its advice names a port, a bracket, or `context.input.host`. A `like` pattern is judged only when it holds no wildcard, where the pattern and `==` make the same comparison. A pattern that holds a wildcard is not judged. `64:ff9b::5db8:*` matches the form that `64:ff9b:0:0:0:5db8:1:2` reads as, and no arm folds that address, so no proof of inertness is possible. A warning for a pattern whose first literal starts on a folded prefix was rejected, because the finding would be a guess, and because a new `PolicyWarning` variant is an interface change. A `when temporal` clause is not judged either, because history keeps the spelling that each event was recorded with. No type moves. `PolicyError::Spelling` keeps its variant and its message. The new startup refusal is observable: a box that holds a working `ip` rule in an IPv6 form now refuses to start, and the operator must write the form the refusal names. This is a breaking change for that operator. It is still correct, because that rule stops protecting them as soon as the fold holds its prefix, and a refusal that names the canonical form is better than silence. `docs/user/policy.md` holds the refusal and the migration guidance, and it names the two NAT64 forms that stay unfolded, for which an IPv6-form rule is the only rule that works. `docs/design/policy.md` adds the class to the list of rule classes the load refuses. `docs/design/egress.md` states the load check beside the fold, as its host paragraph already does. ## Tests `cargo test -p strands-box-policy --all-features`: 31 suites, 471 passed, 0 failed, 7 ignored, and no `skipping` line. With default features: 31 suites, 305 passed, 0 failed, 5 ignored. `cargo build`, `cargo clippy --all-targets` with and without `--all-features`, `cargo fmt --all --check`, and `RUSTDOCFLAGS="-D warnings" cargo doc`: clean, and zero clippy warnings in `crates/policy`. `path_spelling.rs::an_ipv6_encoding_of_an_address_literal_is_refused_and_the_dotted_quad_forbids_the_connect` is the end-to-end claim. It refuses the reviewer's rule at load, with the dotted quad in the message, and it shows that the dotted-quad rule denies five encodings of that address through `decide` and leaves another address to the permit. `spelling.rs::an_address_literal_is_refused_with_the_spelling_a_decision_reads` holds each of the six encodings and both IPv6 cases to their canonical form. `a_canonical_address_literal_is_sound` holds ten spellings that a decision reads, which includes the three unfolded addresses, so the judge refuses nothing an operator needs. `an_address_literal_that_no_pinned_address_can_equal_is_refused` holds the port, the bracket, and the generic advice, and it holds a bare IPv6 with one group too many to the generic advice rather than to a port it does not carry. `an_address_pattern_is_judged_only_when_it_holds_no_wildcard` holds three wildcard patterns to no finding and one wildcard-free pattern to a refusal. `a_reversed_operand_and_a_set_membership_judge_the_address` and `an_address_literal_under_no_action_is_refused_for_its_scope_and_not_its_spelling` hold the other comparison shapes and the scope gate. `shipped_examples.rs::every_shipped_example_policy_loads` holds each committed example policy. Each mutation below was applied, observed, then reverted. | Mutation | Result | |---|---| | Drop `ip` from `input_attribute` | The six literal and pattern tests fail | | Drop `ip` from `Scope::checks` | The same tests fail | | Delete the literal arm in `judge_literal` | Those tests and the end-to-end test fail | | Make the parse failure `Finding::Sound` | The advice test fails | | Drop the `canonical != literal` guard | The sound-literal test fails | | Replace the pattern arm with `Finding::Sound` | The pattern test fails | | Judge each pattern's first literal | The pattern test fails, and `every_shipped_example_policy_loads` fails on `like "169.254.*"` | | Accept a bare IPv6 as an address with a port | The advice test fails on `2001:db8::1:2:3:4:5:6` | | Delete the NAT64 fold arm | The end-to-end test fails, because the load accepts the IPv6 rule | The last two rows are the ones that matter. The seventh shows that the wildcard restriction carries weight, because a wider arm refuses a pattern that each shipped example holds. **Not run.** `test-integ` did not run, and the workspace suite did not link: this host has 7 GB free and the link exhausted the disk. `po_14` also cannot run here, because Linux containment supports ARM64 only and this host is x86_64. Continuous integration must prove those cases. No test was deleted and none was marked `#[ignore]`.
## Problem One `net:connect` field, `ip`, held two different things: an IPv4 address or an IPv6 one. A fold decided which the operator saw, and that cost three things. An operator could not tell from their own rule whether a literal matched. `ip like "fe8*"` reached an IPv6 link-local address and reached no IPv4 address, and nothing in the rule said so. The fold discarded the IPv6 wire form. An address on the well-known NAT64 prefix reached policy as the IPv4 address it wrapped, so a rule on the translator itself was unwritable. "Never route through this translator" could not be said. Any change to the fold silently broke rules written against its earlier output, because the attribute kept its name. The four shipped example policies showed the cost. Each held one clause that mixed IPv4 metadata, IPv6 metadata, and IPv6 link-local, so each was already discriminating by family by hand. ## Solution `net:connect` now reports two optional fields. `context.input.ip4` holds the IPv4 address the destination carries, as a dotted quad. `context.input.ip6` holds the IPv6 form it arrived as, compressed and in lowercase. A destination that wraps an IPv4 address in IPv6 carries both: `ip4` is the address it wraps, and `ip6` is the wire form. `address::embedded_v4` keeps each arm it had, including the guard that excludes `::` and `::1`, and it now populates `ip4` instead of replacing a spelling. An IPv4 destination carries no `ip6`. Extraction from IPv6 is deterministic given the prefix, and synthesis is not: one IPv4 address has many IPv6 encodings, so no IPv6 form is computed. When Box reads no wrapping, `ip4` is absent and `context.input has ip4` is false. `ip` is removed. `spelling::refuse_removed_address_attribute` refuses a `net:connect` rule that reads it, through `.` or through `has`, and names both fields that replace it. The refusal runs before strict validation on both load paths, so the operator reads what to write instead of an undeclared-attribute diagnostic, and the staged path does not park the rule as pending. An `http:request` rule and a tool action's own `ip` argument are untouched. Both history legs carry both fields. `net:connect::response` and `::error` report `ip4` and `ip6` on the same terms as the request leg. One helper, `address::fields`, feeds the Cedar mapping and the history mapping, so the two cannot name different fields. The spelling judge holds each field to its own family. An IPv6 literal under `ip4` is refused with the IPv4 address it carries, or told to compare `ip6` when it carries none. An IPv4 literal under `ip6` is told to compare `ip4`, and names no IPv6 spelling to write, because there is none. The cost is verbosity. An optional field needs a `has` guard, so a rule over both families needs one guard and one clause per family. The four example policies, the copy in the security guide, and the jailbreak verdict policy are each rewritten that way. Two limits are stated and not fixed. A NAT64 prefix a network chose for itself gets no extraction, because RFC 6052 permits one and Box takes none as configuration, so `ip4` is absent for such a destination and an `ip4` rule does not fire. And durable history straddles the change: an event already written carries `ip`, a temporal rule on `ip4` does not match it, and there is no rewind. ## Tests `cargo test -p strands-box-policy --all-features` passes 486 tests, and 318 without `--all-features`. Clippy reports zero warnings owned by the crate both ways, `cargo fmt --all --check` is clean, and `RUSTDOCFLAGS="-D warnings" cargo doc -p strands-box-policy` reports none. The population rules are pinned in `address.rs`. `every_ipv6_encoding_of_an_ipv4_address_populates_both` names each encoding with the wire form it reports, `an_ipv4_destination_populates_ip4_and_no_ip6` pins that no IPv6 form is synthesized, `an_ipv6_address_that_carries_no_ipv4_address_populates_ip6_alone` pins the absent `ip4`, and `unspecified_and_loopback_carry_no_ipv4_address` pins the `::` and `::1` guard. The capability the split adds is pinned twice. `path_spelling.rs::a_translator_prefix_forbids_the_wire_form_and_not_the_address_it_wraps` refuses `64:ff9b::5db8:d822` on `ip6 like "64:ff9b::*"` and leaves `93.184.216.34` reachable, and `adapters::egress::tests::a_translator_prefix_forbid_refuses_the_wire_form_and_leaves_the_address_admitted` does the same through the gateway adapter. `a_wrapped_destination_is_decided_by_a_rule_on_either_family` pins that both fields decide one wrapped destination, and `an_ipv4_destination_carries_no_wire_form_for_a_rule_to_read` pins that `has ip6` is false for an IPv4 destination. The refusal of `ip` is pinned on each load path. `spelling.rs::the_removed_ip_attribute_is_refused_and_names_ip4_and_ip6` covers the literal, the pattern, the reversed operand, set membership, and a bare `has`; `the_removed_ip_attribute_is_refused_only_under_a_network_action` pins that a tool action keeps its own argument; `adapters::egress::tests::the_removed_ip_attribute_is_refused_when_the_policy_loads` covers `PolicyEngine::open`; and `runtime_mcp_policy_staging.rs::staged_validation_refuses_the_removed_address_attribute` covers `validate_staged`, the path a box with a local MCP server reaches before any schema stages. `path_spelling.rs::a_rule_that_names_the_removed_ip_is_refused_at_load` asserts the whole message, because `docs/user/policy.md` quotes it. Both history legs are pinned in `connect_address_history.rs`: `a_connect_that_opened_records_both_attributes_a_rule_reads`, `a_connect_that_did_not_open_records_both_attributes_on_its_error_leg`, and `the_request_and_the_outcome_record_the_same_two_spellings`. The per-family spellings are pinned in `spelling.rs` by `an_ipv6_literal_under_ip4_is_refused_with_the_address_it_carries`, `an_ipv6_literal_that_carries_no_address_under_ip4_names_ip6`, `an_ipv4_literal_under_ip6_is_refused_and_names_ip4`, `an_uncompressed_ipv6_literal_under_ip6_is_refused_with_its_wire_form`, and `a_canonical_address_literal_is_sound_under_its_own_attribute`. `path_spelling.rs::an_uncased_wire_form_under_ip6_is_refused_with_the_spelling_a_decision_reads` asserts that whole message too, for the same reason. `shipped_examples.rs` loads each rewritten example. Each refusal in this change was mutation-tested by introduction and restoration: a synthesized IPv6 form, a dropped `::`/`::1` guard, an outcome leg that reports one field, both cross-family arms deleted, an echoing canonical form, and each of the two wirings of the `ip` refusal, and each produced a named failure. Not run: the deterministic containment suite under `test-integ`, which needs aarch64 Linux or macOS and reports every case as an error on this x86_64 host before any policy decision. `PO-14` and `PO-17` are edited here and are unverified end-to-end; their policy text was loaded through the engine instead, and `PO-17`'s family split was checked by hand against the two addresses `localhost` resolves to. Six `strands-box-containment` tests fail on this host, and `RUSTDOCFLAGS="-D warnings" cargo doc --workspace` reports ten errors in `strands-box-containment` and the vendored `strands-shell`; both sets fail identically on the parent commit. Still wrong and not changed here: `crates/policy/AGENTS.md`, `crates/box/AGENTS.md`, and `crates/egress-gateway/AGENTS.md` each name `context.input.ip`, and `docs/design/decisions.md` cites it in the entry on cloud-metadata protection.
| [PatternElement::Literal(text)] => { | ||
| judge_address(attribute, &format!("the pattern \"{pattern}\""), text) | ||
| } | ||
| _ => Finding::Sound, |
There was a problem hiding this comment.
Important (I think this blocks the merge, because this PR makes every operator rewrite their address rules): a wildcard pattern written in the wrong family loads without a message and never matches. On a forbid, that leaves the address open.
Example: An operator has a rule like the old shipped one, context.input.ip like "169.254.*" || context.input.ip like "fe8*". The new refusal tells them to "write the one for the family the rule means". A plausible rewrite puts every clause on one field. I ran each of these through support::open_policy on this head, and each one loads:
| Rule | Why it never matches |
|---|---|
context.input.ip6 like "169.254.*" |
ip6 always has a : before any ., so IPv4 metadata stays reachable |
context.input.ip4 like "fe8*" |
a dotted quad holds only digits and . |
context.input.ip6 like "FE8*" |
ip6 is lowercase |
context.input.ip6 like "64:ff9b::169.254.*" / like "::ffff:a9fe:*" |
NAT64 is rendered in hex, and an IPv4-mapped address as a dotted quad |
The equivalent literals (ip6 == "169.254.169.254", ip4 == "fe80::1") are already refused by judge_address. Only the wildcard arm returns Sound. docs/user/policy.md says "ip4 like "fe8*" reaches nothing", so the trap is documented but the code does not refuse it. The code could refuse it, because a host pattern with an unreachable byte is already refused (judge_host_pattern, a_host_pattern_is_judged_for_case_a_trailing_dot_and_an_unreachable_byte).
Suggestion: In this arm, refuse a pattern whose literal elements hold a byte the field never holds:
- for
ip4: any byte other than0-9and. - for
ip6: any byte other than0-9,a-f,:, and., plus a first literal that holds a.before any:
Each refusal should name the other field, as the literal refusals already do. Pin it with the ip6 like "169.254.*" case.
| - Guard an optional field with `has` (`arg1`, `arg2`, `tool`, `prompt`, `uri`, `ip`); an unguarded | ||
| read is a load error. | ||
| - Guard an optional field with `has` (`arg1`, `arg2`, `tool`, `prompt`, `uri`, `ip4`, `ip6`); an | ||
| unguarded read is a load error. There is no `ip`: `ip4` is the IPv4 address a destination |
There was a problem hiding this comment.
Important (non-blocking): this change leaves the guidance that coding agents read about ip wrong. Root AGENTS.md does not let that wait for a later change: "Docs ship with the code. A change is not done until its docs match it, in the same commit." It also says AGENTS.md is updated when "a frozen surface … moves", and this PR moves what when temporal observes.
Example: These lines are still wrong at this head:
crates/policy/AGENTS.md:115: "refuse an address with aforbidon the stringip". If an agent follows this, it writes a rule that now fails to load.crates/box/AGENTS.md:296andcrates/egress-gateway/AGENTS.md:43: both namecontext.input.ipas the metadata mechanism.- This file, lines 106 and 340: "Do not read
ipin a temporal predicate — it faults." Now a temporalinput.ipis refused at load by the event schema, and temporalip4/ip6are supported on purpose (connect_address_history.rs).
Suggestion: Update these lines in this PR. Each is a one-line field rename, and the PR description already lists most of them as "owed". Also update the citation in docs/design/decisions.md. Its answer does not move, so rule 2 needs only the citation corrected, not a new entry.
|
Assessment: Request Changes I reviewed head Review categories
Assumptions and rules checked that held
The family table in the description makes the breaking change easy to judge. |
…ts pipeline starts Upstream #246 adds Kernel::check_fallback. The kernel now decides a host-binary or python stage there, before any stage of the pipeline starts, and uses that decision when the stage runs. A decided stage that never runs is recorded as indeterminate. box_shell_cwd.rs::a_denied_tool_directory_stops_every_pipeline_stage passes again. The only other upstream change in the range, #244, edits a comment.
Description
This rule could not be written before. It says "never route through this translator":
A destination on the well-known NAT64 prefix used to reach policy as the IPv4 address it wrapped,
and the wire form was discarded, so there was nothing left to match a prefix against. The rule above
now refuses
64:ff9b::5db8:d822and leaves93.184.216.34, reached directly, alone.One field held two things: an IPv4 address or an IPv6 one, and a fold decided which. That cost three
things. An operator could not tell from their own rule whether a literal matched. The fold was lossy,
as above. And any change to the fold silently broke rules written against its earlier output, which
was the blocking review finding on the pull request this branch continues.
net:connectnow reports two optional fields:ip4ip693.184.216.34"93.184.216.34"2606:2800:220:1::1"2606:2800:220:1::1"64:ff9b::5db8:d822"93.184.216.34""64:ff9b::5db8:d822"Extraction, never synthesis. An IPv6 wire form that wraps an IPv4 address populates both. An IPv4
destination populates
ip4and leavesip6absent, because one IPv4 address has many IPv6encodings and there is no one form to report. Where Box reads no wrapping,
ip4is absent.ipis removed, not deprecated and not aliased. Anet:connectrule that reads it refuses to loadand names both fields:
Both history legs carry both fields, so a temporal rule reads the same names on a request, a
response, and an error.
The four shipped example policies were already discriminating by family by hand. Each held one
clause mixing IPv4 metadata, IPv6 metadata, and IPv6 link-local:
That is the strongest evidence the split is right. Each now says which family it means.
This must land after #227
#227 closes a live Sev-2 and is already approved. This branch is cut from it, not from
main, andrepurposes its work: the IPv6 encodings it taught Box to read are what populate
ip4here.The cost, unsoftened
Rules get more verbose. An optional field needs a
hasguard, so a rule over both familiesneeds one guard and one clause per family:
That is longer than one field was, and every operator who pins an address pays it.
Two limits this does not fix. A NAT64 prefix a network chose for itself still gets no
extraction: RFC 6052 permits one, RFC 7050 exists so a host can discover it, and Box takes none as
configuration. For such a destination
ip4is absent and anip4rule does not fire. And durablehistory straddles the change: an event already written carries the old field, a temporal rule on
ip4does not match it, and there is no rewind.Owed, not in this change. Three
AGENTS.mdfiles and the cloud-metadata entry indocs/design/decisions.mdstill name the removed field. The decisions entry's answer has not moved,only the field it cites.
Related Issues
None.
Type of Change
Breaking change
Testing
486 tests pass with
--all-features, 318 without. Zero clippy warnings owned by the crate eitherway.
cargo fmt --all --checkandRUSTDOCFLAGS="-D warnings" cargo doc -p strands-box-policyareclean.
What the new tests pin:
address.rs::every_ipv6_encoding_of_an_ipv4_address_populates_bothaddress.rs::an_ipv4_destination_populates_ip4_and_no_ip6::and::1are not read as IPv4address.rs::unspecified_and_loopback_carry_no_ipv4_addresspath_spelling.rs::a_translator_prefix_forbids_the_wire_form_and_not_the_address_it_wrapspath_spelling.rs::a_wrapped_destination_is_decided_by_a_rule_on_either_familyspelling.rs::the_removed_ip_attribute_is_refused_and_names_ip4_and_ip6runtime_mcp_policy_staging.rs::staged_validation_refuses_the_removed_address_attributeipargument still loadsspelling.rs::the_removed_ip_attribute_is_refused_only_under_a_network_actionconnect_address_history.rs, three testspath_spelling.rs, two exact-string assertionsEach refusal was mutation-tested by introduction and restoration: a synthesized IPv6 form, a dropped
::/::1guard, an outcome leg reporting one field, both cross-family arms deleted, an echoingcanonical form, and each of the two wirings of the load refusal. Every one produced a named failure.
shipped_examples.rsloads each rewritten example.Not run. The deterministic suite under
test-integneeds aarch64 Linux or macOS, and reportsevery case as an error on an x86_64 host before any policy decision.
PO-14andPO-17are editedhere and are unverified end-to-end — CI has to cover them. Their policy text was loaded through
the engine instead, and
PO-17's family split was checked by hand against the two addresseslocalhostresolves to. Sixstrands-box-containmenttests and ten workspacecargo docerrorsfail identically on the parent commit.
just check, orcargo test --workspace --all-features)cargo fmt --allandcargo clippy --workspace --all-targets --all-features -- -D warningsChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.