Skip to content

fix: bound RPC preauth without blocking admitted callers - #5282

Closed
thymikee wants to merge 17 commits into
fix/daybreak-upload-reviewfrom
fix/rpc-ingress-preauth-budget
Closed

thymikee wants to merge 17 commits into
fix/daybreak-upload-reviewfrom
fix/rpc-ingress-preauth-budget

Conversation

@thymikee

@thymikee thymikee commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

Closed as superseded by #5343 on the 0.22 remediation line. Continue review and implementation there. Historical validation below remains attributed to its original commits; closure does not claim the replacements are merge-ready.

Summary

The daemon and proxy share a five-second preauthorization deadline for the RPC body and authentication. The daemon caps concurrent preauthorization and releases each slot before dispatch. The existing buffered-byte budget remains through execution. Hookless loopback remains local. Stacked on #5281.

Before / after

  • A preauthorization timeout returns a typed, retriable 408 with dispatched: no and preserves a bounded, valid RPC ID when the body has arrived.
  • A full daemon preauthorization pool returns a typed, retriable 503 before dispatch.
  • The proxy preserves valid IDs for ordinary refusals, including strings over 128 characters. Checked-protocol refusals retain their own ID validation.

Validation

At da479265c9, all four focused ingress tests passed with loopback required. Both production HTTP listeners and the default proxy upstream transport verified retriable 408s from proxy and daemon authentication. Each refusal preserved its RPC ID and reported dispatched: no. A fresh RPC reached the fixture handler after each timeout. Daemon-capacity 503 and overlapping native dispatch remain open.

At 08fb3a0b6f, all eight script admission tests and all 19 hosted checks passed.

At bb5053d270, all 14 previous patches match by range-diff after rebasing onto #5281 0cd48fbd30. The follow-up shares the timeout-only ID limit. All 14 proxy cancellation tests passed, including a late auth completion with zero upstream calls. All 19 hosted checks passed.

At 2c6b09defd, all 19 hosted checks passed. The iOS 26.2 receipt at that combined tip observed two successful overlapping inventory RPCs through the production proxy and daemon listeners.

2eedab287a passed merge-tree. CI pending.

@blacksmith-sh

This comment has been minimized.

@github-actions

github-actions Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Size Report

Metric Base Current Diff
Installed (including dependencies) 6.79 MB 6.79 MB +2.0 kB
Package (unpacked) 6.79 MB 6.79 MB +2.0 kB
Package (download) 2.07 MB 2.07 MB +657 B

Startup median (7 runs, lower is better):

Scenario Base Current Diff
CLI --version 22.1 ms 25.8 ms +3.7 ms
CLI --help 47.8 ms 53.5 ms +5.7 ms

Affected plugins

Plugin Installed (including dependencies) Unpacked Download
@agent-device/revyl 181.2 kB → 181.2 kB (0 B) 180.8 kB → 180.8 kB (0 B) 58.6 kB → 58.6 kB (+3 B)
@agent-device/testmu 26.5 kB → 26.5 kB (0 B) 26.2 kB → 26.2 kB (0 B) 9.6 kB → 9.6 kB (0 B)
@agent-device/tizen 99.5 kB → 99.5 kB (0 B) 99.1 kB → 99.1 kB (0 B) 34.9 kB → 34.9 kB (+2 B)
@agent-device/webos 98.5 kB → 98.5 kB (0 B) 98.1 kB → 98.1 kB (0 B) 34.6 kB → 34.6 kB (0 B)

@thymikee
thymikee force-pushed the fix/rpc-ingress-preauth-budget branch from cdee673 to c0fa010 Compare November 30, 2026 16:46
@apex-sentinel-staging

This comment has been minimized.

@thymikee

Copy link
Copy Markdown
Member Author

At 2fa24d9, local lease and leased commands over HTTP regress when they carry a tenant. This needs a fix before merge.

