gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit00517faf00517fafgitoria: shell 404 page for unknown paths (catch-all route, status 404, JSON 404 under /api), 390 gate checksMG00517faf/lib/api.hl

7.3 KB

  1. // lib/api.hl — THE FUNCTION ROUTES (project.hl wires them): thin wrappers — check the request and the session, call the
  2. // topic, answer. The plumbing (JSON / Markdown / plain answers, redirects, the ssh guard, `?next=`) is lib/api-helpers.hl.
  3. // /api/repos, /api/repos/:slug the public reads (repos.hl; Accept: text/markdown → the Markdown read view)
  4. // /login/callback the login button's return (users.hl)
  5. // /settings/connect, /settings/connected connecting a repo to a tickets project (tickets.hl)
  6. // /__git/keys, /__git/access what the sshd container asks (sshkeys.hl, repos.hl)
  7. // (The git protocol's routes are lib/transport.hl gitTransport.)
  8. // A route that WRITES THE SESSION gets the request and the framework's session store BY REFERENCE (`&req`, `&sessions`,
  9. // from project.hl): copied into a second call, the session inside `req` would be a copy too and the write would be lost.
  10. import { Response } from 'hl:http1'
  11. import { randomBytes } from 'hl:crypto'
  12. import { NL } from './util.hl'
  13. import { userOfLoginCode } from './users.hl'
  14. import { repoRows, repoRow, repoBySlug, ownsRepo, mayPush } from './repos.hl'
  15. import { slugError, repoDocument, listDocument } from './repos-helpers.hl'
  16. import { connectHref, finishConnect } from './tickets.hl'
  17. import { keyLine } from './sshkeys.hl'
  18. import { reply, fail, wantsMarkdown, markdownReply, textReply, goTo, sshGuard, repoOfRequest, safeNext } from './api-helpers.hl'
  19. // ---- the API: reads are public (creating a repo is a web action for now) ------------------------
  20. static apiRepos = (route, req) => {
  21. if (req.method != 'GET') { return fail(405, 'GET only') }
  22. rows = repoRows()
  23. if (wantsMarkdown(req)) { return markdownReply(listDocument(rows)) }
  24. return reply(200, { repos = rows })
  25. }
  26. static apiRepo = (route, req) => {
  27. if (req.method != 'GET') { return fail(405, 'GET only') }
  28. row = repoRow(route.params.slug)
  29. if (row == null) { return fail(404, 'no such repository') }
  30. if (wantsMarkdown(req)) { return markdownReply(repoDocument(row)) }
  31. return reply(200, row)
  32. }
  33. // ---- NOTHING ELSE (mission MG, as ident's notFoundPage): the last route of project.hl answers a path no route knows with the
  34. // shell's 404 page (components/notfound.hl) — status 404, the page rendered by the framework with the request's session and host
  35. static notFoundPage = (route, req, &srv, pageComponent) => {
  36. m = { route = { pattern = route.route.pattern component = pageComponent } kind = 'component' params = { path = req.path } path = req.path }
  37. res = srv.page(m, req.session, srv.hostOf(req.headers['host']), srv.requestHeaders(req.headers))
  38. res.status = 404
  39. return res
  40. }
  41. static notFoundApi = (route, req) => { return fail(404, 'no such endpoint') }
  42. // ---- THE LOGIN BUTTON'S RETURN (ident README "How apps use ident") ----------------------------
  43. // ident sends the browser back with ?ident_code= (and our ?next=); the code is exchanged here (users.hl userOfLoginCode),
  44. // the user goes into THIS browser's session (`req.session`, the cookie's — hybriel #11; minted when it brought none),
  45. // then → `?next=` (api-helpers.hl safeNext). A FAILED LOGIN is a page (components/loginfailed.hl): the reason is parked
  46. // in the session, then → /login/failed.
  47. static loginFailed = (&req, &sessions, why) => {
  48. let s = req.session
  49. fresh = s == null
  50. if (fresh) { s = sessions.mint() }
  51. s.data.loginError = why
  52. sessions.save(s)
  53. res = new Response('login failed: ' + why, { status = 302 headers = { 'Location' = '/login/failed' 'Cache-Control' = 'no-store' 'Content-Type' = 'text/plain; charset=utf-8' 'X-Content-Type-Options' = 'nosniff' } })
  54. if (fresh) { res.headers['Set-Cookie'] = sessions.cookieHeader(s.id) }
  55. return res
  56. }
  57. static loginCallback = (route, &req, &sessions) => {
  58. if (req.method != 'GET') { return loginFailed(&req, &sessions, 'GET only') }
  59. q = req.query != null ? req.query : {}
  60. code = q.ident_code
  61. if (code == null || code == '') { return loginFailed(&req, &sessions, 'ident sent no login code') }
  62. x = userOfLoginCode(code)
  63. if (x.error != null) { return loginFailed(&req, &sessions, x.error) }
  64. // A LOGIN GETS A NEW SESSION ID (session fixation, mission SG): a cookie planted before the login must not become the
  65. // logged-in one. The old session (if any) is logged out and left behind.
  66. old = req.session
  67. if (old != null) {
  68. old.user = null
  69. old.data.tag = null
  70. sessions.save(old)
  71. }
  72. let s = sessions.mint()
  73. s.user = { id = x.user.id }
  74. s.data.tag = randomBytes(16)
  75. s.data.loginError = null
  76. sessions.save(s)
  77. res = new Response('logged in', { status = 302 headers = { 'Location' = safeNext(q.next) 'Cache-Control' = 'no-store' 'Content-Type' = 'text/plain; charset=utf-8' 'X-Content-Type-Options' = 'nosniff' 'Set-Cookie' = sessions.cookieHeader(s.id) } })
  78. return res
  79. }
  80. // ---- CONNECT A REPO TO A TICKETS PROJECT (gitoria#18; tickets.hl, components/settings.hl) ----------------------------
  81. // /settings/connect: the owner's click → a fresh nonce parked in the session (bound to the repo) → tickets' /connect.
  82. // /settings/connected: tickets' return with ?code&state → tickets.hl finishConnect (nonce, exchange, key stored per
  83. // repo) → back to /settings. A failure is a note on the settings page.
  84. static connectStart = (route, &req, &sessions) => {
  85. if (req.method != 'GET') { return fail(405, 'GET only') }
  86. repo = repoOfRequest(req)
  87. if (repo == null) { return fail(404, 'no such repository') }
  88. s = req.session
  89. if (s == null || !ownsRepo(repo.slug, s)) { return goTo('/settings') }
  90. nonce = randomBytes(16)
  91. s.data.connectState = repo.slug + '.' + nonce
  92. s.data.connectNote = null
  93. sessions.save(s)
  94. return goTo(connectHref(repo.slug, nonce))
  95. }
  96. static connectBack = (route, &req, &sessions) => {
  97. if (req.method != 'GET') { return fail(405, 'GET only') }
  98. repo = repoOfRequest(req)
  99. if (repo == null) { return fail(404, 'no such repository') }
  100. s = req.session
  101. if (s == null || !ownsRepo(repo.slug, s)) { return goTo('/settings') }
  102. q = req.query != null ? req.query : {}
  103. want = s.data.connectState
  104. s.data.connectState = null
  105. note = finishConnect(repo.slug, want, q)
  106. s.data.connectNote = note == '' ? null : note
  107. sessions.save(s)
  108. return goTo('/settings')
  109. }
  110. // ---- WHAT THE SSHD CONTAINER ASKS (ticket gitoria#7; docker/sshd) — only with the shared secret (api-helpers.hl sshGuard)
  111. // GET /__git/keys?type=<ssh-ed25519>&key=<base64> sshd AuthorizedKeysCommand: the authorized_keys line for a known key
  112. // (sshkeys.hl keyLine), empty for an unknown one
  113. // GET /__git/access?user=<id>&slug=<slug>&write=0|1 gitoria-shell before it starts git: `ok` or 403. Read = every repo (public);
  114. // write = the repo's owner only (repos.hl mayPush — the same rule as the token on HTTPS, transport.hl)
  115. static gitKeys = (route, req) => {
  116. no = sshGuard(req)
  117. if (no != null) { return no }
  118. q = req.query != null ? req.query : {}
  119. return textReply(200, keyLine(q.type, q.key))
  120. }
  121. static gitAccess = (route, req) => {
  122. no = sshGuard(req)
  123. if (no != null) { return no }
  124. q = req.query != null ? req.query : {}
  125. slug = q.slug
  126. if (slug == null || hlTypeName(slug) != 'String' || slugError(slug) != null) { return textReply(404, 'no such repository' + NL) }
  127. repo = repoBySlug(slug)
  128. if (repo == null) { return textReply(404, 'no such repository' + NL) }
  129. if (q.write == '1' && !mayPush(q.user, repo)) { return textReply(403, 'only the owner of this repository can push to it' + NL) }
  130. return textReply(200, 'ok' + NL)
  131. }

