Repository navigation
Conversation
️✅ 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. 🦉 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. |
| const result: ParsedHeaders = {}; | ||
| for (const [name, values] of grouped) { | ||
| result[name] = | ||
| values.length === 1 && !CurlHeadersParser.ARRAY_HEADERS.has(name) |
There was a problem hiding this comment.
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;
whitelistMimesno longer matches, so the body is capped atmaxContentLengthinstead ofmaxBodySizeand 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.
There was a problem hiding this comment.
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/); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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----- | |||
There was a problem hiding this comment.
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
beforeAllinstead 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 aselfsigneddevDependency; - or, if committing a fixture is preferred, exclude it explicitly (
.gitguardian.yamlignored path, or mark the incident as a test credential in the dashboard) so the check passes.
There was a problem hiding this comment.
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', () => { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
3e679a0 to
9a31953
Compare
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.
9a31953 to
882eb72
Compare
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 wasundefined. node-libcurl'sparseHeaderssplits on/:\s(.+)/, which needs at least one character after": ".The same parser also:
name:value(no space)"name:value", valueundefinedname: value" value "Link,Vary, ...)Set-Cookie)Change
CurlFeature.NoHeaderParsingand parse the raw header dump in the newCurlHeadersParser.:, 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.set-cookiestays an array even for a single field, as before.HttpRequestExecutor.parseCurlHeadersis 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.specwith real libcurl: an HTTP/1.1 raw-TCP target and an HTTP/2 TLS target that send emptyexpires,cache-control,pragmaand twoset-cookie. No value isundefinedand no name contains:or whitespace. Both tests fail on the previous implementation.