http-authorization.ts:68: restrictRemoteHttpRequest now treats any request with meta.tenantId as remote, even on a hookless daemon with no network-access marker. A local CLI sends that client-declared field on lease_allocate and on every leased command. It then loses providerCredentialFingerprint and developerDir, so the stale provider credential check returns early and the iOS DEVELOPER_DIR selection is lost. Host-path installs and saveScript calls are refused, and web commands are limited to public networks. The rewritten http-server-rpc-validation test expects this behavior. The rule should be that "remote" comes from provenance only (a configured auth hook or the network-access marker), at every request that passes through this admission point. A tenant-scope field chosen by the client should not decide it. If direct tunneled calls need limits, derive them there from the transport, such as the socket address or a marker set by the tunnel or proxy. Please move that change to its own PR for the #5243 finding and restore the original test expectation. A passing run should show that a hookless loopback lease.allocate with tenantId still compares the credential fingerprint.

Not blocking: these can be taken or left: load the deadline in daemon-proxy.ts:34 lazily like the body budget (or build it inline with AbortSignal.any) so the proxy entry does not grow; give http-server.ts:313 and the proxy one shared RPC_PREAUTH_TIMEOUT_MS with AbortSignal.timeout, so both report the typed operation-deadline reason; open a follow-up for http-server.ts:344, where unauthenticated bodies can repeatedly fill the shared 128 MiB pool and reset admitted callers without a reason (check a present header credential first or give those bytes a smaller share, and return a typed 503); document that http-server.ts:322 now needs about 107 Mbit/s for a 64 MiB body, or add a typed reason and hint to the deadline answer; and make daemon-proxy-cancellation.test.ts:121 assert the exact status and reason, plus add a daemon fake-timer test where a stalled body is refused and a later admitted body of the freed size is accepted.

Could the deadline be smaller: one shared constant, an AbortSignal.timeout passed to auth, one abort listener in readRpcRequestBody that sends the refusal and is removed in release(), and in the proxy a body read that takes AbortSignal.any([request.signal, AbortSignal.timeout(...)]) and returns that signal for authenticateCaller? That would drop rpc-preauth-deadline.ts, the Request re-wrap, the eager import and the finishPreauth threading, with no earlier change required.

The Coverage and Repo Guards failures look related to this change. The proxy eager-closure budget fails because of the static import, and the daemon credential composition test and provider-scenario integration tests send hookless loopback requests with tenantId. The wire-compat and upload-http-deadline failures touch upload code that this diff does not change, so they are likely inherited from the base branch. The Body rules check was cancelled, and one other check was still in progress.

Not verified here: tests were not run, job logs were not read, and the upload failures were not confirmed on the base branch. The tenant impact assumes a local CLI using HTTP or dual server mode, and the byte-pool concern comes from reading the code.

Before merge, the tenant check needs to be reverted or moved to a transport-based rule, the proxy budget failure needs a fix, and CI needs a new run.

@thymikee
thymikee force-pushed the fix/rpc-ingress-preauth-budget branch from 2fa24d9 to b339cf2 Compare November 30, 2026 18:48
@thymikee
thymikee force-pushed the fix/rpc-ingress-preauth-budget branch from b339cf2 to 9b8070c Compare November 30, 2026 19:00
@thymikee

Copy link
Copy Markdown
Member Author

Fixed the blocking loopback regression in 6f5fdaffc6. Remote restrictions now depend on the configured auth hook or network-access marker, not client-declared meta.tenantId. The existing HTTP lease-allocation regression again requires the local credential fingerprint to reach admission, and the script admission test confirms a tenant field alone does not make the request remote. Those focused files passed (20 tests).

The proxy preauth deadline now loads only on the RPC path, addressing the eager-closure import failure. I updated the security documentation and PR body to remove the direct-tenant restriction claim. CI is pending at this head; the ADR 0006 wire-compatibility failures remain inherited from the stack base. The other suggestions in this comment remain optional follow-up work.

@apex-sentinel-staging

This comment has been minimized.

@thymikee
thymikee force-pushed the fix/rpc-ingress-preauth-budget branch from 6f5fdaf to 630af4f Compare November 30, 2026 19:46
@thymikee

Copy link
Copy Markdown
Member Author

At 630af4f, the loopback regression from the earlier review is fixed, and no new code problem needs a change. Remote restrictions again depend only on a configured auth hook or the network-access marker (http-authorization.ts), and the original test again expects a local lease.allocate with tenantId to compare the credential fingerprint. The proxy now loads the deadline module only for RPC requests.

Repo Guards and Coverage failures look unrelated to this change. They report wire-compat changes in upload and artifact declarations that this PR does not touch, and the base commit f97e504 from #5281 fails with the same annotations. The earlier eager-closure, credential composition and provider-scenario failures are gone, and one other check is still in progress.

