gitoria

notes

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit2e7f86642e7f8664notes#13: the list and back links pass the note key from the loop (emit pick(e, r.key)) instead of writing data-note into the View and reading it back with getAttribute; the two window.history.replaceState calls and the identcode e.target.value read stay (hl:web has no replace-navigation, a bound hidden input is not adopted on a script's change event), see NO13 reportmre2e7f8664/tests/ident-access-notes.mjs

12.0 KB

  1. // tests/ident-access-notes.mjs — GATE: notes' API accepts an ident access key (`Authorization: Bearer idt_…`, ident#27, app side).
  2. // A LOCAL ident (tests/identkit.mjs, NOTES_GATE_IDENT_DIR) with an app 'notes' and identities that were given keys through ident's own API;
  3. // a LOCAL notes started with that ident's key and secret. Checked: a write key reads /api/me (the identity's user), saves, reads back, lists
  4. // and deletes a note; the same note is there for the SAME identity's browser session and not for another identity; a read key lists and
  5. // reads but save is 403; revoked, unknown, wrong-shape and other-app keys are 401; ident down is 503 and nothing is saved; the cookie session
  6. // still works as before (and still refuses another Origin).
  7. // node tests/ident-access-notes.mjs (NOTES_IDA_PORT 8760, NOTES_IDA_IDENT_PORT 8761; every PID it starts is killed at the end)
  8. import { spawn } from 'node:child_process';
  9. import { rmSync, mkdirSync, existsSync } from 'node:fs';
  10. import { dirname, join, resolve } from 'node:path';
  11. import { fileURLToPath } from 'node:url';
  12. import { startIdent } from './identkit.mjs';
  13. const APP = resolve(dirname(fileURLToPath(import.meta.url)), '..');
  14. const BIN = join(APP, 'bin/hybriel');
  15. const PORT = Number(process.env.NOTES_IDA_PORT || 8760), IPORT = Number(process.env.NOTES_IDA_IDENT_PORT || 8761);
  16. const BASE = `http://127.0.0.1:${PORT}`;
  17. const IDENT_DIR = process.env.NOTES_GATE_IDENT_DIR || [resolve(APP, '../ident.worldapi.org'), '/media/STORAGE/projects/WorldAPI/ident.worldapi.org'].find(d => existsSync(join(d, 'project.hl')));
  18. const WORK = process.env.NOTES_IDA_DIR || join(APP, '.scratch/ident-access-gate');
  19. const COLD_MS = Number(process.env.NOTES_COLD_MS || 120000);
  20. const J = JSON.stringify;
  21. const sleep = (ms) => new Promise(r => setTimeout(r, ms));
  22. let passes = 0, failures = 0;
  23. const check = (label, ok, detail = '') => { console.log(`${ok ? ' ok ' : ' FAIL'} ${label}${ok ? '' : ' — ' + String(detail).slice(0, 400)}`); if (ok) passes++; else failures++; };
  24. rmSync(WORK, { recursive: true, force: true });
  25. mkdirSync(join(WORK, 'mpackdb'), { recursive: true }); mkdirSync(join(WORK, 'sessions'), { recursive: true });
  26. let ident = null, server = null, log = '';
  27. const stopAll = async () => { try { server && server.kill(); } catch {} try { ident && await ident.stop(); } catch {} };
  28. process.on('exit', () => { try { server && server.kill(); } catch {} try { ident && ident.proc.kill(); } catch {} });
  29. try {
  30. ident = await startIdent({ identDir: IDENT_DIR, workDir: join(WORK, 'ident'), port: IPORT });
  31. const alice = await ident.signIn('[email protected]');
  32. const mkApp = async (name, origins) => { const a = await ident.registerApp(alice, name, origins); a.id = a.raw.app.id; return a; };
  33. const nt = await mkApp('notes', [BASE]), other = await mkApp('otherapp', ['http://127.0.0.1:1']);
  34. const give = async (name, apps, level, identity) => {
  35. const r = await ident.emit('accGiveAccess', [identity ? { identity, apps, level, label: name } : { newName: name, apps, level, label: name }], alice.cookie);
  36. if (!r.value || !r.value.key) throw new Error('give failed: ' + r.raw);
  37. return r.value;
  38. };
  39. // the writer's key belongs to alice's OWN first identity: the one her browser login signs in as
  40. const aliceId = (alice.identities[0] || {}).id;
  41. if (!aliceId) throw new Error('alice has no identity: ' + J(alice.identities));
  42. const writer = await give('Hermes', [nt.id], 'write', aliceId);
  43. const reader = await give('Reader', [nt.id], 'read');
  44. const stranger = await give('Stranger', [other.id], 'write');
  45. const victim = await give('Victim', [nt.id], 'write');
  46. server = spawn(BIN, ['project.hl'], { cwd: APP, stdio: ['ignore', 'pipe', 'pipe'], env: { ...process.env, NOTES_PORT: String(PORT),
  47. NOTES_STORAGE: join(WORK, 'mpackdb'), NOTES_SESSIONS: join(WORK, 'sessions'), NOTES_WATCH: '0', NOTES_PUBLIC_URL: BASE, HL_HOST: '127.0.0.1',
  48. IDENT_URL: ident.base, IDENT_EXCHANGE_URL: ident.base, IDENT_API_KEY: nt.key, IDENT_API_SECRET: nt.secret } });
  49. server.stdout.on('data', d => log += d); server.stderr.on('data', d => log += d);
  50. for (const end = Date.now() + COLD_MS; Date.now() < end;) { try { await fetch(BASE + '/api/me'); break; } catch { await sleep(300); } }
  51. const call = async (method, path, token, body, extra = {}) => {
  52. const h = { ...extra }; if (token) h.authorization = 'Bearer ' + token;
  53. if (body !== undefined) h['content-type'] = 'application/json';
  54. const r = await fetch(BASE + path, { method, headers: h, body: body === undefined ? undefined : J(body) });
  55. const t = await r.text(); let j = null; try { j = JSON.parse(t); } catch {}
  56. return { status: r.status, j, t };
  57. };
  58. const save = (key, id, text, base = 0, extra) => call('POST', '/api/notes', key, { op: 'save', id, local: id, text, base, at: Date.now() }, extra);
  59. console.log('# an idt_ write key');
  60. let r = await call('GET', '/api/me', writer.key);
  61. const meUser = r.j && r.j.user;
  62. check('/api/me with a write key gives a user id', r.status === 200 && typeof meUser === 'string' && meUser !== '', r.status + ' ' + r.t);
  63. r = await call('GET', '/api/me', writer.key);
  64. check('a second call gives the same user (none made twice)', r.j && r.j.user === meUser, r.t);
  65. r = await save(writer.key, 'keynote1', '# Made by a key\nno browser login');
  66. check('a write key saves a note (200)', r.status === 200 && r.j && r.j.text && r.j.text.startsWith('# Made by a key'), r.status + ' ' + r.t);
  67. const nid = r.j && r.j.id;
  68. r = await call('GET', '/api/notes?id=' + nid, writer.key);
  69. check('…and reads it back', r.status === 200 && r.j.text === '# Made by a key\nno browser login', r.status + ' ' + r.t);
  70. r = await call('GET', '/api/notes', writer.key);
  71. check('…and lists it', r.status === 200 && r.j.notes.some(n => n.id === nid && !n.gone), r.status + ' ' + r.t);
  72. console.log('# the same identity in a browser session sees it; another identity does not');
  73. const scode = await ident.selectorCode(alice, nt, BASE);
  74. const cb = await fetch(BASE + '/login/callback?ident_code=' + scode, { redirect: 'manual' });
  75. const ck = (cb.headers.get('set-cookie') || '').split(';')[0];
  76. const lj = await (await fetch(BASE + '/api/notes', { headers: { cookie: ck } })).json();
  77. check('the browser session of the key’s identity lists the key’s note', !!ck && !!lj.notes && lj.notes.some(n => n.id === nid), J(lj).slice(0, 200));
  78. const bobKey = (await give('Bobkey', [nt.id], 'write')).key;
  79. r = await call('GET', '/api/notes', bobKey);
  80. const meBob = (await call('GET', '/api/me', bobKey)).j.user;
  81. check('a second identity’s key is a different user', meBob && meBob !== meUser, meBob + ' ' + meUser);
  82. check('a key never sees another identity’s notes', r.status === 200 && !r.j.notes.some(n => n.id === nid), r.t);
  83. r = await call('GET', '/api/notes?id=' + nid, bobKey);
  84. check('…nor reads one by id (404)', r.status === 404, r.status + ' ' + r.t);
  85. console.log('# an idt_ read key');
  86. const rme = await call('GET', '/api/me', reader.key);
  87. check('a read key: /api/me 200', rme.status === 200 && rme.j.user, rme.status + ' ' + rme.t);
  88. r = await call('GET', '/api/notes', reader.key);
  89. check('a read key: list 200', r.status === 200, r.status + ' ' + r.t);
  90. const rn = await call('GET', '/api/notes?id=' + nid, reader.key);
  91. r = await save(reader.key, 'readernote', 'nope');
  92. check('a read key may not save: 403', r.status === 403, r.status + ' ' + r.t);
  93. r = await call('POST', '/api/notes', reader.key, { op: 'delete', id: nid, base: 0 });
  94. check('a read key may not delete: 403', r.status === 403, r.status + ' ' + r.t);
  95. r = await call('GET', '/api/notes?id=' + nid, writer.key);
  96. check('the note is untouched by the read key', r.status === 200 && r.j.text.startsWith('# Made by a key'), r.t);
  97. const wl = (await call('GET', '/api/notes', writer.key)).j.notes;
  98. check('nothing was saved by the read key', !wl.some(n => n.id === 'readernote' || n.id === 'readernote'), J(wl));
  99. console.log('# refused keys');
  100. const countNow = async () => (await call('GET', '/api/notes', writer.key)).j.notes.length;
  101. const n0 = await countNow();
  102. r = await save(stranger.key, 'foreign1', 'foreign');
  103. check('a key for another app: 401', r.status === 401, r.status + ' ' + r.t);
  104. r = await save('idt_' + '0'.repeat(48), 'unknown1', 'unknown');
  105. check('an unknown well-formed key: 401', r.status === 401, r.status + ' ' + r.t);
  106. r = await save('idt_abc', 'short1', 'short');
  107. check('a malformed idt_ key: 401', r.status === 401, r.status + ' ' + r.t);
  108. r = await call('GET', '/api/me', 'idt_' + '0'.repeat(48));
  109. check('/api/me with an unknown key: 401', r.status === 401, r.status + ' ' + r.t);
  110. r = await save(victim.key, 'victim1', 'before revoke');
  111. check('before the revoke the victim key writes (200)', r.status === 200, r.status + ' ' + r.t);
  112. await ident.emit('accRevokeCell', [victim.identity, nt.id], alice.cookie);
  113. r = await save(victim.key, 'victim2', 'after revoke');
  114. check('revoked: 401 at the very next call', r.status === 401, r.status + ' ' + r.t);
  115. check('no refusal carries the key back', !r.t.includes(victim.key));
  116. check('refused keys wrote nothing for the writer or for bob', (await countNow()) === n0 && (await call('GET', '/api/notes', bobKey)).j.notes.length === 0, '');
  117. console.log('# delete by key, then the old ways');
  118. r = await call('POST', '/api/notes', writer.key, { op: 'delete', id: nid, base: (await call('GET', '/api/notes?id=' + nid, writer.key)).j.edited });
  119. check('a write key deletes its note', r.status === 200 && r.j.ok, r.status + ' ' + r.t);
  120. r = await call('GET', '/api/notes?id=' + nid, writer.key);
  121. check('…and it is gone (404)', r.status === 404, r.status + ' ' + r.t);
  122. r = await call('GET', '/api/notes', null);
  123. check('no header: still 401', r.status === 401, r.status + ' ' + r.t);
  124. r = await call('POST', '/api/notes', null, { op: 'save', id: 'x', local: 'x', text: 'x', base: 0, at: 1 });
  125. check('no header, POST: still 401', r.status === 401, r.status + ' ' + r.t);
  126. r = await call('GET', '/api/notes', 'tkt_' + '0'.repeat(48));
  127. check('another Bearer scheme: still 401 (session path)', r.status === 401, r.status + ' ' + r.t);
  128. if (ck) {
  129. const pr = await fetch(BASE + '/api/notes', { method: 'POST', headers: { cookie: ck, 'content-type': 'application/json' }, body: J({ op: 'save', id: 'sess1', local: 'sess1', text: 'by session', base: 0, at: Date.now() }) });
  130. check('a browser session still saves', pr.status === 200, '' + pr.status);
  131. const pr2 = await fetch(BASE + '/api/notes', { method: 'POST', headers: { cookie: ck, origin: 'https://evil.example', 'content-type': 'application/json' }, body: J({ op: 'save', id: 'sess2', local: 'sess2', text: 'csrf', base: 0, at: Date.now() }) });
  132. check('a session with another Origin is still refused (403)', pr2.status === 403, '' + pr2.status);
  133. const mr = await (await fetch(BASE + '/api/me', { headers: { cookie: ck } })).json();
  134. check('/api/me by session gives the same user as the key', mr.user === meUser, J(mr));
  135. } else check('a browser session still saves', false, 'no session');
  136. console.log('# ident down');
  137. await ident.stop();
  138. const c1 = await countNow().catch(() => -1);
  139. r = await save(writer.key, 'down1', 'while ident is down');
  140. check('ident down: a valid write key is refused with 503', r.status === 503 && !r.t.includes(writer.key) && !r.t.includes(nt.secret), r.status + ' ' + r.t);
  141. r = await call('GET', '/api/notes', reader.key);
  142. check('ident down: a read key is refused with 503 too', r.status === 503, r.status + ' ' + r.t);
  143. r = await call('GET', '/api/notes', null);
  144. check('ident down: no header is a plain 401', r.status === 401, r.status + ' ' + r.t);
  145. if (ck) {
  146. const lr = await fetch(BASE + '/api/notes', { headers: { cookie: ck } });
  147. const lj = await lr.json();
  148. check('ident down: the session still lists, and nothing was saved by the 503', lr.status === 200 && !lj.notes.some(n => n.id === 'down1'), lr.status + ' ' + J(lj).slice(0, 200));
  149. }
  150. } catch (e) {
  151. failures++; console.log('FAIL gate error: ' + (e.stack || e) + '\n' + log.slice(-1500));
  152. } finally {
  153. await stopAll();
  154. }
  155. console.log(`\n${passes} passed, ${failures} failed`);
  156. process.exit(failures ? 1 : 0);

