ident
All repositories: gitoria
7.7 KB
// tests/gitoria-origin.mjs — GATE G48: gitoria's registration in ident answers every repo address.// gitoria serves its selector on <repo>.gitoria.worldapi.org with the one API key of its main address. The September registration holds the main// address only, so ident refused the repo addresses ("ident does not answer this site"). At load ident now adds the wildcard origin// https://*.gitoria.worldapi.org to the app named gitoria.worldapi.org (and to no other app). This gate registers the app the old way (main address// only), proves a repo address is refused, restarts ident on the same storage (the load step), and proves: the repo address is answered with CORS, the// main address still is, a lookalike / a sub-sub-domain / another app's key are still refused, and a second restart changes nothing.// Own server (NEVER the dev server, NEVER real mail): ident on G48_PORT (default 8780), storage .scratch/gitoria-origin-gate (wiped at start).// Run: node tests/gitoria-origin.mjs (G48_APP=<checkout> runs it against that code)import { spawn } from 'node:child_process';import { readFileSync, readdirSync, rmSync, mkdirSync, writeFileSync } from 'node:fs';import { dirname, join } from 'node:path';import { fileURLToPath } from 'node:url';const APP = process.env.G48_APP || join(dirname(fileURLToPath(import.meta.url)), '..');const G = join(APP, '.scratch/gitoria-origin-gate');const PORT = Number(process.env.G48_PORT || 8780);const ID = 'http://127.0.0.1:' + PORT;const sleep = (ms) => new Promise(r => setTimeout(r, ms));let passed = 0, failed = 0;const check = (name, ok, detail = '') => { if (ok) { passed++; console.log(' ok ' + name); } else { failed++; console.log(' FAIL ' + name + (detail ? ' — ' + detail : '')); } };rmSync(G, { recursive: true, force: true });mkdirSync(G, { recursive: true });const sessDir = join(G, 'sess');const mail = join(G, 'mail.txt');let p = null, out = '', runs = 0;const start = async () => {runs++; out = '';p = spawn(join(APP, 'bin/hybriel'), ['project.hl'], { cwd: APP, env: { ...process.env, SMTP_HOST: '', SMTP_USER: '', SMTP_PASSWORD: '', IDENT_PORT: String(PORT), IDENT_STORAGE: join(G, 'ident'), IDENT_SESSIONS: sessDir + '/', IDENT_MAIL_SINK: mail, IDENT_IP_LIMIT: '1000', IDENT_IP_DAY_LIMIT: '1000' }, stdio: ['ignore', 'pipe', 'pipe'] });p.stdout.on('data', d => { out += d; }); p.stderr.on('data', d => { out += d; });const q = p;q.on('exit', () => writeFileSync(join(G, 'ident-' + runs + '.log'), out));for (const end = Date.now() + Number(process.env.IDENT_COLD_MS || 120000); ; await sleep(250)) { try { await fetch(ID + '/', { redirect: 'manual' }); break; } catch { if (Date.now() > end) throw new Error('ident did not come up'); } }};const stop = async () => { if (!p) return; const q = p; p = null; const done = new Promise(r => q.on('exit', r)); try { q.kill(); } catch {} await done; };// a visitor: its own cookie jarconst visitor = () => {const jar = {};const take = (r) => { for (const c of r.headers.getSetCookie()) { const [kv] = c.split(';'); const i = kv.indexOf('='); jar[kv.slice(0, i)] = kv.slice(i + 1); } };const cookie = () => Object.entries(jar).map(([k, v]) => k + '=' + v).join('; ');const get = async (path, headers = {}) => { const r = await fetch(ID + path, { redirect: 'manual', headers: { cookie: cookie(), ...headers } }); take(r); return { status: r.status, location: r.headers.get('location'), text: await r.text() }; };const post = async (path, body, headers = {}) => { const r = await fetch(ID + path, { method: 'POST', redirect: 'manual', headers: { 'content-type': 'application/json', cookie: cookie(), ...headers }, body: JSON.stringify(body) }); take(r); return { status: r.status, text: await r.text() }; };let n = 0;const face = async (event, payload) => { const r = await post('/__hl/emit', { t: 'emit', i: ++n, event, payload }); try { const j = JSON.parse(r.text); return j.ok ? j.value : { transport: r.status, text: r.text.slice(0, 200) }; } catch { return { transport: r.status, text: r.text.slice(0, 200) }; } };return { get, post, face };};const lastCode = (email) => { const l = readFileSync(mail, 'utf8').trim().split('\n').filter(x => x.startsWith(email + ' ')); return l.length ? l[l.length - 1].split(' ')[1] : null; };const sel = async (key, origin) => { const r = await fetch(ID + '/api/selector/identities?key=' + key, { headers: { origin } }); return { status: r.status, acao: r.headers.get('access-control-allow-origin'), cred: r.headers.get('access-control-allow-credentials') }; };const MAIN = 'https://gitoria.worldapi.org';try {await start();const a = visitor();await a.get('/');const em = '[email protected]';await a.post('/api/code', { email: em });const vc = await a.face('verifyCode', [em, lastCode(em), 'Europe/Vienna']);check('setup: signed in', !!(vc.account && vc.account.email === em), JSON.stringify(vc).slice(0, 160));const g = await a.face('appCreate', [{ name: 'gitoria.worldapi.org', origins: [MAIN] }]);const t = await a.face('appCreate', [{ name: 'tickets.worldapi.org', origins: ['https://tickets.worldapi.org'] }]);const gk = g.app && g.app.apiKey, tk = t.app && t.app.apiKey;check('setup: the gitoria app is registered the old way (main address only)', /^pk_/.test(gk || '') && JSON.stringify(g.app.origins) === JSON.stringify([MAIN]), JSON.stringify(g).slice(0, 200));check('setup: a second app (tickets) is registered', /^pk_/.test(tk || ''), JSON.stringify(t).slice(0, 200));// BEFORE the load step: a repo address is refused (the bug)const before = await sel(gk, 'https://calendar.gitoria.worldapi.org');check('BEFORE: a repo address is refused (403, no CORS) — the reported bug', before.status === 403 && before.acao == null, JSON.stringify(before));check('BEFORE: the main address is answered', (await sel(gk, MAIN)).status === 200);// restart on the same storage: the load step adds the wildcardawait stop();await start();check('the load step says it added the wildcard to one app', /ident: 1 app got the wildcard origin/.test(out), out.slice(0, 300));for (const sub of ['calendar', 'mpackdb', 'a-b-1']) {const r = await sel(gk, 'https://' + sub + '.gitoria.worldapi.org');check('AFTER: https://' + sub + '.gitoria.worldapi.org is answered with CORS and credentials', r.status === 200 && r.acao === 'https://' + sub + '.gitoria.worldapi.org' && r.cred === 'true', JSON.stringify(r));}const m = await sel(gk, MAIN);check('AFTER: the main address is still answered', m.status === 200 && m.acao === MAIN, JSON.stringify(m));for (const bad of ['https://evil-gitoria.worldapi.org', 'https://gitoria.worldapi.org.evil.com', 'https://a.b.gitoria.worldapi.org', 'http://calendar.gitoria.worldapi.org', 'https://calendar.worldapi.org']) {const r = await sel(gk, bad);check('AFTER: ' + bad + ' is still refused', r.status === 403 && r.acao == null, JSON.stringify(r));}const tr = await sel(tk, 'https://calendar.gitoria.worldapi.org');check('AFTER: another app\'s key is not opened to gitoria\'s repo addresses', tr.status === 403, JSON.stringify(tr));const tr2 = await sel(tk, 'https://x.tickets.worldapi.org');check('AFTER: the tickets app got no wildcard of its own', tr2.status === 403, JSON.stringify(tr2));// a second load changes nothingawait stop();await start();check('a second load adds nothing (0 apps)', /ident: 0 app got the wildcard origin/.test(out), out.slice(0, 300));const r2 = await sel(gk, 'https://calendar.gitoria.worldapi.org');check('a second load: the repo address is still answered', r2.status === 200, JSON.stringify(r2));} catch (err) {check('gate ran without an exception', false, String(err && err.stack || err));} finally {await stop();}console.log(`\ngitoria-origin: ${passed} passed, ${failed} failed`);process.exit(failed ? 1 : 0);
Branches
Latest commits
- 0f6d1592Merge branch 'f18b-shell-session' into mainIdent
- 19868218|||PR ident#39 shell reads its session member instead of a cross-file emitmre
- 5d1b3fedMerge branch 'v61-day61' into mainIdent
- 83faeb15|||PR ident: vendor Hybriel day61mre
- b2572b34Merge branch 'v59-day59' into mainIdent
- 67cf016d|||PR ident: vendor Hybriel day59mre
- 600c043dMerge branch 'v58-day58' into mainIdent
- 365f8c80|||PR ident: vendor Hybriel day58mre
- 585126e5Merge branch 'f16-setf' into mainIdent
- 953bf03a|||PR ident setF mirror dropped (hybriel#370)mre
- a1419d9cMerge branch 'f15-navigate' into mainIdent
- 3b3e451f|||PR ident#36 navigate to the app instead of location.assignmre
- e36f4516Merge branch 'f14-online' into mainIdent
- 2c5eac91|||PR ident#38 online state without probe timermre
- bf1980f2Merge branch 'v56-day56' into mainIdent
- 62ae0adf|||PR ident: vendor Hybriel day56mre
- 0d18abc4Merge branch 'f13-avatar' into mainIdent
- 2a99730a|||PR ident#37 avatar in hlmre
- fa4ed25bMerge branch 'f11-upsert' into mainIdent
- 8f3d9bc9|||PR ident#42 keyed upsert and removemre