SPB Git

Repository Public MIT Selected

crawl-guard.test.mjs 4.6 KB 83 lines javascript

1/**2 * ─────────────────────────────────────────────3 *  SPB Git — Personal Git Platform4 * ─────────────────────────────────────────────5 *  Author  : Simon-Pierre Boucher6 *  Contact : contact@spboucher.ai7 *  File    : test/crawl-guard.test.mjs8 *  Purpose : Historical-URL detection, per-address budget, visitor pass and pruning of stale commit caches9 *  License : MIT © Simon-Pierre Boucher10 * ─────────────────────────────────────────────11 */12import { mkdirSync, mkdtempSync, writeFileSync, existsSync, utimesSync } from 'node:fs';13import { tmpdir } from 'node:os';14import { join } from 'node:path';15import { describe, expect, it } from 'vitest';16import { createCrawlGuard, createVisitorPass, isHistoricalUrl, readPassCookie } from '../src/lib/crawl-guard.mjs';17import { Cache } from '../src/lib/cache.mjs';1819describe('isHistoricalUrl', () => {20  it('flags pages pinned to a commit SHA', () => {21    for (const url of ['/wp9_uqo/blob/1705d9c/paper/main.tex', '/wp9_uqo/tree/1705d9c0a1b2c3d4e5f60718293a4b5c6d7e8f90', '/wp9_uqo/commit/1705d9c',22      '/wp9_uqo/commits/1705d9c?page=2', '/raw/wp9_uqo/1705d9c/README.md', '/wp9_uqo/blame/1705d9c/src/a.py']) expect(isHistoricalUrl(url), url).toBe(true);23  });24  it('leaves branch pages, the home page and assets alone', () => {25    for (const url of ['/', '/wp9_uqo', '/wp9_uqo/blob/main/README.md', '/wp9_uqo/tree/review-20261103', '/raw/wp9_uqo/main/docs/readme/banner.svg',26      '/wp9_uqo/commits', '/wp9_uqo/commits/main', '/assets/css/app.css', '/api/v1/repos', '/wp9_uqo.git/info/refs?service=git-upload-pack'])27      expect(isHistoricalUrl(url), url).toBe(false);28  });29});3031describe('createCrawlGuard', () => {32  it('allows `max` requests per window and address, then refuses until the window ends', () => {33    const guard = createCrawlGuard({ max: 3, windowMs: 1000 });34    expect([1, 2, 3, 4].map(() => guard.hit('a', 0).allowed)).toEqual([true, true, true, false]);35    expect(guard.hit('b', 10).allowed).toBe(true);36    expect(guard.hit('a', 500).retryAfter).toBe(1);37    expect(guard.hit('a', 1001).allowed).toBe(true);38  });39});4041describe('createVisitorPass', () => {42  const pass = createVisitorPass({ secret: 'k', ttlMs: 1000 });43  it('accepts its own fresh passes and refuses expired, altered, foreign or malformed ones', () => {44    const token = pass.mint(5000);45    expect(pass.verify(token, 5500)).toBe(true);46    expect(pass.verify(token, 6001)).toBe(false);47    expect(pass.verify(token.slice(0, -1) + (token.endsWith('A') ? 'B' : 'A'), 5500)).toBe(false);48    expect(createVisitorPass({ secret: 'other' }).verify(token, 5500)).toBe(false);49    for (const bad of [undefined, '', 'abc', '.abc', 'zz.' + 'a'.repeat(80)]) expect(pass.verify(bad, 5500)).toBe(false);50    expect(pass.verify(pass.mint(9_000_000), 5500)).toBe(false);51  });52  it('reads the pass among other cookies', () => {53    expect(readPassCookie('theme=dark; spb_pass=abc.def; x=1')).toBe('abc.def');54    expect(readPassCookie('theme=dark')).toBeUndefined();55    expect(readPassCookie(undefined)).toBeUndefined();56  });57  it('one shared budget stops a crawler spread over many addresses', () => {58    const anon = createCrawlGuard({ max: 20, windowMs: 60_000 });59    const served = Array.from({ length: 300 }, () => anon.hit('*', 0).allowed).filter(Boolean).length;60    expect(served).toBe(20);61  });62});6364describe('Cache#pruneRepo', () => {65  it('removes old commit directories that are not tips and keeps tips and recent ones', async () => {66    const root = mkdtempSync(join(tmpdir(), 'spbgit-prune-'));67    const cache = new Cache(root);68    const old = new Date(Date.now() - 48 * 3600_000);69    for (const sha of ['tip0000', 'old1111', 'new2222']) {70      mkdirSync(join(root, 'repos', 'demo', sha), { recursive: true });71      writeFileSync(join(root, 'repos', 'demo', sha, 'x.json'), '{}');72    }73    utimesSync(join(root, 'repos', 'demo', 'tip0000'), old, old);74    utimesSync(join(root, 'repos', 'demo', 'old1111'), old, old);75    const removed = await cache.pruneRepo('demo', new Set(['tip0000']), 24 * 3600_000);76    expect(removed).toBe(1);77    expect(existsSync(join(root, 'repos', 'demo', 'tip0000'))).toBe(true);78    expect(existsSync(join(root, 'repos', 'demo', 'old1111'))).toBe(false);79    expect(existsSync(join(root, 'repos', 'demo', 'new2222'))).toBe(true);80    expect(await cache.pruneRepo('missing', new Set(), 1)).toBe(0);81  });82});83
Files main
Menu

Theme