gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commite7af3ecee7af3eceMerge branch 'w52b-approval-followup' into mainGitoriae7af3ece/lib/api-helpers.hl

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

Branches

Latest commits

  • e7af3eceMerge branch 'w52b-approval-followup' into mainGitoria
  • c01e66c6|||PR approvals follow-up: live Merge after status writes, helpers (#52)mre
  • 841da497Merge branch 'w53-pulls-speed-pr' into mainGitoria
  • 80307ae2|||PR pulls page cache: live PR numbers, complete scans onlymre
  • 69141f65|||PR pulls page: cache the git part per repo by branch tipsmre
  • 155861f6Merge branch 'w52-person-approval' into mainGitoria
  • b5435de8|||PR person approvals: second Kunibert round (#52)mre
  • 3a8b2fb8|||PR person approvals: Kunibert review fixes (#52)mre
  • 2a89d8d0|||PR person approvals: Approve button for people; merge needs the required checks or a listed person's approval (#52)mre
  • 83f0c953Report person approval verificationmre
  • 3ae94b86Record browser failures and verify owned tab cleanupmre
  • 54eec687Gate person approvals and merge alternativesmre
  • 12b08829Add person approvals for pull request headsmre
  • 55c84779Merge branch 'gp-gate-ports' into mainGitoria
  • 57af42bbREADME: point to deploy.sh for the port listmre
  • d63375d2README: gate commands with explicit portsmre
  • 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