|
| 1 | +import { afterEach, expect, it } from 'vitest'; |
| 2 | +import { mkdtempSync, mkdirSync, writeFileSync, symlinkSync, rmSync } from 'node:fs'; |
| 3 | +import { tmpdir } from 'node:os'; |
| 4 | +import { join } from 'node:path'; |
| 5 | +import { Writable } from 'node:stream'; |
| 6 | +import { literalPattern } from './literal-pattern.mjs'; |
| 7 | +import { serveStaticAsset } from './smoke-static-assets.mjs'; |
| 8 | + |
| 9 | +const temporary = []; |
| 10 | +afterEach(() => { for (const path of temporary.splice(0)) rmSync(path, { recursive: true, force: true }); }); |
| 11 | + |
| 12 | +it.each(['a.b', 'a+b', '(a+)+$', '[project]', 'a\\b'])('treats a project name as literal text: %s', name => { |
| 13 | + const pattern = literalPattern(name, { start: true, end: true }); |
| 14 | + expect(pattern.test(name)).toBe(true); |
| 15 | + expect(pattern.test(`prefix${name}`)).toBe(false); |
| 16 | + expect(pattern.test(`${name}suffix`)).toBe(false); |
| 17 | +}); |
| 18 | + |
| 19 | +it('does not expand project-name wildcards', () => { |
| 20 | + expect(literalPattern('a.b', { start: true, end: true }).test('axb')).toBe(false); |
| 21 | +}); |
| 22 | + |
| 23 | +function fixture() { |
| 24 | + const root = mkdtempSync(join(tmpdir(), 'cbm-static-assets-')); temporary.push(root); |
| 25 | + mkdirSync(join(root, 'spike')); mkdirSync(join(root, 'node_modules')); mkdirSync(join(root, 'spike-extra')); |
| 26 | + writeFileSync(join(root, 'spike', 'index.html'), 'fixture'); |
| 27 | + writeFileSync(join(root, 'outside.txt'), 'outside-root'); |
| 28 | + writeFileSync(join(root, 'spike-extra', 'outside.txt'), 'sibling-root'); |
| 29 | + return root; |
| 30 | +} |
| 31 | +function request(root, url) { |
| 32 | + return new Promise((resolve, reject) => { |
| 33 | + const chunks = []; let status; |
| 34 | + const response = new Writable({ write(chunk, encoding, done) { chunks.push(Buffer.from(chunk)); done(); } }); |
| 35 | + response.writeHead = code => { status = code; }; |
| 36 | + response.on('error', reject); |
| 37 | + response.on('finish', () => resolve({ status, body: Buffer.concat(chunks).toString() })); |
| 38 | + try { serveStaticAsset(root, { url }, response); } catch (error) { reject(error); } |
| 39 | + }); |
| 40 | +} |
| 41 | + |
| 42 | +it('serves an allowed static fixture and rejects traversal and sibling prefixes', async () => { |
| 43 | + const root = fixture(); |
| 44 | + expect(await request(root, '/spike/index.html')).toEqual({ status: 200, body: 'fixture' }); |
| 45 | + expect((await request(root, '/spike/../outside.txt')).status).toBe(404); |
| 46 | + expect((await request(root, '/spike/%2e%2e/outside.txt')).status).toBe(404); |
| 47 | + expect((await request(root, '/spike-extra/outside.txt')).status).toBe(404); |
| 48 | +}); |
| 49 | + |
| 50 | +it('rejects a symlink escaping an allowed static root', async () => { |
| 51 | + const root = fixture(); |
| 52 | + symlinkSync(join(root, 'outside.txt'), join(root, 'spike', 'escape.txt')); |
| 53 | + expect((await request(root, '/spike/escape.txt')).status).toBe(404); |
| 54 | +}); |
| 55 | + |
| 56 | +it('returns not found for malformed URL encoding instead of throwing', async () => { |
| 57 | + expect((await request(fixture(), '/spike/%zz')).status).toBe(404); |
| 58 | +}); |
| 59 | + |
| 60 | +it('allows a symlink whose real target stays inside the static root', async () => { |
| 61 | + const root = fixture(); |
| 62 | + symlinkSync(join(root, 'spike', 'index.html'), join(root, 'spike', 'alias.html')); |
| 63 | + expect(await request(root, '/spike/alias.html')).toEqual({ status: 200, body: 'fixture' }); |
| 64 | +}); |
0 commit comments