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

fix: content-disposition header parsing #1911

Merged
merged 9 commits into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 6 additions & 0 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

const assert = require('assert')
const net = require('net')
const Buffer = require('buffer')
const util = require('./core/util')
const timers = require('./timers')
const Request = require('./core/request')
Expand Down Expand Up @@ -618,6 +619,11 @@ class Parser {
this.connection += buf.toString()
} else if (key.length === 14 && key.toString().toLowerCase() === 'content-length') {
this.contentLength += buf.toString()
} else if (key.length === 19 && key.toString().toLowerCase() === 'content-disposition') {
// https://www.rfc-editor.org/rfc/rfc6266#section-4.3
// we need to recieve the buffer as latin1
// so it would not transform to utf-8 wrongly
this.headers[len - 1] = Buffer.transcode(buf, 'latin1', 'utf8')
climba03003 marked this conversation as resolved.
Show resolved Hide resolved
}

this.trackHeader(buf.length)
Expand Down
78 changes: 78 additions & 0 deletions test/issue-1903.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
'use strict'

const { createServer } = require('node:http')
climba03003 marked this conversation as resolved.
Show resolved Hide resolved
const { test } = require('tap')
const { request } = require('..')

function createPromise() {
const result = {}
result.promise = new Promise((resolve) => {
result.resolve = resolve
})
return result
}

test('should parse content-disposition consistencely', async (t) => {
t.plan(5)

// create promise to allow server spinup in parallel
const spinup1 = createPromise()
const spinup2 = createPromise()
const spinup3 = createPromise()

// variables to store content-disposition header
const header = []

const server = createServer((req, res) => {
res.writeHead(200, {
'content-length': 2,
'content-disposition': 'attachment; filename="år.pdf"'
})
header.push('attachment; filename="år.pdf"')
res.end('OK', spinup1.resolve)
})
t.teardown(server.close.bind(server))
server.listen(0, spinup1.resolve)

const proxy1 = createServer(async (req, res) => {
const { statusCode, headers, body } = await request(`http://localhost:${server.address().port}`, {
method: "GET"
climba03003 marked this conversation as resolved.
Show resolved Hide resolved
})
header.push(headers['content-disposition'])
delete headers['transfer-encoding']
res.writeHead(statusCode, headers)
body.pipe(res)
})
t.teardown(proxy1.close.bind(proxy1))
proxy1.listen(0, spinup2.resolve)

const proxy2 = createServer(async (req, res) => {
const { statusCode, headers, body } = await request(`http://localhost:${proxy1.address().port}`, {
method: "GET"
climba03003 marked this conversation as resolved.
Show resolved Hide resolved
})
header.push(headers['content-disposition'])
delete headers['transfer-encoding']
res.writeHead(statusCode, headers)
body.pipe(res)
})
t.teardown(proxy2.close.bind(proxy2))
proxy2.listen(0, spinup3.resolve)

// wait until all server spinup
await Promise.all([spinup1.promise, spinup2.promise, spinup3.promise])

const { statusCode, headers, body } = await request(`http://localhost:${proxy2.address().port}`, {
method: "GET"
climba03003 marked this conversation as resolved.
Show resolved Hide resolved
})
header.push(headers['content-disposition'])
t.equal(statusCode, 200)
t.equal(await body.text(), 'OK')

// we check header
// must not be the same in first proxy
t.notSame(header[0], header[1])
// chaining always the same value
t.equal(header[1], header[2])
t.equal(header[2], header[3])
})

climba03003 marked this conversation as resolved.
Show resolved Hide resolved