Skip to content

use in to check for prototype violation #484

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

Merged
merged 2 commits into from
Sep 30, 2023
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
5 changes: 2 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const secureJSON = require('secure-json-parse')

const kMultipart = Symbol('multipart')
const kMultipartHandler = Symbol('multipartHandler')
const getDescriptor = Object.getOwnPropertyDescriptor

const PartsLimitError = createError('FST_PARTS_LIMIT', 'reach parts limit', 413)
const FilesLimitError = createError('FST_FILES_LIMIT', 'reach files limit', 413)
Expand Down Expand Up @@ -249,7 +248,7 @@ function fastifyMultipart (fastify, options, done) {

function onField (name, fieldValue, fieldnameTruncated, valueTruncated, encoding, contentType) {
// don't overwrite prototypes
if (getDescriptor(Object.prototype, name)) {
if (name in Object.prototype) {
onError(new PrototypeViolationError())
return
}
Expand Down Expand Up @@ -295,7 +294,7 @@ function fastifyMultipart (fastify, options, done) {

function onFile (name, file, filename, encoding, mimetype) {
// don't overwrite prototypes
if (getDescriptor(Object.prototype, name)) {
if (name in Object.prototype) {
// ensure that stream is consumed, any error is suppressed
sendToWormhole(file)
onError(new PrototypeViolationError())
Expand Down
135 changes: 135 additions & 0 deletions test/multipart-security.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,141 @@ test('should not allow __proto__ as field name', function (t) {
})
})

test('should not allow toString as field name', function (t) {
t.plan(4)

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

fastify.register(multipart)

fastify.post('/', async function (req, reply) {
t.ok(req.isMultipart())

try {
await req.file()
reply.code(200).send()
} catch (error) {
t.ok(error instanceof fastify.multipartErrors.PrototypeViolationError)
reply.code(500).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, 500)
res.resume()
res.on('end', () => {
t.pass('res ended successfully')
})
})
form.append('toString', 'world')

form.pipe(req)
})
})

test('should not allow hasOwnProperty as field name', function (t) {
t.plan(4)

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

fastify.register(multipart)

fastify.post('/', async function (req, reply) {
t.ok(req.isMultipart())

try {
await req.file()
reply.code(200).send()
} catch (error) {
t.ok(error instanceof fastify.multipartErrors.PrototypeViolationError)
reply.code(500).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, 500)
res.resume()
res.on('end', () => {
t.pass('res ended successfully')
})
})
form.append('hasOwnProperty', 'world')

form.pipe(req)
})
})

test('should not allow propertyIsEnumerable as field name', function (t) {
t.plan(4)

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

fastify.register(multipart)

fastify.post('/', async function (req, reply) {
t.ok(req.isMultipart())

try {
await req.file()
reply.code(200).send()
} catch (error) {
t.ok(error instanceof fastify.multipartErrors.PrototypeViolationError)
reply.code(500).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, 500)
res.resume()
res.on('end', () => {
t.pass('res ended successfully')
})
})
form.append('propertyIsEnumerable', 'world')

form.pipe(req)
})
})

test('should use default for fileSize', async function (t) {
t.plan(4)

Expand Down