Merge commit from fork
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Yusuke Wada · committed by GitHub · parent 6792789 · Browse files · 2 files·+81 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+81 added−3 removed
- tests:+68 added−0 removed
- other:+13 added−3 removed
Provenance
- Commit
605c705- Tree
ca5890d- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
6792789
2 files changed+81 added−3 removed
modified:src/middleware/body-limit/index.test.ts+68 added
128 unmodified linesExpand all129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201128 unmodified linesExpand all expect(res.status).toBe(413) expect(await res.text()).toBe('no') }) })
describe('Transfer-Encoding and Content-Length headers', () => { beforeEach(() => { app = new Hono() app.use('*', bodyLimit({ maxSize: 10 })) app.post('/test', async (c) => { return c.text(await c.req.text()) }) })
it('should prioritize Transfer-Encoding over Content-Length', async () => { // Create a chunked body that exceeds the limit const largeContent = 'this is a large content that exceeds 10 bytes' const chunks = [largeContent.slice(0, 20), largeContent.slice(20)]
const stream = new ReadableStream({ start(controller) { chunks.forEach((chunk) => { controller.enqueue(new TextEncoder().encode(chunk)) }) controller.close() }, })
const res = await app.request('/test', { method: 'POST', headers: { 'Content-Length': '5', // Small content-length (bypass attempt) 'Transfer-Encoding': 'chunked', // But chunked encoding with large content }, body: stream, duplex: 'half', } as RequestInit)
// Should reject based on actual chunked content size, not Content-Length expect(res.status).toBe(413) })
it('should handle only Content-Length header correctly', async () => { const smallContent = 'small' const res = await app.request('/test', buildRequestInit({ body: smallContent }))
expect(res.status).toBe(200) expect(await res.text()).toBe(smallContent) })
it('should handle only Transfer-Encoding header correctly', async () => { const content = 'test' const stream = new ReadableStream({ start(controller) { controller.enqueue(new TextEncoder().encode(content)) controller.close() }, })
const res = await app.request('/test', { method: 'POST', headers: { 'Transfer-Encoding': 'chunked', }, body: stream, duplex: 'half', } as RequestInit)
expect(res.status).toBe(200) expect(await res.text()).toBe(content) }) })})modified:src/middleware/body-limit/index.ts+13 added-3 removed
69 unmodified lines70 // maybe GET or HEAD request71 return next()72 }73 74- if (c.req.raw.headers.has('content-length')) {75- // we can trust content-length header because it's already validated by server74+ const hasTransferEncoding = c.req.raw.headers.has('transfer-encoding')75+ const hasContentLength = c.req.raw.headers.has('content-length')76+77+ // RFC 7230: If both Transfer-Encoding and Content-Length are present,78+ // Transfer-Encoding takes precedence and Content-Length should be ignored79+ if (hasTransferEncoding && hasContentLength) {80+ // Both headers present - follow RFC 7230 and ignore Content-Length81+ // This might indicate request smuggling attempt82+ }83+84+ if (hasContentLength && !hasTransferEncoding) {85+ // Only Content-Length present - we can trust it86 const contentLength = parseInt(c.req.raw.headers.get('content-length') || '0', 10)87 return contentLength > maxSize ? onError(c) : next()88 }89 80- // maybe chunked transfer encoding90+ // Transfer-Encoding present (chunked) or no length headers91 92 let size = 093 const rawReader = c.req.raw.body.getReader()94 const reader = new ReadableStream({31 unmodified lines