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

Always include form value's content-type #391

Merged
merged 2 commits into from
Sep 16, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export interface SavedMultipartFile extends MultipartFile {
export interface MultipartValue<T = unknown> {
value: T;
fieldname: string;
mimetype?: string;
mimetype: string;
encoding: string;
fieldnameTruncated: boolean;
valueTruncated: boolean;
Expand Down
6 changes: 2 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,6 @@ function fastifyMultipart (fastify, options, done) {
request.pipe(bb)

function onField (name, fieldValue, fieldnameTruncated, valueTruncated, encoding, contentType) {
let mimetype

// don't overwrite prototypes
if (getDescriptor(Object.prototype, name)) {
onError(new PrototypeViolationError())
Expand All @@ -390,7 +388,7 @@ function fastifyMultipart (fastify, options, done) {

try {
fieldValue = secureJSON.parse(fieldValue)
mimetype = 'application/json'
dwickern marked this conversation as resolved.
Show resolved Hide resolved
contentType = 'application/json'
} catch (e) {
onError(new InvalidJSONFieldError())
return
Expand All @@ -399,7 +397,7 @@ function fastifyMultipart (fastify, options, done) {

const value = {
fieldname: name,
mimetype,
mimetype: contentType,
encoding,
value: fieldValue,
fieldnameTruncated,
Expand Down
46 changes: 45 additions & 1 deletion test/multipart-json.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ test('should not parse JSON fields forms if no content-type is set', function (t
fastify.post('/', async function (req, reply) {
for await (const part of req.parts()) {
t.notOk(part.filename)
t.notOk(part.mimetype)
t.equal(part.mimetype, 'text/plain')
t.type(part.value, 'string')
}

Expand Down Expand Up @@ -93,6 +93,50 @@ test('should not parse JSON fields forms if no content-type is set', function (t
})
})

test('should not parse JSON fields forms if non-json content-type is set', function (t) {
t.plan(5)

const fastify = Fastify()
t.teardown(fastify.close.bind(fastify))

fastify.register(multipart)

fastify.post('/', async function (req, reply) {
for await (const part of req.parts()) {
t.notOk(part.filename)
t.equal(part.mimetype, 'text/css')
t.type(part.value, 'string')
}

reply.code(200).send()
})

fastify.listen({ port: 0 }, async function () {
// request
const form = new FormData()
const opts = {
protocol: 'http:',
hostname: 'localhost',
port: fastify.server.address().port,
path: '/',
headers: form.getHeaders(),
method: 'POST'
}

const req = http.request(opts, res => {
t.equal(res.statusCode, 200)
res.resume()
res.on('end', () => {
t.pass('res ended successfully')
})
})

form.append('css', 'body { width: 100% }', { contentType: 'text/css' })

form.pipe(req)
})
})

test('should throw error when parsing JSON fields failed', function (t) {
t.plan(2)

Expand Down