Unverified Commit 87acbdb5 authored by Lukáš Huvar's avatar Lukáš Huvar Committed by GitHub

Http methods update for static files and pages (#7721)

* Change static method handling and pages revert

* Allow and reflect old behaviour

* Post should return 501

* Add back old test

* Invert logic

* Handle 501 for pages in separate location

* Remove usage of 501 HTTP status code

* Prevent information leak

* Add test

* Only 405 if the page exists -- 404 if it doesn't

* Handle sent responses

* Revert "Handle sent responses"

This reverts commit 5e9012ae29b611c25ed1e090756a25dbe3437182.

* Fix bad test

* Switch back to quiet

* Update test/integration/production/test/index.test.js
Co-Authored-By: 's avatarJJ Kasper <jj@jjsweb.site>
parent 12f1a634
...@@ -417,12 +417,7 @@ export default class Server { ...@@ -417,12 +417,7 @@ export default class Server {
throw err throw err
} }
if (req.method === 'GET' || req.method === 'HEAD') { await this.render404(req, res, parsedUrl)
await this.render404(req, res, parsedUrl)
} else {
res.statusCode = 501
res.end('Not Implemented')
}
} }
private async sendHTML( private async sendHTML(
...@@ -537,6 +532,12 @@ export default class Server { ...@@ -537,6 +532,12 @@ export default class Server {
return this.findPageComponents(pathname, query) return this.findPageComponents(pathname, query)
.then( .then(
result => { result => {
if (!(req.method === 'GET' || req.method === 'HEAD')) {
res.statusCode = 405
res.setHeader('Allow', ['GET', 'HEAD'])
return this.renderError(null, req, res, pathname, query)
}
return this.renderToHTMLWithComponents( return this.renderToHTMLWithComponents(
req, req,
res, res,
...@@ -558,8 +559,14 @@ export default class Server { ...@@ -558,8 +559,14 @@ export default class Server {
} }
return this.findPageComponents(dynamicRoute.page, query).then( return this.findPageComponents(dynamicRoute.page, query).then(
result => result => {
this.renderToHTMLWithComponents( if (!(req.method === 'GET' || req.method === 'HEAD')) {
res.statusCode = 405
res.setHeader('Allow', ['GET', 'HEAD'])
return this.renderError(null, req, res, pathname, query)
}
return this.renderToHTMLWithComponents(
req, req,
res, res,
dynamicRoute.page, dynamicRoute.page,
...@@ -567,6 +574,7 @@ export default class Server { ...@@ -567,6 +574,7 @@ export default class Server {
result, result,
{ ...this.renderOpts, amphtml, hasAmp, dataOnly } { ...this.renderOpts, amphtml, hasAmp, dataOnly }
) )
}
) )
} }
...@@ -656,6 +664,12 @@ export default class Server { ...@@ -656,6 +664,12 @@ export default class Server {
return this.render404(req, res, parsedUrl) return this.render404(req, res, parsedUrl)
} }
if (!(req.method === 'GET' || req.method === 'HEAD')) {
res.statusCode = 405
res.setHeader('Allow', ['GET', 'HEAD'])
return this.renderError(null, req, res, path)
}
try { try {
await serveStatic(req, res, path) await serveStatic(req, res, path)
} catch (err) { } catch (err) {
......
...@@ -5,8 +5,8 @@ import { NextPageContext } from 'next-server/dist/lib/utils' ...@@ -5,8 +5,8 @@ import { NextPageContext } from 'next-server/dist/lib/utils'
const statusCodes: { [code: number]: string } = { const statusCodes: { [code: number]: string } = {
400: 'Bad Request', 400: 'Bad Request',
404: 'This page could not be found', 404: 'This page could not be found',
405: 'Method Not Allowed',
500: 'Internal Server Error', 500: 'Internal Server Error',
501: 'Not Implemented',
} }
export type ErrorProps = { export type ErrorProps = {
......
...@@ -145,13 +145,13 @@ function runTests (serverless = false) { ...@@ -145,13 +145,13 @@ function runTests (serverless = false) {
expect(data).toEqual({ nextjs: 'cool' }) expect(data).toEqual({ nextjs: 'cool' })
}) })
// it('should return 404 on POST on pages', async () => { it('should return 405 on POST on pages', async () => {
// const res = await fetchViaHTTP(appPort, '/user', null, { const res = await fetchViaHTTP(appPort, '/user', null, {
// method: 'POST' method: 'POST'
// }) })
//
// expect(res.status).toEqual(404) expect(res.status).toEqual(405)
// }) })
it('should return JSON on post on API', async () => { it('should return JSON on post on API', async () => {
const data = await fetchViaHTTP(appPort, '/api/blog?title=Nextjs', null, { const data = await fetchViaHTTP(appPort, '/api/blog?title=Nextjs', null, {
......
...@@ -80,18 +80,52 @@ describe('Production Usage', () => { ...@@ -80,18 +80,52 @@ describe('Production Usage', () => {
expect(res.status).toBe(404) expect(res.status).toBe(404)
}) })
it('should render 405 for POST on page', async () => {
const res = await fetch(`http://localhost:${appPort}/about`, {
method: 'POST'
})
expect(res.status).toBe(405)
})
it('should render 404 for POST on missing page', async () => {
const res = await fetch(`http://localhost:${appPort}/fake-page`, {
method: 'POST'
})
expect(res.status).toBe(404)
})
it('should render 404 for _next routes that do not exist', async () => { it('should render 404 for _next routes that do not exist', async () => {
const url = `http://localhost:${appPort}/_next/abcdef` const url = `http://localhost:${appPort}/_next/abcdef`
const res = await fetch(url) const res = await fetch(url)
expect(res.status).toBe(404) expect(res.status).toBe(404)
}) })
it('should render 404 even if the HTTP method is not GET or HEAD', async () => {
const url = `http://localhost:${appPort}/_next/abcdef`
const methods = ['POST', 'PUT', 'DELETE']
for (const method of methods) {
const res = await fetch(url, { method })
expect(res.status).toBe(404)
}
})
it('should render 404 for dotfiles in /static', async () => { it('should render 404 for dotfiles in /static', async () => {
const url = `http://localhost:${appPort}/static/.env` const url = `http://localhost:${appPort}/static/.env`
const res = await fetch(url) const res = await fetch(url)
expect(res.status).toBe(404) expect(res.status).toBe(404)
}) })
it('should return 405 method on static then GET and HEAD', async () => {
const res = await fetch(
`http://localhost:${appPort}/static/data/item.txt`,
{
method: 'POST'
}
)
expect(res.headers.get('allow').includes('GET')).toBe(true)
expect(res.status).toBe(405)
})
it('should set Content-Length header', async () => { it('should set Content-Length header', async () => {
const url = `http://localhost:${appPort}` const url = `http://localhost:${appPort}`
const res = await fetch(url) const res = await fetch(url)
...@@ -283,7 +317,12 @@ describe('Production Usage', () => { ...@@ -283,7 +317,12 @@ describe('Production Usage', () => {
this.finished = true this.finished = true
} }
} }
const html = await app.renderToHTML({}, res, '/finish-response', {}) const html = await app.renderToHTML(
{ method: 'GET' },
res,
'/finish-response',
{}
)
expect(html).toBeFalsy() expect(html).toBeFalsy()
}) })
......
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