feat: validate values for cache-control and content-type headers in dev mode#13114
Conversation
🦋 Changeset detectedLatest commit: 15d9f74 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
preview: https://svelte-dev-git-preview-kit-13114-svelte.vercel.app/ this is an automated message |
72f42fd to
e651a53
Compare
cache-control and content-type headers in dev mode
e651a53 to
adc6077
Compare
| 'cache-control': (value) => { | ||
| const directives = value | ||
| .split(',') | ||
| .map((directive) => directive.trim().split('=')[0].toLowerCase()); |
There was a problem hiding this comment.
| .map((directive) => directive.trim().split('=')[0].toLowerCase()); | |
| .map((directive) => directive.trim().split('=').at(0)?.toLowerCase()); |
Is it possible for the string to end up with an empty entry after splitting on ','? Like cache-control: stale-while-revalidate,,no-transform or something? Obviously that's still wrong, but hopefully we'd catch that and show an error, not throw a hard-to-debug runtime error 🤔
There was a problem hiding this comment.
This was a good point! I've introduced an additional check to catch the empty directives.
|
|
||
| beforeAll(() => { | ||
| console_warn = console.warn; | ||
| // @ts-expect-error |
There was a problem hiding this comment.
Same thing here, I think you can add a global declaration to the top and avoid using globalThis here.
There was a problem hiding this comment.
I found success in the other declarations, but I didn't have much luck silencing it here, despite trying a couple of things 🤔.
adc6077 to
838fd8b
Compare
| ]); | ||
|
|
||
| const CONTENT_TYPE_PATTERN = | ||
| /^(application|audio|font|image|model|text|video|x-[a-z]+)\/[-+.\w]+$/i; |
There was a problem hiding this comment.
| /^(application|audio|font|image|model|text|video|x-[a-z]+)\/[-+.\w]+$/i; | |
| /^(application|audio|font|image|model|text|video|x-[a-z]+)\/[-+.\w]+$/i; |
The IANA spec defines the following top-level types, some of which are not included here:
There was a problem hiding this comment.
Thanks! Updated to reflect this.
There was a problem hiding this comment.
You can move this to the test-dev-only package -- that package only runs dev tests, which means you don't need to fake the globalThis.__SVELTEKIT_DEV__ thing.
838fd8b to
4f7c4b9
Compare
|
Thanks @JR-G! I pushed a commit with a few improvements to the tests (mainly just simplifying setup) and some additional information in the error messages. Great PR. Merging as soon as CI passes :) |
Fix for #12784
Adds validation for common HTTP headers in dev mode to help catch invalid values early:
run
pnpm devfromtest/apps/basicsand navigate to http://localhost:5173/headers/invalid. In your console you'll see:Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
Tests
pnpm testand lint the project withpnpm lintandpnpm checkChangesets
pnpm changesetand following the prompts. Changesets that add features should beminorand those that fix bugs should bepatch. Please prefix changeset messages withfeat:,fix:, orchore:.Edits