Skip to content

fix(canvas): serve Content-Length on Canvas document HEAD responses - #117964

Merged
vincentkoc merged 3 commits into
openclaw:mainfrom
zenglingbiao:fix/canvas-document-head-content-length
Aug 2, 2026
Merged

vincentkoc merged 3 commits into
openclaw:mainfrom
zenglingbiao:fix/canvas-document-head-content-length

Conversation

@zenglingbiao

@zenglingbiao zenglingbiao commented Aug 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The gateway's managed Canvas document endpoint (src/canvas/serve.runtime.ts) accepts HEAD requests but had no HEAD branch: it called res.end(opened.data) unconditionally, so HEAD responses never carried a Content-Length header.

What Problem This Solves

RFC 9110 recommends sending the same header fields for HEAD as for the corresponding GET when they are available. It permits Content-Length on HEAD only when the value equals the octet count a corresponding GET would send. Node's HTTP server suppresses the response body for HEAD automatically, but it does not synthesize that representation length.

  • handleCanvasDocumentHttpRequest allows GET and HEAD (src/canvas/serve.runtime.ts:57), but both methods fell through to res.end(opened.data).
  • For GET, Node derives Content-Length from the ended buffer; for HEAD the body is dropped and no length header is ever set.
  • Clients preflighting /__openclaw__/canvas/documents/<id>/index.html (asset probes, caches, download managers, curl -I) cannot learn the payload size.

代码证据:before the fix, HEAD /__openclaw__/canvas/documents/widget-1/index.html returned 200 with Content-Security-Policy but no Content-Length, while GET returned Content-Length: 32.

This is the same defect class fixed for control-ui static assets in #117049; here it is simpler because the representation bytes are already fully buffered (opened.data), so the length is exact for both HTML and static assets — no injection math required.

Root Cause

The handler computed the full representation in memory but only let Node's implicit res.end(buf) behavior produce Content-Length. That implicit path never runs for HEAD (body suppressed), and no explicit Content-Length header was set. The violated invariant: every 200 response on this endpoint should describe its representation metadata regardless of method.

Why This Fix

  • Set Content-Length: opened.data.byteLength once, before the method branches — it is exact for both GET and HEAD, for HTML and static assets alike (the HTML is served unmodified, unlike the A2UI host).
  • Add explicit HEAD branches that end the response without a body after the full header set (Content-Type, CSP sandbox header, Cache-Control, Content-Length) is in place, so HEAD mirrors GET's headers exactly.
  • Alternative considered: only set Content-Length inside the HEAD branches — rejected because making the header explicit for GET too removes the reliance on Node's implicit behavior and keeps both methods on one code path.

User Impact

  • Any client issuing HEAD against a managed Canvas document URL now receives an accurate Content-Length (plus the same CSP header as GET).
  • No behavior change for GET responses.

Evidence

  • Before: loopback server on main — HEAD .../index.html → Content-Length=null while GET served 32 bytes; HEAD .../style.css → Content-Length=null while GET served 20 bytes → FAIL.
  • After: same loopback on this branch — HEAD returns Content-Length: 32 / Content-Length: 20 matching the GET bodies, CSP preserved → PASS.

Real Behavior Proof

Before (on main)

$ node --import tsx proof.mjs
/__openclaw__/canvas/documents/widget-1/index.html
  GET  status=200 Content-Length=32 bodyBytes=32
  HEAD status=200 Content-Length=null CSP=sandbox allow-scripts bodyBytes=0
  FAIL: HEAD Content-Length (null) !== GET body bytes (32)
/__openclaw__/canvas/documents/widget-1/style.css
  GET  status=200 Content-Length=20 bodyBytes=20
  HEAD status=200 Content-Length=null CSP=null bodyBytes=0
  FAIL: HEAD Content-Length (null) !== GET body bytes (20)
RESULT: FAIL

After (with fix)

$ node --import tsx proof.mjs
/__openclaw__/canvas/documents/widget-1/index.html
  GET  status=200 Content-Length=32 bodyBytes=32
  HEAD status=200 Content-Length=32 CSP=sandbox allow-scripts bodyBytes=0
  PASS: HEAD Content-Length matches GET body bytes
/__openclaw__/canvas/documents/widget-1/style.css
  GET  status=200 Content-Length=20 bodyBytes=20
  HEAD status=200 Content-Length=20 CSP=null bodyBytes=0
  PASS: HEAD Content-Length matches GET body bytes
RESULT: PASS

