Skip to content

fix(gateway): handle settled HTTP request authority failures - #157501

Merged
steipete merged 1 commit into
mainfrom
fix/gateway-http-authority-outcomes
Sep 26, 2026
Merged

steipete merged 1 commit into
mainfrom
fix/gateway-http-authority-outcomes

Conversation

@steipete

@steipete steipete commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Maintainer decision: reviewed and approved for landing by @steipete. Touches Gateway HTTP authorization outcome handling; no policy change. Rebased onto current main (conflict-free, diff unchanged); exact-head CI green on f12f7df.

What Problem This Solves

Closing a Control UI tab while plugin icon requests were still in flight made the Gateway log, once per request, with a stack trace:

[gateway-http] unhandled error in request handler: Error: HTTP request authority expired
    at Object.assertCurrent (http-auth-utils…)
    at handlePluginIconHttpRequest (plugin-icon-http…)
    at handleRequest (server-start…)

Observed live on main (d36ba5479c3) with an isolated dev Gateway: five such entries at the moment the browser disconnected. These are expected lifecycle outcomes, not failures, and the generic catch also tried to finish the already-closed response. The same path mishandled two siblings:

  • after assertCurrent already sent a 401 for lapsed client authority, it threw a plain Error("Unauthorized") that was again logged as unhandled;
  • a GatewayOperatorAccessDeniedError raised mid-request reached the generic catch and became a 500 instead of an operator-access denial.

Why This Change Was Made

bindHttpResponseAuthority threw plain errors for outcomes whose response was already settled, and the request owners (server-http.ts and the plugin route handler) could only treat them as unexpected. This change:

  • adds one GatewayHttpRequestAuthorityError ("authority ended; the response is already answered or closed") and uses it at every such throw site, replacing the duplicate string-message errors in http-auth-utils, http-auth-plugin-cookie, and plugin route scope checks;
  • lets both request owners consume that error and GatewayOperatorAccessDeniedError through one finishGatewayHttpAuthorityError: nothing is written to a closed response, operator denial gets the existing auth-failure response when headers are unsent, and a partial response is closed;
  • makes sendUnauthorized itself own "headers already sent" and cookie removal;
  • routes plugin route runtime scopes through the same bindHttpResponseAuthority check instead of a parallel inline check.

Genuine exceptions still log unhandled error in request handler and get the normal failure response.

User Impact

No more alarming stack traces in Gateway logs when a browser tab closes, one clean 401 when client authority lapses mid-request, and a proper denial instead of a 500 when operator access is revoked mid-request.

Evidence

  • New real-request boundary suite src/gateway/server-http.authority-outcomes.test.ts (TCP requests against the real handler): client disconnect (no unhandled log, no write), lapsed authority (exactly one 401), operator denial (denial response when unsent, closed partial response), and a genuine thrown error (still logged with 500). The regression cases fail on current main.
  • node scripts/run-vitest.mjs across 10 Gateway HTTP suites (authority outcomes, plugin routes and runtime scopes, auth utils, plugin icon, user-profile avatars, lazy Control UI, plugin auth cookie, http-common): 239 passed.
  • Focused lint, oxfmt --check, and git diff --check pass. The repo-wide Knip/storage scans could not finish locally under extreme host load; CI covers them.
  • Codex autoreview: scoped-clean ("patch is correct"), including a check that the plugin route refactor does not weaken authorization.

Production LOC: +73 / −43 (net +30): one error class and the shared finisher replace three plain-error throw sites and the plugin route's parallel authority check. Tests: +229 / −22.

@clawsweeper

clawsweeper Bot commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime size: M labels Sep 24, 2026
@steipete
steipete marked this pull request as ready for review September 24, 2026 19:01
@github-actions

github-actions Bot commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

⚠️ Security sensitive changes

This maintainer PR changes sensitive security components. This comment is informational because the PR author has repository Maintain or Admin access.

  • Current SHA: f12f7df7d786375da652bcbfa50b3390596338c2
  • Maintainer: @steipete
  • Repository role: admin

These security sensitive changes were made:

  • src/gateway/http-auth-plugin-cookie.ts: Controls Gateway authentication, pairing, or caller permissions. Check identity validation, scope checks, and revocation.
  • src/gateway/http-auth-utils.ts: Controls Gateway authentication, pairing, or caller permissions. Check identity validation, scope checks, and revocation.
  • src/gateway/http-request-authority.ts: Controls Gateway authentication, pairing, or caller permissions. Check identity validation, scope checks, and revocation.
  • src/gateway/server-http.ts: Controls Gateway authentication, pairing, or caller permissions. Check identity validation, scope checks, and revocation.

Carefully review these changes before merging.

@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. labels Sep 24, 2026
@clawsweeper

clawsweeper Bot commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed September 25, 2026, 10:21 PM ET / September 26, 2026, 02:21 UTC (Revision 3).

ClawSweeper review

What this changes

Gateway HTTP handlers classify expired or revoked request authority so core and plugin routes can send a denial or close the response without logging an expected failure.

