gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit570138cd570138cddeploy: security gate (tests/security.mjs)mre570138cd/lib/api.hl

6.6 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. // ---- THE LOGIN BUTTON'S RETURN (ident README "How apps use ident") ----------------------------
  34. // ident sends the browser back with ?ident_code= (and our ?next=); the code is exchanged here (users.hl userOfLoginCode),
  35. // the user goes into THIS browser's session (`req.session`, the cookie's — hybriel #11; minted when it brought none),
  36. // then → `?next=` (api-helpers.hl safeNext). A FAILED LOGIN is a page (components/loginfailed.hl): the reason is parked
  37. // in the session, then → /login/failed.
  38. static loginFailed = (&req, &sessions, why) => {
  39. let s = req.session
  40. fresh = s == null
  41. if (fresh) { s = sessions.mint() }
  42. s.data.loginError = why
  43. sessions.save(s)
  44. 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' } })
  45. if (fresh) { res.headers['Set-Cookie'] = sessions.cookieHeader(s.id) }
  46. return res
  47. }
  48. static loginCallback = (route, &req, &sessions) => {
  49. if (req.method != 'GET') { return loginFailed(&req, &sessions, 'GET only') }
  50. q = req.query != null ? req.query : {}
  51. code = q.ident_code
  52. if (code == null || code == '') { return loginFailed(&req, &sessions, 'ident sent no login code') }
  53. x = userOfLoginCode(code)
  54. if (x.error != null) { return loginFailed(&req, &sessions, x.error) }
  55. // A LOGIN GETS A NEW SESSION ID (session fixation, mission SG): a cookie planted before the login must not become the
  56. // logged-in one. The old session (if any) is logged out and left behind.
  57. old = req.session
  58. if (old != null) {
  59. old.user = null
  60. old.data.tag = null
  61. sessions.save(old)
  62. }
  63. let s = sessions.mint()
  64. s.user = { id = x.user.id }
  65. s.data.tag = randomBytes(16)
  66. s.data.loginError = null
  67. sessions.save(s)
  68. 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) } })
  69. return res
  70. }
  71. // ---- CONNECT A REPO TO A TICKETS PROJECT (gitoria#18; tickets.hl, components/settings.hl) ----------------------------
  72. // /settings/connect: the owner's click → a fresh nonce parked in the session (bound to the repo) → tickets' /connect.
  73. // /settings/connected: tickets' return with ?code&state → tickets.hl finishConnect (nonce, exchange, key stored per
  74. // repo) → back to /settings. A failure is a note on the settings page.
  75. static connectStart = (route, &req, &sessions) => {
  76. if (req.method != 'GET') { return fail(405, 'GET only') }
  77. repo = repoOfRequest(req)
  78. if (repo == null) { return fail(404, 'no such repository') }
  79. s = req.session
  80. if (s == null || !ownsRepo(repo.slug, s)) { return goTo('/settings') }
  81. nonce = randomBytes(16)
  82. s.data.connectState = repo.slug + '.' + nonce
  83. s.data.connectNote = null
  84. sessions.save(s)
  85. return goTo(connectHref(repo.slug, nonce))
  86. }
  87. static connectBack = (route, &req, &sessions) => {
  88. if (req.method != 'GET') { return fail(405, 'GET only') }
  89. repo = repoOfRequest(req)
  90. if (repo == null) { return fail(404, 'no such repository') }
  91. s = req.session
  92. if (s == null || !ownsRepo(repo.slug, s)) { return goTo('/settings') }
  93. q = req.query != null ? req.query : {}
  94. want = s.data.connectState
  95. s.data.connectState = null
  96. note = finishConnect(repo.slug, want, q)
  97. s.data.connectNote = note == '' ? null : note
  98. sessions.save(s)
  99. return goTo('/settings')
  100. }
  101. // ---- WHAT THE SSHD CONTAINER ASKS (ticket gitoria#7; docker/sshd) — only with the shared secret (api-helpers.hl sshGuard)
  102. // GET /__git/keys?type=<ssh-ed25519>&key=<base64> sshd AuthorizedKeysCommand: the authorized_keys line for a known key
  103. // (sshkeys.hl keyLine), empty for an unknown one
  104. // GET /__git/access?user=<id>&slug=<slug>&write=0|1 gitoria-shell before it starts git: `ok` or 403. Read = every repo (public);
  105. // write = the repo's owner only (repos.hl mayPush — the same rule as the token on HTTPS, transport.hl)
  106. static gitKeys = (route, req) => {
  107. no = sshGuard(req)
  108. if (no != null) { return no }
  109. q = req.query != null ? req.query : {}
  110. return textReply(200, keyLine(q.type, q.key))
  111. }
  112. static gitAccess = (route, req) => {
  113. no = sshGuard(req)
  114. if (no != null) { return no }
  115. q = req.query != null ? req.query : {}
  116. slug = q.slug
  117. if (slug == null || hlTypeName(slug) != 'String' || slugError(slug) != null) { return textReply(404, 'no such repository' + NL) }
  118. repo = repoBySlug(slug)
  119. if (repo == null) { return textReply(404, 'no such repository' + NL) }
  120. if (q.write == '1' && !mayPush(q.user, repo)) { return textReply(403, 'only the owner of this repository can push to it' + NL) }
  121. return textReply(200, 'ok' + NL)
  122. }

Branches

Latest commits

  • 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
  • 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