gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitafdb0cbfafdb0cbfgitoria#43: rename the moved helper (name clash in the browser build)mreafdb0cbf/lib/api-helpers.hl

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

Branches

Latest commits

  • afdb0cbfgitoria#43: rename the moved helper (name clash in the browser build)mre
  • 15e01e94gitoria#43: tests/events.mjs and its probemre
  • 77829b73gitoria#43: signed events to connected apps (deliveries table, sender timer, pull_request events, ping)mre
  • 1997e77dgitoria#40: stable PR numbers, refs/pull/N/head, ssh pushes reach the app (merge w40-pr-number)mre
  • 9364fb16gitoria#40: ssh pushes call /__git/pushed before and after (ensurePulls, notifyPush, syncPulls); gate over the sshd imagemre
  • fdb9ed28gitoria#41: repo owners can connect an app (merge w41-apps-connect)mre
  • 2ef23df0gitoria#40: the browser gate reads the title without the #Nmre
  • 9af07ecegitoria#41: connect an app to a repo (apps table, connect flow, manual add, allow-list, settings)mre
  • 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