repositories

daemon-sec-cheatsheet

The cheatsheet vault for operators: AD, enumeration, exploitation, priv-esc, web, DFIR

clonegit clone https://git.daemon-sec.xyz/daemon-sec-cheatsheet.git
internal-links.test.mjsJavaScript57 lines2.0 KiBraw download
import assert from 'node:assert/strict';
import { readdir, readFile } from 'node:fs/promises';
import test from 'node:test';

const SHEETS = new URL('../src/content/sheets/', import.meta.url);

// Every sheet on disk, keyed the way an in-page link addresses it.
const collectSheets = async () => {
  const sheets = [];
  for (const category of await readdir(SHEETS, { withFileTypes: true })) {
    if (!category.isDirectory()) continue;
    const dir = new URL(`${category.name}/`, SHEETS);
    for (const file of await readdir(dir)) {
      if (!file.endsWith('.md')) continue;
      sheets.push({
        id: `${category.name}/${file.slice(0, -3)}`,
        url: new URL(file, dir),
      });
    }
  }
  return sheets;
};

const LINK = /\]\(\/sheets\/([a-z0-9][a-z0-9-]*)\/([a-z0-9][a-z0-9-]*)(?=[)#])/g;

// Slug renames and category moves rot these links silently: the build still
// succeeds and the dead href only shows up when a reader clicks it. Two
// previous commits repaired them by hand, so the check lives here instead.
test('every internal /sheets/ link resolves to a sheet that exists', async () => {
  const sheets = await collectSheets();
  const known = new Set(sheets.map((s) => s.id));
  const broken = [];

  for (const sheet of sheets) {
    const body = await readFile(sheet.url, 'utf8');
    for (const [, category, slug] of body.matchAll(LINK)) {
      const target = `${category}/${slug}`;
      if (!known.has(target)) broken.push(`${sheet.id} -> /sheets/${target}`);
    }
  }

  assert.deepEqual(broken, [], `broken internal links:\n  ${broken.join('\n  ')}`);
});

test('no sheet links to itself', async () => {
  const sheets = await collectSheets();
  const selfLinks = [];

  for (const sheet of sheets) {
    const body = await readFile(sheet.url, 'utf8');
    for (const [, category, slug] of body.matchAll(LINK)) {
      if (`${category}/${slug}` === sheet.id) selfLinks.push(sheet.id);
    }
  }

  assert.deepEqual(selfLinks, [], `self-referential links:\n  ${selfLinks.join('\n  ')}`);
});