Not verified here: tests were not run locally. The focused RPC validation and script admission tests are absent from the failing annotations, which suggests that they passed in CI. The pending live remote and device evidence and wire review belong to #5281.

Nothing more is needed in this PR. Before CI can pass, #5281 must resolve the wire-compat failures under ADR 0006, either with a DAEMON_RPC_PROTOCOL_VERSION bump or a ledger acknowledgment.

@apex-sentinel-staging

This comment has been minimized.

@apex-sentinel-staging apex-sentinel-staging Bot 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.

Apex Sentinel: 1 new finding on 630af4f. The verdict and the full list are in the summary comment.

@apex-sentinel-staging

This comment has been minimized.

@apex-sentinel-staging apex-sentinel-staging Bot 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.

Apex Sentinel: 1 new finding on e0096e3. The verdict and the full list are in the summary comment.

Comment thread packages/proxy/src/daemon-proxy.ts Resolved
@apex-sentinel-staging

This comment has been minimized.

@thymikee

Copy link
Copy Markdown
Member Author

The code at c29cc21 is ready on behavior, but one CI failure from this change must be fixed before merge. Since the review of 630af4f, preauthorization expiry has a typed, documented reason in both the daemon and the proxy.

In Coverage, the eager-closure budget check fails because daemon-proxy.ts now statically imports the new rpc-refusals.ts, so the proxy entry evaluates 9 modules instead of 8. That check passed at 630af4f. The proxy entry must evaluate no module that the merge base did not: keep both moved functions in daemon-proxy.ts and add only the 408 branch there, or load rpc-refusals.ts with a function-scoped await import, then rerun Coverage. Not verified here: whether a file-size limit prompted the move; if it did, use the lazy import.

Not blocking: the daemon reason can be taken or left, but its comment and error-reasons.md row could say "RPC body or authentication did not finish before its preauthorization deadline" because the timer also covers authentication, and the error could add retriable: true to match the proxy.

These threads no longer apply and can be resolved: daemon refusal reason is fixed, with a typed reason and dispatched: no; proxy expiry response is fixed, with a 408, a documented reason and dispatched: no.

The other Coverage failures (wire-mutations and wire-compat.test.ts) and the Repo Guards step "Compare the daemon RPC wire surface" list only upload, download and LeaseRegistry declarations that this PR does not touch. They come from base #5281 (f97e504) and still need an ADR 0006 bump or ledger acknowledgment there before this PR can pass. The cancelled Body rules check is unrelated to the diff.

Not verified here: tests were not run locally, and passing status for the changed tests is inferred from their absence in the failure annotations. Repo Guards stopped at the wire comparison, so "Check for production-unused exports" was skipped at this commit.

@thymikee

Copy link
Copy Markdown
Member Author

Addressed the eager-closure regression in 14688d4: rpc-refusals.ts now loads through function-scoped imports on the RPC paths, so the proxy entry no longer imports that module during evaluation. daemon-proxy.ts remains below its file-size limit at 957 lines. The focused proxy cancellation test passed 12/12. Current-head Coverage is pending; the inherited protocol-3 wire ledger failures remain a separate draft blocker.

@apex-sentinel-staging

This comment has been minimized.

@thymikee

Copy link
Copy Markdown
Member Author

No code problems were found at 14688d4. The earlier eager-closure-budgets failure is fixed, and that annotation no longer appears in Coverage.

Both earlier threads are fixed and can be resolved: the daemon preauth refusal still carries RPC_PREAUTH_DEADLINE_EXCEEDED with dispatched no, and the proxy expiry still answers 408 with PROXY_RPC_PREAUTH_DEADLINE_EXCEEDED.

The remaining Coverage failures (wire-mutations.test.ts:354 and wire-compat.test.ts:58/78/114) and the Repo Guards failure at "Compare the daemon RPC wire surface against the last released tag" are likely unrelated to this PR. The Coverage failures name only declarations in artifact-download.ts, upload-client.ts, upload-http.ts and resumable-upload.ts, which this PR does not touch. They come from base change #5281 (f97e504).

Not verified here: the changed tests were not run locally, and their passing is only inferred from their absence in the Coverage failure annotations. "Check for production-unused exports" was also skipped because Repo Guards stopped earlier.

