Skip to content

Commit

Permalink
fix: removes duplicate base from resolved src
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Dassardo-Joseph authored and castarco committed Nov 2, 2024
1 parent bdafbd4 commit e2041b2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
10 changes: 9 additions & 1 deletion @kindspells/astro-shield/src/core.mts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ export const updateStaticPageSriHashes = async (
h: HashesCollection,
allowInlineScripts: 'all' | 'static' | false = 'all',
allowInlineStyles: 'all' | 'static' | false = 'all',
state?: IntegrationState,
): Promise<string> => {
const pageHashes =
h.perPageSriHashes.get(relativeFilepath) ??
Expand Down Expand Up @@ -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?
Expand Down Expand Up @@ -472,6 +477,7 @@ const processHTMLFile = async (
distDir: string,
h: HashesCollection,
sri?: SRIOptions,
state?: IntegrationState,
): Promise<void> => {
const content = await readFile(filePath, 'utf8')
const updatedContent = await updateStaticPageSriHashes(
Expand All @@ -482,6 +488,7 @@ const processHTMLFile = async (
h,
sri?.allowInlineScripts ?? 'all',
sri?.allowInlineStyles ?? 'all',
state,
)

if (updatedContent !== content) {
Expand Down Expand Up @@ -759,6 +766,7 @@ export const processStaticFiles = async (
processHTMLFile,
file => extname(file) === '.html',
sri,
state,
)

if (securityHeaders?.enableOnStaticPages !== undefined) {
Expand Down
12 changes: 10 additions & 2 deletions @kindspells/astro-shield/src/fs.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean> => {
Expand All @@ -34,9 +39,11 @@ export const scanDirectory = async (
distDir: string,
h: HashesCollection,
sri?: SRIOptions,
state?: IntegrationState,
) => Promise<void>,
filenameCondition: (filename: string) => boolean,
sri?: SRIOptions,
state?: IntegrationState,
): Promise<void> => {
for (const file of await readdir(currentPath)) {
const filePath = resolve(currentPath, file)
Expand All @@ -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)
}
}
}

0 comments on commit e2041b2

Please sign in to comment.