fix(webview): relay the request Content-Length so length-only dashboards get the body

The proxy hands undici the request body as a stream and dropped the browser's
content-length, assuming undici recomputes it. For a stream it cannot: undici
frames it as Transfer-Encoding: chunked. A dashboard that reads only
Content-Length (Python's http.server, many small Go/C servers) then saw an
empty body on every POST through a web tab. Found on a WoL dashboard whose
Shut down posted {"confirm":true} and was refused for a missing flag on every
click through Codeman, while curl straight at it worked.

buildUpstreamRequestHeaders now puts the browser's content-length back when a
body goes upstream (relayBodyLength), it is a plain number, and no
transfer-encoding contradicts it. GET/HEAD never carry it. undici 6.28 frames
the stream with that length and fails cleanly on a mismatch. Side effect:
a DELETE/OPTIONS with content-length 0 now goes upstream with neither header
nor body instead of an empty chunked body.

test/webview-proxy-body-framing.test.ts pins it end to end against a
Content-Length-only upstream; it fails on the old code with
"expected 'chunked' to be null".

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Codeman maintainer committed 2026-10-21 08:34:14 +02:00
1 parent 39077281d1
commit 2b9aa055cd
5 files changed
+201 -10

No files matched your search

+30 -2
View File
@@ -247,17 +247,45 @@ describe('buildUpstreamRequestHeaders', () => {
expect(headers.referer).toBe('http://127.0.0.1:4000/panel');
});
it('drops hop-by-hop and recomputed headers', () => {
it('drops hop-by-hop headers, and a length that a transfer-encoding contradicts', () => {
const headers = buildUpstreamRequestHeaders(
{ host: 'codeman.local', connection: 'keep-alive', 'transfer-encoding': 'chunked', 'content-length': '5' },
upstream,
{ forwardCookies: false, sessionCookieName: 'codeman_session' }
{ forwardCookies: false, sessionCookieName: 'codeman_session', relayBodyLength: true }
);
expect(headers.host).toBeUndefined();
expect(headers.connection).toBeUndefined();
expect(headers['transfer-encoding']).toBeUndefined();
expect(headers['content-length']).toBeUndefined();
});
// A streamed body without a length goes upstream chunked, and a dashboard that
// reads only Content-Length (Python's http.server) then sees an empty body.
it('relays the browser content-length alongside a body', () => {
const headers = buildUpstreamRequestHeaders(
{ 'content-length': '16', 'content-type': 'application/json' },
upstream,
{ forwardCookies: false, sessionCookieName: 'codeman_session', relayBodyLength: true }
);
expect(headers['content-length']).toBe('16');
});
it('never relays a content-length without a body, or one that is not a plain number', () => {
const opts = { forwardCookies: false, sessionCookieName: 'codeman_session' };
expect(buildUpstreamRequestHeaders({ 'content-length': '16' }, upstream, opts)['content-length']).toBeUndefined();
expect(
buildUpstreamRequestHeaders({ 'content-length': '16' }, upstream, { ...opts, relayBodyLength: false })[
'content-length'
]
).toBeUndefined();
for (const bad of ['-1', '16, 16', ' 16', '0x10', '']) {
expect(
buildUpstreamRequestHeaders({ 'content-length': bad }, upstream, { ...opts, relayBodyLength: true })[
'content-length'
]
).toBeUndefined();
}
});
});
describe('buildDownstreamResponseHeaders', () => {