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

add busboy tests #2924

Merged
merged 5 commits into from
Mar 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
23 changes: 20 additions & 3 deletions lib/web/fetch/formdata-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ const { HTTP_TOKEN_CODEPOINTS, isomorphicDecode } = require('./data-url')
const { isFileLike, File: UndiciFile } = require('./file')
const { makeEntry } = require('./formdata')
const assert = require('node:assert')
const { isAscii } = require('node:buffer')
const { isAscii, File: NodeFile } = require('node:buffer')

const File = globalThis.File ?? UndiciFile
const File = globalThis.File ?? NodeFile ?? UndiciFile

const formDataNameBuffer = Buffer.from('form-data; name="')
const filenameBuffer = Buffer.from('; filename="')
const filenameBuffer = Buffer.from('; filename')
const dd = Buffer.from('--')
const ddcrlf = Buffer.from('--\r\n')

Expand Down Expand Up @@ -110,6 +110,11 @@ function multipartFormDataParser (input, mimeType) {
// the first byte.
const position = { position: 0 }

// Note: undici addition, allow \r\n before the body.
if (input[0] === 0x0d && input[1] === 0x0a) {
position.position += 2
}

// 5. While true:
while (true) {
// 5.1. If position points to a sequence of bytes starting with 0x2D 0x2D
Expand Down Expand Up @@ -301,6 +306,18 @@ function parseMultipartFormDataHeaders (input, position) {

// 5. If position points to a sequence of bytes starting with `; filename="`:
if (bufferStartsWith(input, filenameBuffer, position)) {
// Note: undici also handles filename*
let check = position.position + filenameBuffer.length

if (input[check] === 0x2a) {
position.position += 1
check += 1
}

if (input[check] !== 0x3d || input[check + 1] !== 0x22) { // ="
return 'failure'
}

// 1. Advance position so it points at the byte after the next 0x22 (") byte
// (the one in the sequence of bytes matched above).
position.position += 12
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"test:cookies": "borp -p \"test/cookie/*.js\"",
"test:node-fetch": "borp -p \"test/node-fetch/**/*.js\"",
"test:eventsource": "npm run build:node && borp --expose-gc -p \"test/eventsource/*.js\"",
"test:fetch": "npm run build:node && borp --expose-gc -p \"test/fetch/*.js\" && borp -p \"test/webidl/*.js\"",
"test:fetch": "npm run build:node && borp --expose-gc -p \"test/fetch/*.js\" && borp -p \"test/webidl/*.js\" && borp -p \"test/busboy/*.js\"",
"test:jest": "cross-env NODE_V8_COVERAGE= jest",
"test:unit": "borp --expose-gc -p \"test/*.js\"",
"test:node-test": "borp -p \"test/node-test/**/*.js\"",
Expand Down
19 changes: 19 additions & 0 deletions test/busboy/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright Brian White. All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
73 changes: 73 additions & 0 deletions test/busboy/test-types-multipart-charsets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
'use strict'

const assert = require('node:assert')
const { inspect } = require('node:util')
const { test } = require('node:test')
const { Response } = require('../..')

const input = Buffer.from([
'-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
'Content-Disposition: form-data; ' +
'name="upload_file_0"; filename="テスト.dat"',
'Content-Type: application/octet-stream',
'',
'A'.repeat(1023),
'-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k--'
].join('\r\n'))
const boundary = '---------------------------paZqsnEHRufoShdX6fh0lUhXBP4k'
const expected = [
{
type: 'file',
name: 'upload_file_0',
data: Buffer.from('A'.repeat(1023)),
info: {
filename: 'テスト.dat',
encoding: '7bit',
mimeType: 'application/octet-stream'
}
}
]

test('unicode filename', async (t) => {
const response = new Response(input, {
headers: {
'content-type': `multipart/form-data; boundary=${boundary}`
}
})

const fd = await response.formData()
const results = []

for (const [name, value] of fd) {
if (typeof value === 'string') { // field
results.push({
type: 'field',
name,
val: value,
info: {
encoding: '7bit',
mimeType: 'text/plain'
}
})
} else { // File
results.push({
type: 'file',
name,
data: Buffer.from(await value.arrayBuffer()),
info: {
filename: value.name,
encoding: '7bit',
mimeType: value.type
}
})
}
}

assert.deepStrictEqual(
results,
expected,
'Results mismatch.\n' +
`Parsed: ${inspect(results)}\n` +
`Expected: ${inspect(expected)}`
)
})
Loading
Loading