Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

14.7 KB
Raw Blame
// @vitest-environment node

import { readFileSync } from 'node:fs';
import { execFileSync } from 'node:child_process';
import { fileURLToPath } from 'node:url';
import { describe, expect, it } from 'vitest';

interface PackageManifest {
  dependencies: Record<string, string>;
  devDependencies: Record<string, string>;
  overrides: Record<string, string>;
}

interface PackageLock {
  packages: Record<string, { version?: string }>;
}

const manifest = JSON.parse(
  readFileSync(new URL('../../../package.json', import.meta.url), 'utf8'),
) as PackageManifest;
const lock = JSON.parse(
  readFileSync(new URL('../../../package-lock.json', import.meta.url), 'utf8'),
) as PackageLock;

const tsconfig = readFileSync(new URL('../../../tsconfig.json', import.meta.url), 'utf8');
const libMatch = /"lib"\s*:\s*\[([^\]]*)\]/.exec(tsconfig);
const declaredLib = (libMatch?.[1] ?? '')
  .split(',')
  .map((entry) => entry.trim().replace(/^"|"$/g, ''))
  .filter(Boolean);

const parseVersion = (version: string): [number, number, number] => {
  const [major = 0, minor = 0, patch = 0] = version
    .split('-', 1)[0]
    .split('.')
    .map((part) => Number.parseInt(part, 10));
  return [major, minor, patch];
};

const atLeast = (version: string, floor: [number, number, number]): boolean => {
  const current = parseVersion(version);
  for (let index = 0; index < current.length; index += 1) {
    if (current[index] !== floor[index]) return current[index] > floor[index];
  }
  return true;
};

const lockedVersions = (packageName: string): string[] =>
  Object.entries(lock.packages)
    .filter(
      ([path]) =>
        path === `node_modules/${packageName}` || path.endsWith(`/node_modules/${packageName}`),
    )
    .map(([, entry]) => entry.version)
    .filter((version): version is string => Boolean(version));

const braceExpansionIsPatched = (version: string): boolean => {
  const [major] = parseVersion(version);
  // GHSA-q2hr-2g5m-vwhr (quadratic {a},b} rewrite) is fixed in 1.1.21 and 5.0.12.
  if (major === 1) return atLeast(version, [1, 1, 21]);
  if (major === 2) return atLeast(version, [2, 1, 4]);
  if (major === 3) return atLeast(version, [3, 0, 6]);
  return major >= 5 && atLeast(version, [5, 0, 12]);
};

const nanoidIsPatched = (version: string): boolean => {
  const [major] = parseVersion(version);
  if (major === 3) return atLeast(version, [3, 3, 17]);
  return major >= 5 && atLeast(version, [5, 1, 6]);
};

// Accept maintained 3.x patch/minor upgrades without pinning the manifest to
// one historical fix. Reject broader ranges, prereleases and unreviewed majors.
const dompurifyRangeIsPatched = (range: string): boolean =>
  /^\^3\.\d+\.\d+$/.test(range) && atLeast(range.slice(1), [3, 4, 13]);

// Prettier remains an exact, stable 3.x declaration. Do not turn one reviewed
// patch into a permanent freeze, or accept a broad range/unreviewed major.
const prettierPinIsReviewed = (version: string): boolean =>
  /^3\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)$/.test(version) && atLeast(version, [3, 9, 9]);