Branches

Latest commits

  • 00517fafgitoria: shell 404 page for unknown paths (catch-all route, status 404, JSON 404 under /api), 390 gate checksMG
  • d9e977f0reports/MG-mobile.md, LOGMG
  • 2f2725actests/browser.mjs: commit-row check (one row, centred date)MG
  • 4df1ea01tests/browser.mjs: MG phone checks (list first, no dead strip, one branch heading, tab grid, commit rows, PR title row, ticket columns, wrapping commands)MG
  • c52424e9gitoria mobile: repo list before the forms, 3x2 tab grid, no duplicate branch heading, ordered commit rows, aligned ticket rows, pull title on its own row, wrapping clone commands, no dead strip under formsMG
  • 3a95252dG3: cache parsed README blocks by sha256 of the text (bounded LRU), reportmre
  • 570138cddeploy: security gate (tests/security.mjs)mre
  • da6b20fcgitoria SG: security audit fixes (uploadMax 0, Secure cookie on https, session rotation at login, nosniff on function routes, no identity in page data) + tests/security.mjs gate + reportmre
  • f98c981fgitoria B1: report wordingmre
  • c6364cedgitoria B1: compact Markdown blocks (one kind flag per block/span, empty and false fields omitted); same rendered HTML, page values -67%mre
  • cd99d2feWIP gitoria#22: always render the repo level's slot (missing-repo boot hang), pages render nothing without a repo; hydration diagnosis in reports/G2-hydrate.mdmre
  • e5404b9breport: commit idsmre
  • 63cfa21ereports/G2-repotabs.mdmre
  • 9b0516fdgitoria#22: mark the tab underline functional (check-theme), chain E2E: serve/measure split, bring the lisbeth tab to frontmre
  • 19bc1df8gitoria#22: all repo pages under ONE layout level (components/repo.hl, hl:web D-CHAIN): a tab click replaces only the page in the slot; head and tab nav stay the same DOM nodesmre
  • 40658417tests/browser.mjs: wait for the page to be live (hydrated) before clicking Merge — a click on the server-rendered button before the client mounted was lost (flaky 'Clashing change' under load)mre
  • 48a6154bre-vendor Hybriel 5406d146 (hl:web batches 9+10: #72 emits while the socket is closed delivered after reconnect, browser boot linear, faster list appends; contains int 6–14 + m406)mre
  • e972e8d7tests: short-id-switch (GITORIA_SWITCH_IDENT_PORT/_PORT) and ssh tunnel (GITORIA_GATE_TUNNEL_PORT) ports from env, defaults unchangedmre
  • f06f5349re-vendor Hybriel 6f8a0689 (int 6–11: streaming cursors, #203 index heal + close() fix, fd403 listDir/cursor fds, hl:time browser side, hl:web batches 5+6)mre
  • c7d3cc87docker-compose: nofile 65536 (Hybriel int 6+ boot opened >1024 files in the tracker)mre