Skip to content
Open
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
1 change: 1 addition & 0 deletions lib/core/symbols.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ module.exports = {
kConstruct: Symbol('constructable'),
kListeners: Symbol('listeners'),
kHTTPContext: Symbol('http context'),
kSocketUsed: Symbol('socket used'),
kMaxConcurrentStreams: Symbol('max concurrent streams'),
kHostAuthority: Symbol('host authority'),
kHTTP2InitialWindowSize: Symbol('http2 initial window size'),
Expand Down
4 changes: 2 additions & 2 deletions lib/dispatcher/client-h1.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ const {
kResume,
kHTTPContext,
kClosed,
kUseMilo
kUseMilo,
kSocketUsed
} = require('../core/symbols.js')

const constants = require('../llhttp/constants.js')
Expand All @@ -66,7 +67,6 @@ const FastBuffer = Buffer[Symbol.species]
const removeAllListeners = util.removeAllListeners
const kIdleSocketValidation = Symbol('kIdleSocketValidation')
const kIdleSocketValidationTimeout = Symbol('kIdleSocketValidationTimeout')
const kSocketUsed = Symbol('kSocketUsed')
const kTypeOfService = Symbol('kTypeOfService')

let extractBody
Expand Down
20 changes: 19 additions & 1 deletion lib/dispatcher/parser-h1.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ const {
kMaxResponseSize,
kResume,
kHTTPContext,
kMaxHeadersSize
kMaxHeadersSize,
kPending,
kSocketUsed
} = require('../core/symbols.js')

const FastBuffer = Buffer[Symbol.species]
Expand Down Expand Up @@ -598,6 +600,13 @@ class H1Parser {
return this.fail(this.milo.ERROR_CALLBACK_ERROR, 'Other side closed')
}

// A response while no request is running is unsolicited. Do not
// attribute it to a request that is queued but not yet written.
if (client[kRunning] === 0) {
util.destroy(socket, new SocketError('bad response', util.getSocketInfo(socket)))
return this.fail(this.milo.ERROR_CALLBACK_ERROR, 'Bad response')
}

const request = client[kQueue][client[kRunningIdx]]
if (!request) {
return this.fail(this.milo.ERROR_CALLBACK_ERROR, 'Missing request')
Expand Down Expand Up @@ -673,6 +682,7 @@ class H1Parser {
request.onResponseEnd(trailers)

client[kQueue][client[kRunningIdx]++] = null
socket[kSocketUsed] = client[kPending] === 0

if (socket[kWriting]) {
assert(client[kRunning] === 0)
Expand Down Expand Up @@ -773,6 +783,14 @@ class H1Parser {
return this.fail(milo.ERROR_CALLBACK_ERROR, 'Other side closed')
}

if (client[kRunning] === 0) {
util.destroy(
socket,
new SocketError('bad response', util.getSocketInfo(socket))
)
return this.fail(milo.ERROR_CALLBACK_ERROR, 'Bad response')
}

const request = client[kQueue][client[kRunningIdx]]
if (!request) {
return this.fail(milo.ERROR_CALLBACK_ERROR, 'Missing request')
Expand Down
45 changes: 45 additions & 0 deletions test/response-queue-poisoning.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,48 @@ test('should not reuse an idle socket with buffered unsolicited response bytes',
assert.strictEqual(response2.headers['poison-free-socket'], undefined)
assert.strictEqual(await readBody(response2.body), '/request2')
})

for (const useMilo of [false, true]) {
test(`should not attribute buffered unsolicited response bytes to a queued request (useMilo: ${useMilo})`, async () => {
let evilServerSocket

const server = createServer((req, res) => {
if (!evilServerSocket) {
evilServerSocket = req.socket
}

res.end(req.url)
})
after(() => server.close())

await new Promise(resolve => server.listen(0, resolve))

const client = new Client(`http://localhost:${server.address().port}`, {
keepAliveTimeout: 300e3,
useMilo
})
after(() => client.close())

const response1 = await client.request({ path: '/request1', method: 'GET' })
assert.strictEqual(await readBody(response1.body), '/request1')

// The unsolicited response reaches the client's receive buffer, but is
// not read yet when the next request is dispatched.
const response2 = await new Promise((resolve, reject) => {
setTimeout(() => {
evilServerSocket.write(
'HTTP/1.1 200 OK\r\n' +
'Poison-Free-Socket: true\r\n' +
'Connection: keep-alive\r\n' +
'Keep-Alive: timeout=300\r\n' +
'Content-Length: 0\r\n' +
'\r\n'
)
client.request({ path: '/request2', method: 'GET' }).then(resolve, reject)
}, 10)
})

assert.strictEqual(response2.headers['poison-free-socket'], undefined)
assert.strictEqual(await readBody(response2.body), '/request2')
})
}
Loading