Merge readiness

✅ Ready for maintainer review

Keep open. Current main still sends settled authority failures through generic error handling, and the proposed fix remains useful. The introduced patch has no supported blocking correctness finding.

Priority: P2
Reviewed head: f12f7df7d786375da652bcbfa50b3390596338c2

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) Focused production-boundary checks and a compact shared repair support a normal mergeable-quality rating.
Proof confidence 🐚 platinum hermit (4/6) Sufficient (terminal): The production Gateway HTTP server and plugin request handler are exercised through a real TCP client with injected disconnect, expiry, and revocation outcomes; the reported passing suite observes 401/403 responses, partial-connection closure, and preserved unexpected-error diagnostics. The fixture does not establish every full authentication-admission path. No stored-data contract changes.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Verified Sufficient (terminal): The production Gateway HTTP server and plugin request handler are exercised through a real TCP client with injected disconnect, expiry, and revocation outcomes; the reported passing suite observes 401/403 responses, partial-connection closure, and preserved unexpected-error diagnostics. The fixture does not establish every full authentication-admission path. No stored-data contract changes.
Evidence reviewed 7 items Current-main behavior: The current-main core catch logs every thrown request error as unhandled and finishes it as a generic failure; it has no settled-authority branch.
Introduced outcome handling: The introduced finisher distinguishes operator denial from a response whose authority has already ended, while preserving the ordinary error path for other exceptions.
Plugin boundary: Plugin routes check live client and operator authority before invoking the next matched route; the changed HTTP path uses the shared response-authority check.
Findings None None.
Security None None.

How this fits together

The Gateway receives HTTP requests from the Control UI and plugin routes, checks their authority, and passes them to route handlers. The response owner decides whether a request can return data, needs a denial, or must close a partial connection.

flowchart LR
 A[HTTP client] --> B[Gateway router]
 B --> C[Live authority check]
 C --> D[Core or plugin handler]
 C --> E[Response finisher]
 D --> E
 E --> F[Response or closed connection]
Loading

Before merge

None.

Agent review details

Security

None.

PR surface

Source +30, Tests +207. Total +237 across 8 files.

View PR surface stats
Area Files Added Removed Net
Source 6 73 43 +30
Tests 2 229 22 +207
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 8 302 65 +237

Review metrics

Metric Value Why it matters
Production versus test growth production +73/−43 (net +30); tests +229/−22 (net +207) The PR explains the production growth as a shared typed outcome and response handling, while the larger test change exercises HTTP request outcomes.

Technical review

Best possible solution:

Use one Gateway-owned authority outcome contract across core and plugin HTTP requests, with live checks before data is returned and unexpected failures retaining diagnostics.

Do we have a high-confidence way to reproduce the issue?

Yes. The reported live failure and current-main generic catch establish a clear path, though this read-only review did not rerun it.

Is this the best way to solve the issue?

Yes. A typed settled-authority outcome handled by the two HTTP request owners is a narrow repair that retains unexpected-error logging.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against a410b4d0fb53.

Labels

Label changes:

No label changes.

Label justifications:

  • P2: This addresses noisy Gateway failures and incorrect HTTP denial outcomes with limited user-facing blast radius.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (terminal): The production Gateway HTTP server and plugin request handler are exercised through a real TCP client with injected disconnect, expiry, and revocation outcomes; the reported passing suite observes 401/403 responses, partial-connection closure, and preserved unexpected-error diagnostics. The fixture does not establish every full authentication-admission path. No stored-data contract changes.
  • proof: sufficient: Contributor real behavior proof is sufficient. The production Gateway HTTP server and plugin request handler are exercised through a real TCP client with injected disconnect, expiry, and revocation outcomes; the reported passing suite observes 401/403 responses, partial-connection closure, and preserved unexpected-error diagnostics. The fixture does not establish every full authentication-admission path. No stored-data contract changes.

Evidence

What I checked:

Likely related people:

  • steipete: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)
  • Shakker: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

Workflow

  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

History

Review history (2 earlier review cycles)
  • reviewed 2026-09-24T19:09:39.465Z sha e76b201 :: needs maintainer review before merge. :: none
  • reviewed 2026-09-26T00:16:34.089Z sha f12f7df :: needs maintainer review before merge. :: none

@steipete steipete self-assigned this Sep 25, 2026
Keep normal disconnects and HTTP authority expiry out of unhandled-error
logs. Use one error class for requests whose response is already answered
or closed. Reuse operator denial and its existing 403 response helper;
close partial responses while preserving diagnostics for unexpected errors.

Share response-authority checks across cookie and plugin routes. Preserve
absence of client authority on plugin-auth routes: downstream consumers
use callback presence as source evidence. Both HTTP request owners consume
the same two-class error contract. The failure finisher keeps its interface.

Production: +73/-43 (net +30). Tests: +229/-22 (net +207).
The remaining production growth supplies the typed terminal outcome,
shared request-owner handling, and safe closed/partial-response settlement.
The real TCP cases share one server and took 316ms in the final run.