(Loopback http.createServer + real handleCanvasDocumentHttpRequest against a temp OPENCLAW_STATE_DIR with a managed Canvas document; Node suppresses the HEAD body itself, so bodyBytes=0 on HEAD is expected — the header is what matters.)

Tests and Validation

@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. P2 Normal backlog priority with limited blast radius. merge-risk: 🚨 other 🚨 Merging this PR has meaningful risk outside the owned taxonomy. labels Aug 2, 2026
@clawsweeper

clawsweeper Bot commented Aug 2, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed August 2, 2026, 7:20 AM ET / 11:20 UTC.

ClawSweeper review

What this changes

The PR adds explicit representation-length metadata and bodyless HEAD handling for managed Canvas HTML documents and static document assets, with focused regression coverage.

Merge readiness

⚠️ Ready for maintainer review - 1 item remains

This PR remains necessary: current main accepts managed Canvas HEAD requests but does not set Content-Length. The updated branch fixes that at the response owner, incorporates the prior review’s invalid-UTF-8 correction, and has no remaining actionable patch finding.

Priority: P2
Reviewed head: e1dcbd25b6547886e3978ebaba2fc6760d2a182e

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused, owner-boundary fix with convincing real-handler evidence and regression coverage; no correctness blocker remains.
Proof confidence 🦞 diamond lobster (5/6) Sufficient (terminal): The PR body includes redaction-safe before-and-after terminal output from a loopback server using the real Canvas handler for both HTML and CSS, and the updated focused test covers the invalid-UTF-8 HTML representation edge case.
Patch quality 🦞 diamond lobster (5/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Verified Sufficient (terminal): The PR body includes redaction-safe before-and-after terminal output from a loopback server using the real Canvas handler for both HTML and CSS, and the updated focused test covers the invalid-UTF-8 HTML representation edge case.
Evidence reviewed 5 items Current-main defect: Current main explicitly permits GET and HEAD but, after selecting the representation, only calls res.end(...); it never sets Content-Length for either HTML or binary Canvas documents.
Candidate representation ownership: The PR measures the decoded UTF-8 HTML string before sending it, measures static assets from their raw byte buffer, and ends HEAD responses only after their matching metadata is installed.
Prior-review correction is present: The follow-up commit adds an invalid-UTF-8 copied HTML case, proving the header follows the decoded representation actually returned by GET rather than the raw file length.
Findings None None.
Security None None.

How this fits together

Managed Canvas documents are stored locally and reached through the gateway’s authenticated Canvas-document request stage. The Canvas response handler selects either decoded HTML or raw asset bytes, then sends HTTP metadata and the response to the client.

flowchart LR
  A[Managed Canvas document] --> B[Gateway Canvas request stage]
  B --> C[Canvas document handler]
  C --> D{Representation type}
  D -->|HTML| E[Decoded UTF-8 body]
  D -->|Static asset| F[Raw file bytes]
  E --> G[GET or HEAD metadata]
  F --> G
  G --> H[HTTP client]
Loading

Before merge

  • Complete next step (P2) - No mechanical repair is needed: the earlier review blocker was fixed in the current head, and the remaining action is ordinary maintainer merge review.
Agent review details

Security

None.

PR surface

Source +13, Tests +53. Total +66 across 2 files.

View PR surface stats
Area Files Added Removed Net
Source 1 14 1 +13
Tests 1 55 2 +53
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 69 3 +66

Review metrics

None.

Stored data model

Persistent data-model change detected: serialized state: src/canvas/serve.runtime.test.ts. Migration or upgrade compatibility proof is recorded; maintainers should verify it before merge.

Technical review

Best possible solution:

Merge the representation-specific calculation: use the decoded HTML string length for HTML and raw byte length for static assets, while keeping Canvas storage, authentication, and gateway routing unchanged.

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

Yes. Current main permits HEAD at src/canvas/serve.runtime.ts:57 but sends the selected representation without explicitly setting Content-Length; the PR supplies before/after loopback output through the real handler and a focused regression test.

Is this the best way to solve the issue?

Yes. The final response owner is the narrowest durable place to derive representation metadata: decoded UTF-8 length for HTML and raw buffer length for assets. The prior concern about invalid UTF-8 is addressed by the added regression case.

AGENTS.md: found and applied where relevant.

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

Labels

Label changes:

  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🦞 diamond lobster and patch quality is 🦞 diamond lobster.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (terminal): The PR body includes redaction-safe before-and-after terminal output from a loopback server using the real Canvas handler for both HTML and CSS, and the updated focused test covers the invalid-UTF-8 HTML representation edge case.
  • remove rating: 🦐 gold shrimp: Current PR rating is rating: 🐚 platinum hermit, so this older rating label is no longer current.
  • remove status: ⏳ waiting on author: Current PR status label is status: 👀 ready for maintainer look.
  • remove merge-risk: 🚨 other: Current PR review selected no merge-risk labels.

Label justifications:

  • P2: This repairs missing HTTP representation metadata for Canvas document probes, with limited blast radius outside the Canvas document endpoint.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🦞 diamond lobster and patch quality is 🦞 diamond lobster.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (terminal): The PR body includes redaction-safe before-and-after terminal output from a loopback server using the real Canvas handler for both HTML and CSS, and the updated focused test covers the invalid-UTF-8 HTML representation edge case.
  • proof: sufficient: Contributor real behavior proof is sufficient. The PR body includes redaction-safe before-and-after terminal output from a loopback server using the real Canvas handler for both HTML and CSS, and the updated focused test covers the invalid-UTF-8 HTML representation edge case.

Evidence

What I checked:

  • Current-main defect: Current main explicitly permits GET and HEAD but, after selecting the representation, only calls res.end(...); it never sets Content-Length for either HTML or binary Canvas documents. (src/canvas/serve.runtime.ts:57, 247278262234)
  • Candidate representation ownership: The PR measures the decoded UTF-8 HTML string before sending it, measures static assets from their raw byte buffer, and ends HEAD responses only after their matching metadata is installed. (src/canvas/serve.runtime.ts:82, e1dcbd25b654)
  • Prior-review correction is present: The follow-up commit adds an invalid-UTF-8 copied HTML case, proving the header follows the decoded representation actually returned by GET rather than the raw file length. (src/canvas/serve.runtime.test.ts:84, e1dcbd25b654)
  • Gateway caller and owner boundary: The gateway routes authenticated Canvas-document paths to this handler after checking that the core Canvas host is enabled, so the response handler is the correct owner for representation metadata. (src/gateway/server-http.ts:597, 247278262234)
  • Feature provenance and release scope: Blame attributes the original Canvas HTTP handler, including its HEAD acceptance and missing length calculation, to the Canvas feature introduction. The latest release tag does not contain this new handler path, so this is a current-main Canvas behavior rather than a shipped-upgrade compatibility change. (src/canvas/serve.runtime.ts:49, 8fb9dcd54e07)

Likely related people:

  • Shakker: Git blame attributes the complete Canvas document HTTP handler, including its GET/HEAD dispatch and representation response path, to the feature-introduction commit. (role: introduced current Canvas HTTP behavior; confidence: high; commits: 8fb9dcd54e07; files: src/canvas/serve.runtime.ts, src/canvas/documents.ts)

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 (1 earlier review cycle)
  • reviewed 2026-08-02T10:52:38.622Z sha e7e6004 :: needs changes before merge. :: [P2] Measure the decoded HTML representation before setting its length

@clawsweeper clawsweeper Bot added 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. and removed rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. merge-risk: 🚨 other 🚨 Merging this PR has meaningful risk outside the owned taxonomy. labels Aug 2, 2026
@vincentkoc vincentkoc self-assigned this Aug 2, 2026
@vincentkoc
vincentkoc merged commit 568bfd0 into openclaw:main Aug 2, 2026
132 of 140 checks passed
@vincentkoc

Copy link
Copy Markdown
Member

Merged via squash.

vincentkoc added a commit that referenced this pull request Aug 2, 2026
* origin/main:
  fix(sqlite): skip slow-lock diagnostic when busyTimeoutMs is zero (#115990)
  fix(canvas): serve Content-Length on Canvas document HEAD responses (#117964)
  fix(workboard): diagnose archived cards still in an active status (#116359) (#117290)
  feat(diagnostics): configure OTEL metric name prefixes (#116687)
  fix(ui): respect UTC when filtering pending usage sessions (#118004)
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Aug 3, 2026
…penclaw#117964)

* fix(canvas): serve Content-Length on Canvas document HEAD responses

* fix(canvas): measure decoded HTML representation for Content-Length
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Aug 3, 2026
* origin/main:
  fix(sqlite): skip slow-lock diagnostic when busyTimeoutMs is zero (openclaw#115990)
  fix(canvas): serve Content-Length on Canvas document HEAD responses (openclaw#117964)
  fix(workboard): diagnose archived cards still in an active status (openclaw#116359) (openclaw#117290)
  feat(diagnostics): configure OTEL metric name prefixes (openclaw#116687)
  fix(ui): respect UTC when filtering pending usage sessions (openclaw#118004)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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. size: S 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.

2 participants