Branches

Latest commits

  • 2e7f8664notes#13: the list and back links pass the note key from the loop (emit pick(e, r.key)) instead of writing data-note into the View and reading it back with getAttribute; the two window.history.replaceState calls and the identcode e.target.value read stay (hl:web has no replace-navigation, a bound hidden input is not adopted on a script's change event), see NO13 reportmre
  • 4dc98282notes#15: a deleted note made in this tab leaves the list at once (the delete now removes this tab's edit under every key the row has: local id, server id); gate: browser.mjs notes#15 section, red on the old codemre
  • e4e7402fnotes#11: plain declarations instead of let for variables never reassigned (components/notes.hl 3, lib/jsoncheck.hl 5, lib/util.hl 2, tools/migrate-short-ids.hl 8)mre
  • 27b6a09enotes#8: Style blocks in the nested form (note-list, note-editor, .ask with their children nested, :empty as a nested pseudo-class); the same CSS rules, the pages look the samemre
  • 71a9538bnotes: B27W report, log linemre
  • 78ba0073notes: an ident access key (Authorization: Bearer idt_…) reads and writes its identity's notes through /api/me and /api/notes; gate tests/ident-access-notes.mjs (ident#27)mre
  • 3de46dacnotes: the sign-in callback keeps the session it finds and signs it in (same cookie, nothing signed out; ident#28); security gate 73+5 failing → 78 passedmre
  • 5909d458notes: MdEditor from components 59704ce (text on the clipboard always pastes, a file manager copy still attaches; pipe rows without a delimiter row; tickets#49, notes#5)mre
  • 98bd6a98N5b: pipe rows without a delimiter row are a table (MdEditor ad4e929, plain copy); gate tests/dashboard-table.mjs; reportmre
  • dbf93319N5 report: components gate rerun 613 passed, 0 failedmre
  • 3a8bc0d2N5: empty Table-button table is written as a real pipe table (MdEditor 4bd1112, plain copy); gate tests/table-button.mjs; reportmre
  • c16bd443N5: step 1 report (tables pass locally; his DASHBOARD note has no delimiter row)mre
  • 6091320anotes: MdEditor 7437e32 (surface drawn at once when the page is claimed); gate measures claim-to-text on 5 note loads at 1280 and 390mre
  • 0aa9277enotes: move each component's styles into its own Style block (shell and header selector in main.hl, list, editor frame and delete question in notes.hl); styles.hl keeps the theme inherit, the accent and the message colour; .gitignore ignores a bin symlink too; tests/shots.mjs for the pixel comparemre
  • 9ba01de3notes: write the standby service out in full so the boot gate can read the main servicemre
  • 98cab40anotes: standby service on its port plus 10 and a deploy that hands over through it without a gapmre
  • 1fadb473notes gate: the editor table now has 1 px cell lines from the notes thememre
  • 8762115cnotes: take the components e54f83a (paste as Markdown, text typed before the editor is built is kept, Table button); table lines come from the notes thememre
  • ac23e194ident#31: the selector's sign-in returns to the page the user started on (return attribute = the login callback with next), gate checkmre
  • 2804f0d3Merge branch 'iw-ident-widget'mre