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

Ensure invalid request to static page is handled correctly #34346

Merged
merged 4 commits into from
Feb 15, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/next/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1143,6 +1143,23 @@ export default abstract class Server {
res.statusCode = parseInt(pathname.substr(1), 10)
}

// static pages can only respond to GET/HEAD
// requests so ensure we respond with 405 for
// invalid requests
if (
!is404Page &&
!is500Page &&
pathname !== '/_error' &&
req.method !== 'HEAD' &&
req.method !== 'GET' &&
(typeof components.Component === 'string' || isSSG)
) {
res.statusCode = 405
res.setHeader('Allow', ['GET', 'HEAD'])
await this.renderError(null, req, res, pathname)
return null
}

// handle static page
if (typeof components.Component === 'string') {
return {
Expand Down
8 changes: 8 additions & 0 deletions test/e2e/getserversideprops/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,14 @@ const runTests = (dev = false) => {
expect(await res.text()).toBe('hello from gssp')
})

it('should allow POST request for getServerSideProps page', async () => {
const res = await fetchViaHTTP(next.url, '/', undefined, {
method: 'POST',
})
expect(res.status).toBe(200)
expect(await res.text()).toMatch(/hello.*?world/)
})

it('should render correctly when notFound is false (non-dynamic)', async () => {
const res = await fetchViaHTTP(next.url, '/not-found')

Expand Down
8 changes: 8 additions & 0 deletions test/e2e/prerender.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,14 @@ describe('Prerender', () => {
const runTests = (dev = false) => {
navigateTest(dev)

it('should respond with 405 for POST to static page', async () => {
const res = await fetchViaHTTP(next.url, '/', undefined, {
method: 'POST',
})
expect(res.status).toBe(405)
expect(await res.text()).toContain('Method Not Allowed')
})

it('should SSR normal page correctly', async () => {
const html = await renderViaHTTP(next.url, '/')
expect(html).toMatch(/hello.*?world/)
Expand Down
6 changes: 6 additions & 0 deletions test/integration/api-support/pages/user.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
export default function Page() {
return <div>User</div>
}

export function getServerSideProps() {
return {
props: {},
}
}
10 changes: 9 additions & 1 deletion test/integration/production/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ describe('Production Usage', () => {
)
})

it('should respond with 405 for POST to static page', async () => {
const res = await fetchViaHTTP(appPort, '/', undefined, {
method: 'POST',
})
expect(res.status).toBe(405)
expect(await res.text()).toContain('Method Not Allowed')
})

it('should contain generated page count in output', async () => {
const pageCount = 40
expect(output).toContain(`Generating static pages (0/${pageCount})`)
Expand Down Expand Up @@ -369,7 +377,7 @@ describe('Production Usage', () => {
it('should render 200 for POST on page', async () => {
const res = await fetchViaHTTP(
`http://localhost:${appPort}`,
'/about',
'/fully-dynamic',
undefined,
{
method: 'POST',
Expand Down