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(nuxt): throw 404 when accessing /__nuxt_error directly #20497

Merged
merged 6 commits into from Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/nuxt/src/core/runtime/nitro/renderer.ts
Expand Up @@ -180,7 +180,10 @@ export default defineRenderHandler(async (event) => {
}

if (ssrError && event.node.req.socket.readyState !== 'readOnly' /* direct request */) {
throw createError('Cannot directly render error page!')
throw createError({
statusCode: 404,
statusMessage: 'Page Not Found: /__nuxt_error'
pi0 marked this conversation as resolved.
Show resolved Hide resolved
})
}

// Check for island component rendering
Expand Down
19 changes: 19 additions & 0 deletions test/basic.test.ts
Expand Up @@ -574,6 +574,25 @@ describe('errors', () => {
expect(await res.text()).toContain('This is a custom error')
})

it('should not allow accessing error route directly', async () => {
const res = await fetch('/__nuxt_error', {
headers: {
accept: 'application/json'
}
})
expect(res.status).toBe(404)
const error = await res.json()
delete error.stack
expect(error).toMatchInlineSnapshot(`
{
"message": "Page Not Found: /__nuxt_error",
"statusCode": 404,
"statusMessage": "Page Not Found: /__nuxt_error",
"url": "/__nuxt_error",
}
`)
})

// TODO: need to create test for webpack
it.runIf(!isDev() && !isWebpack)('should handle chunk loading errors', async () => {
const { page, consoleLogs } = await renderPage('/')
Expand Down