gitoria

gitoria

All repositories: gitoria

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

Branches

Latest commits

  • 1075c38cW48 mission, logmre
  • 17bdd0d6V37/V39 missions, logmre
  • 146de5f8Re-vendor gitoria to Hybriel set-day39 (51e921d7)mre
  • e874e44dtests/status-keyed: app token lives 60 s so it outlives the gate's two restartsmre
  • 270b8d61deploy.sh: DEPLOY_STOP_START=1 stops, waits for the old pid, then starts (mpackdb freeze, replaces the standby swap)mre
  • 3f9235b8project.hl: drop the dummy /pulls/:n component route (hybriel#294 is fixed in day37)mre
  • c90daf19Re-vendor gitoria to Hybriel set-day37 (f6b3d45b)mre
  • 03b5bb08W39 report to reports/, drop scratch repro, mission + logmre
  • 154d333fMerge w39-create-api: POST /api/repos with a user token (#39)mre
  • 149dd512Rewrite W39 reportmre
  • e5385075Expect 401 for POST /api/repos without a tokenmre
  • 1a76055cDocument repository creation APImre
  • 4d779b3bAdd create-api gatemre
  • 88563122Add POST /api/repos for user tokensmre
  • b00860e3Create repositories with member checks in createRepoInmre
  • 0a38f236Report repository creation API blockermre
  • b5f72187Record malformed JSON blocker for repository creationmre
  • a2ae8302logmre
  • 6947cd58logmre
  • 2175e48clogmre