gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Main branchmain0f6d1592Merge branch 'f18b-shell-session' into mainIdentmain/tests/gitoria-origin.mjs

7.7 KB

  1. // tests/gitoria-origin.mjs — GATE G48: gitoria's registration in ident answers every repo address.
  2. // gitoria serves its selector on <repo>.gitoria.worldapi.org with the one API key of its main address. The September registration holds the main
  3. // address only, so ident refused the repo addresses ("ident does not answer this site"). At load ident now adds the wildcard origin
  4. // 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
  5. // 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
  6. // main address still is, a lookalike / a sub-sub-domain / another app's key are still refused, and a second restart changes nothing.
  7. // Own server (NEVER the dev server, NEVER real mail): ident on G48_PORT (default 8780), storage .scratch/gitoria-origin-gate (wiped at start).
  8. // Run: node tests/gitoria-origin.mjs (G48_APP=<checkout> runs it against that code)
  9. import { spawn } from 'node:child_process';
  10. import { readFileSync, readdirSync, rmSync, mkdirSync, writeFileSync } from 'node:fs';
  11. import { dirname, join } from 'node:path';
  12. import { fileURLToPath } from 'node:url';
  13. const APP = process.env.G48_APP || join(dirname(fileURLToPath(import.meta.url)), '..');
  14. const G = join(APP, '.scratch/gitoria-origin-gate');
  15. const PORT = Number(process.env.G48_PORT || 8780);
  16. const ID = 'http://127.0.0.1:' + PORT;
  17. const sleep = (ms) => new Promise(r => setTimeout(r, ms));
  18. let passed = 0, failed = 0;
  19. const check = (name, ok, detail = '') => { if (ok) { passed++; console.log(' ok ' + name); } else { failed++; console.log(' FAIL ' + name + (detail ? ' — ' + detail : '')); } };
  20. rmSync(G, { recursive: true, force: true });
  21. mkdirSync(G, { recursive: true });
  22. const sessDir = join(G, 'sess');
  23. const mail = join(G, 'mail.txt');
  24. let p = null, out = '', runs = 0;
  25. const start = async () => {
  26. runs++; out = '';
  27. 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'] });
  28. p.stdout.on('data', d => { out += d; }); p.stderr.on('data', d => { out += d; });
  29. const q = p;
  30. q.on('exit', () => writeFileSync(join(G, 'ident-' + runs + '.log'), out));
  31. 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'); } }
  32. };
  33. 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; };
  34. // a visitor: its own cookie jar
  35. const visitor = () => {
  36. const jar = {};
  37. 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); } };
  38. const cookie = () => Object.entries(jar).map(([k, v]) => k + '=' + v).join('; ');
  39. 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() }; };
  40. 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() }; };
  41. let n = 0;
  42. 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) }; } };
  43. return { get, post, face };
  44. };
  45. 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; };
  46. 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') }; };
  47. const MAIN = 'https://gitoria.worldapi.org';
  48. try {
  49. await start();
  50. const a = visitor();
  51. await a.get('/');
  52. const em = '[email protected]';
  53. await a.post('/api/code', { email: em });
  54. const vc = await a.face('verifyCode', [em, lastCode(em), 'Europe/Vienna']);
  55. check('setup: signed in', !!(vc.account && vc.account.email === em), JSON.stringify(vc).slice(0, 160));
  56. const g = await a.face('appCreate', [{ name: 'gitoria.worldapi.org', origins: [MAIN] }]);
  57. const t = await a.face('appCreate', [{ name: 'tickets.worldapi.org', origins: ['https://tickets.worldapi.org'] }]);
  58. const gk = g.app && g.app.apiKey, tk = t.app && t.app.apiKey;
  59. 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));
  60. check('setup: a second app (tickets) is registered', /^pk_/.test(tk || ''), JSON.stringify(t).slice(0, 200));
  61. // BEFORE the load step: a repo address is refused (the bug)
  62. const before = await sel(gk, 'https://calendar.gitoria.worldapi.org');
  63. check('BEFORE: a repo address is refused (403, no CORS) — the reported bug', before.status === 403 && before.acao == null, JSON.stringify(before));
  64. check('BEFORE: the main address is answered', (await sel(gk, MAIN)).status === 200);
  65. // restart on the same storage: the load step adds the wildcard
  66. await stop();
  67. await start();
  68. 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));
  69. for (const sub of ['calendar', 'mpackdb', 'a-b-1']) {
  70. const r = await sel(gk, 'https://' + sub + '.gitoria.worldapi.org');
  71. 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));
  72. }
  73. const m = await sel(gk, MAIN);
  74. check('AFTER: the main address is still answered', m.status === 200 && m.acao === MAIN, JSON.stringify(m));
  75. 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']) {
  76. const r = await sel(gk, bad);
  77. check('AFTER: ' + bad + ' is still refused', r.status === 403 && r.acao == null, JSON.stringify(r));
  78. }
  79. const tr = await sel(tk, 'https://calendar.gitoria.worldapi.org');
  80. check('AFTER: another app\'s key is not opened to gitoria\'s repo addresses', tr.status === 403, JSON.stringify(tr));
  81. const tr2 = await sel(tk, 'https://x.tickets.worldapi.org');
  82. check('AFTER: the tickets app got no wildcard of its own', tr2.status === 403, JSON.stringify(tr2));
  83. // a second load changes nothing
  84. await stop();
  85. await start();
  86. check('a second load adds nothing (0 apps)', /ident: 0 app got the wildcard origin/.test(out), out.slice(0, 300));
  87. const r2 = await sel(gk, 'https://calendar.gitoria.worldapi.org');
  88. check('a second load: the repo address is still answered', r2.status === 200, JSON.stringify(r2));
  89. } catch (err) {
  90. check('gate ran without an exception', false, String(err && err.stack || err));
  91. } finally {
  92. await stop();
  93. }
  94. console.log(`\ngitoria-origin: ${passed} passed, ${failed} failed`);
  95. 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