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: route body limit #36

Merged
merged 2 commits into from
Oct 25, 2022
Merged

fix: route body limit #36

merged 2 commits into from
Oct 25, 2022

Conversation

Eomm
Copy link
Owner

@Eomm Eomm commented Oct 24, 2022

fix #35

Copy link

@paolochiodi paolochiodi left a comment

Choose a reason for hiding this comment

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

Hi! Thank you for the quick PR.
I think this will fix my use case as reported in #35.
I hope you don't mine me adding a couple of comments.

t.fail('body is not ok')
})

await t.test('must not throw if body is smaller than limit', async t => {

Choose a reason for hiding this comment

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

I'd specify:

must not throw if body is smaller than route limit

Comment on lines 54 to 68
await t.test('must reject if body is bigger than limit', async t => {
const res = await app.inject({
method: 'POST',
url: '/50',
payload
})

t.equal(res.statusCode, 413)
t.same(res.json(), {
statusCode: 413,
code: 'FST_ERR_CTP_BODY_TOO_LARGE',
error: 'Payload Too Large',
message: 'Request body is too large'
})
})

Choose a reason for hiding this comment

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

Why using different routes instead of different payloads?

I'd rather have just two routes:

  1. route-limit
  2. server-limit

And you test both with a bigger and a smaller payload

Choose a reason for hiding this comment

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

I'd actually split route-limit into two tests: one were route-limit is bigger than server-limit and one where route-limit is smaller then server-limit.

This way you can test that the route level limit is always respected

Copy link
Owner Author

Choose a reason for hiding this comment

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

Done

plugin.js Outdated
@@ -56,16 +56,22 @@ function rawBody (fastify, opts, next) {
next()

function preparsingRawBody (request, reply, payload, done) {
const applyLimit = request.context._parserOptions.limit || fastify.initialConfig.bodyLimit

Choose a reason for hiding this comment

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

should you check for null or undefined instead? is 0 technically a valid limit?

Copy link
Owner Author

Choose a reason for hiding this comment

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

Fastify rejects it

'bodyLimit' option must be an integer > 0. Got '0'

I fixed the check anyway by using the ?? operator instead

@Eomm Eomm marked this pull request as ready for review October 25, 2022 17:41
@Eomm Eomm merged commit 4e79cc2 into main Oct 25, 2022
@Eomm Eomm deleted the route-limit branch October 25, 2022 18:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

fastify-raw-body doesn't respect the bodyLimit set at route level
2 participants