Before merge, the wire-compat and wire-mutations failures from #5281 need an ADR 0006 bump or a ledger acknowledgment on the base.

@thymikee
thymikee force-pushed the fix/rpc-ingress-preauth-budget branch from 14688d4 to db1dfe8 Compare December 1, 2026 09:20
@thymikee
thymikee force-pushed the fix/rpc-ingress-preauth-budget branch from db1dfe8 to d60d53b Compare December 1, 2026 09:46
@apex-sentinel-staging

This comment has been minimized.

@thymikee

thymikee commented Dec 2, 2026

Copy link
Copy Markdown
Member Author

At 08fb3a0, the timeout limit and tenant confinement assertions from the earlier review are fixed, but the live evidence still needs to cover the merge head. The existing inventory overlap and install receipt cover different observations, and the focused reports do not provide a combined listener receipt.

Before merge, attach merge-head evidence for native dispatch overlap through both listeners. Use isolated integration coverage to verify that each refusal comes from the intended listener, preserves its typed status and retriable and dispatch flags, and allows a fresh call after release. The current daemon fixture injects dispatch and the proxy fixture injects upstream transport. Listener admission

The five open inline threads can be resolved: preauthorization accounting covers the intended preauthorization count; response socket handling closes the connection; timeout length is fixed at head; late authorization is stopped before forwarding; and confinement assertions are fixed at head. No lower-priority threads still apply.

The latest GitHub check snapshot for 08fb3a0 reports 19 passed, with no pending or failed checks. No conflicts are known.

Not verified here: the reported device and focused test runs were not independently executed, and their raw logs were not supplied. Static tracing establishes the route from the shared HTTP client through the proxy, daemon runtime, and backend handoff.

@thymikee

thymikee commented Dec 2, 2026

Copy link
Copy Markdown
Member Author

GitHub reports 19 passing checks for 08fb3a0b6f77df19d8c1f97443145dd73f2c7dfc. Coverage and Integration Tests both checked out PR merge commit 3f86a0de28b0c10c6f72a698880b428f6ef7fc91 with that head. Coverage records execution in the daemon HTTP server, proxy and proxy refusal builder. Its changed-line report leaves the daemon’s preauthorization capacity response lines 331–332 uncovered. The integration job does not supply a combined refusal or native-dispatch receipt.

The two-listener inventory overlap remains attributed to 2c6b09defd. For the final candidate, use two ordinary native commands on independent authorized sessions and record that the second reaches native dispatch before the first settles. Use the actual listeners with a controlled auth mock for each timeout origin, and a fixture with a controlled occupied slot for the daemon 503 forwarded through the proxy. Assert HTTP status, reason, retry flag, dispatched: no, no dispatch for the refused request, and success of one fresh call after release. This leaves the production limits intact and needs no traffic-volume trial.

@thymikee

thymikee commented Dec 2, 2026

Copy link
Copy Markdown
Member Author

Partial follow-up to the listener and native-dispatch evidence request at da479265c938b8f712fd3091173041c2442db15a.

  • With one ordinary session_list call at a time, the production Node proxy listener, its default upstream HTTP transport, and the production daemon HTTP listener exercised both authentication timeout origins. A controlled hook and fake clock produced HTTP 408 with the same RPC ID, retriable: true, and dispatched: no for each origin. The proxy reason was PROXY_RPC_PREAUTH_DEADLINE_EXCEEDED and it made zero daemon calls. The daemon reason was RPC_PREAUTH_DEADLINE_EXCEEDED and it was forwarded by the proxy. The refused requests produced zero handler invocations. After releasing each hook, one fresh RPC returned HTTP 200 and invoked the fixture handler once.
  • The daemon preauthorization count has a fixed 128-slot limit and no small-capacity seam. The existing daemon fixture shows a healthy request below capacity and after disconnect. The existing proxy adapter fixture writes a synthetic 503 from a stub handler after a body cancellation. A daemon-origin RPC_PREAUTH_CAPACITY_EXCEEDED 503 through the proxy and recovery after a full pool remain unverified. Overlapping native dispatch through both listeners remains outstanding.
  • AGENT_DEVICE_REQUIRE_LOOPBACK_TESTS=1 pnpm exec vitest run --project unit-core packages/daemon/src/server/http/http-server-rpc-ingress-budget.test.ts passed 4/4. Local Git merge-tree checks found no conflicts with fix: bind upload ownership and shared staging capacity #5281 at 0cd48fbd30 or fix: bind artifact access to producing lease #5278 at a5c5a46b4c. Both lower branch heads were left untouched. Hosted checks on the new head are pending.

