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/transport.hl

5.8 KB

  1. // lib/transport.hl — CLONE, FETCH AND PUSH OVER HTTPS (ticket gitoria#7; docs/git-backend.md). Git's "smart HTTP" protocol,
  2. // answered by this app itself with the `git` binary: on a repo address `<slug>.<domain>` the paths
  3. // /<slug>.git/info/refs?service=git-upload-pack | git-receive-pack (what a client asks first)
  4. // /<slug>.git/git-upload-pack (fetch / clone) /<slug>.git/git-receive-pack (push)
  5. // spawn `git upload-pack | receive-pack --stateless-rpc` and hand the request body over and the answer back.
  6. // The clone URL is https://<slug>.<domain>/<slug>.git — the folder git makes is named like the repo.
  7. // * READ (clone, fetch) needs no login: every repo is public (the concept has no private repos yet).
  8. // * WRITE (push) needs a token as the password (tokens.hl) of THE REPO'S OWNER — nobody else may push (decision below).
  9. // * The body travels through temp files, byte for byte (a String here holds raw bytes; hl:fs writes and reads them as
  10. // they are): hl:proc run() has no stdin, so the child reads `< body` and writes `> answer` in a tiny `sh -c` with
  11. // the paths as positional arguments (no user text in the script). A gzip body (git compresses big requests) is
  12. // unpacked first. Git protocol v2 is passed through (`Git-Protocol` → GIT_PROTOCOL, digits only).
  13. // * No hook: pull requests and releases are read from the commits when their page is built.
  14. // The checks, the plain answers and the pkt-line are in transport-helpers.hl.
  15. import { Response } from 'hl:http1'
  16. import { run } from 'hl:proc'
  17. import { readFile, writeFile, remove, exists } from 'hl:fs'
  18. import { randomBytes } from 'hl:crypto'
  19. import { NL, slugOfHost, publicUrl } from './util.hl'
  20. import { repoBySlug, mayPush } from './repos.hl'
  21. import { slugError } from './repos-helpers.hl'
  22. import { userOfToken } from './tokens.hl'
  23. import { repoDir, tmpDir } from './git.hl'
  24. import { notifyPush } from './tickets.hl'
  25. import { passwordOf, plain, protocolEnv, pktLine } from './transport-helpers.hl'
  26. static seconds = 300 // one upload-pack / receive-pack call
  27. static maxBody = 500000000 // a push or fetch request above this is refused (bytes)
  28. // ONE GIT CALL: the request body (or none) in, the answer out. → the answer's bytes as a String, or null (git failed and said nothing)
  29. static callGit = (slug, service, advertise, req) => {
  30. dir = tmpDir()
  31. name = dir + '/' + randomBytes(12, 'hex')
  32. inFile = name + '.in'
  33. outFile = name + '.out'
  34. let zipped = false
  35. let script = 'exec git ' + service.slice(4) + ' --stateless-rpc --advertise-refs "$1" > "$3"'
  36. if (!advertise) {
  37. body = req.body == null ? '' : req.body
  38. writeFile(inFile, body, 384)
  39. enc = req.headers['content-encoding']
  40. zipped = enc != null && hlTypeName(enc) == 'String' && (enc.toLowerCase() == 'gzip' || enc.toLowerCase() == 'x-gzip')
  41. script = zipped ? 'gzip -dc < "$2" | git ' + service.slice(4) + ' --stateless-rpc "$1" > "$3"' : 'exec git ' + service.slice(4) + ' --stateless-rpc "$1" < "$2" > "$3"'
  42. }
  43. r = run(['sh', '-c', script, 'sh', repoDir(slug), inFile, outFile], { timeout = seconds env = protocolEnv(req) })
  44. out = exists(outFile) ? readFile(outFile) : null
  45. if (exists(inFile)) { remove(inFile) }
  46. if (exists(outFile)) { remove(outFile) }
  47. // git answers nothing to the client's "0000" probe of a big push (exit 0): that is a 200 with an empty body, as git http-backend does
  48. if (out == null || (out == '' && (r == null || r.exit != 0))) { return null }
  49. return out
  50. }
  51. // THE ROUTE (project.hl, a function route): `/:repo/info/refs`, `/:repo/git-upload-pack`, `/:repo/git-receive-pack`
  52. static gitTransport = (route, req) => {
  53. hostHeader = req.headers['host']
  54. slug = slugOfHost(hostHeader == null ? '' : hostHeader.split(':')[0])
  55. repoName = route.params.repo
  56. if (slug == '' || slugError(slug) != null || repoName != slug + '.git' || repoBySlug(slug) == null) { return plain(404, 'no such repository') }
  57. last = req.path.slice(req.path.lastIndexOf('/') + 1)
  58. advertise = last == 'refs'
  59. service = advertise ? (req.query != null ? req.query.service : null) : last
  60. if (service != 'git-upload-pack' && service != 'git-receive-pack') { return plain(403, 'only the smart git protocol is served here: use git clone / fetch / push') }
  61. if (advertise && req.method != 'GET') { return plain(405, 'GET only') }
  62. if (!advertise && req.method != 'POST') { return plain(405, 'POST only') }
  63. if (req.body != null && req.body.length > maxBody) { return plain(413, 'that request is too big') }
  64. if (service == 'git-receive-pack') {
  65. realm = { 'WWW-Authenticate' = 'Basic realm="gitoria"' }
  66. pw = passwordOf(req.headers['authorization'])
  67. if (pw == null) { return plain(401, 'pushing needs a token: log in at ' + publicUrl + ', make one under "Access tokens", and use it as the password', realm) }
  68. userId = userOfToken(pw)
  69. if (userId == null) { return plain(401, 'that token is not valid (removed, or mistyped)', realm) }
  70. repo = repoBySlug(slug)
  71. if (!mayPush(userId, repo)) { return plain(403, 'only the owner of this repository can push to it') }
  72. }
  73. let out = callGit(slug, service, advertise, req)
  74. if (out == null) { return plain(500, 'git gave no answer') }
  75. let headers = { 'Cache-Control' = 'no-cache, max-age=0, must-revalidate' 'Pragma' = 'no-cache' 'X-Content-Type-Options' = 'nosniff' }
  76. if (advertise) {
  77. headers['Content-Type'] = 'application/x-' + service + '-advertisement'
  78. // protocol v2 answers without the service banner; v0/v1 starts with it (as git http-backend does)
  79. v2 = protocolEnv(req).GIT_PROTOCOL != null && protocolEnv(req).GIT_PROTOCOL.includes('version=2')
  80. if (!v2) { out = pktLine('# service=' + service + NL) + '0000' + out }
  81. } else {
  82. headers['Content-Type'] = 'application/x-' + service + '-result'
  83. // a push arrived: tell the connected tickets project about commits naming a ticket (tickets.hl; never fails the push)
  84. if (service == 'git-receive-pack') { notifyPush(slug, false) }
  85. }
  86. return new Response(out, { headers = headers })
  87. }

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