Skip to content

Commit

Permalink
fix: headers are not correctly configured for vercel
Browse files Browse the repository at this point in the history
Signed-off-by: Andres Correa Casablanca <[email protected]>
  • Loading branch information
castarco committed Sep 30, 2024
1 parent efc4bbd commit 6a3f1bb
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
2 changes: 1 addition & 1 deletion @kindspells/astro-shield/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kindspells/astro-shield",
"version": "1.6.0",
"version": "1.6.1",
"description": "Astro integration to enhance your website's security with SubResource Integrity hashes, Content-Security-Policy headers, and other techniques.",
"private": false,
"type": "module",
Expand Down
40 changes: 25 additions & 15 deletions @kindspells/astro-shield/src/tests/vercel.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -157,31 +157,35 @@ describe('buildVercelConfig', () => {
version: 3,
routes: [
{
src: '/nothing.html',
src: '^/nothing\\.html$',
headers: {
'content-security-policy': "script-src 'none'; style-src 'none'",
},
continue: true,
},
{
src: '/onlyscripts.html',
src: '^/onlyscripts\\.html$',
headers: {
'content-security-policy':
"script-src 'self' 'sha256-071spvYLMvnwaR0H7M2dfK0enB0cGtydTbgJkdoWq7c=' 'sha256-KWrCkmqpW9eWGwZRBZ9KqXsoHtAbAH/zPJvmUhsMKpA='; style-src 'none'",
},
continue: true,
},
{
src: '/onlystyles.html',
src: '^/onlystyles\\.html$',
headers: {
'content-security-policy':
"script-src 'none'; style-src 'self' 'sha256-VC84dQdO3Mo7nZIRaNTJgrqPQ0foHI8gdp/DS+e9/lk=' 'sha256-iwd3GNfA+kImEozakD3ZZQSZ8VVb3MFBOhJH6dEMnDE='",
},
continue: true,
},
{
src: '/scriptsandstyles.html',
src: '^/scriptsandstyles\\.html$',
headers: {
'content-security-policy':
"script-src 'self' 'sha256-071spvYLMvnwaR0H7M2dfK0enB0cGtydTbgJkdoWq7c=' 'sha256-KWrCkmqpW9eWGwZRBZ9KqXsoHtAbAH/zPJvmUhsMKpA='; style-src 'self' 'sha256-VC84dQdO3Mo7nZIRaNTJgrqPQ0foHI8gdp/DS+e9/lk=' 'sha256-iwd3GNfA+kImEozakD3ZZQSZ8VVb3MFBOhJH6dEMnDE='",
},
continue: true,
},
],
} satisfies VercelConfig)
Expand Down Expand Up @@ -225,25 +229,28 @@ describe('buildVercelConfig', () => {
version: 3,
routes: [
{
src: '/nested/',
src: '^/nested/$',
headers: {
'content-security-policy':
"script-src 'self' 'sha256-071spvYLMvnwaR0H7M2dfK0enB0cGtydTbgJkdoWq7c=' 'sha256-KWrCkmqpW9eWGwZRBZ9KqXsoHtAbAH/zPJvmUhsMKpA='; style-src 'self' 'sha256-VC84dQdO3Mo7nZIRaNTJgrqPQ0foHI8gdp/DS+e9/lk=' 'sha256-iwd3GNfA+kImEozakD3ZZQSZ8VVb3MFBOhJH6dEMnDE='",
},
continue: true,
},
{
src: '/nested/index.html',
src: '^/nested/index\\.html$',
headers: {
'content-security-policy':
"script-src 'self' 'sha256-071spvYLMvnwaR0H7M2dfK0enB0cGtydTbgJkdoWq7c=' 'sha256-KWrCkmqpW9eWGwZRBZ9KqXsoHtAbAH/zPJvmUhsMKpA='; style-src 'self' 'sha256-VC84dQdO3Mo7nZIRaNTJgrqPQ0foHI8gdp/DS+e9/lk=' 'sha256-iwd3GNfA+kImEozakD3ZZQSZ8VVb3MFBOhJH6dEMnDE='",
},
continue: true,
},
{
src: '/notindex.html',
src: '^/notindex\\.html$',
headers: {
'content-security-policy':
"script-src 'self' 'sha256-071spvYLMvnwaR0H7M2dfK0enB0cGtydTbgJkdoWq7c=' 'sha256-KWrCkmqpW9eWGwZRBZ9KqXsoHtAbAH/zPJvmUhsMKpA='; style-src 'self' 'sha256-VC84dQdO3Mo7nZIRaNTJgrqPQ0foHI8gdp/DS+e9/lk=' 'sha256-iwd3GNfA+kImEozakD3ZZQSZ8VVb3MFBOhJH6dEMnDE='",
},
continue: true,
},
],
})
Expand Down Expand Up @@ -287,25 +294,28 @@ describe('buildVercelConfig', () => {
version: 3,
routes: [
{
src: '/nested',
src: '^/nested$',
headers: {
'content-security-policy':
"script-src 'self' 'sha256-071spvYLMvnwaR0H7M2dfK0enB0cGtydTbgJkdoWq7c=' 'sha256-KWrCkmqpW9eWGwZRBZ9KqXsoHtAbAH/zPJvmUhsMKpA='; style-src 'self' 'sha256-VC84dQdO3Mo7nZIRaNTJgrqPQ0foHI8gdp/DS+e9/lk=' 'sha256-iwd3GNfA+kImEozakD3ZZQSZ8VVb3MFBOhJH6dEMnDE='",
},
continue: true,
},
{
src: '/nested/index.html',
src: '^/nested/index\\.html$',
headers: {
'content-security-policy':
"script-src 'self' 'sha256-071spvYLMvnwaR0H7M2dfK0enB0cGtydTbgJkdoWq7c=' 'sha256-KWrCkmqpW9eWGwZRBZ9KqXsoHtAbAH/zPJvmUhsMKpA='; style-src 'self' 'sha256-VC84dQdO3Mo7nZIRaNTJgrqPQ0foHI8gdp/DS+e9/lk=' 'sha256-iwd3GNfA+kImEozakD3ZZQSZ8VVb3MFBOhJH6dEMnDE='",
},
continue: true,
},
{
src: '/notindex.html',
src: '^/notindex\\.html$',
headers: {
'content-security-policy':
"script-src 'self' 'sha256-071spvYLMvnwaR0H7M2dfK0enB0cGtydTbgJkdoWq7c=' 'sha256-KWrCkmqpW9eWGwZRBZ9KqXsoHtAbAH/zPJvmUhsMKpA='; style-src 'self' 'sha256-VC84dQdO3Mo7nZIRaNTJgrqPQ0foHI8gdp/DS+e9/lk=' 'sha256-iwd3GNfA+kImEozakD3ZZQSZ8VVb3MFBOhJH6dEMnDE='",
},
continue: true,
},
],
})
Expand Down Expand Up @@ -344,16 +354,16 @@ describe('mergeVercelConfig', () => {
version: 3,
routes: [
{
src: '/nothing.html',
src: '/onlystyles.html',
headers: {
'content-security-policy': "script-src 'none'; style-src 'none'",
'content-security-policy':
"script-src 'none'; style-src 'self' 'sha256-VC84dQdO3Mo7nZIRaNTJgrqPQ0foHI8gdp/DS+e9/lk=' 'sha256-iwd3GNfA+kImEozakD3ZZQSZ8VVb3MFBOhJH6dEMnDE='",
},
},
{
src: '/onlystyles.html',
src: '/nothing.html',
headers: {
'content-security-policy':
"script-src 'none'; style-src 'self' 'sha256-VC84dQdO3Mo7nZIRaNTJgrqPQ0foHI8gdp/DS+e9/lk=' 'sha256-iwd3GNfA+kImEozakD3ZZQSZ8VVb3MFBOhJH6dEMnDE='",
'content-security-policy': "script-src 'none'; style-src 'none'",
},
},
],
Expand Down
8 changes: 6 additions & 2 deletions @kindspells/astro-shield/src/vercel.mts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ export const buildVercelConfig = (
}

if (Object.keys(headers).length > 0) {
routes.push({ src: `/${page}`, headers })
routes.push({
src: `^/${page.replaceAll('.', '\\.')}$`,
headers,
continue: true,
})
}
}

Expand All @@ -115,7 +119,7 @@ export const mergeVercelConfig = (
base: VercelConfig,
patch: VercelConfig,
): VercelConfig => {
return { ...base, routes: [...(base.routes ?? []), ...(patch.routes ?? [])] }
return { ...base, routes: [...(patch.routes ?? []), ...(base.routes ?? [])] }
}

export const serializeVercelConfig = (config: VercelConfig): string => {
Expand Down

0 comments on commit 6a3f1bb

Please sign in to comment.