Repository navigation
Fix res.links with empty values, res.append with an empty value, res.redirect for non-standard statuses, and quoted ";" in acceptParams - #70
Merged
Conversation
…redirect for non-standard statuses, and quoted ";" in acceptParams - res.links collects the link values first and leaves the Link header alone when there are none (an empty object, or relations given empty arrays), so no empty Link header and no trailing ", " (upstream PRs expressjs#7320, expressjs#7578). - res.append keeps an existing empty header value, or a number 0 set with res.setHeader, instead of replacing it (upstream PR expressjs#7332). - res.redirect names a status without a standard message by its number in the text and HTML bodies, as res.sendStatus does, instead of "undefined" (upstream PRs expressjs#7045, expressjs#7407). - acceptParams ends a parameter at the next ";" outside a quoted string, with backslash quoted-pairs, so a quoted ";" or q stays in its value (upstream PR expressjs#7479, rewritten: its escape check fails on an escaped backslash). Regression tests, edge-case tests, a property test against Express 5.3.0, differential scenarios for each fix, and the ROADMAP, History and docs.
# Conflicts: # ROADMAP.md # docs/fork/differences.md
Coverage Report for CI Build 45022718148Coverage remained the same at 100.0%Details
Uncovered ChangesNo uncovered changes found. Coverage RegressionsNo coverage regressions found. Coverage Stats
💛 - Coveralls |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Four bug-fix rows from ROADMAP.md, section "Bug fixes on the compatible line", all in
src/response.tsandacceptParamsinsrc/utils.ts:res.linkswith empty values (fix(res.links): skip empty-array rel values to prevent trailing comma in Link header expressjs/express#7320, fix(res.links): ignore empty object to prevent header corruption expressjs/express#7578)res.appendkeeps an existing empty header value (fix(res.append): preserve empty header values expressjs/express#7332)res.redirectbody saysundefinedfor non-standard status codes (fix(res.redirect): fallback to 'Redirecting' for non-standard status … expressjs/express#7407, fix: non standard status code will result in undefined expressjs/express#7045);inacceptParams(fix: acceptParams truncates quoted values containing a semicolon expressjs/express#7479)Rows 2 and 3 said "repro needed first". Both reproduce on Express 5.3.0 (
express-baseline) and on master before this change:res.set('Warning', ''); res.append('Warning', '199 x')gaveres.get('Warning')"199 x"(the empty value was dropped), and so did a number0set withres.setHeader.res.redirect(399, '/x')answeredundefined. Redirecting to /x(text) and<title>undefined</title><p>undefined. Redirecting to /x</p>(HTML). 310 does the same.Changes
res.links: the root cause was joining per-relation strings that could be empty, and always adding,after an existing header. The function now collects the link values first. If there are none (an empty object, or only empty arrays), it returns without touching the header. Otherwise it joins them after the existing value as before. This covers both upstream PRs: fix(res.links): skip empty-array rel values to prevent trailing comma in Link header expressjs/express#7320 filters empty strings but still sets an emptyLinkforres.links({})on a fresh response, and fix(res.links): ignore empty object to prevent header corruption expressjs/express#7578 handles only{}, not{ prev: [] }.res.append:if (prev)becomesif (prev !== undefined), ported from fix(res.append): preserve empty header values expressjs/express#7332. An empty string, or a number 0 set throughres.setHeader, is a value the app set, and it is kept.res.redirect: the message is nowstatuses.message[status] || String(status). It is computed once and used in the text body and in the HTML title and body, the same fallbackres.sendStatusalready uses. This is fix: non standard status code will result in undefined expressjs/express#7045's choice, with the HTML<title>fixed too, which fix: non standard status code will result in undefined expressjs/express#7045 misses. fix(res.redirect): fallback to 'Redirecting' for non-standard status … expressjs/express#7407's'Redirecting'would read "Redirecting. Redirecting to ...".acceptParams: our own fix. A newparamEnd()finds the next;outside a quoted string. Inside quotes, a backslash escapes the next character (a quoted-pair, RFC 9110 section 5.6.4). The loop now walks parameter by parameter and caches the next=, so the string is still scanned once. It no longer needs the backwardlastIndexOfjump. fix: acceptParams truncates quoted values containing a semicolon expressjs/express#7479 is not ported as is: its helper treats any quote after a backslash as escaped, sofoo="a\\"; bar=baz(an escaped backslash, then the closing quote) swallowedbar. A test pins that case.Behaviour
res.links({ next: '/2', prev: [] })Link: </2>; rel="next",Link: </2>; rel="next"res.links({}), fresh responseLink:(empty)Linkheaderres.links({})after a link,res.set('X-A', ''); res.append('X-A', 'b')X-A: bX-A:andX-A: b(res.getgives['', 'b'])res.redirect(399, '/next'), textundefined. Redirecting to /next399. Redirecting to /nextres.redirect(399, '/next'), HTML<title>undefined</title>...<title>399</title>...normalizeType('text/plain; foo="a;q=0"; bar=baz')foo: '"a', quality 0foo: '"a;q=0"', quality 1res.appendtargets and unquoted type parameters are unchanged.acceptParamsfix doesn't change any response:res.formatonly uses the type before the parameters. It shows throughrequire('express/lib/utils').Proof
All run locally on Node 24.17.0 in this worktree.
npm run lint,npm run typecheckandnpm run test:typesare clean.npm run test-ci: 143 files, 8,588 passed and 5 skipped (8,593). Coverage is 100% for statements, branches, functions and lines, and the coverage-files check reports all 23 source files.REGRESSIONS_ALL=1 npx vitest run, compared by test name with master at f594ffb: the only failing test on both is Rewriting req.url in router with prefix path results in missing slash (/) expressjs/express#4059. Nothing that passes on master fails here. 53 tests are new; the name diff also shows 12 security tests whose titles hold the checkout path, which differs between the two checkouts.test/regressions/response.js: PR fix(res.links): skip empty-array rel values to prevent trailing comma in Link header expressjs/express#7320, PR fix(res.links): ignore empty object to prevent header corruption expressjs/express#7578, PR fix(res.append): preserve empty header values expressjs/express#7332, PR fix(res.redirect): fallback to 'Redirecting' for non-standard status … expressjs/express#7407 and PR fix: acceptParams truncates quoted values containing a semicolon expressjs/express#7479.test/fork-response-headers.js:res.links: empty object, only empty arrays, existing headers fromres.links,res.setand anres.appendarray, an empty array between relations, order, an empty URL string, chaining, andres.links(null)still throws.res.append: an appended array, a number 0, two appends, the raw header lines, no header, and an empty array.res.redirect: text, HTML, HEADContent-Length, 999, a standard status, the empty-body branch, and a non-integer status still rejected before any body.acceptParams: an escaped quote, an escaped backslash, a quotedq, quoted=, parameters without=, unterminated quotes, empty parameters and keys, andres.formatwith a quoted key.test/property/accept-params.js:;,=and quoted-pairs, come back whole, with the quality outside quotes.allowed, with reasons: 2 forres.links, 1 forres.append, 2 forres.redirect, and 1 fornormalizeTypethrough each side'slib/utils. 2 generated calls are also inallowed:res.redirect 999, "/foo"andres.links {}.res.linksfixres.linksearly return aloneres.appendfixres.redirectfixacceptParamsnode scripts/ecosystem-suites.js, on a private copy of the cache): every test that passes on 5.3.0 passes here. That covers cors 49, express-session 160, multer 173, express-rate-limit 252, http-proxy-middleware 325, express-basic-auth 25, express-openapi-validator 468, express-fileupload 203, express-mongo-sanitize 20 and hpp 17.Benchmark
Not run. The benchmark workflows are paused, and bug fixes don't gate on them.
Risks / follow-ups
res.appendwith an empty value now sends an extra empty header line. That's harmless for list headers: RFC 9110 section 5.6.1 says recipients ignore empty list elements. It also follows whatappendpromises. An app that set a header to''meaning "unset" and then appended would see the extra line. No middleware suite does this.res.links({})no longer sends an emptyLinkheader. Nothing should depend on an emptyLink.res.redirectbodies for statuses without a standard message change fromundefinedto the number, andContent-Lengthchanges with them.acceptParamskeeps quotes in parameter values, as before; it doesn't unquote them.res.linksstill throws fornullorundefined, as before. fix(res.links): ignore empty object to prevent header corruption expressjs/express#7578 returns early for a falsy argument; that was left out, since it's a separate behaviour change.