diff --git a/.github/workflows/docs-links.yml b/.github/workflows/docs-links.yml index 999b2b11..46f72d88 100644 --- a/.github/workflows/docs-links.yml +++ b/.github/workflows/docs-links.yml @@ -4,19 +4,27 @@ on: push: paths: - '**.md' + - '.env.example' + - 'apps/web/.env.example' + - 'package.json' + - 'scripts/check-deploy-docs.mjs' - 'scripts/check-links.js' - 'scripts/docs-links-ci.test.mjs' - '.github/workflows/docs-links.yml' pull_request: paths: - '**.md' + - '.env.example' + - 'apps/web/.env.example' + - 'package.json' + - 'scripts/check-deploy-docs.mjs' - 'scripts/check-links.js' - 'scripts/docs-links-ci.test.mjs' - '.github/workflows/docs-links.yml' permissions: - # Los cuatro workflows solo leen el repo: clonan, instalan y corren - # tests. Ninguno hace push, ni comenta, ni publica, ni toca secrets. + # The workflow only reads the repo: clones, installs, and runs tests. + # It does not push, comment, publish, or touch secrets. contents: read concurrency: @@ -38,5 +46,8 @@ jobs: node-version: '22' - name: Check docs link workflow configuration run: node --test scripts/docs-links-ci.test.mjs + # Validates deployment documentation consistency with .env examples and package scripts + - name: Validate deployment documentation + run: node scripts/check-deploy-docs.mjs - name: Check local markdown links run: node scripts/check-links.js diff --git a/.github/workflows/font-assets.yml b/.github/workflows/font-assets.yml new file mode 100644 index 00000000..a5bab17f --- /dev/null +++ b/.github/workflows/font-assets.yml @@ -0,0 +1,40 @@ +name: Font assets check + +on: + push: + paths: + - '**.woff' + - '**.woff2' + - 'scripts/check-font-assets.mjs' + - 'scripts/check-font-assets.test.mjs' + - '.github/workflows/font-assets.yml' + pull_request: + paths: + - '**.woff' + - '**.woff2' + - 'scripts/check-font-assets.mjs' + - 'scripts/check-font-assets.test.mjs' + - '.github/workflows/font-assets.yml' + +permissions: + contents: read + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + check-fonts: + runs-on: ubuntu-latest + timeout-minutes: 5 + steps: + - uses: actions/checkout@v4 + with: + persist-credentials: false + - uses: actions/setup-node@v4 + with: + node-version: '22' + - name: Run font asset checker tests + run: node --test scripts/check-font-assets.test.mjs + - name: Verify font assets magic bytes + run: node scripts/check-font-assets.mjs diff --git a/package.json b/package.json index 4b43c776..8fe3c776 100644 --- a/package.json +++ b/package.json @@ -83,6 +83,8 @@ "snapshot:check": "node scripts/check-snapshots.mjs", "docs:check": "node scripts/check-deploy-docs.mjs", "docs:check-links": "node scripts/check-links.js", + "fonts:check": "node scripts/check-font-assets.mjs", + "fonts:test": "node --test scripts/check-font-assets.test.mjs", "coverage:test": "node --test scripts/check-rust-audit.test.mjs && node scripts/check-rust-audit.mjs && node --test scripts/run-ts-coverage.test.mjs && node scripts/run-ts-coverage.mjs", "rust:audit:check": "node scripts/check-rust-audit.mjs", "rust:audit:test": "node --test scripts/check-rust-audit.test.mjs", diff --git a/scripts/check-font-assets.mjs b/scripts/check-font-assets.mjs new file mode 100644 index 00000000..e33dcb64 --- /dev/null +++ b/scripts/check-font-assets.mjs @@ -0,0 +1,111 @@ +#!/usr/bin/env node +// Copyright (c) 2026 Sub Rosa contributors +import { openSync, readSync, closeSync, readdirSync } from "node:fs"; +import { join, relative, dirname } from "node:path"; +import { execSync } from "node:child_process"; +import { fileURLToPath } from "node:url"; + +export const WOFF_MAGIC = Buffer.from([0x77, 0x4f, 0x46, 0x46]); // "wOFF" +export const WOFF2_MAGIC = Buffer.from([0x77, 0x4f, 0x46, 0x32]); // "wOF2" + +/** + * Reads ONLY the first 4 bytes of a font file and checks its magic bytes. + * The file is never executed or evaluated. + */ +export function verifyFontMagicBytes(filePath) { + let fd; + try { + fd = openSync(filePath, "r"); + const buffer = Buffer.alloc(4); + const bytesRead = readSync(fd, buffer, 0, 4, 0); + if (bytesRead < 4) { + return false; + } + if (filePath.endsWith(".woff2")) { + return buffer.equals(WOFF2_MAGIC); + } + if (filePath.endsWith(".woff")) { + return buffer.equals(WOFF_MAGIC); + } + return false; + } catch { + return false; + } finally { + if (fd !== undefined) { + closeSync(fd); + } + } +} + +/** + * Discovers font files in repository using git ls-files if available, + * or recursively walking the directory as a fallback. + */ +export function discoverFontFiles(repoRoot = process.cwd()) { + try { + const output = execSync('git ls-files "*.woff" "*.woff2"', { + cwd: repoRoot, + encoding: "utf8", + stdio: ["pipe", "pipe", "ignore"], + }); + return output + .trim() + .split("\n") + .map((line) => line.trim()) + .filter(Boolean) + .map((rel) => join(repoRoot, rel)); + } catch { + return walkFontFiles(repoRoot); + } +} + +function walkFontFiles(dir) { + const results = []; + try { + const entries = readdirSync(dir, { withFileTypes: true }); + for (const entry of entries) { + if (entry.name === ".git" || entry.name === "node_modules") continue; + const full = join(dir, entry.name); + if (entry.isDirectory()) { + results.push(...walkFontFiles(full)); + } else if (entry.name.endsWith(".woff") || entry.name.endsWith(".woff2")) { + results.push(full); + } + } + } catch { + // Ignore unreadable directories + } + return results; +} + +export function checkFontAssets(fontFiles, repoRoot = process.cwd()) { + const invalidFiles = []; + for (const file of fontFiles) { + if (!verifyFontMagicBytes(file)) { + invalidFiles.push(relative(repoRoot, file) || file); + } + } + return invalidFiles; +} + +export function run(repoRoot = process.cwd()) { + const files = discoverFontFiles(repoRoot); + const invalid = checkFontAssets(files, repoRoot); + + if (invalid.length > 0) { + for (const filePath of invalid) { + console.error(filePath); + } + process.exit(1); + } + + process.exit(0); +} + +const isDirectRun = + process.argv[1] && + fileURLToPath(import.meta.url) === process.argv[1]; + +if (isDirectRun) { + run(); +} diff --git a/scripts/check-font-assets.test.mjs b/scripts/check-font-assets.test.mjs new file mode 100644 index 00000000..ca871e2d --- /dev/null +++ b/scripts/check-font-assets.test.mjs @@ -0,0 +1,100 @@ +// Copyright (c) 2026 Sub Rosa contributors +import assert from "node:assert/strict"; +import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { spawnSync } from "node:child_process"; +import { test } from "node:test"; +import { fileURLToPath } from "node:url"; + +import { + checkFontAssets, + verifyFontMagicBytes, + WOFF_MAGIC, + WOFF2_MAGIC +} from "./check-font-assets.mjs"; + +const SCRIPT_PATH = fileURLToPath(new URL("./check-font-assets.mjs", import.meta.url)); + +test("verifyFontMagicBytes accepts valid WOFF and WOFF2 files", (t) => { + const dir = mkdtempSync(join(tmpdir(), "sub-rosa-fonts-valid-")); + t.after(() => rmSync(dir, { recursive: true, force: true })); + + const validWoff = join(dir, "font.woff"); + const validWoff2 = join(dir, "font.woff2"); + + writeFileSync(validWoff, Buffer.concat([WOFF_MAGIC, Buffer.from("dummy-font-payload")])); + writeFileSync(validWoff2, Buffer.concat([WOFF2_MAGIC, Buffer.from("dummy-font-payload")])); + + assert.strictEqual(verifyFontMagicBytes(validWoff), true); + assert.strictEqual(verifyFontMagicBytes(validWoff2), true); +}); + +test("verifyFontMagicBytes rejects text-disguised-as-font files", (t) => { + const dir = mkdtempSync(join(tmpdir(), "sub-rosa-fonts-invalid-")); + t.after(() => rmSync(dir, { recursive: true, force: true })); + + const fakeWoff = join(dir, "exploit.woff"); + const fakeWoff2 = join(dir, "malicious.woff2"); + const shortFile = join(dir, "tiny.woff"); + + writeFileSync(fakeWoff, "alert('evil payload embedded as text');"); + writeFileSync(fakeWoff2, ""); + writeFileSync(shortFile, "hi"); + + assert.strictEqual(verifyFontMagicBytes(fakeWoff), false); + assert.strictEqual(verifyFontMagicBytes(fakeWoff2), false); + assert.strictEqual(verifyFontMagicBytes(shortFile), false); +}); + +test("checkFontAssets reports only invalid font paths", (t) => { + const dir = mkdtempSync(join(tmpdir(), "sub-rosa-font-assets-")); + t.after(() => rmSync(dir, { recursive: true, force: true })); + + const valid = join(dir, "valid.woff2"); + const invalid = join(dir, "fake.woff"); + + writeFileSync(valid, Buffer.concat([WOFF2_MAGIC, Buffer.from("data")])); + writeFileSync(invalid, "console.log('javascript payload');"); + + const invalidList = checkFontAssets([valid, invalid], dir); + assert.deepStrictEqual(invalidList, ["fake.woff"]); +}); + +test("CLI check exits non-zero and outputs only the invalid path when fonts fail check", (t) => { + const dir = mkdtempSync(join(tmpdir(), "sub-rosa-font-cli-")); + t.after(() => rmSync(dir, { recursive: true, force: true })); + + const fakeFont = join(dir, "disguised-script.woff2"); + writeFileSync(fakeFont, "function exploit() { return 42; }"); + + // Run script directly in dir containing the invalid font + const result = spawnSync(process.execPath, [SCRIPT_PATH], { + cwd: dir, + encoding: "utf8" + }); + + assert.strictEqual(result.status, 1, "Must exit non-zero on invalid font"); + assert.match( + result.stderr.trim(), + /disguised-script\.woff2$/, + "Must report the invalid font path on stderr" + ); + assert.strictEqual(result.stdout.trim(), "", "Stdout must be empty (only invalid path reported)"); +}); + +test("CLI check exits 0 when no invalid font assets exist", (t) => { + const dir = mkdtempSync(join(tmpdir(), "sub-rosa-font-empty-")); + t.after(() => rmSync(dir, { recursive: true, force: true })); + + const validFont = join(dir, "Inter.woff2"); + writeFileSync(validFont, Buffer.concat([WOFF2_MAGIC, Buffer.from("font-data")])); + + const result = spawnSync(process.execPath, [SCRIPT_PATH], { + cwd: dir, + encoding: "utf8" + }); + + assert.strictEqual(result.status, 0, "Must exit 0 when all fonts are valid"); + assert.strictEqual(result.stderr.trim(), ""); +}); diff --git a/scripts/docs-links-ci.test.mjs b/scripts/docs-links-ci.test.mjs index de1fa8ea..e939593d 100644 --- a/scripts/docs-links-ci.test.mjs +++ b/scripts/docs-links-ci.test.mjs @@ -14,5 +14,13 @@ test("docs-links workflow defines branch-scoped concurrency cancellation", () => /^concurrency:\n(?:\s+#.*\n)* group: \$\{\{ github\.workflow \}\}-\$\{\{ github\.ref \}\}\n cancel-in-progress: true$/m ); assert.match(workflow, /^permissions:\n(?:\s+#.*\n)* contents: read$/m); + assert.match(workflow, /run: node scripts\/check-deploy-docs\.mjs/); assert.match(workflow, /run: node scripts\/check-links\.js/); }); + +test("docs-links workflow validates deployment documentation alongside link check", () => { + assert.match( + workflow, + /- name: Validate deployment documentation\s*\n\s*run: node scripts\/check-deploy-docs\.mjs/ + ); +});