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

Support for saveRequestFiles with attachFieldsToBody set true #409

Merged
merged 8 commits into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
38 changes: 34 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const util = require('util')
const createError = require('@fastify/error')
const sendToWormhole = require('stream-wormhole')
const deepmergeAll = require('@fastify/deepmerge')({ all: true })
const { PassThrough, pipeline } = require('stream')
const { PassThrough, pipeline, Readable } = require('stream')
const pump = util.promisify(pipeline)
const secureJSON = require('secure-json-parse')

Expand Down Expand Up @@ -101,6 +101,7 @@ function busboy (options) {
}

function fastifyMultipart (fastify, options, done) {
const attachFieldsToBody = options.attachFieldsToBody
if (options.addToBody === true) {
if (typeof options.sharedSchemaId === 'string') {
fastify.addSchema({
Expand Down Expand Up @@ -180,14 +181,16 @@ function fastifyMultipart (fastify, options, done) {
const PrototypeViolationError = createError('FST_PROTO_VIOLATION', 'prototype property is not allowed as field name', 400)
const InvalidMultipartContentTypeError = createError('FST_INVALID_MULTIPART_CONTENT_TYPE', 'the request is not multipart', 406)
const InvalidJSONFieldError = createError('FST_INVALID_JSON_FIELD_ERROR', 'a request field is not a valid JSON as declared by its Content-Type', 406)
const FileBufferNotFoundError = createError('FST_FILE_BUFFER_NOT_FOUND', 'the file buffer was not found', 500)

fastify.decorate('multipartErrors', {
PartsLimitError,
FilesLimitError,
FieldsLimitError,
PrototypeViolationError,
InvalidMultipartContentTypeError,
RequestFileTooLargeError
RequestFileTooLargeError,
FileBufferNotFoundError
})

fastify.addContentTypeParser('multipart/form-data', setMultipart)
Expand Down Expand Up @@ -507,10 +510,14 @@ function fastifyMultipart (fastify, options, done) {
}

async function saveRequestFiles (options) {
let files
if (attachFieldsToBody === true) {
files = filesFromFields.call(this, this.body)
} else {
files = await this.files(options)
}
const requestFiles = []
const tmpdir = (options && options.tmpdir) || os.tmpdir()

const files = await this.files(options)
this.tmpUploads = []
for await (const file of files) {
const filepath = path.join(tmpdir, toID() + path.extname(file.filename))
Expand All @@ -528,6 +535,29 @@ function fastifyMultipart (fastify, options, done) {
return requestFiles
}

async function * filesFromFields (container) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why async generator but not a generator?
It doesn't really need to be async.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already changed

try {
for (const field of Object.values(container)) {
if (Array.isArray(field)) {
for await (const subField of filesFromFields.call(this, field)) {
yield subField
}
}
if (!field.file) {
continue
}
if (!field._buf) {
throw new FileBufferNotFoundError()
}
field.file = Readable.from(field._buf)
yield field
}
} catch (err) {
this.log.error({ err }, 'save request file failed')
throw err
}
}

async function cleanRequestFiles () {
if (!this.tmpUploads) {
return
Expand Down
143 changes: 143 additions & 0 deletions test/fix-313.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
'use strict'

const test = require('tap').test
const FormData = require('form-data')
const Fastify = require('fastify')
const multipart = require('..')
const http = require('http')
const path = require('path')
const fs = require('fs')
const { access } = require('fs').promises
const EventEmitter = require('events')
const { once } = EventEmitter

const filePath = path.join(__dirname, '../README.md')

test('should store file on disk, remove on response when attach fields to body is true', async function (t) {
t.plan(22)

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

fastify.register(multipart, {
attachFieldsToBody: true
})

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

const files = await req.saveRequestFiles()

t.ok(files[0].filepath)
t.equal(files[0].fieldname, 'upload')
t.equal(files[0].filename, 'README.md')
t.equal(files[0].encoding, '7bit')
t.equal(files[0].mimetype, 'text/markdown')
t.ok(files[0].fields.upload)
t.ok(files[1].filepath)
t.equal(files[1].fieldname, 'upload')
t.equal(files[1].filename, 'README.md')
t.equal(files[1].encoding, '7bit')
t.equal(files[1].mimetype, 'text/markdown')
t.ok(files[1].fields.upload)
t.ok(files[2].filepath)
t.equal(files[2].fieldname, 'other')
t.equal(files[2].filename, 'README.md')
t.equal(files[2].encoding, '7bit')
t.equal(files[2].mimetype, 'text/markdown')
t.ok(files[2].fields.upload)

await access(files[0].filepath, fs.constants.F_OK)
await access(files[1].filepath, fs.constants.F_OK)
await access(files[2].filepath, fs.constants.F_OK)

reply.code(200).send()
})
const ee = new EventEmitter()

// ensure that file is removed after response
fastify.addHook('onResponse', async (request, reply) => {
try {
await access(request.tmpUploads[0], fs.constants.F_OK)
} catch (error) {
t.equal(error.code, 'ENOENT')
t.pass('Temp file was removed after response')
ee.emit('response')
}
})

await fastify.listen({ port: 0 })
// 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)
form.append('upload', fs.createReadStream(filePath))
form.append('upload', fs.createReadStream(filePath))
form.append('other', fs.createReadStream(filePath))

form.pipe(req)

const [res] = await once(req, 'response')
t.equal(res.statusCode, 200)
res.resume()
await once(res, 'end')
await once(ee, 'response')
})

test('should throw on saving request files when attach fields to body is true but buffer is not stored', async function (t) {
t.plan(3)

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

fastify.register(multipart, {
attachFieldsToBody: true,
onFile: async (part) => {
for await (const chunk of part.file) {
chunk.toString()
}
}
})

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

try {
await req.saveRequestFiles()
reply.code(200).send()
} catch (error) {
t.ok(error instanceof fastify.multipartErrors.FileBufferNotFoundError)
reply.code(500).send()
}
})

await fastify.listen({ port: 0 })
// 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)
form.append('upload', fs.createReadStream(filePath))

form.pipe(req)

const [res] = await once(req, 'response')
t.equal(res.statusCode, 500)
res.resume()
await once(res, 'end')
})