diff --git a/@kindspells/astro-shield/src/core.mts b/@kindspells/astro-shield/src/core.mts index 5cc9e02..95c2ff6 100644 --- a/@kindspells/astro-shield/src/core.mts +++ b/@kindspells/astro-shield/src/core.mts @@ -148,6 +148,7 @@ export const updateStaticPageSriHashes = async ( h: HashesCollection, allowInlineScripts: 'all' | 'static' | false = 'all', allowInlineStyles: 'all' | 'static' | false = 'all', + state?: IntegrationState, ): Promise => { const pageHashes = h.perPageSriHashes.get(relativeFilepath) ?? @@ -234,7 +235,11 @@ export const updateStaticPageSriHashes = async ( ) resourceContent = await resourceResponse.arrayBuffer() } else if (src.startsWith('/')) { - const resourcePath = resolve(distDir, `.${src}`) + const base = state?.config.base ?? '' + const updatedSrc = src.startsWith(base) + ? src.replace(base, '') + : src + const resourcePath = resolve(distDir, `.${updatedSrc}`) resourceContent = await readFile(resourcePath) } else { // TODO: should we remove the element? @@ -472,6 +477,7 @@ const processHTMLFile = async ( distDir: string, h: HashesCollection, sri?: SRIOptions, + state?: IntegrationState, ): Promise => { const content = await readFile(filePath, 'utf8') const updatedContent = await updateStaticPageSriHashes( @@ -482,6 +488,7 @@ const processHTMLFile = async ( h, sri?.allowInlineScripts ?? 'all', sri?.allowInlineStyles ?? 'all', + state, ) if (updatedContent !== content) { @@ -759,6 +766,7 @@ export const processStaticFiles = async ( processHTMLFile, file => extname(file) === '.html', sri, + state, ) if (securityHeaders?.enableOnStaticPages !== undefined) { diff --git a/@kindspells/astro-shield/src/fs.mts b/@kindspells/astro-shield/src/fs.mts index d1a9bb2..1072a3a 100644 --- a/@kindspells/astro-shield/src/fs.mts +++ b/@kindspells/astro-shield/src/fs.mts @@ -7,7 +7,12 @@ import { readdir, stat } from 'node:fs/promises' import { resolve } from 'node:path' -import type { HashesCollection, Logger, SRIOptions } from './types.mts' +import type { + HashesCollection, + IntegrationState, + Logger, + SRIOptions, +} from './types.mts' /** @internal */ export const doesFileExist = async (path: string): Promise => { @@ -34,9 +39,11 @@ export const scanDirectory = async ( distDir: string, h: HashesCollection, sri?: SRIOptions, + state?: IntegrationState, ) => Promise, filenameCondition: (filename: string) => boolean, sri?: SRIOptions, + state?: IntegrationState, ): Promise => { for (const file of await readdir(currentPath)) { const filePath = resolve(currentPath, file) @@ -51,9 +58,10 @@ export const scanDirectory = async ( processFile, filenameCondition, sri, + state, ) } else if (stats.isFile() && filenameCondition(file)) { - await processFile(logger, filePath, rootPath, h, sri) + await processFile(logger, filePath, rootPath, h, sri, state) } } }