gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitb9137ff0b9137ff0gitoria#40: pull requests are stored with a number, refs/pull/N/head, syncPulls and pullObject; /pulls shows #Nmreb9137ff0/lib/api-helpers.hl

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

Branches

Latest commits

  • b9137ff0gitoria#40: pull requests are stored with a number, refs/pull/N/head, syncPulls and pullObject; /pulls shows #Nmre
  • 06d622daown image tag gitoria-runtime:debian12 for gitoria and standby (shared tag lost git)mre
  • 20caa33agitoria#37: media queries in the language's @media block (minWidth = '60rem') in list, codebrowser and repomre
  • c4175e6dgitoria: a startup error prints its message before exit 1 (hybriel#247)mre
  • c50d17efKeep function-route not-found style in shared stylesheetmre
  • e42d78d3Preserve ticket state CSS and document function-route style gapmre
  • c6b7154eWIP gitoria#26: move styles into nested component blocksmre
  • 77a2c31cgitoria#31: plain declarations instead of needless let in lib/ and tools/ (103 let removed, 137 kept because reassigned); no other changemre
  • 4b98ab62gitoria: an ident access key (idt_) works as the git password; read key clones only, bad key 401, ident down 503 (ident#27); gate tests/ident-access-gitoria.mjs in deploy.shmre
  • 1185f686gitoria#36: a failed return from tickets shows a note on the settings page and leaves one log line per outcome; no code is exchangedmre
  • 5d2c385agitoria: the sign-in callback keeps the session it finds and signs it in (same cookie, nothing signed out; ident#28); security gate 71+5 failing → 76 passed; deploy.sh runs tests/private.mjs toomre
  • 241ef3f9deploy.sh: rebuild the sshd image before up -d (GO34c)mre
  • 821cdbb1gitoria#34b: styles, create-repo tool, docs, push/ssh gates sign in through the selector, shots scriptmre
  • a81abdc9gitoria#34: org page via function route, member-reachable orgs, selects, reportmre
  • 17f031f0gitoria#34: private repos, organizations, members, move tool, gatemre
  • 71b69343gitoria#23: the sidebar's Repositories link was a 404 after any in-page navigation (static menu re-evaluated publicUrl in the browser); the menu is an instance property now; gate taps it after a tab, folder, filemre
  • 90b4f728gitoria#25: restore the homepage cache and the parsed-blocks cache (they store data under immutable keys, no HTML); the page key, the framework readme key and the textHash key stay removedmre
  • a487248cgitoria#25: remove every cache that hid the page build (page key, Markdown render key, homepage cache, parsed-blocks cache) and the gate that only tested themmre
  • 12f2f4fegitoria: re-vendor Hybriel set-day27b, a startup fault in on Error exits 1mre
  • c21c084egitoria: write the standby service out in full so the boot gate can read the main servicemre