describe('frontend dependency security floors', () => {
  it('checks the actual locked Solid serialization edges before accepting security overrides', () => {
    const check = fileURLToPath(
      new URL('../../../../scripts/check-frontend-serialization.mjs', import.meta.url),
    );
    expect(execFileSync(process.execPath, [check], { encoding: 'utf8' })).toContain(
      'Solid runtime edges match, or use the exact qualified security override',
    );
  });

  it.each<[string, [number, number, number]]>([
    // GHSA-p6vx-979v-rg4c and GHSA-jp82-f5mq-hwhp: both Seroval fixes
    // are required, including the TypedArray fix after the first 1.6.x patch.
    ['seroval', [1, 6, 3]],
    // GHSA-rj75-hqrm-r3gf: typography's exact 6.0.10 needs a 7.x override.
    ['postcss-selector-parser', [7, 1, 6]],
    // GHSA-68fv-2mgg-jv7q: all PostCSS/source-map consumers need the fix.
    ['source-map-js', [1, 2, 2]],
  ])('keeps every %s copy above the October deserialization and parsing floors', (name, floor) => {
    // Seroval now deliberately crosses Solid's declared minor. Pin that one
    // reviewed tuple rather than allowing the caret to admit future minors.
    expect(manifest.overrides[name]).toBe(name === 'seroval' ? '1.7.3' : `^${floor.join('.')}`);
    const versions = lockedVersions(name);
    expect(versions, `${name} must be locked`).not.toHaveLength(0);
    for (const version of versions) {
      expect(version).not.toContain('-');
      expect(parseVersion(version)[0], `${name} must stay on the reviewed major`).toBe(floor[0]);
      expect(atLeast(version, floor), `${name} ${version} is vulnerable`).toBe(true);
    }
  });

  it('pins every Seroval copy to the override exercised by the real Solid consumers', () => {
    expect(manifest.overrides.seroval).toBe('1.7.3');
    expect(lockedVersions('seroval')).toEqual(['1.7.3']);
    expect(lockedVersions('solid-js')).toEqual(['1.9.17']);
    expect(lockedVersions('seroval-plugins')).toEqual(['1.6.8']);
  });

  it('keeps Vitest and its mocker above the redirect-mock file-read floor', () => {
    // GHSA-82fw-gwwq-j7x9: the maintained 4.x fix starts at 4.1.11.
    // GHSA-qxg6-89mg-rqv8 (manual mock registration code injection) and
    // GHSA-xp26-3454-mjgp (Windows backslash redirect bypass) move it to 4.1.12.
    expect(manifest.devDependencies.vitest).toBe('^4.1.12');
    expect(manifest.devDependencies['@vitest/coverage-v8']).toBe('^4.1.12');
    const runner = lockedVersions('vitest');
    expect(runner).toHaveLength(1);
    for (const name of ['vitest', '@vitest/mocker', '@vitest/coverage-v8']) {
      const versions = lockedVersions(name);
      expect(versions).not.toHaveLength(0);
      for (const version of versions) {
        expect(version).not.toContain('-');
        expect(atLeast(version, [4, 1, 12]), `${name} ${version} is vulnerable`).toBe(true);
        expect(version, `${name} must match the runner`).toBe(runner[0]);
      }
    }
  });

  it('keeps Vite above the bundled launch-editor and dev-server fs floors', () => {
    // GHSA-587h-7wmj-h5pm, GHSA-r825-w8mm-5pxw and GHSA-698v-4r3x-3vcv: Vite
    // bundles launch-editor, fixed on 6.x in 6.4.4. 6.4.5 also applies the
    // server.fs check after stripping the query.
    expect(manifest.devDependencies.vite).toBe('^6.4.5');
    const versions = lockedVersions('vite');
    expect(versions).toHaveLength(1);
    expect(versions[0]).not.toContain('-');
    expect(parseVersion(versions[0])[0], 'vite must stay on the reviewed major').toBe(6);
    expect(atLeast(versions[0], [6, 4, 5]), `vite ${versions[0]} is vulnerable`).toBe(true);
  });

  it('keeps every js-yaml copy above the empty-merge CPU exhaustion floor', () => {
    // GHSA-2883-xcg3-v3hh: our override keeps every transitive copy on patched 4.x.
    // The Kubernetes docs tests parse the actual copied RBAC YAML. Declare that
    // consumer directly instead of relying on legacy ESLint's transitive copy.
    expect(manifest.devDependencies['js-yaml']).toBe('^4.3.2');
    expect(manifest.overrides['js-yaml']).toBe('^4.3.2');
    const versions = lockedVersions('js-yaml');
    expect(versions, 'the Kubernetes docs parser must be installed').not.toHaveLength(0);
    for (const version of versions) {
      expect(version).not.toContain('-');
      expect(atLeast(version, [4, 3, 2]), `js-yaml ${version} is vulnerable`).toBe(true);
    }
  });

  it('keeps modern glob consumers above the minimatch amplification floor', () => {
    // GHSA-g3hv-75wg-m249 affects every minimatch release before 11.0.1.
    // Scope the override to modern v10 edges, not callable/default v3 consumers.
    expect(manifest.overrides['minimatch@^10.0.0']).toBe('11.0.1');
    expect(manifest.overrides.minimatch).toBeUndefined();
    expect(lockedVersions('@eslint/eslintrc')).toEqual([]);
    const versions = lockedVersions('minimatch');
    expect(versions).not.toHaveLength(0);
    for (const version of versions) {
      expect(version).not.toContain('-');
      expect(atLeast(version, [11, 0, 1]), `minimatch ${version} is vulnerable`).toBe(true);
    }
  });

  it.each(['^3.4.13', '^3.4.14', '^3.5.0'])('accepts patched DOMPurify range %s', (range) => {
    expect(dompurifyRangeIsPatched(range)).toBe(true);
  });

  it.each(['^3.4.12', '^3.3.99', '^4.0.0', '^3.4.13-beta.1', '*', '>=3.4.13', '^3.4.13 || ^2.0.0'])(
    'rejects unsafe or unreviewed DOMPurify range %s',
    (range) => {
      expect(dompurifyRangeIsPatched(range)).toBe(false);
    },
  );

  it('keeps DOMPurify above the hook-detachment XSS floor', () => {
    expect(dompurifyRangeIsPatched(manifest.dependencies.dompurify)).toBe(true);
    const versions = lockedVersions('dompurify');
    expect(versions).not.toHaveLength(0);
    for (const version of versions) {
      expect(version).not.toContain('-');
      expect(atLeast(version, [3, 4, 13]), `dompurify ${version} is vulnerable`).toBe(true);
    }
  });

  it('keeps every brace-expansion line above both denial-of-service advisory floors', () => {
    const versions = lockedVersions('brace-expansion');
    expect(versions).not.toHaveLength(0);
    for (const version of versions) {
      expect(braceExpansionIsPatched(version), `brace-expansion ${version} is vulnerable`).toBe(
        true,
      );
    }
  });

  it('keeps braces out of the dependency graph', () => {
    // GHSA-vfj7-8cjw-p6xm affects every braces release (<= 3.0.3) and has no
    // patched version, so no override can fix it. Tailwind CSS 3 (via chokidar,
    // fast-glob and micromatch) and jscpd 4 pulled it in; Tailwind CSS 4 and
    // jscpd 5 do not. Keep it out rather than allowlisting the advisory.
    expect(lockedVersions('braces')).toEqual([]);
  });

  it('keeps nanoid custom generators above the zero-size loop floor', () => {
    const versions = lockedVersions('nanoid');
    expect(versions).not.toHaveLength(0);
    for (const version of versions) {
      expect(nanoidIsPatched(version), `nanoid ${version} is vulnerable`).toBe(true);
    }
  });

  it('keeps browserslist above the unbounded-cache and custom-stats floors', () => {
    const versions = lockedVersions('browserslist');
    expect(versions).not.toHaveLength(0);
    for (const version of versions) {
      expect(atLeast(version, [4, 28, 7]), `browserslist ${version} is vulnerable`).toBe(true);
    }
  });

  it('keeps @types/node and its undici-types companion aligned with the declared ES2022 lib', () => {
    // @types/node 20 shipped an .at() compatibility polyfill that masked the
    // missing ES2022 lib; 26 drops it, so tsconfig must declare ES2022 itself
    // (Dependabot #2099). Keep the manifest range, the lock and the lib in step.
    const range = manifest.devDependencies['@types/node'];
    expect(/^\^26\.\d+\.\d+$/.test(range), `unexpected @types/node range ${range}`).toBe(true);
    expect(atLeast(range.slice(1), [26, 5, 1])).toBe(true);
    const nodeTypes = lockedVersions('@types/node');
    expect(nodeTypes).toHaveLength(1);
    expect(nodeTypes[0]).not.toContain('-');
    expect(
      atLeast(nodeTypes[0], [26, 5, 1]),
      `@types/node ${nodeTypes[0]} is below the reviewed floor`,
    ).toBe(true);
    const undici = lockedVersions('undici-types');
    expect(undici).not.toHaveLength(0);
    for (const version of undici) {
      expect(
        atLeast(version, [8, 9, 0]),
        `undici-types ${version} must satisfy @types/node 26`,
      ).toBe(true);
    }
    expect(declaredLib).toContain('ES2022');
  });

  it('keeps the reviewed npm-minor-patch floors', () => {
    // Dependabot npm-minor-patch group, reviewed 2026-09-21 and 2026-09-23.
    // Each floor is the lowest version this review accepted, so a later
    // downgrade is rejected. The 2026-10-05 refresh raises @types/node in
    // frontend-modern only; tests/integration is unchanged.
    const floors: Array<[string, [number, number, number]]> = [
      // GHSA-p98j-92pf-mc4p (IN_PLACE afterSanitize hook XSS) affects 3.4.13-3.4.15.
      // GHSA-cg2g-xh64-v4q7, GHSA-mrv2-3pqr-j5jr and GHSA-c6gr-qvv4-5v3f
      // (SAFE_FOR_TEMPLATES, IN_PLACE detached handlers, SMIL href) need 3.4.17.
      ['dompurify', [3, 4, 17]],
      ['solid-js', [1, 9, 15]],
      ['@types/node', [26, 6, 3]],
      ['lucide-solid', [0, 577, 0]],
      ['typescript-eslint', [8, 71, 0]],
      ['@typescript-eslint/eslint-plugin', [8, 71, 0]],
      ['@typescript-eslint/parser', [8, 71, 0]],
      ['eslint-plugin-solid', [0, 18, 0]],
      ['postcss', [8, 5, 28]],
      ['vite-plugin-solid', [2, 11, 14]],
      ['vite-plugin-sri-gen', [1, 7, 4]],
    ];
    for (const [name, floor] of floors) {
      const versions = lockedVersions(name);
      expect(versions, `${name} must be locked`).not.toHaveLength(0);
      for (const version of versions) {
        expect(version).not.toContain('-');
        expect(atLeast(version, floor), `${name} ${version} is below the reviewed floor`).toBe(
          true,
        );
      }
    }
    expect(prettierPinIsReviewed(manifest.devDependencies.prettier)).toBe(true);
    const prettier = lockedVersions('prettier');
    expect(prettier).toHaveLength(1);
    expect(prettier[0]).toBe(manifest.devDependencies.prettier);
  });

  it.each(['3.9.9', '3.9.10', '3.10.0'])('accepts a reviewed exact Prettier pin %s', (version) => {
    expect(prettierPinIsReviewed(version)).toBe(true);
  });

  it.each(['3.9.8', '^3.9.9', '~3.9.9', '*', '>=3.9.9', '4.0.0', '3.9.9-beta.1', '3.09.9'])(
    'rejects an unsafe or unreviewed Prettier declaration %s',
    (version) => {
      expect(prettierPinIsReviewed(version)).toBe(false);
    },
  );

  it('keeps every typescript-eslint helper on the same reviewed release as its wrapper', () => {
    const wrapper = lockedVersions('typescript-eslint');
    expect(wrapper).toHaveLength(1);
    for (const name of [
      'typescript-eslint',
      '@typescript-eslint/parser',
      '@typescript-eslint/eslint-plugin',
    ]) {
      const range = manifest.devDependencies[name];
      expect(/^\^8\.\d+\.\d+$/.test(range), `unexpected ${name} range ${range}`).toBe(true);
      expect(atLeast(range.slice(1), [8, 71, 0])).toBe(true);
    }
    for (const name of [
      'typescript-eslint',
      '@typescript-eslint/eslint-plugin',
      '@typescript-eslint/parser',
      '@typescript-eslint/project-service',
      '@typescript-eslint/scope-manager',
      '@typescript-eslint/tsconfig-utils',
      '@typescript-eslint/type-utils',
      '@typescript-eslint/types',
      '@typescript-eslint/typescript-estree',
      '@typescript-eslint/utils',
      '@typescript-eslint/visitor-keys',
    ]) {
      const versions = lockedVersions(name);
      expect(versions, `${name} must be locked`).not.toHaveLength(0);
      for (const version of versions) {
        expect(version).not.toContain('-');
        expect(atLeast(version, [8, 71, 0]), `${name} ${version} is below the reviewed floor`).toBe(
          true,
        );
        expect(version, `${name} must match the wrapper`).toBe(wrapper[0]);
      }
    }
  });
});