Validation: 239 tests passed across 10 Gateway HTTP suites:
node scripts/run-vitest.mjs \
  src/gateway/server-http.authority-outcomes.test.ts \
  src/gateway/server/plugins-http.runtime-scopes.test.ts \
  src/gateway/http-auth-utils.paired-device.test.ts \
  src/gateway/http-auth-utils.test.ts \
  src/gateway/plugin-icon-http.test.ts \
  src/gateway/server/plugins-http.test.ts \
  src/gateway/user-profiles-http.gravatar.test.ts \
  src/gateway/server-http.control-ui-lazy.test.ts \
  src/gateway/control-ui-plugin-auth-cookie.test.ts \
  src/gateway/http-common.test.ts

The first CI-mode attempt exhausted the 900s no-output watchdog before
any test verdict, after 822s of worker preparation. Removed the launcher's unnecessary CI flag, prepared
and verified artifacts through createVitestWorkerRun, then reran with the
normal local cache policy and unchanged test limits. Final wrapper: 1108.93s.
All execution used isolated test state and free ports.

Typed lint reported only the test file's line limit; removing duplicated
logger assertions fixed it. The repository focused lint path passed with
0 warnings/errors, as did formatting and git diff --check. Independent
review was clean at the configured P0 threshold. An unscoped lint attempt
timed out preparing SDK declarations at 300s; that broad check remains
inconclusive. Repo-wide Knip/storage scans were intentionally not rerun.

Follow-up: audit the broad completed-response catch in
src/gateway/http-endpoint-helpers.ts separately.
@steipete
steipete force-pushed the fix/gateway-http-authority-outcomes branch from e76b201 to f12f7df Compare September 26, 2026 00:09
@steipete
steipete merged commit 76e32e9 into main Sep 26, 2026
196 checks passed
@steipete
steipete deleted the fix/gateway-http-authority-outcomes branch September 26, 2026 02:32
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Sep 26, 2026
…w#157501)

Keep normal disconnects and HTTP authority expiry out of unhandled-error
logs. Use one error class for requests whose response is already answered
or closed. Reuse operator denial and its existing 403 response helper;
close partial responses while preserving diagnostics for unexpected errors.

Share response-authority checks across cookie and plugin routes. Preserve
absence of client authority on plugin-auth routes: downstream consumers
use callback presence as source evidence. Both HTTP request owners consume
the same two-class error contract. The failure finisher keeps its interface.

Production: +73/-43 (net +30). Tests: +229/-22 (net +207).
The remaining production growth supplies the typed terminal outcome,
shared request-owner handling, and safe closed/partial-response settlement.
The real TCP cases share one server and took 316ms in the final run.

Validation: 239 tests passed across 10 Gateway HTTP suites:
node scripts/run-vitest.mjs \
  src/gateway/server-http.authority-outcomes.test.ts \
  src/gateway/server/plugins-http.runtime-scopes.test.ts \
  src/gateway/http-auth-utils.paired-device.test.ts \
  src/gateway/http-auth-utils.test.ts \
  src/gateway/plugin-icon-http.test.ts \
  src/gateway/server/plugins-http.test.ts \
  src/gateway/user-profiles-http.gravatar.test.ts \
  src/gateway/server-http.control-ui-lazy.test.ts \
  src/gateway/control-ui-plugin-auth-cookie.test.ts \
  src/gateway/http-common.test.ts

The first CI-mode attempt exhausted the 900s no-output watchdog before
any test verdict, after 822s of worker preparation. Removed the launcher's unnecessary CI flag, prepared
and verified artifacts through createVitestWorkerRun, then reran with the
normal local cache policy and unchanged test limits. Final wrapper: 1108.93s.
All execution used isolated test state and free ports.

Typed lint reported only the test file's line limit; removing duplicated
logger assertions fixed it. The repository focused lint path passed with
0 warnings/errors, as did formatting and git diff --check. Independent
review was clean at the configured P0 threshold. An unscoped lint attempt
timed out preparing SDK declarations at 300s; that broad check remains
inconclusive. Repo-wide Knip/storage scans were intentionally not rerun.

Follow-up: audit the broad completed-response catch in
src/gateway/http-endpoint-helpers.ts separately.

Co-authored-by: Peter Steinberger <[email protected]>
steipete added a commit that referenced this pull request Sep 26, 2026
Discard abandoned representation headers before sending a complete 401 or
operator-denial 403 response. Share the existing 500 preparation and terminal
response handling, preserving security/CORS headers and cookie policy.

Extend the existing core/plugin TCP cases with staged response metadata and
retain the admitted runtime-scope cancellation signal assertion.

Related: #157501
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Sep 27, 2026
Discard abandoned representation headers before sending a complete 401 or
operator-denial 403 response. Share the existing 500 preparation and terminal
response handling, preserving security/CORS headers and cookie policy.

Extend the existing core/plugin TCP cases with staged response metadata and
retain the admitted runtime-scope cancellation signal assertion.

Related: openclaw#157501
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gateway Gateway runtime P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. security-sensitive-changed size: M status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant