fix(h2): validate maxConcurrentStreams and read settings.initialWindowSize - #5608
fix(h2): validate maxConcurrentStreams and read settings.initialWindowSize#5608marko1olo wants to merge 1 commit into
Conversation
…wSize The h2 option namespacing in nodejs#5498 left two wires crossed. Neither has shipped yet: nodejs#5498 landed after the v8.9.0 bump, so both are on main only. The guard for `h2Options.maxConcurrentStreams` tests `h2Options.connectionWindowSize`, copied from the block below it. With `maxConcurrentStreams` set and `connectionWindowSize` absent, `Number.isInteger(undefined)` is false and the negation makes the condition true, so `new Client(url, { h2Options: { maxConcurrentStreams: 10 } })` throws `h2Options.maxConcurrentStreams must be a positive integer, greater than 0` for the integer 10. Adding an unrelated `connectionWindowSize` makes the same call pass. It fails open the other way too: `{ maxConcurrentStreams: 'not-a-number', connectionWindowSize: 65535 }` is accepted. Through an Agent or Pool it is worse, because clients are built lazily — construction succeeds and then every request rejects with that message, including over a plain HTTP/1.1 origin where no h2 session exists. `h2Options.settings.initialWindowSize` is the documented shape and is what the validation above checks, but the session options read `h2Options.initialWindowSize`, so the documented option was validated and then dropped, and an undocumented flat one was honoured instead. test/http2-options.js covers both: it fails on the current code and passes here. Signed-off-by: marko1olo <marko1olo@users.noreply.github.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #5608 +/- ##
==========================================
- Coverage 93.50% 93.49% -0.02%
==========================================
Files 110 110
Lines 38303 38303
==========================================
- Hits 35816 35812 -4
- Misses 2487 2491 +4 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
| // - connectionWindowSize: 524288 (512KB) vs Node.js default (none set) | ||
| // Provides better flow control for the entire connection across multiple streams. | ||
| initialWindowSize: h2Options?.initialWindowSize ?? initialWindowSize ?? 262144 | ||
| initialWindowSize: h2Options?.settings?.initialWindowSize ?? initialWindowSize ?? 262144 |
There was a problem hiding this comment.
This seems like a behavior change
There was a problem hiding this comment.
It is, yes — and intentionally so. The existing code validated maxConcurrentStreams against connectionWindowSize (a copy-paste error from the block above), so the check was effectively dead for any caller that didn't also set connectionWindowSize. The same bug let invalid values through undetected.
This replaces the cross-wired check with one that tests maxConcurrentStreams against itself, which is what the error message has always said it does. Happy to add a note in the PR description if the intent isn't obvious from the commit message alone.
There was a problem hiding this comment.
Yeah, the change is valid; this is an oversight on my end, it is included within the new h2 namespacing.
It should be all right to do the move as we are supporting two ways of setting this up (legacy top-level, namepaced)
This relates to...
A regression from #5498, which landed after the v8.9.0 bump, so both halves below are on
mainand have not shipped yet.Rationale
The h2 option namespacing left two wires crossed.
1.
h2Options.maxConcurrentStreamsis validated against the wrong property. The guard testsh2Options.connectionWindowSize:It is the block below it, copied. With
maxConcurrentStreamsset andconnectionWindowSizeabsent,Number.isInteger(undefined)isfalseand the negation makes the condition true, so a valid integer throws. It also fails open in the other direction.Through
AgentorPoolit is worse, because clients are created lazily: construction succeeds and then every request rejects with that message, including against a plain HTTP/1.1 origin where no h2 session is ever negotiated.2.
h2Options.settings.initialWindowSizeis validated and then dropped. The validation checksh2Options.settings?.initialWindowSize, which matchesdocs/docs/api/Client.md, but the session options readh2Options?.initialWindowSize, so the documented option never reaches the session and an undocumented flat one is honoured instead.{ h2Options: { settings: { initialWindowSize: 131072 } } }leavessessionOptions.initialWindowSizeundefined.Changes
Two lines in
lib/dispatcher/client.js: validatemaxConcurrentStreamsagainst itself, and readsettings.initialWindowSizewhere the session options are built. I keptNumber.isIntegerrather than the loosertypeof !== 'number'used by the legacy branch, because it is what the error message promises.test/http2-options.jscovers both, readingclient[kHTTP2Options]directly. It fails 0/2 on currentmainand passes 2/2 here.npm run test:h2is green.Features
Bug Fixes
Status