r
This commit is contained in:
1 parent
76a4db6a9e
commit
783c95b797
21 files changed
+1245
-46
No files matched your search
+15
-4
@@ -14,10 +14,11 @@ test('Evidence mappings use real process IDs and complete translations', () => {
|
||||
for (const e of p.evidence) {
|
||||
assert.ok(e.codes.every(c => ids.has(c)), e.path);
|
||||
assert.ok(e.label.length === 2 && e.label.every(Boolean));
|
||||
const url = new URL(source(e.path, e.directory));
|
||||
assert.equal(url.hostname, 'github.com');
|
||||
assert.ok(url.pathname.includes(revision));
|
||||
assert.equal(decodeURIComponent(url.pathname.split(`${revision}/`)[1]), e.path);
|
||||
const url = new URL(source(e.path, e.directory, p));
|
||||
const pin = p.repository?.revision || revision;
|
||||
assert.equal(url.hostname, p.repository ? new URL(p.repository.url).hostname : 'github.com');
|
||||
assert.ok(url.pathname.includes(pin));
|
||||
assert.equal(decodeURIComponent(url.pathname.split(`${pin}/`)[1]), e.path);
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -26,3 +27,13 @@ test('Collaboration gaps remain explicit rather than claiming unsupported eviden
|
||||
assert.ok(!projects.some(p => p.evidence.some(e => e.codes.includes(code))));
|
||||
}
|
||||
});
|
||||
|
||||
test('Repository links distinguish GitHub and Gitea and encode paths', () => {
|
||||
const revision = 'a'.repeat(40);
|
||||
const github = { repository: { url: 'https://github.com/example/repo', revision, forge: 'github' } };
|
||||
const gitea = { repository: { url: 'https://codealvin.xyz/example/repo', revision, forge: 'gitea' } };
|
||||
assert.equal(source('a b/test.md', false, github), github.repository.url + '/blob/' + revision + '/a%20b/test.md');
|
||||
assert.equal(source('', true, github), github.repository.url + '/tree/' + revision);
|
||||
assert.equal(source('src', true, gitea), gitea.repository.url + '/src/commit/' + revision + '/src');
|
||||
assert.equal(source('a b.md', false, gitea), gitea.repository.url + '/src/commit/' + revision + '/a%20b.md');
|
||||
});
|
||||
@@ -9,6 +9,7 @@ import { runInNewContext } from 'node:vm';
|
||||
test('content compiler preserves source data', async () => {
|
||||
const result = await import(`data:text/javascript;base64,${Buffer.from(await compileContent()).toString('base64')}`);
|
||||
assert.deepEqual(result.projects, projects);
|
||||
assert.deepEqual(Object.keys(result).sort(), ['processes', 'profile', 'projects', 'revision', 'source']);
|
||||
});
|
||||
test('deleting all projects or evidence is supported', () => {
|
||||
assert.doesNotThrow(() => validateProjects([], processes));
|
||||
@@ -41,3 +42,41 @@ test('homepage renders after removing the featured project or all projects', asy
|
||||
else assert.ok(html.includes('No projects added yet.'));
|
||||
}
|
||||
});
|
||||
|
||||
test('Repository overrides require safe supported URLs and full commit pins', () => {
|
||||
for (const repository of [null, {}, { url: 'javascript:alert(1)', revision: 'a'.repeat(40), forge: 'gitea' }, { url: 'https://github.com/example/repo', revision: 'main', forge: 'github' }, { url: 'https://codealvin.xyz/example/repo', revision: 'a'.repeat(40), forge: 'github' }]) {
|
||||
assert.throws(() => validateProjects([{ ...projects[0], repository }], processes));
|
||||
}
|
||||
assert.throws(() => validateProjects([{ ...projects[0], setting: ['Only one language'] }], processes));
|
||||
});
|
||||
|
||||
test('process injection is escaped in option values, labels, summaries and dossier links', async () => {
|
||||
const code = (await readFile(new URL('../app.js', import.meta.url), 'utf8')).replace(/^import .*;\r?\n/, '');
|
||||
const payload = '\"><img src=x onerror=alert(1)>&\'';
|
||||
const escaped = '"><img src=x onerror=alert(1)>&'';
|
||||
for (const language of ['nl', 'en']) {
|
||||
for (const hash of ['#evidence', `#project/${projects[0].id}`]) {
|
||||
const elements = new Map();
|
||||
const document = {
|
||||
documentElement: {},
|
||||
querySelector(selector) {
|
||||
if (!elements.has(selector)) elements.set(selector, { innerHTML: '', addEventListener() {} });
|
||||
return elements.get(selector);
|
||||
},
|
||||
querySelectorAll() { return []; },
|
||||
};
|
||||
runInNewContext(code, { profile, source, projects: [{ ...projects[0], codes: [payload] }], processes: [{ ...processes[0], code: payload }], document, URLSearchParams, location: { search: `?lang=${language}`, hash }, window: { addEventListener() {} } });
|
||||
const html = elements.get('main').innerHTML;
|
||||
assert.ok(!html.includes(payload));
|
||||
assert.ok(!html.includes('<img'));
|
||||
if (hash === '#evidence') {
|
||||
assert.ok(html.includes(`value="${escaped}"`));
|
||||
assert.ok(html.includes(`>${escaped} ·`));
|
||||
assert.ok(html.includes(`process-code">${escaped}</span>`));
|
||||
} else {
|
||||
assert.ok(html.includes(`href="#evidence/${escaped}"`));
|
||||
assert.ok(html.includes(`>${escaped}</a>`));
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,79 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { mkdtemp, cp, mkdir, writeFile, readFile, readdir, rm, symlink } from 'node:fs/promises';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import { spawn, execFileSync } from 'node:child_process';
|
||||
import { request } from 'node:http';
|
||||
import { once } from 'node:events';
|
||||
import { validateSettings } from '../scripts/content.mjs';
|
||||
const root = new URL('../', import.meta.url);
|
||||
const settings = JSON.parse(await readFile(new URL('content/settings.json', root)));
|
||||
const assets = ['app.js', 'content.js', 'favicon.svg', 'index.html', 'styles.css'];
|
||||
async function fixture(t) {
|
||||
const dir = await mkdtemp(join(tmpdir(), 'portfolio-security-'));
|
||||
t.after(() => rm(dir, { recursive: true, force: true }));
|
||||
for (const name of ['scripts', 'content', ...assets]) await cp(new URL(name, root), join(dir, name), { recursive: true });
|
||||
return dir;
|
||||
}
|
||||
test('build replaces stale output from any cwd and preserves output on invalid content', async t => {
|
||||
const dir = await fixture(t);
|
||||
await mkdir(join(dir, 'dist/private'), { recursive: true });
|
||||
await writeFile(join(dir, 'dist/private/secret.txt'), 'synthetic');
|
||||
const build = () => execFileSync(process.execPath, [join(dir, 'scripts/build.mjs')], { cwd: tmpdir(), stdio: 'pipe' });
|
||||
build();
|
||||
assert.deepEqual((await readdir(join(dir, 'dist'))).sort(), assets);
|
||||
for (const file of assets) assert.deepEqual(await readFile(join(dir, 'dist', file)), await readFile(join(dir, file)));
|
||||
await writeFile(join(dir, 'content/settings.json'), '{}');
|
||||
assert.throws(build);
|
||||
assert.deepEqual((await readdir(join(dir, 'dist'))).sort(), assets);
|
||||
});
|
||||
test('build refuses a redirected publication directory', async t => {
|
||||
const dir = await fixture(t);
|
||||
const outside = join(dir, 'protected');
|
||||
await mkdir(outside);
|
||||
await writeFile(join(outside, 'keep'), 'keep');
|
||||
await symlink(outside, join(dir, 'dist'), process.platform === 'win32' ? 'junction' : 'dir');
|
||||
assert.throws(() => execFileSync(process.execPath, [join(dir, 'scripts/build.mjs')], { stdio: 'pipe' }));
|
||||
assert.equal(await readFile(join(outside, 'keep'), 'utf8'), 'keep');
|
||||
});
|
||||
test('settings reject invalid structures, URLs, exports, duplicate codes and injection', () => {
|
||||
assert.doesNotThrow(() => validateSettings(settings));
|
||||
const mutations = [
|
||||
s => { s.extra = 'unexpected'; }, s => { s['x = 1; alert(1); //'] = 1; },
|
||||
s => { s.profile = null; }, s => { delete s.profile.name; },
|
||||
s => { s.revision = 'main'; }, s => { s.revision = 'A'.repeat(40); },
|
||||
s => { s.processes = {}; }, s => { s.processes.push(s.processes[0]); },
|
||||
s => { s.processes[0].code = '\"><img src=x onerror=alert(1)>'; },
|
||||
s => { s.processes[0].title = ['Dutch', '']; }, s => { s.processes[0].description = ['one']; },
|
||||
s => { s.profile.reviewed = '2026-02-30'; },
|
||||
s => { s.profile.reviewed = '2026-99-99'; },
|
||||
...['javascript:alert(1)', 'http://example.com', 'https://user:pass@example.com', 'https://example.com/\" onmouseover=\"alert(1)', 'https://example.com\\evil', 'https://'].map(url => s => { s.profile.qualification = url; }),
|
||||
s => { s.profile.school = 'https://github.com/user/repo?evil'; },
|
||||
s => { s.profile.github = 'https://github.com.evil.test/user'; },
|
||||
];
|
||||
for (const mutate of mutations) { const value = structuredClone(settings); mutate(value); assert.throws(() => validateSettings(value)); }
|
||||
});
|
||||
test('preview survives malformed URLs and denies private/source paths', async t => {
|
||||
const child = spawn(process.execPath, ['scripts/serve.mjs'], { cwd: root, env: { ...process.env, PORT: '0' }, stdio: ['ignore', 'pipe', 'pipe'] });
|
||||
t.after(async () => { child.kill(); if (child.exitCode === null) await once(child, 'exit'); });
|
||||
const address = await new Promise((resolve, reject) => {
|
||||
child.stdout.once('data', data => resolve(String(data).match(/:(\d+)/)[1]));
|
||||
child.once('error', reject); child.once('exit', code => reject(new Error(`Preview exited: ${code}`)));
|
||||
});
|
||||
const get = path => new Promise((resolve, reject) => {
|
||||
const req = request({ hostname: '127.0.0.1', port: address, path }, res => { res.resume(); res.on('end', () => resolve(res.statusCode)); });
|
||||
req.on('error', reject); req.end();
|
||||
});
|
||||
assert.equal(await get('http://[invalid'), 400);
|
||||
assert.equal(await get('/'), 200);
|
||||
for (const path of ['/content/settings.json', '/scripts/content.mjs', '/.env', '/docs/verification.md', '/package.json', '/%2e%2e/.env', '/dist/index.html']) assert.equal(await get(path), 404, path);
|
||||
for (const file of assets) assert.equal(await get(`/${file}`), 200);
|
||||
});
|
||||
|
||||
|
||||
test('invalid review dates produce a descriptive validation error', () => {
|
||||
const value = structuredClone(settings);
|
||||
value.profile.reviewed = '2026-99-99';
|
||||
assert.throws(() => validateSettings(value), /profile.reviewed: expected a valid ISO date/);
|
||||
});
|
||||
Reference in new issue
Block a user