Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: smaller package #98

Merged
merged 3 commits into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

# testing artifacts
**/tests/playground/*.mjs
**/*-test-artifact.*

# moon
.moon/cache
Expand Down
17 changes: 10 additions & 7 deletions @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.4.1",
"version": "1.4.2",
"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 All @@ -22,7 +22,10 @@
}
},
"imports": {
"#as/*": "./src/*"
"#as/*": {
"types": "./src/*.mts",
"import": "./dist/*.mjs"
}
},
"files": ["dist/*"],
"scripts": {
Expand Down Expand Up @@ -60,15 +63,15 @@
"astro": "^4.0.0"
},
"devDependencies": {
"@types/node": "^22.5.4",
"astro": "^4.15.4",
"get-tsconfig": "^4.8.0",
"@types/node": "^22.5.5",
"astro": "^4.15.6",
"get-tsconfig": "^4.8.1",
"rollup": "^4.21.3",
"rollup-plugin-dts": "^6.1.1",
"rollup-plugin-esbuild": "^6.1.1",
"typescript": "^5.6.2",
"vite": "^5.4.4",
"vitest": "^2.0.5"
"vite": "^5.4.5",
"vitest": "^2.1.1"
},
"repository": {
"type": "git",
Expand Down
10 changes: 6 additions & 4 deletions @kindspells/astro-shield/rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ const outputBaseConfig = {

export default defineConfig([
{
input: 'src/main.mts',
output: [{ format: 'esm', file: 'dist/main.mjs', sourcemap: true }],
input: 'src/core.mts',
output: [{ format: 'esm', file: 'dist/core.mjs', sourcemap: true }],
...outputBaseConfig,
},
{
input: 'src/core.mts',
output: [{ format: 'esm', file: 'dist/core.mjs', sourcemap: true }],
input: 'src/main.mts',
output: [{ format: 'esm', file: 'dist/main.mjs', sourcemap: true }],
external: ['#as/core'],
...outputBaseConfig,
},
{
Expand All @@ -40,6 +41,7 @@ export default defineConfig([
{
input: 'src/main.mts',
output: [{ format: 'esm', file: 'dist/main.d.mts' }],
external: ['#as/core'],
plugins: [dts()],
},
])
27 changes: 4 additions & 23 deletions @kindspells/astro-shield/src/core.mts
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,15 @@ import type { Plugin } from 'vite'
import { doesFileExist, scanDirectory } from './fs.mts'
import { patchHeaders } from './headers.mts'
import type {
HashesCollection,
Logger,
MiddlewareHashes,
PerPageHashes,
SecurityHeadersOptions,
SRIOptions,
StrictShieldOptions,
} from './types.mts'

export type MiddlewareHashes = {
scripts: Map<string, string>
styles: Map<string, string>
}

export type PerPageHashes = { scripts: Set<string>; styles: Set<string> }
export type PerPageHashesCollection = Map<string, PerPageHashes>

export type HashesCollection = {
inlineScriptHashes: Set<string>
inlineStyleHashes: Set<string>
extScriptHashes: Set<string>
extStyleHashes: Set<string>
perPageSriHashes: PerPageHashesCollection
perResourceSriHashes: MiddlewareHashes
}

export type Logger = {
info(msg: string): void
warn(msg: string): void
error(msg: string): void
}

export type HashesModule = {
[k in keyof HashesCollection]: HashesCollection[k] extends Set<string>
? string[] | undefined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import {
it,
} from 'vitest'

import type { HashesModule } from '#as/core.mjs'
import { generateSRIHash } from '#as/core.mjs'
import { doesFileExist } from '#as/fs.mjs'
import type { HashesModule } from '../core.mts'
import { generateSRIHash } from '../core.mts'
import { doesFileExist } from '../fs.mts'

const execFile = promisify(_execFile)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"license": "MIT",
"dependencies": {
"@astrojs/node": "^8.3.3",
"astro": "^4.15.4"
"astro": "^4.15.6"
},
"devDependencies": {
"@kindspells/astro-shield": "workspace:*"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"license": "MIT",
"dependencies": {
"@astrojs/node": "^8.3.3",
"astro": "^4.15.4"
"astro": "^4.15.6"
},
"devDependencies": {
"@kindspells/astro-shield": "workspace:*"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"license": "MIT",
"dependencies": {
"@astrojs/node": "^8.3.3",
"astro": "^4.15.4"
"astro": "^4.15.6"
},
"devDependencies": {
"@kindspells/astro-shield": "workspace:*"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"license": "MIT",
"dependencies": {
"@astrojs/node": "^8.3.3",
"astro": "^4.15.4"
"astro": "^4.15.6"
},
"devDependencies": {
"@kindspells/astro-shield": "workspace:*"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"license": "MIT",
"dependencies": {
"astro": "^4.15.4"
"astro": "^4.15.6"
},
"devDependencies": {
"@kindspells/astro-shield": "workspace:*"
Expand Down
3 changes: 1 addition & 2 deletions @kindspells/astro-shield/src/fs.mts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
import { readdir, stat } from 'node:fs/promises'
import { resolve } from 'node:path'

import type { HashesCollection, Logger } from './core.mts'
import type { SRIOptions } from './types.mts'
import type { HashesCollection, Logger, SRIOptions } from './types.mts'

export const doesFileExist = async (path: string): Promise<boolean> => {
try {
Expand Down
4 changes: 2 additions & 2 deletions @kindspells/astro-shield/src/headers.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { PerPageHashes } from './core.mts'
import type { PerPageHashes } from './types.mts'
import type {
CSPDirectives,
CSPOptions,
Expand Down Expand Up @@ -67,7 +67,7 @@ export const patchCspHeader = (
plainHeaders['content-security-policy'] as string,
),
}
: cspOpts.cspDirectives ?? ({} satisfies CSPDirectives)
: (cspOpts.cspDirectives ?? ({} satisfies CSPDirectives))

if (pageHashes.scripts.size > 0) {
setSrcDirective(directives, 'script-src', pageHashes.scripts)
Expand Down
2 changes: 1 addition & 1 deletion @kindspells/astro-shield/src/main.mts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { fileURLToPath } from 'node:url'

import type { AstroIntegration } from 'astro'

import { getAstroConfigSetup, processStaticFiles } from './core.mts'
import { getAstroConfigSetup, processStaticFiles } from '#as/core'
import type { ShieldOptions, SRIOptions } from './types.mts'

type AstroHooks = AstroIntegration['hooks']
Expand Down
2 changes: 1 addition & 1 deletion @kindspells/astro-shield/src/state.mts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* SPDX-License-Identifier: MIT
*/

import type { MiddlewareHashes } from './core.mts'
import type { MiddlewareHashes } from './types.mts'

let globalHashes: MiddlewareHashes

Expand Down
10 changes: 5 additions & 5 deletions @kindspells/astro-shield/src/tests/core.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import {
sriHashesEqual,
updateDynamicPageSriHashes,
updateStaticPageSriHashes,
} from '#as/core.mts'
import { doesFileExist } from '#as/fs.mts'
} from '../core.mts'
import { doesFileExist } from '../fs.mts'

type SriHashes = {
scripts: Record<string, string>
Expand Down Expand Up @@ -615,7 +615,7 @@ describe('updateStaticPageSriHashes', () => {
<title>My Test Page</title>
</head>
<body>
<script type="module" src="/core.mjs"></script>
<script type="module" src="/state.mjs"></script>
</body>
</html>`

Expand All @@ -624,7 +624,7 @@ describe('updateStaticPageSriHashes', () => {
<title>My Test Page</title>
</head>
<body>
<script type="module" src="/core.mjs" integrity="sha256-Pl+u7V7n6H0yGnuW+XJq9vAtnskDhX+uuHdOPF/MuEU="></script>
<script type="module" src="/state.mjs" integrity="sha256-GnAS2y0U5qIXDOpQOY7sE+D1/ncp19EQxwySQzSR/bQ="></script>
</body>
</html>`

Expand All @@ -642,7 +642,7 @@ describe('updateStaticPageSriHashes', () => {

expect(
h.extScriptHashes.has(
'sha256-Pl+u7V7n6H0yGnuW+XJq9vAtnskDhX+uuHdOPF/MuEU=',
'sha256-GnAS2y0U5qIXDOpQOY7sE+D1/ncp19EQxwySQzSR/bQ=',
),
).toBe(true)
expect(h.inlineScriptHashes.size).toBe(0)
Expand Down
6 changes: 3 additions & 3 deletions @kindspells/astro-shield/src/tests/fs.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import { relative, resolve } from 'node:path'

import { describe, expect, it } from 'vitest'

import type { HashesCollection } from '#as/core.mts'
import { generateSRIHash } from '#as/core.mts'
import { doesFileExist, scanDirectory } from '#as/fs.mts'
import type { HashesCollection } from '../types.mts'
import { generateSRIHash } from '../core.mts'
import { doesFileExist, scanDirectory } from '../fs.mts'

const testsDir = new URL('.', import.meta.url).pathname

Expand Down
4 changes: 2 additions & 2 deletions @kindspells/astro-shield/src/tests/headers.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import {
serialiseCspDirectives,
serialiseHashes,
setSrcDirective,
} from '#as/headers.mjs'
import type { CSPDirectives, SecurityHeadersOptions } from '#as/types.mts'
} from '../headers.mjs'
import type { CSPDirectives, SecurityHeadersOptions } from '../types.mts'

describe('serialiseHashes', () => {
it('returns an empty string for an empty set', () => {
Expand Down
2 changes: 1 addition & 1 deletion @kindspells/astro-shield/src/tests/main.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import type { AstroIntegration } from 'astro'
import { describe, expect, it } from 'vitest'

import defaultIntegrationExport, { shield } from '#as/main.mts'
import defaultIntegrationExport, { shield } from '../main.mts'

describe('sriCSP', () => {
it('is exported as default', () => {
Expand Down
2 changes: 1 addition & 1 deletion @kindspells/astro-shield/src/tests/state.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import { describe, expect, it } from 'vitest'

import { getGlobalHashes } from '#as/state.mts'
import { getGlobalHashes } from '../state.mts'

describe('getGlobalHashes', () => {
it('returns a singleton', () => {
Expand Down
30 changes: 30 additions & 0 deletions @kindspells/astro-shield/src/types.mts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,14 @@ export type SRIOptions = {
stylesAllowListUrls?: string[]
}

type VercelConfig = {
provider: 'vercel'
configFile: string // TODO: auto-discovery?
}

export type SecurityHeadersOptions = {
enableOnStaticPages?: VercelConfig | undefined

/**
* - If set, it controls how the CSP (Content Security Policy) header will be
* generated in the middleware.
Expand Down Expand Up @@ -151,3 +158,26 @@ export type StrictShieldOptions = ShieldOptions & {
distDir: string
sri: SRIOptions & { enableStatic: boolean; enableMiddleware: boolean }
}

export type Logger = {
info(msg: string): void
warn(msg: string): void
error(msg: string): void
}

export type MiddlewareHashes = {
scripts: Map<string, string>
styles: Map<string, string>
}

export type PerPageHashes = { scripts: Set<string>; styles: Set<string> }
export type PerPageHashesCollection = Map<string, PerPageHashes>

export type HashesCollection = {
inlineScriptHashes: Set<string>
inlineStyleHashes: Set<string>
extScriptHashes: Set<string>
extStyleHashes: Set<string>
perPageSriHashes: PerPageHashesCollection
perResourceSriHashes: MiddlewareHashes
}
2 changes: 1 addition & 1 deletion @kindspells/astro-shield/vitest.config.e2e.mts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {
include: ['e2e/**/*.test.mts'],
include: ['src/e2e/**/*.test.mts'],
},
})
2 changes: 1 addition & 1 deletion biome.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "https://biomejs.dev/schemas/1.8.3/schema.json",
"$schema": "https://biomejs.dev/schemas/1.9.0/schema.json",
"organizeImports": { "enabled": true },
"files": {
"include": ["*.json", "*.js", "*.mjs", "*.mts", "*.d.ts"],
Expand Down
4 changes: 2 additions & 2 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
"dependencies": {
"astro-sst": "^2.43.5",
"sharp": "0.33.5",
"sst": "^3.1.7"
"sst": "^3.1.10"
},
"devDependencies": {
"@astrojs/check": "^0.9.3",
"@astrojs/starlight": "^0.27.1",
"@astrojs/ts-plugin": "^1.10.2",
"@kindspells/astro-shield": "workspace:^",
"astro": "^4.15.4",
"astro": "^4.15.6",
"typescript": "^5.6.2"
}
}
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
}
],
"devDependencies": {
"@biomejs/biome": "^1.8.3",
"@biomejs/biome": "^1.9.0",
"@moonrepo/cli": "^1.28.2",
"@vitest/coverage-v8": "^2.0.5",
"@vitest/coverage-v8": "^2.1.1",
"publint": "^0.2.10",
"vitest": "^2.0.5"
"vitest": "^2.1.1"
},
"engines": {
"node": ">= 18.0.0"
Expand Down
Loading