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

Repo sync #31958

Merged
merged 2 commits into from
Mar 5, 2024
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
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,8 @@
"sass": "^1.52.3",
"start-server-and-test": "^2.0.3",
"ts-jest": "29.1.1",
"tsx": "4.7.0",
"typescript": "^5.2.2",
"tsx": "4.7.1",
"typescript": "^5.3.3",
"unist-util-remove": "^4.0.0",
"unist-util-visit-parents": "6.0.1",
"website-scraper": "^5.3.1"
Expand Down
16 changes: 3 additions & 13 deletions src/shielding/middleware/handle-invalid-headers.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
import statsd from '#src/observability/lib/statsd.js'
import { errorCacheControl } from '#src/frame/middleware/cache-control.js'

const STATSD_KEY = 'middleware.handle_invalid_headers'

const INVALID_HEADER_KEYS = [
// Next.js will pick this up and override the status code.
// We don't want that to happen because `x-invoke-status: 203` can
Expand All @@ -15,14 +10,9 @@ const INVALID_HEADER_KEYS = [
export default function handleInvalidNextPaths(req, res, next) {
const header = INVALID_HEADER_KEYS.find((key) => req.headers[key])
if (header) {
// This way you can't hammer the backend with invalid requests.
// Since the CDN will cache based on the status code not being one
// of success, we don't have to worry about this being cached when
// the URL is the same but the headers *not invalid*.
errorCacheControl(res)

const tags = [`ip:${req.ip}`, `path:${req.path}`, `header:${header}`]
statsd.increment(STATSD_KEY, 1, tags)
// There's no point attempting to set a cache-control on this.
// The CDN will not cache if the status code is not a success
// and not a 404.

return res.status(400).type('text').send('Invalid request headers')
}
Expand Down
4 changes: 0 additions & 4 deletions src/shielding/tests/invalid-headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@ describe('invalid headers', () => {
test('400 if containing x-invoke-status (instead of redirecting)', async () => {
const res = await get('/', { headers: { 'x-invoke-status': '203' } })
expect(res.statusCode).toBe(400)
expect(res.headers['cache-control']).toMatch('public')
expect(res.headers['cache-control']).toMatch(/max-age=[1-9]/)
})
test('400 if containing x-invoke-status (instead of 200)', async () => {
const res = await get('/en', { headers: { 'x-invoke-status': '203' } })
expect(res.statusCode).toBe(400)
expect(res.headers['cache-control']).toMatch('public')
expect(res.headers['cache-control']).toMatch(/max-age=[1-9]/)
})
})
Loading