Skip to content

fix(egress): SBX0.2b egress-credential follow-ups - #870

Merged
webchang merged 11 commits into
rossoctl:mainfrom
webchang:pr/i653-egress-cred-followups
Nov 17, 2026
Merged

webchang merged 11 commits into
rossoctl:mainfrom
webchang:pr/i653-egress-cred-followups

Conversation

@webchang

Copy link
Copy Markdown
Member

Part of #653

The issue's small, self-contained follow-ups, one commit each. The first section ("test gaps that hide real checks") was done by #655 and #669. What is left after this PR needs a decision or is a larger piece of work; it is listed under "Out of scope" with the reason.

What changed and why

Proxy (remote-worker/internal/egress)

  • A cached credential's TTL now counts from the lookup's start (cpcreds.go, fly). The expiry used to be stamped when the control plane's answer arrived. The server's ttlSeconds starts when it answered, which is after the request left, so a slow lookup let an entry outlive the server's TTL by the length of the call, up to the client timeout. Counting from the start can only end an entry early.
  • An intercepted refusal is bounded by drainTimeout, not the idle clock (intercept.go, icConn.refuse; deadline.go). The guest TLS runs over an idleConn, whose every Write refreshes the idle clock. So the refusal write's own refresh replaced the one-second drain deadline with the idle one, and a guest that stopped reading held the conn, and its per-VM slot, for the whole idle bound. idleClock gains stop(), which turns later refreshes into no-ops, and refuse stops it first. tls.Conn's close_notify writes set their own five-second deadline, so two more changes keep that out too: a refusal the guest did not take closes the socket at once, and the drain timer is armed before CloseWrite.

Conformance suite (remote-worker/egress/conformance)

  • redactEvent is deep. It used to look only at top-level keys and strings, so an authorization key or a Bearer string inside a nested object or array was printed in a failure message.
  • The sub rule is checked on every event a case reads. The contract is that sub is present on credential_injected events and absent or "" on every other. Only one case checked the second half; harness.next now checks it, the way wait already checks via. doc.go says so.

Control plane

  • The lookup's two 500s are audited (egress-handlers.ts). credential_unsafe and destination_ambiguous are now written as egress_credential_refused, with the error code as reason and the credential names involved (never a value). Before, only issued and none were audited. Other resolver failures (the store being down, for example) still pass through unaudited. The spec's §4.2 audit item is updated.

Deploy

  • build-snapshot.sh --egress-ca says when openssl is missing. Without openssl, a good certificate was refused as "not a parseable X.509 certificate".
  • install-host.sh: install_egress removes its temp file on every failure. Under set -e, a failing render or install of the unit exited between the mktemp and the rm. The render failure now also names the missing unit source.
  • install-host.sh removes only its own credential files. --uninstall-egress and the last worker's uninstall deleted, as root, every path that an egress credential manifest line named. Now only the drop-in and the files directly in the credential directory are removed; any other path is kept, and the log names it.

Coverage (test-only)

  • install-host.test.sh: an --egress re-run with a new token (replaced, 0600, recorded once, moca-egress restarted, no leak, CA kept).

  • egress-config.test.ts: TTLs that Number() alone would accept (60s, 60.5, 6e1, 0x3c, +60), an uppercase-hex token, and a multi-entry KEK ring (accepted, and refused when one entry is bad).

  • credential-store.test.ts: the text of the consumer refusal (credential 'gh' has consumer 'sandbox-egress', not 'inference').

  • sbx-credentials.test.ts (mocactl): add --json shape, --host a,b splitting and trimming, and a non-preset basic read as field=value lines from stdin.

  • An empty credential name (egress credential <dir>/, the directory itself) is now kept and named, not logged as removed with its line forgotten; a test pins it and fails without the fix.

Validation

Each fix has a test that fails without it (the fix reverted, the test kept) and passes with it:

