Skip to content

Commit

Permalink
fix: do not autoquote csp directives
Browse files Browse the repository at this point in the history
Signed-off-by: Andres Correa Casablanca <[email protected]>
  • Loading branch information
castarco committed Apr 29, 2024
1 parent 2e3af4e commit 2efc44e
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 23 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.3.5",
"version": "1.3.6",
"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
7 changes: 3 additions & 4 deletions @kindspells/astro-shield/src/headers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ export const serialiseHashes = hashes =>
* @param {Set<string>} hashes
* @returns {string}
*/
export const safeSerialiseHashes = hashes =>
export const serializeCspDirectiveSources = hashes =>
Array.from(hashes)
.sort()
.map(h => (h.match(/^'[^']+'$/i) ? h : `'${h}'`))
.join(' ')

/**
Expand All @@ -46,12 +45,12 @@ export const setSrcDirective = (directives, srcType, hashes) => {
const baseSrcDirective = directives[srcType]
if (baseSrcDirective) {
const srcDirective = new Set(
baseSrcDirective.split(/\s+/).filter(v => v !== "'self'"),
baseSrcDirective.split(/\s+/),
)
for (const hash of hashes) {
srcDirective.add(`'${hash}'`)
}
directives[srcType] = `'self' ${safeSerialiseHashes(srcDirective)}`
directives[srcType] = serializeCspDirectiveSources(srcDirective)
} else {
directives[srcType] = `'self' ${serialiseHashes(hashes)}`
}
Expand Down
19 changes: 1 addition & 18 deletions @kindspells/astro-shield/tests/headers.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { describe, expect, it } from 'vitest'
import {
parseCspDirectives,
patchHeaders,
safeSerialiseHashes,
serialiseCspDirectives,
serialiseHashes,
setSrcDirective,
Expand All @@ -35,22 +34,6 @@ describe('serialiseHashes', () => {
})
})

describe('safeSerialiseHashes', () => {
it('returns an empty string for an empty set', () => {
expect(safeSerialiseHashes(new Set())).toBe('')
})

it('returns a string with sorted hashes', () => {
const hashes = new Set(['d', 'c', 'a', 'b'])
expect(safeSerialiseHashes(hashes)).toBe("'a' 'b' 'c' 'd'")
})

it('avoids duplicated single quotes', () => {
const hashes = new Set(["'a'", "'b'", "'c'", "'d'"])
expect(safeSerialiseHashes(hashes)).toBe("'a' 'b' 'c' 'd'")
})
})

describe('serialiseCspDirectives', () => {
it('returns an empty string for an empty object', () => {
expect(serialiseCspDirectives({})).toBe('')
Expand Down Expand Up @@ -92,7 +75,7 @@ describe('setSrcDirective', () => {
)

expect(directives['script-src']).toBe(
"'self' 'abc1' 'abc2' 'dbc1' 'xyz2' 'xyz3'",
"'abc1' 'abc2' 'dbc1' 'self' 'xyz2' 'xyz3'",
)
})
})
Expand Down

0 comments on commit 2efc44e

Please sign in to comment.