gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit61b72ded61b72dedgitoria#25: re-vendor Hybriel master c1b1010f (W149 page cache), key the repo Readme page by slug and main commit, extend the readme-cache gatemre61b72ded/lib/api-helpers.hl

3.9 KB

  1. // lib/api-helpers.hl — the plumbing of the function routes (lib/api.hl): JSON / Markdown / plain answers, redirects,
  2. // the guard of the internal ssh routes, the repo of a request's host, where a login may return to. Statics only.
  3. import { Response } from 'hl:http1'
  4. import { NL, scheme, hostPort, slugOfHost } from './util.hl'
  5. import { slugError } from './repos-helpers.hl'
  6. import { repoBySlug } from './repos.hl'
  7. import { sshSecret, sshEnabled } from './sshkeys.hl'
  8. static jsonHeaders = { 'Content-Type' = 'application/json; charset=utf-8' 'X-Content-Type-Options' = 'nosniff' }
  9. static reply = (status, value) => {
  10. return new Response(JSON.stringify(value), { status = status headers = jsonHeaders })
  11. }
  12. static fail = (status, message) => { return reply(status, { error = message }) }
  13. // THE MARKDOWN READ VIEW: `Accept: text/markdown` (for LLM readers)
  14. static wantsMarkdown = (req) => {
  15. a = req.headers['accept']
  16. return a != null && hlTypeName(a) == 'String' && a.includes('text/markdown')
  17. }
  18. static markdownReply = (text) => {
  19. return new Response(text, { status = 200 headers = { 'Content-Type' = 'text/markdown; charset=utf-8' 'X-Content-Type-Options' = 'nosniff' } })
  20. }
  21. // a plain-text answer of the internal ssh routes
  22. static textReply = (status, text) => {
  23. return new Response(text, { status = status headers = { 'Content-Type' = 'text/plain; charset=utf-8' 'Cache-Control' = 'no-store' 'X-Content-Type-Options' = 'nosniff' } })
  24. }
  25. static goTo = (where) => {
  26. return new Response('redirect', { status = 302 headers = { 'Location' = where 'Cache-Control' = 'no-store' 'Content-Type' = 'text/plain; charset=utf-8' 'X-Content-Type-Options' = 'nosniff' } })
  27. }
  28. // THE INTERNAL SSH ROUTES' GUARD (docker/sshd asks them): null when the caller may ask, else the refusing answer. Without
  29. // GITORIA_SSH_SECRET they do not exist (404); every call needs `X-Gitoria-Secret` = that secret, and a request that came
  30. // through the public proxy (it carries X-Forwarded-For / X-Real-IP) is refused.
  31. static sshGuard = (req) => {
  32. if (!sshEnabled) { return textReply(404, 'not found' + NL) }
  33. if (req.method != 'GET') { return textReply(405, 'GET only' + NL) }
  34. if (req.headers['x-forwarded-for'] != null || req.headers['x-real-ip'] != null) { return textReply(403, 'internal only' + NL) }
  35. given = req.headers['x-gitoria-secret']
  36. if (given == null || hlTypeName(given) != 'String' || given != sshSecret) { return textReply(403, 'wrong secret' + NL) }
  37. return null
  38. }
  39. // the repo of a request's host (a repo address), or null
  40. static repoOfRequest = (req) => {
  41. h = req.headers['host']
  42. slug = slugOfHost(h == null ? '' : h.split(':')[0])
  43. return slug == '' || slugError(slug) != null ? null : repoBySlug(slug)
  44. }
  45. // BACK TO THE PAGE after a login: /login.js puts `?next=` into the button's return URL at the click. Only a same-origin PATH
  46. // goes (one `/`, URL-safe characters, ≤ 500) — or a full URL of THIS system: <scheme>://<label>.<host>
  47. // with a valid, non-reserved repo label and such a path. Anything else → `/`.
  48. static nextChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~/?&=%+,;@!$()*:'
  49. static nextPath = (want) => {
  50. if (want == null || hlTypeName(want) != 'String' || want == '' || want.length > 500) { return '/' }
  51. if (want.slice(0, 1) != '/' || want.slice(0, 2) == '//' || want.slice(0, 7) == '/login/') { return '/' }
  52. let i = 0
  53. while (i < want.length) {
  54. if (!nextChars.includes(want[i])) { return '/' }
  55. i = i + 1
  56. }
  57. return want
  58. }
  59. static safeNext = (want) => {
  60. if (want == null || hlTypeName(want) != 'String' || want.length > 500) { return '/' }
  61. prefix = scheme + '://'
  62. if (!want.startsWith(prefix)) { return nextPath(want) }
  63. rest = want.slice(prefix.length)
  64. slash = rest.indexOf('/')
  65. hp = slash < 0 ? rest : rest.slice(0, slash)
  66. path = slash < 0 ? '/' : rest.slice(slash)
  67. dot = hp.indexOf('.')
  68. if (dot < 1 || hp.slice(dot + 1) != hostPort || slugError(hp.slice(0, dot)) != null) { return '/' }
  69. p = nextPath(path)
  70. if (p == '/' && path != '/') { return '/' }
  71. return prefix + hp + p
  72. }

Branches

Latest commits

  • 61b72dedgitoria#25: re-vendor Hybriel master c1b1010f (W149 page cache), key the repo Readme page by slug and main commit, extend the readme-cache gatemre
  • 5b0b4b9ereadme-cache gate: use the security gate's ports when no own ports are givenmre
  • bc8f20d9gitoria#25: re-vendor Hybriel c09538bb with the W141 render cache, cache the homepage per commit, add the readme-cache gatemre
  • 9feebbd3Re-vendor Hybriel to ded47c13 (W145: Firefox start script fix)mre
  • 28ed41e5gitoria#24: one sign-in comes from hl:web batch 20, the /session/share workaround is gonemre
  • 3b2669e4gs: drop the unbuildable legacy check, fix pwa fetchmre
  • 2a2fdd77gs: C2 check before cookie experimentsmre
  • 76e0587aWIP gs 2mre
  • a80da7bdWIP gs: security gate for lazy sessions, g24 browser checksmre
  • 23fb05d8re-vendor Hybriel aaa39e69 (hl:web batch 20: login emit and Sessions.mint, tab follows a login, signed-in session cookie wins among duplicates and is re-issued with the Domain, W85 faster render, lazy sessions)mre
  • a0727ca3ident#31: the selector's sign-in returns to the page the user started on (return attribute = the login callback with next), gate checkmre
  • a06975f9gitoria: a gap between your name and Log out in the phone header (they touched); gate checks itmre
  • 32ba9d59gitoria: leave the host-only cookie experiment out of the browser gate (gitoria#24 is a separate mission)mre
  • 43ad2bcfgitoria header: the selector alone plus your name and Log out as before the theme missions; tests follow (WIP: host-only cookie experiment fails)mre
  • ac2c4e3fident selector back in the header (creator 2026-10-24): selector top right signed in and out on phone and desktop, no second Log in button, new gate checks (WIP: gates not re-run after the last edits)mre
  • b279f536theme: sync from layouts 4994037 (list rows stack, td word-keep, heading code, edited tap target)mre
  • 2b01515ctests/ticketskit: a failed start kills the tickets server (own process group); stop() kills the groupmre
  • 109cfc5ctests/ticketskit: keep the rotated session cookie of the tickets login (tickets ST2 session rotation)mre
  • 7eb6be27sessions last 99 years (creator 2026-10-23: logins never expire; only sign out ends them)mre
  • b3911918reports/MG-mobile.md: 404 fixed, rebased gatesMG