-
Notifications
You must be signed in to change notification settings - Fork 9
feat: configurable bodyLimit (createServer + --body-limit + JSS_BODY_LIMIT) — closes #474 #543
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| /** | ||
| * Configurable bodyLimit (#474). | ||
| * | ||
| * Before #474 the per-request body cap was hard-coded to 10 MiB inside | ||
| * createServer's fastifyOptions, so any operator who wanted to accept | ||
| * larger `git push` payloads (e.g. an established app repo with several | ||
| * MB of history) had no way to raise it. | ||
| * | ||
| * The fix exposes `bodyLimit` as a createServer option / CLI flag | ||
| * (`--body-limit`) / env var (`JSS_BODY_LIMIT`) / config key, accepting | ||
| * either a number (bytes) or a size string ("100MB", "1GB", …). | ||
| * | ||
| * Tests here verify the surface end-to-end — server actually rejects | ||
| * over-size requests with 413, AND the size-string parsing path works | ||
| * the same as the numeric path. The default (10 MiB) is covered | ||
| * implicitly by every other test in the suite continuing to pass. | ||
| */ | ||
|
|
||
| import { describe, it, before, after, afterEach } from 'node:test'; | ||
| import assert from 'node:assert'; | ||
| import { createServer } from '../src/server.js'; | ||
| import fs from 'fs-extra'; | ||
|
|
||
| const TEST_DATA_DIR = './test-data-body-limit'; | ||
|
|
||
| let server; | ||
| let baseUrl; | ||
| let originalDataRoot; | ||
|
|
||
| async function startWith(bodyLimit) { | ||
| await fs.emptyDir(TEST_DATA_DIR); | ||
| server = createServer({ | ||
| logger: false, | ||
| forceCloseConnections: true, | ||
| root: TEST_DATA_DIR, | ||
| bodyLimit, | ||
| }); | ||
| await server.listen({ port: 0, host: '127.0.0.1' }); | ||
| const address = server.server.address(); | ||
| baseUrl = `http://127.0.0.1:${address.port}`; | ||
| } | ||
|
|
||
| describe('configurable bodyLimit (#474)', () => { | ||
| before(() => { | ||
| // createServer({ root }) mutates process.env.DATA_ROOT | ||
| // (src/server.js:180). Snapshot the previous value so we can | ||
| // restore it after the suite — otherwise the test directory | ||
| // leaks into any subsequent test that reads DATA_ROOT. | ||
| originalDataRoot = process.env.DATA_ROOT; | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| if (server) { | ||
| await server.close(); | ||
| server = null; | ||
| } | ||
| await fs.remove(TEST_DATA_DIR); | ||
| }); | ||
|
Comment on lines
+52
to
+58
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in 14f0862 via the same snapshot/restore added in the paired thread. |
||
|
|
||
| after(() => { | ||
| if (originalDataRoot === undefined) delete process.env.DATA_ROOT; | ||
| else process.env.DATA_ROOT = originalDataRoot; | ||
| }); | ||
|
|
||
| it('rejects requests larger than a numeric bodyLimit with 413', async () => { | ||
| await startWith(100); // 100 bytes | ||
| // Body well over the cap — Fastify's body parser fires the 413 | ||
| // before any route handler (or auth) runs. | ||
| const oversized = 'x'.repeat(500); | ||
| const res = await fetch(`${baseUrl}/anywhere`, { | ||
| method: 'PUT', | ||
| headers: { 'Content-Type': 'text/plain' }, | ||
| body: oversized, | ||
| }); | ||
| assert.strictEqual(res.status, 413, | ||
| `expected 413 for 500-byte body against a 100-byte limit, got ${res.status}`); | ||
| }); | ||
|
|
||
| it('parses string bodyLimit ("100B") via parseSize (same behaviour as numeric)', async () => { | ||
| await startWith('100B'); | ||
| const oversized = 'x'.repeat(500); | ||
| const res = await fetch(`${baseUrl}/anywhere`, { | ||
| method: 'PUT', | ||
| headers: { 'Content-Type': 'text/plain' }, | ||
| body: oversized, | ||
| }); | ||
| assert.strictEqual(res.status, 413, | ||
| `expected 413 from string-parsed bodyLimit "100B"; got ${res.status}`); | ||
| }); | ||
|
|
||
| it('accepts requests within the configured bodyLimit (no 413)', async () => { | ||
| await startWith('10KB'); | ||
| // Small body, well under the 10KB cap — must NOT 413. (We don't care | ||
| // which downstream status fires — auth/404/etc. are fine; only that | ||
| // the body-parser doesn't reject.) | ||
| const small = 'x'.repeat(100); | ||
| const res = await fetch(`${baseUrl}/anywhere`, { | ||
| method: 'PUT', | ||
| headers: { 'Content-Type': 'text/plain' }, | ||
| body: small, | ||
| }); | ||
| assert.notStrictEqual(res.status, 413, | ||
| `100-byte body under a 10KB cap should not 413; got ${res.status}`); | ||
| }); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 14f0862 via the snapshot/restore added in the paired threads.
beforecaptures the pre-suite value,afterrestores it.