gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitf0e7ff8cf0e7ff8cdeploy: backup copies index files first and *.mpack data last (hot-copy skew, hybriel #203)mref0e7ff8c/lib/api-helpers.hl

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

Branches

Latest commits

  • f0e7ff8cdeploy: backup copies index files first and *.mpack data last (hot-copy skew, hybriel #203)mre
  • fe5f707cgitoria 010: report, mission, comment on why current = true staysmre
  • 9705ca86gitoria 010: gate: menu marker in the server HTMLmre
  • 8cff663agitoria 010: main-menu.hl from layouts 0bcc629 (route marker)mre
  • 359c79f7gitoria 010: re-vendor Hybriel master f9614007 (plugins whole, bin/ git-ignored)mre
  • 6bd7fb3cgitoria 009: theme re-copied from layouts 76f99f6 (menu marks the current page), current = true on the menu item, e2e marker checks, reportmre
  • 44de915cidentkit: refuse a taken ident port, cold-start deadline, abort on dead ident, clear no-code error (as notes)mre
  • 2bf5343egitoria 007: theme re-copied from layouts dbe3de7 (44 px taps), redundant tap overrides dropped, crumbs class, real-browser walk test, reportmre
  • 18843648deploy.sh: a gate without a 'passed,' line (check-theme) no longer ends the script silently (grep under pipefail) — print check-theme's summary, as layouts doesmre
  • e6dff0cbgitoria#23: report 005, STATUS, LOGmre
  • 7f992d62gitoria#23: phone first on the shared theme — brand, branch and pull links 44 px, readme code 14 px; gate block "phone first" (15 pages at 390 + the three dropdowns, fails 20 on old), dropdown-aware login/logout clicks, check-theme in GATESmre
  • a2a840a5gitoria#23 WIP (quota stop): theme from layouts, styles inherit it, shell on the theme's header/menus/sidebar, phone measurement script; gates not runmre
  • 12b665b0mission brief 005mre
  • b680c483Revert "gitoria#22: RepoTabs wrapping component for the repo tabs (active tab, no content for a missing repo)"mre
  • 4fda244fgitoria#22: RepoTabs wrapping component for the repo tabs (active tab, no content for a missing repo)mre
  • 0fd5e280mission brief 004mre
  • e1d425e7paths: projects moved into /media/STORAGE/projects/WorldAPI/mre
  • e59ff114deploy.sh: wait up to DEPLOY_URL_WAIT (120 s) for 200 after the restart, as the other apps (cold compile, hybriel#148)mre
  • bcafcceb003: compose cache volume + tmpfs /tmp, gate start waits 120 s, report/LOG/STATUSmre
  • bed3f2d5Re-vendor Hybriel to the shared static build 0f396652 (plugins; bin/ is git-ignored)mre