Skip to content

fix(repeater): deliver empty response header values as empty strings - #835

Open
aborovsky wants to merge 1 commit into
nextfrom
fix/empty-response-header-values
Open

aborovsky wants to merge 1 commit into
nextfrom
fix/empty-response-header-values

Conversation

@aborovsky

@aborovsky aborovsky commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Problem

When a target answers with a header that has an empty value (expires:, cache-control:, pragma:), the repeater sent the header to bridges as { "expires: ": undefined }: the name kept the colon and the value was undefined. node-libcurl's parseHeaders splits on /:\s(.+)/, which needs at least one character after ": ".

The same parser also:

Target sends Parser returned
name:value (no space) key "name:value", value undefined
name: value " value "
repeated name (Link, Vary, ...) last value only (except Set-Cookie)
same name, different case one value lost after lowercasing
bytes 0x80-0xFF that are not valid UTF-8 invalid bytes became U+FFFD

Change

  • Enable CurlFeature.NoHeaderParsing and parse the raw header dump in the new CurlHeadersParser.
  • Split on the first :, trim optional whitespace, use '' for an empty value, collect repeated names into an array in order. Single-valued fields (content-type, content-length, location, ...) keep the last value as a string. Values are decoded as UTF-8 and fall back to latin1 only for invalid sequences.
  • Only the last header block is used (1xx and redirect blocks are skipped).
  • set-cookie stays an array even for a single field, as before.
  • HttpRequestExecutor.parseCurlHeaders is removed. The node-libcurl fork is unchanged.

Tests

  • CurlHeadersParser.spec: every case from the table above, obsolete line folding, multiple header blocks, bare LF, empty input.
  • HttpRequestExecutor.spec with real libcurl: an HTTP/1.1 raw-TCP target and an HTTP/2 TLS target that send empty expires, cache-control, pragma and two set-cookie. No value is undefined and no name contains : or whitespace. Both tests fail on the previous implementation.

@gitguardian

gitguardian Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

️✅ There are no secrets present in this pull request anymore.

If these secrets were true positive and are still valid, we highly recommend you to revoke them.
While these secrets were previously flagged, we no longer have a reference to the
specific commits where they were detected. Once a secret has been leaked into a git
repository, you should consider it compromised, even if it was deleted immediately.
Find here more information about risks.


🦉 GitGuardian detects secrets in your source code to help developers and security teams secure the modern development process. You are seeing this because you or someone else with access to this repository has authorized GitGuardian to scan your pull request.

