gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitfeeaa701feeaa701|||PR gates: explicit ports only, deploy.sh passes themmrefeeaa701/lib/api-helpers.hl

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

Branches

Latest commits

  • feeaa701|||PR gates: explicit ports only, deploy.sh passes themmre
  • 6cc1f943Merge branch 'records-1112' into mainGitoria
  • 9448c438|||PR records: LOG, missions and reports since 11-09mre
  • 6dc08a8eMerge branch 'p51-public-pr-reads' into mainGitoria
  • 7b88cfe0Type publicReaderOfRequest parameters (L10)Gitoria
  • 2a4b3dc1|||PR Anonymous reads of public repos' pulls and statusesmre
  • 21b2c141Merge branch 's25-no-session-on-login-error' into mainGitoria
  • 698458e2|||PR No session for cookieless login errors: READMEmre
  • cce7b785|||PR No session for cookieless login errorsmre
  • 678d6303Merge branch 'v59' into mainGitoria
  • 5803cf4f|||PR vendor: Hybriel set-day59 and GC budget 32 MiBmre
  • 9eff6627Merge branch 'f2-api-bugs' into mainGitoria
  • 26117255|||PR apps API: review comments on files with spaces in their namemre
  • b8796cecMerge branch 'f1-broken-json' into mainGitoria
  • 2f2959c5|||PR apps API: broken JSON body gives 400 invalid_json, not 500mre
  • f7c6ca07Merge branch 'ident-chooser-selector' into mainGitoria
  • 691abb8e|||PR tests: find ident identity choice without the removed .choose classmre
  • 16a3530fMerge branch 'heap-revert' into mainGitoria
  • 220a80e0|||PR Remove temporary /__heap route and heap helper; logmre
  • 13f0c342Merge branch 'hl25-records' into mainGitoria