gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitcd442066cd442066gitoria#43: app-auth unit test reads the apps table from its new filemrecd442066/lib/api-helpers.hl

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

Branches

Latest commits

  • cd442066gitoria#43: app-auth unit test reads the apps table from its new filemre
  • 5dccbe38gitoria#43: in-flight rows are left alone by the other process; gate in deploy.sh; READMEmre
  • 8c8805f3merge main (gitoria#42), delivery log and redelivery routes, sweep fixmre
  • 5a996ea8gitoria#42: short-lived app tokens, read-only git fetch with them (merge w42-app-token)mre
  • 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
  • b5d696fegitoria#42: app tokens (gat_, one hour), appOfRequest, app token as the git read passwordmre
  • 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