@aborovsky aborovsky self-assigned this Nov 26, 2026
@aborovsky aborovsky added the Type: bug Something isn't working. label Nov 26, 2026
const result: ParsedHeaders = {};
for (const [name, values] of grouped) {
result[name] =
values.length === 1 && !CurlHeadersParser.ARRAY_HEADERS.has(name)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Repeated single-value fields now come out as arrays, and HttpRequestExecutor.parseContentType doesn't handle that.

If a target sends Content-Type twice (some proxies and frameworks do), headers['content-type'] is now ['text/html; charset=windows-1251', ...]. parseContentType casts it as string, and safeParse returns defaultContentType (type: '') for any non-string. As a result:

  • the charset is dropped and the body is decoded as UTF-8;
  • whitelistMimes no longer matches, so the body is capped at maxContentLength instead of maxBodySize and can be truncated or omitted.

Before this change the last value won and stayed a string. Bridges also see the contract change: location, content-length and content-type were always strings and can now be arrays.

Suggestion: keep singleton fields as one value (Node's http does this for content-type, content-length, location, etc.), or at least make parseContentType pick one value from an array (the last one, matching the old behavior). Either way, add a test with a duplicated Content-Type that carries a charset.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed, this was a real regression. Fixed in 3e679a0: fields that are single-valued by definition (content-type, content-length, location, expires, etag, server, ... the set Node's http collapses) stay one string. I kept the last value for a duplicate, not Node's first, to match what bridges received before this change. Fields where repeats are meaningful (link, vary, x-*) and set-cookie still come out as arrays, which is the point of the change. Tests: parser spec for duplicated content-type/content-length/location, plus an executor test with a duplicated Content-Type: ...; charset=windows-1251 and a cp1251 body (fails on the previous commit).


public static parse(raw: Buffer): ParsedHeaders {
// latin1 maps every byte 1:1, so obs-text (0x80-0xFF) survives untouched.
const lines = raw.toString('latin1').split(/\r?\n/);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

latin1 is lossless here, but the request side still encodes header strings as UTF-8 (buildCurlHeaders → HTTPHEADER, and node-libcurl converts JS strings to UTF-8). So values the engine echoes back no longer round-trip. For example, a UTF-8 Set-Cookie: n=é (bytes C3 A9) is now delivered as é and sent back in Cookie as C3 83 C2 A9. A UTF-8 Location or Content-Disposition filename also reaches bridges as mojibake. Before this change, valid UTF-8 was decoded correctly and only invalid bytes were lost.

Unless the engine re-encodes these values as latin1, consider decoding each line as strict UTF-8 and falling back to latin1 only when that fails (new TextDecoder('utf-8', { fatal: true }) in a try/catch). That keeps the old behavior for valid UTF-8 and still preserves 0x80-0xFF bytes that are not valid UTF-8. The obs-text test would then need a UTF-8 case next to the invalid-byte case.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed. The request side sends UTF-8 (HTTPHEADER), so latin1-only decoding would break round-trips of valid UTF-8 values. Fixed in 3e679a0: the buffer is still split as latin1 (lossless), then each value is decoded with a fatal TextDecoder('utf-8') and falls back to the latin1 string only when the bytes are not valid UTF-8. I did it per value rather than per line, so one bad header cannot change how the others are decoded. Tests: a UTF-8 case (set-cookie: n=é, a CJK filename) next to the existing invalid-bytes case.

@@ -0,0 +1,20 @@
// Self-signed ECDSA P-256 pair (CN=127.0.0.1, 100 years) for loopback TLS test
// servers. The executor disables peer verification, so trust is irrelevant.
export const TEST_TLS_KEY = `-----BEGIN PRIVATE KEY-----

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The GitGuardian check on this PR fails on this key ("1 secret uncovered"). Even though it is a throwaway test key, the check stays red, and every future scan will report it again.

Options:

  • generate the pair in beforeAll instead of committing it, e.g. execFileSync('openssl', ['req', '-x509', '-newkey', 'ec', '-pkeyopt', 'ec_paramgen_curve:P-256', '-nodes', '-subj', '/CN=127.0.0.1', '-days', '1', '-keyout', ..., '-out', ...]), or add a selfsigned devDependency;
  • or, if committing a fixture is preferred, exclude it explicitly (.gitguardian.yaml ignored path, or mark the incident as a test credential in the dashboard) so the check passes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed. Removed the committed key in 3e679a0; the HTTP/2 test now generates a 2048-bit RSA self-signed pair at run time. I did not use openssl (not guaranteed on Windows/dev machines) or selfsigned (adds ~780 lines to the lockfile through @peculiar/x509 and pkijs). node-forge was already in the tree via win-ca, so it is now a declared devDependency with @types/node-forge. The helper lives in the spec file so it is not part of the production build. Note: the key is still in the history of 5347dcc, so the GitGuardian check may stay red until the two commits are squashed; I will do that on your word, since it needs a force-push.

}).toEqual(expect.objectContaining({ withinBudget: true }));
}, 60000);

describe('response headers', () => {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Nested describe blocks in this spec are named after methods of the class under test (protocol, execute). response headers is a scenario label. Move the two its directly under describe('execute'); their titles already describe the scenario. The expectWellFormed helper can move up into that scope.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed, response headers is a scenario label and breaks the rule that nested describes are method names. Both tests now sit directly under describe('execute') with expectWellFormed moved up into that scope (3e679a0).

@aborovsky
aborovsky force-pushed the fix/empty-response-header-values branch from 3e679a0 to 9a31953 Compare November 26, 2026 11:59
node-libcurl's header parser splits on /:\s(.+)/, so a target field with an
empty value (`expires:`) came out as the key "expires: " with an undefined
value. The same parser also kept the raw padding of `name:   value  `, split
`name:value` incorrectly, kept only the last of repeated fields and replaced
invalid UTF-8 bytes with U+FFFD.

Read the raw header dump (CurlFeature.NoHeaderParsing) and parse it in
CurlHeadersParser: split on the first colon, trim optional whitespace, use ''
for an empty value, collect repeated names in order and keep only the last
header block (1xx and redirects). Single-valued fields (content-type,
content-length, location, ...) stay a string holding the last value, so
charset detection keeps working. Values are decoded as UTF-8 and fall back to
latin1 only for invalid sequences, so they round-trip with the UTF-8 request
side.
@aborovsky
aborovsky force-pushed the fix/empty-response-header-values branch from 9a31953 to 882eb72 Compare November 26, 2026 12:02

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Type: bug Something isn't working.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant