gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitd3c065a8d3c065a8security audit (SI): uploadMax 0, Secure __Host- cookie behind https, headers on function routes/files, page CSP+referrer meta, app/identity caps, tests/security.mjs, reportmred3c065a8/lib/api-helpers.hl

7.8 KB

  1. // lib/api-helpers.hl — the plumbing of the function routes (lib/api.hl): JSON answers, redirects, the HTML error
  2. // page, the STRICT JSON body, the app's key + secret check of a call, the selector's CORS answers. Statics only.
  3. //
  4. // STRICT: a body must be a JSON object; an unknown field, a missing field and a field of
  5. // the wrong type are each a 400 whose `error` names the field. hl:json's JSON.parse
  6. // aborts the request (500) on invalid input and the language has no catch (hybriel #12),
  7. // so the body's syntax is checked first by jsoncheck.hl (copied from tickets).
  8. import { Response } from 'hl:http1'
  9. import { jsonErrorAt } from './jsoncheck.hl'
  10. import { appByKey, appOfSecret } from './apps.hl'
  11. import { originsOf } from './apps-helpers.hl'
  12. import { publicUrl } from './invites.hl'
  13. // SECURITY HEADERS of every answer these routes make (mission SI): no sniffing, no referrer, no framing, and a JSON / redirect answer may
  14. // load nothing at all. (A component page's headers are hl:web's, not ours: reports/SI-sec.md.)
  15. static noSniff = 'nosniff'
  16. static dataPolicy = "default-src 'none'; frame-ancestors 'none'"
  17. static pagePolicy = "default-src 'none'; style-src 'unsafe-inline'; form-action 'none'; base-uri 'none'; frame-ancestors 'none'"
  18. static jsonHeaders = { 'Content-Type' = 'application/json; charset=utf-8' 'Cache-Control' = 'no-store' 'X-Content-Type-Options' = noSniff 'Referrer-Policy' = 'no-referrer' 'X-Frame-Options' = 'DENY' 'Content-Security-Policy' = dataPolicy }
  19. static htmlHeaders = { 'Content-Type' = 'text/html; charset=utf-8' 'Cache-Control' = 'no-store' 'X-Content-Type-Options' = noSniff 'Referrer-Policy' = 'no-referrer' 'X-Frame-Options' = 'DENY' 'Content-Security-Policy' = pagePolicy }
  20. // the headers a framework-rendered page (srv.page: the code page, the 404 page) gets from us
  21. static hardenPage = (res) => {
  22. res.headers['X-Content-Type-Options'] = noSniff
  23. res.headers['Referrer-Policy'] = 'same-origin'
  24. res.headers['X-Frame-Options'] = 'DENY'
  25. res.headers['Content-Security-Policy'] = "frame-ancestors 'none'"
  26. return res
  27. }
  28. static reply = (status, value) => { return new Response(JSON.stringify(value), { status = status headers = jsonHeaders }) }
  29. static fail = (status, message) => { return reply(status, { error = message }) }
  30. static redirect = (url) => { return new Response('', { status = 302 headers = { 'Location' = url 'Cache-Control' = 'no-store' 'X-Content-Type-Options' = noSniff 'Referrer-Policy' = 'no-referrer' 'X-Frame-Options' = 'DENY' } }) }
  31. // THE STRICT BODY: `spec` maps each allowed field to { type ('String' | 'Number' |
  32. // 'Boolean' | 'List'), required }. Answers { error } or { body }.
  33. static strictBody = (req, spec) => {
  34. raw = req.body
  35. if (raw == null || raw.trim() == '') { return { error = 'a JSON object body is required' } }
  36. at = jsonErrorAt(raw)
  37. if (at >= 0) { return { error = 'the body is not valid JSON (at character ' + at + ')' } }
  38. t = raw.trim()
  39. if (t[0] != '{') { return { error = 'the body must be a JSON object' } }
  40. b = JSON.parse(raw)
  41. for (k of b.keys()) {
  42. if (spec[k] == null) { return { error = 'unknown field: ' + k } }
  43. }
  44. for (k of spec.keys()) {
  45. let f = spec[k]
  46. let v = b[k]
  47. if (v == null) {
  48. if (f.required) { return { error = 'missing field: ' + k } }
  49. } else if (f.type == 'List') {
  50. if (hlTypeName(v) != 'Hybrid' || v.length == null) { return { error = 'field ' + k + ' must be a list' } }
  51. } else if (hlTypeName(v) != f.type) {
  52. return { error = 'field ' + k + ' must be a ' + f.type.toLowerCase() }
  53. }
  54. }
  55. return { body = b }
  56. }
  57. // ---- the HTML error page of the login button (never a redirect) ------------------------
  58. static escapeHtml = (s) => { return ('' + s).replaceAll('&', '&amp;').replaceAll('<', '&lt;').replaceAll('>', '&gt;').replaceAll('"', '&quot;') }
  59. static errorPage = (status, title, message) => {
  60. html = '<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>ident | ' + escapeHtml(title) + '</title><style>:root{--dark:rgb(25, 30, 35);--darker:rgb(15, 20, 25);--light:rgb(195, 200, 205);--lighter:rgb(245, 250, 255);--red:#f44747;--color-background:var(--dark);--color-surface:rgb(35, 40, 45);--color-border:rgb(70, 75, 80);--color-text:var(--light);--color-danger:var(--red);--color-accent:#ce9178}html{color-scheme:dark}body{margin:0;font:16px/1.5 system-ui,sans-serif;color:var(--color-text);background:var(--color-background)}application-header{display:block;padding:.6rem 1rem;background:var(--darker);border-bottom:1px solid var(--color-border)}application-header a{display:inline-flex;align-items:center;min-height:2.75rem;font-weight:800;font-size:1.15rem;color:var(--color-accent);text-decoration:none}main{padding:2rem 1rem}ident-error{display:block;max-width:34rem;margin:0 auto;padding:1rem;background:var(--color-surface);border:1px solid var(--color-border);border-radius:.5rem}h1{margin:0 0 .5rem;font-size:1.2rem;color:var(--color-danger)}p{margin:0;overflow-wrap:anywhere}</style></head><body><application-header><a href="/">ident</a></application-header><main><ident-error id="identerror"><h1>' + escapeHtml(title) + '</h1><p id="errormessage">' + escapeHtml(message) + '</p></ident-error></main></body></html>'
  61. return new Response(html, { status = status headers = htmlHeaders })
  62. }
  63. // ---- THE APP'S CALLS (key + secret, strict JSON body) ------------------------------------------
  64. static field = (n, t) => { return { type = t required = n } }
  65. // POST only, the strict body `spec`, then the app of its key + secret: answers { res } (the refusal) or { app, body }
  66. static inviteCaller = (req, spec) => {
  67. if (req.method != 'POST') { return { res = fail(405, 'POST only') } }
  68. b = strictBody(req, spec)
  69. if (b.error != null) { return { res = fail(400, b.error) } }
  70. a = appOfSecret(b.body.key, b.body.secret)
  71. if (a == null) { return { res = fail(401, 'unknown API key or wrong secret') } }
  72. return { app = a body = b.body }
  73. }
  74. // where an invite link points: IDENT_PUBLIC_URL, else the request's own host
  75. static baseOf = (req) => {
  76. if (publicUrl != null) { return publicUrl }
  77. proto = req.headers['x-forwarded-proto'] != null ? req.headers['x-forwarded-proto'] : 'http'
  78. return proto + '://' + req.headers['host']
  79. }
  80. // a login request id is 32 hex (lib/apps.hl openRequest)
  81. static hexId = (v) => {
  82. if (v.length != 32) { return false }
  83. let i = 0
  84. while (i < v.length) {
  85. if (!'0123456789abcdef'.includes(v[i])) { return false }
  86. i = i + 1
  87. }
  88. return true
  89. }
  90. // ---- THE SELECTOR's CORS answers (lib/selector.hl) ----------------------------------------------
  91. static jsonType = 'application/json; charset=utf-8'
  92. // the refusal: no CORS headers → the browser gives the page nothing
  93. static refuse = (status, message) => {
  94. return new Response(JSON.stringify({ error = message }), { status = status headers = { 'Content-Type' = jsonType 'Cache-Control' = 'no-store' 'Vary' = 'Origin' 'X-Content-Type-Options' = noSniff 'Referrer-Policy' = 'no-referrer' } })
  95. }
  96. static corsHeaders = (origin) => {
  97. return { 'Content-Type' = jsonType 'Cache-Control' = 'no-store' 'Vary' = 'Origin' 'X-Content-Type-Options' = noSniff 'Referrer-Policy' = 'no-referrer' 'Access-Control-Allow-Origin' = origin 'Access-Control-Allow-Credentials' = 'true' }
  98. }
  99. static answer = (origin, status, value) => {
  100. return new Response(JSON.stringify(value), { status = status headers = corsHeaders(origin) })
  101. }
  102. // WHO ASKS: answers { app (record), origin } or { status, error }
  103. static caller = (req) => {
  104. let origin = req.headers['origin']
  105. if (origin == null || origin == '') { return { status = 403 error = 'the selector answers browsers only (no Origin header)' } }
  106. q = req.query != null ? req.query : {}
  107. a = appByKey(q.key)
  108. if (a == null) { return { status = 403 error = 'no app has this API key' } }
  109. if (!originsOf(a).includes(origin)) { return { status = 403 error = 'this origin is not registered for the app' } }
  110. return { app = a origin = origin }
  111. }

Branches

Latest commits

  • d3c065a8security audit (SI): uploadMax 0, Secure __Host- cookie behind https, headers on function routes/files, page CSP+referrer meta, app/identity caps, tests/security.mjs, reportmre
  • 5561a0cbreport I4-mobilemre
  • 4ca2ed20404 page in the shell (viewport, status 404, way home); phone: account actions live in the user menu so the open menu covers nothing; mobile gate; signoutall waits for a cold startmre
  • 9baad745tests: browser gate waits for the reloaded document (offline reload race under CPU throttle); HL_CPU_THROTTLE knobmre
  • b96796bdtests: wait for hydration before real clicks after a navigation (H2)mre
  • 9722964fre-vendor Hybriel 5406d146 (hl:web batches 9+10: #72 emits while the socket is closed delivered after reconnect, browser boot linear, faster list appends; contains int 6–14 + m406)mre
  • c9b196c9tests/iplimit: wait for the welcome form itself after B's first login (identities list renders before it); mission 017 reportmre
  • 72e0f587ident#26: identity page with "Used in" (app, since, last use, Notif. chip, empty state); lastLogin recorded at exchange; gates + lisbeth E2E; mission 016 reportmre
  • 94576fbcident#26 WIP: identity page /identity/:iid with "Used in" (app, since, last use, Notif. chip), lastLogin recorded at exchange, "Used in…" link on the identities list; apps gate 4b, e2e-usedin.mjsmre
  • 349cb502tests/invites: wait for hydration on the code page before entering the code (native GET submit raced the welcome form); mission 015 reportmre
  • d1bf4720tests: no hard-coded ports — every gate's ports and fixture origins come from <GATE>_PORT / _CHROME env (defaults unchanged; Bob/fixture origins follow, selector cookie name too)mre
  • 6280c92fre-vendor Hybriel 6f8a0689 (int 6–11: streaming cursors, #203 index heal + close() fix, fd403 listDir/cursor fds, hl:time browser side, hl:web batches 5+6)mre
  • 147744bftests/migration: the fixture app origin follows MIGRATION_GATE_TESTAPP_PORT (oldstore-009 OLD_TESTAPP)mre
  • 48cd36bctests/migration: ports from env (MIGRATION_GATE_PORT, MIGRATION_GATE_TESTAPP_PORT); 8395/8396 were taken by a foreign servermre
  • 3d9b2fd8docker-compose: nofile 65536 (Hybriel int 6+ boot opened >1024 files in the tracker)mre
  • 19b7e3c3deploy: backup copies index files first and *.mpack data last (hot-copy skew, hybriel #203)mre
  • c8a596f4re-vendor Hybriel 66c3db71 (web batch 4); e2e: main menu marked after in-place sign-in is a normal check; report 014mre
  • 8f3dc1ectests: delivery allow-list for the stopped server's network errors uses the gate port, not 8700mre
  • 9cc8eddftests: notify and iplimit gate Chrome ports from env (NOTIFY_GATE_CHROME, IPLIMIT_GATE_CHROME_A/B)mre
  • d6da2d02tests: delivery and migration gate ports from env (DELIVERY_GATE_PORT, DELIVERY_GATE_CHROME, MIGRATION_GATE_CHROME) — hard-coded 8700-8719 collided with a worker during deploymre