Unverified Commit 0f0398c8 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Make sure to error when setting too large of preview data (#10831)

* Make sure to error when setting too large of preview data

* Update to check size after signing and limit to 2KB
parent a4eef950
...@@ -377,6 +377,14 @@ function setPreviewData<T>( ...@@ -377,6 +377,14 @@ function setPreviewData<T>(
} }
) )
// limit preview mode cookie to 2KB since we shouldn't store too much
// data here and browsers drop cookies over 4KB
if (payload.length > 2048) {
throw new Error(
`Preview data is limited to 2KB currently, reduce how much data you are storing as preview data to continue`
)
}
const { serialize } = require('cookie') as typeof import('cookie') const { serialize } = require('cookie') as typeof import('cookie')
const previous = res.getHeader('Set-Cookie') const previous = res.getHeader('Set-Cookie')
res.setHeader(`Set-Cookie`, [ res.setHeader(`Set-Cookie`, [
......
export default (req, res) => { export default (req, res) => {
res.setPreviewData(req.query) if (req.query.tooBig) {
try {
res.setPreviewData(new Array(2000).fill('a').join(''))
} catch (err) {
return res.status(500).end('too big')
}
} else {
res.setPreviewData(req.query)
}
res.status(200).end() res.status(200).end()
} }
...@@ -156,6 +156,12 @@ function runTests(startServer = nextStart) { ...@@ -156,6 +156,12 @@ function runTests(startServer = nextStart) {
expect(cookies[1]).not.toHaveProperty('Max-Age') expect(cookies[1]).not.toHaveProperty('Max-Age')
}) })
it('should throw error when setting too large of preview data', async () => {
const res = await fetchViaHTTP(appPort, '/api/preview?tooBig=true')
expect(res.status).toBe(500)
expect(await res.text()).toBe('too big')
})
/** @type import('next-webdriver').Chain */ /** @type import('next-webdriver').Chain */
let browser let browser
it('should start the client-side browser', async () => { it('should start the client-side browser', async () => {
......
export default (req, res) => { export default (req, res) => {
res.setPreviewData(req.query) if (req.query.tooBig) {
try {
res.setPreviewData(new Array(2000).fill('a').join(''))
} catch (err) {
return res.status(500).end('too big')
}
} else {
res.setPreviewData(req.query)
}
res.status(200).end() res.status(200).end()
} }
...@@ -63,6 +63,12 @@ function runTests(startServer = nextStart) { ...@@ -63,6 +63,12 @@ function runTests(startServer = nextStart) {
expect(pre).toBe('undefined and undefined') expect(pre).toBe('undefined and undefined')
}) })
it('should throw error when setting too large of preview data', async () => {
const res = await fetchViaHTTP(appPort, '/api/preview?tooBig=true')
expect(res.status).toBe(500)
expect(await res.text()).toBe('too big')
})
let previewCookieString let previewCookieString
it('should enable preview mode', async () => { it('should enable preview mode', async () => {
const res = await fetchViaHTTP(appPort, '/api/preview', { lets: 'goooo' }) const res = await fetchViaHTTP(appPort, '/api/preview', { lets: 'goooo' })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment