Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: check maxHeadersSize on client instantiation and not on Parser instantion #3654

Merged
merged 5 commits into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions lib/dispatcher/client-h1.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,6 @@ class Parser {
* @param {*} llhttp
*/
constructor (client, socket, { exports }) {
assert(Number.isFinite(client[kMaxHeadersSize]) && client[kMaxHeadersSize] > 0)
metcoder95 marked this conversation as resolved.
Show resolved Hide resolved

this.llhttp = exports
this.ptr = this.llhttp.llhttp_alloc(constants.TYPE.RESPONSE)
this.client = client
Expand Down
21 changes: 16 additions & 5 deletions lib/dispatcher/client.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// @ts-check

'use strict'

const assert = require('node:assert')
Expand Down Expand Up @@ -60,6 +58,13 @@ const connectH2 = require('./client-h2.js')

const kClosedResolve = Symbol('kClosedResolve')

const getDefaultNodeMaxHeaderSize = http &&
http.maxHeaderSize &&
Number.isInteger(http.maxHeaderSize) &&
http.maxHeaderSize > 0
? () => http.maxHeaderSize
: () => { throw new InvalidArgumentError('http module not available or http.maxHeaderSize invalid') }

const noop = () => {}

function getPipelining (client) {
Expand Down Expand Up @@ -123,8 +128,14 @@ class Client extends DispatcherBase {
throw new InvalidArgumentError('unsupported maxKeepAliveTimeout, use keepAliveMaxTimeout instead')
}

if (maxHeaderSize != null && !Number.isFinite(maxHeaderSize)) {
throw new InvalidArgumentError('invalid maxHeaderSize')
if (maxHeaderSize != null) {
if (!Number.isInteger(maxHeaderSize) || maxHeaderSize < 1) {
throw new InvalidArgumentError('invalid maxHeaderSize')
}
} else {
// If maxHeaderSize is not provided, use the default value from the http module
// or if that is not available, throw an error.
maxHeaderSize = getDefaultNodeMaxHeaderSize()
}

if (socketPath != null && typeof socketPath !== 'string') {
Expand Down Expand Up @@ -204,7 +215,7 @@ class Client extends DispatcherBase {
this[kUrl] = util.parseOrigin(url)
this[kConnector] = connect
this[kPipelining] = pipelining != null ? pipelining : 1
this[kMaxHeadersSize] = maxHeaderSize || http.maxHeaderSize
this[kMaxHeadersSize] = maxHeaderSize
this[kKeepAliveDefaultTimeout] = keepAliveTimeout == null ? 4e3 : keepAliveTimeout
this[kKeepAliveMaxTimeout] = keepAliveMaxTimeout == null ? 600e3 : keepAliveMaxTimeout
this[kKeepAliveTimeoutThreshold] = keepAliveTimeoutThreshold == null ? 2e3 : keepAliveTimeoutThreshold
Expand Down
31 changes: 0 additions & 31 deletions test/client-errors.js

This file was deleted.

19 changes: 19 additions & 0 deletions test/client-node-max-header-size.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,23 @@ describe("Node.js' --max-http-header-size cli option", () => {

await t.completed
})

test('--max-http-header-size with Client API', async (t) => {
t = tspl(t, { plan: 6 })
const command = 'node -e "new (require(\'.\').Client)(new URL(\'http://localhost:200\'))"'

exec(`${command} --max-http-header-size=0`, { stdio: 'pipe' }, (err, stdout, stderr) => {
t.strictEqual(err.code, 1)
t.strictEqual(stdout, '')
t.match(stderr, /http module not available or http.maxHeaderSize invalid/, '--max-http-header-size=0 should result in an Error when using the Client API')
})

exec(command, { stdio: 'pipe' }, (err, stdout, stderr) => {
t.ifError(err)
t.strictEqual(stdout, '')
t.strictEqual(stderr, '', 'default max-http-header-size should not throw')
})

await t.completed
})
})
71 changes: 68 additions & 3 deletions test/node-test/client-errors.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
'use strict'

const { test } = require('node:test')
const assert = require('node:assert')
const https = require('node:https')
const net = require('node:net')
const { Readable } = require('node:stream')
const { test, after } = require('node:test')
const { Client, Pool, errors } = require('../..')
const { createServer } = require('node:http')
const https = require('node:https')
const pem = require('https-pem')
const { Readable } = require('node:stream')
const { tspl } = require('@matteo.collina/tspl')

const { kSocket } = require('../../lib/core/symbols')
Expand Down Expand Up @@ -399,6 +400,46 @@ test('invalid options throws', (t, done) => {
assert.strictEqual(err.message, 'invalid maxHeaderSize')
}

try {
new Client(new URL('http://localhost:200'), { // eslint-disable-line
maxHeaderSize: 0
})
assert.ok(0)
} catch (err) {
assert.ok(err instanceof errors.InvalidArgumentError)
assert.strictEqual(err.message, 'invalid maxHeaderSize')
}

try {
new Client(new URL('http://localhost:200'), { // eslint-disable-line
maxHeaderSize: 0
})
assert.ok(0)
} catch (err) {
assert.ok(err instanceof errors.InvalidArgumentError)
assert.strictEqual(err.message, 'invalid maxHeaderSize')
}

try {
new Client(new URL('http://localhost:200'), { // eslint-disable-line
maxHeaderSize: -10
})
assert.ok(0)
} catch (err) {
assert.ok(err instanceof errors.InvalidArgumentError)
assert.strictEqual(err.message, 'invalid maxHeaderSize')
}

try {
new Client(new URL('http://localhost:200'), { // eslint-disable-line
maxHeaderSize: 1.5
})
assert.ok(0)
} catch (err) {
assert.ok(err instanceof errors.InvalidArgumentError)
assert.strictEqual(err.message, 'invalid maxHeaderSize')
}

try {
new Client(1) // eslint-disable-line
assert.ok(0)
Expand Down Expand Up @@ -1324,3 +1365,27 @@ test('SocketError should expose socket details (tls)', async (t) => {

await p.completed
})

test('parser error', async (t) => {
t = tspl(t, { plan: 2 })

const server = net.createServer()
server.once('connection', (socket) => {
socket.write('asd\n\r213123')
})
after(() => server.close())

server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`)
after(() => client.destroy())

client.request({ path: '/', method: 'GET' }, (err) => {
t.ok(err)
client.close((err) => {
t.ifError(err)
})
})
})

await t.completed
})
Loading