@apex-sentinel-staging

This comment has been minimized.

@thymikee
thymikee force-pushed the fix/rpc-ingress-preauth-budget branch from da47926 to 2eedab2 Compare December 2, 2026 16:54
@apex-sentinel-staging

Copy link
Copy Markdown

Apex Sentinel · ✅ Approve · review of 2eedab2

Since the review of da47926: 2 new · 0 still open · 0 no longer raised

2 finding(s): 2 minor.

🟡 Minor (2)

Where Finding
packages/proxy/src/daemon-proxy.ts:219 🆕 handleRpcRoute builds const signal = AbortSignal.any([request.signal, preauth.signal]) and passes it to `authenti…
packages/proxy/src/daemon-proxy.ts:234 🆕 handleRpcRoute re-sends an envelope refusal after the 408 already answered

Reviewed with this repository's own Sentinel configuration (7c6ef4a2ed46).

💬 Talking to Sentinel

In a finding's thread, reply with:

Command What it does
@apex-sentinel-staging note <context> Adds context; Sentinel re-checks the cited code
@apex-sentinel-staging pivot <why> Says the finding is wrong; Sentinel re-checks the cited code
@apex-sentinel-staging like Marks the finding as useful
@apex-sentinel-staging dislike <why> Marks it as not useful, so it is not raised again

On this conversation, comment @apex-sentinel-staging review to run a full review once again.

@apex-sentinel-staging apex-sentinel-staging Bot 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.

Apex Sentinel: 1 new finding on 2eedab2. The verdict and the full list are in the summary comment.

Comment on lines 234 to +244
const rpcBody = ingress.body;
// The checked route checks its envelope after authentication and the protocol: a caller on
// another protocol is told so before the proxy reads what its own protocol defines.
const envelopeRefusal =
route === DAEMON_HTTP_CHECKED_RPC_ROUTE ? null : refuseInvalidRpcEnvelope(rpcBody);
if (envelopeRefusal) return envelopeRefusal;
try {
return await handleDaemonRoute(request, route, options, instanceId, rpcBody);
} catch (error) {
return rpcErrorResponse(readJsonRpcId(rpcBody), error);
const caller = await authenticateCaller(request, options, rpcBody, signal);
signal.throwIfAborted();
clearTimeout(deadline);
deadline = undefined;
return await handleDaemonRoute(request, route, options, instanceId, rpcBody, caller);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🆕 🟡 Minor · handleRpcRoute re-sends an envelope refusal after the 408 already answered

Sequence: the RPC body arrives at t=4.9s, the preauth deadline fires at t=5s and writes the 408 (res.writableEnded true), then the stalled body read resolves read(). handleRpcRoute runs refuseInvalidRpcEnvelope(rpcBody), gets a refusal for a malformed body, and returns it as a Response with no write guard — signal.throwIfAborted() sits only on the success path below it. Every other answer path in this function gained an aborted-response guard in this PR, but the envelope-refusal path was missed; add the same guard before returning the refusal.

Details

Also raised here

Also raised at this location by persona maintainability: void reader.cancel().catch(() => {}) is applied to the two error paths that leave read() but not to the signal-ca… — void reader.cancel().catch(() => {}) is applied to the two error paths that leave read() but not to the signal-cancellation path just above it, which still awaits the cancel inside awaitWithSignal's cleanup. A stalled request body whose cancel() never settles therefore still wedges the cancellation path for the very reason those two lines were changed — the cancel is fire-and-forget only where handleRpcRoute's own deadline already races it. Make all three paths non-blocking, or drop the two voids so one rule covers the function. Same asymmetry as the await/void split in the two new tests around a hanging cancel().

persona: javascript, maintainability

Useful? Reply @apex-sentinel-staging like, or @apex-sentinel-staging dislike <why> if it is not.

Reply @apex-sentinel-staging note ... to add context, or @apex-sentinel-staging pivot ... if it is wrong — I will re-check the cited code.

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