Test Without the fix With it
TestTTLMeasuredFromRequestStart (cpcreds_test.go) calls = 1, want 2: the entry outlived the server's TTL by the call's duration pass
TestInterceptRefuseWriteBoundedByDrainTimeout (abort_test.go), a refusal over a net.Pipe whose guest never reads refuse still blocked after 5.001s: the idle refresh overrode the drain deadline (with only the stop() call removed, it also fails: the close_notify writes alone hold it about 7 s) pass in 1.01 s, -count=3
TestRedactEventIsDeep (redact_test.go) the fixture credential printed in the nested fields pass
sub check in harness.next, mutation: the proxy stamps a subject on every event 42 conformance subtests fail with a tunnel event carries sub ... pass (unmutated)
egress-handlers.test.ts: two audits a ... refusal cases expected [] to deep equally contain ObjectContaining{…} (both) pass; the unsafe case also checks the stored value is in no log line
build-snapshot.test.sh: no openssl: ... the refusal names the missing tool (want '1', got '0'), the good certificate is not called unparseable (want '0', got '1') PASS
install-host.test.sh: failed egress install FAIL: a failed egress install left its temp file: tmp.… pass
install-host.test.sh: foreign credential lines FAIL: --uninstall-egress deleted …/cred-outside/precious, which is not a file of …/etc/egress pass

The coverage tests pass on main, which is expected. Where it applies, they were mutation-checked: parsing the TTL with Number() and matching tokens case-insensitively fail five of the new egress-config cases, and dropping the --host comma split fails the splitting case.

Suites run:

  • remote-worker: go vet ./... clean; go test -race ./internal/egress/... ./egress/... ok (the conformance suite runs in-process and against the built binary).
  • packages/control-plane: tsc --noEmit clean; vitest run 80 files, 1947 passed, 3 skipped.
  • packages/mocactl: sbx-credentials.test.ts 23/23. The full suite had timing failures (Test timed out in 5000ms, in app.test.tsx, sbx-push.test.ts, sbx-doctor.test.ts and others) on a heavily loaded machine. This PR changes no mocactl source, only that one test file.
  • packages/server: not affected (no server or harness file, and no environment variable name, changes here).
  • deploy/sbx/tests/install-host.test.sh and deploy/microvm/tests/build-snapshot.test.sh exit 0; shellcheck -x -S warning is clean on every changed script.
  • python3 scripts/check-doc-links.py: 0 broken links.

Out of scope

Each remaining point of #653, with its state on main:

  • Done on main already: the URL rules (NewCPCredentials refuses userinfo, a query, a fragment and a path, pinned by TestNewCPCredentialsURLRefusals). Spec §8 already says that a Set-Cookie minted from the injected token reaches the guest, and docs/sbx/security-review.md lists it as an accepted risk. A credential deleted mid-flight is audited (egress_credential_none when none is left, pinned by answers no_credential, audited, when the only credential vanishes mid-flight; a partial delete issues the survivors under their own names). The vault store's array-binding round trip is covered (vault-store.test.ts, round-trips a multi-binding ... descriptor unchanged). remove_egress already leaves an unknown egress <kind> line and its file alone (*) continue).
  • Needs a decision:
    • leaf_unavailable audited with status 502 although no HTTP response is sent. The issue asks whether to keep it. It is documented in conformance/doc.go.
    • Stale host tokens while the feature is off (setup.sh). By design, an off run leaves sbx-egress alone so the KEK survives. Revoking the tokens on an off run would mean every host's bundle must be copied again when the feature is turned back on. While the feature is off, the control plane does not read the tokens.
    • The CA's manifest lines persist after --uninstall-egress (and log "kept" again). The CA is kept on purpose, because snapshots carry it, and its line is the record that this script owns it. Dropping the line would orphan the file.
    • Found while adding the sub check: when an injected request's upstream fails, icConn.exchange refuses with the injection's event, so the upstream_unreachable event keeps sub (and, on purpose, credential). The contract says sub appears only on credential_injected. No conformance case reaches this path today. Either the contract should allow sub on a refusal that carried a token, or the refusal should clear it. This PR changes neither.
    • wait_egress_ready checks only that the socket exists. The unit is Type=notify, so a successful start already means the socket is bound. A connect probe would need a tool (socat, python) the host may not have.
  • Larger: the cache eviction's full scan under the mutex. It runs only when an insert meets a full cache (4,096 entries). Fixing it means an expiry-ordered structure, which is a design change rather than a follow-up. Also larger: live acceptance item 3, which needs a second enrolled user, and the Vault-backed credential store.

Every commit is signed off (DCO).

Assisted-By: Claude Code

CPCredentials stamped a cache entry's expiry with the time the control
plane's answer arrived. The server's ttlSeconds starts when it answered,
which is after the request left, so a slow lookup let the entry outlive the
server's TTL by the call's duration, up to the client timeout. fly now reads
the clock before the call and counts the TTL from there, which can only end
an entry early.

TestTTLMeasuredFromRequestStart advances the fake clock inside the fake
control plane to stand in for a slow call and fails without the fix.

Part of rossoctl#653

Assisted-By: Claude Code
Signed-off-by: Rong Chang <[email protected]>
…le clock

icConn.refuse set a drainTimeout write deadline on the guest TLS and then
wrote the refusal through it. The guest TLS runs over an idleConn, whose
every Write refreshes the idle clock, so the write's own refresh replaced
the one-second drain deadline with the idle one: a guest that stopped
reading held the conn, and its per-VM slot, for the whole idle bound.

The idle clock gains stop(), which makes later touches no-ops, and refuse
stops it before setting its deadlines; nothing on a conn being refused is
live any more. Two tls.Conn close_notify writes would still each take up to
the standard library's own five seconds against such a guest, so a refusal
the guest did not take closes the socket at once, and the drain timer is
armed before CloseWrite so the close_notify falls inside the drain bound.

TestInterceptRefuseWriteBoundedByDrainTimeout runs a refusal over a
net.Pipe whose guest never reads; without the fix refuse is still blocked
after five seconds, with it it returns after one.

Part of rossoctl#653

Assisted-By: Claude Code
Signed-off-by: Rong Chang <[email protected]>
…event

Two gaps in the public conformance suite's audit handling.

redactEvent, which keeps credentials out of the suite's failure messages,
looked at top-level keys and strings only. An implementation may emit
nested objects or arrays (a header map, a list of attempts), and an
authorization key or a Bearer string inside one printed as is. It now walks
objects and arrays; TestRedactEventIsDeep fails without that.

The audit contract says sub is present on credential_injected events and
absent or "" on every other, but only one case (the forged-headers one)
checked the second half. harness.next now checks it on every event a case
reads, the way wait already checks via. Mutation-checked: making the proxy
stamp a subject on every event fails 42 subtests with "a tunnel event
carries sub ...".

Part of rossoctl#653

Assisted-By: Claude Code
Signed-off-by: Rong Chang <[email protected]>
…egress-ca file

validate_egress_ca parses the certificate with openssl and discards its
stderr, so on a build host without openssl a good certificate was refused
as "not a parseable X.509 certificate", pointing the operator at the wrong
problem. The tool is now checked first, with its own message.

The new build-snapshot.test.sh case runs validate_egress_ca with a PATH that
has grep but no openssl; on main it reports the certificate unparseable.

Part of rossoctl#653

Assisted-By: Claude Code
Signed-off-by: Rong Chang <[email protected]>
POST /v1/egress/credential audited the two answers it gives most often,
egress_credential_issued and egress_credential_none, but not its two 500s:
credential_unsafe (a stored value that cannot render) and
destination_ambiguous (two credentials on one port and prefix, reachable
only by a write race). Both mean a sandbox's request went out without the
credential its owner stored, and the audit could not say why.

Both are now audited as egress_credential_refused, with the error code as
reason and the credential names involved (never a value), then thrown
unchanged. Other resolver failures, such as the store being down, are not a
credential's doing and still pass through unaudited. The spec's §4.2 audit
item says so.

Two new egress-handlers tests fail without the change; the unsafe one also
checks that the stored value is not in any log line.

Part of rossoctl#653

Assisted-By: Claude Code
Signed-off-by: Rong Chang <[email protected]>
install_egress renders the moca-egress unit into a mktemp file, compares it
with the installed unit and installs it. Under `set -e` a failing render or
install ended the script between the mktemp and the rm, leaving the temp
file behind on every failed run. Both steps now remove it before dying, and
the render failure says which file is missing. ca_fingerprint's hash step
gets the same treatment.

The new install-host.test.sh case runs a script copy whose unit source is
missing, with mktemp shimmed into one directory (macOS's ignores TMPDIR
without a template). On main it fails with "a failed egress install left
its temp file".

Part of rossoctl#653

Assisted-By: Claude Code
Signed-off-by: Rong Chang <[email protected]>
uninstall_egress_credentials and remove_egress delete, as root, every path
an `egress credential` manifest line names. The manifest is a file under
SBX_ENV_DIR, not proof that a path is this script's, so an edited or
corrupted line could delete any file on the host. remove_credential_file
now removes only the drop-in and files directly in the credential
directory; anything else is kept and named ("kept PATH (not a credential
file of this script)"), and its line stays.

The new install-host.test.sh case plants three such lines (a path outside
the directory, one below it, one through `..`); on main --uninstall-egress
deletes all three.

Part of rossoctl#653

Assisted-By: Claude Code
Signed-off-by: Rong Chang <[email protected]>
…old one

The --egress tests re-ran with the same token only. A rotated token (or a
host re-listed by setup.sh) is the re-run that matters: the token file must
hold the new token alone, stay 0600 and be recorded once, moca-egress must
be restarted to read it, the new token must not reach a unit file, the
output or a command line, and the CA must survive. Test-only; it passes on
main.

Part of rossoctl#653

Assisted-By: Claude Code
Signed-off-by: Rong Chang <[email protected]>
…nsumer refusal's text

egressConfigFromEnv: a TTL that Number() alone would take (a unit suffix, a
fraction, an exponent, hex, a sign) and an uppercase-hex host token are
refused; a multi-entry KEK ring (what a rotation leaves) is accepted, and a
ring with one bad entry is refused naming that entry. Mutation-checked:
parsing the TTL with Number() and matching tokens case-insensitively fail
five of the new cases.

resolveInferenceName's refusal of a named non-inference credential was
checked by code only; its text, which the user sees, is now pinned too.

Test-only.

Part of rossoctl#653

Assisted-By: Claude Code
Signed-off-by: Rong Chang <[email protected]>
…ield=value stdin

Three paths of `mocactl sbx credentials add` had no test: a comma-separated
--host (split and trimmed, across repeated flags), a non-preset multi-field
kind read as field=value lines from stdin (a value may itself hold "=", and
a missing field is a usage error before any request), and the --json
output (one object on stdout, never the secret). Mutation-checked: dropping
the comma split fails the first.

Test-only.

Part of rossoctl#653

Assisted-By: Claude Code
Signed-off-by: Rong Chang <[email protected]>
…gged as removed

An `egress credential <dir>/` manifest line (the directory itself) passed the path check: nothing was
deleted, but the log said removed and the line was forgotten (review nit).

Part of rossoctl#653

Assisted-By: Claude Code
Signed-off-by: Rong Chang <[email protected]>
@pdettori pdettori added the ready-for-ai-review Request automated AI code review from clawgenti label Nov 17, 2026

@ingpaolodettori-dev ingpaolodettori-dev 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.

Review at e6d34b1

This PR takes 11 small follow-ups from #653. Checked statically:

  • refuse always ends the conn, so stopping the shared idle clock can't stall live traffic, and the early pc.c.Close() is safe next to the idempotent releases.
  • Counting the TTL from start only shortens a cache entry.
  • reason already exists on the audit entry, and refused rethrows the original error unchanged.
  • Every version of install-host.sh on main records only the drop-in and files directly in the credential directory, so the tighter remove_credential_file keeps nothing an older install wrote.
  • The strict sub check matches the reference proxy on every path conformance reaches today. The intercepted upstream-failure case is still open in #653, as the PR body says.
  • The spec §4.2 text matches the code.

Author: webchang (MEMBER — maintainer)
Areas reviewed: Go (egress, conformance), TypeScript (control-plane, mocactl tests), Shell (install-host.sh, build-snapshot.sh, tests), Docs
Agent/IDE config (.claude/.vscode): none
Commits: 11 commits, all signed-off: yes
CI status: all checks passing at submit time except deploy-scripts-bash32 (macOS), still pending

nit: 7 of 11 commit subjects are over 72 characters (e.g. test(control-plane): cover the egress settings' edge cases and the consumer refusal's text, 90). Advisory only.

Drafted by an AI reviewer and approved by a human before posting.

@webchang
webchang merged commit 383fb0e into rossoctl:main Nov 17, 2026
23 of 24 checks passed
@pdettori pdettori added this to the v0.11.0 milestone Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-ai-review Request automated AI code review from clawgenti

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants