gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commite9f950a8e9f950a8reports: G25c soakmree9f950a8/lib/sshkeys.hl

6.0 KB

  1. // lib/sshkeys.hl — SSH KEYS for git over SSH (ticket gitoria#7; docs/git-backend.md "Credentials"). A logged-in user pastes a
  2. // PUBLIC key; the sshd container (docker/sshd) asks this app which user a key belongs to (`/__git/keys`, lib/api.hl gitKeys → keyLine), so a new
  3. // key works at once and no authorized_keys file is edited. Only the public key is stored — nothing secret.
  4. // sshKeysTable pk @id index !key, user { user (users @id), name, type, key (base64), fingerprint, created } storage/mpackdb/sshkeys.db
  5. // The key is checked by `ssh-keygen -l -f` (the real parser): one line, an allowed type, a real key, RSA at least 2048 bits.
  6. import { MPackDB } from 'hl:mpackdb'
  7. import { run } from 'hl:proc'
  8. import { now } from 'hl:time'
  9. import { writeFile, remove, exists } from 'hl:fs'
  10. import { randomBytes } from 'hl:crypto'
  11. import { NL, storageDir, countOfList, firstOf, plainError, envOr, isBase64, newestFirst, localStamp } from './util.hl'
  12. import { userRecord } from './users.hl'
  13. import { tmpDir } from './git.hl'
  14. static sshKeysTable = new MPackDB(file = storageDir + '/sshkeys.db', primaryKey = '@id', indexes = ['!key', 'user'])
  15. static maxKeys = 20
  16. // SSH is offered only when the sshd container is set up: the shared secret it uses to ask this app is in the environment
  17. static sshSecret = envOr('GITORIA_SSH_SECRET', '')
  18. static sshPort = envOr('GITORIA_SSH_PORT', '2222')
  19. // the host name in SSH clone addresses: repo addresses go through Cloudflare's proxy, which does not pass SSH, so
  20. // production sets a DNS-only name (GITORIA_SSH_HOST=ssh.gitoria.worldapi.org); empty = the page's own host (dev, gates)
  21. static sshHost = envOr('GITORIA_SSH_HOST', '')
  22. static sshEnabled = sshSecret != ''
  23. static allowedTypes = ['ssh-ed25519', 'ecdsa-sha2-nistp256', 'ecdsa-sha2-nistp384', 'ecdsa-sha2-nistp521', 'ssh-rsa', '[email protected]', '[email protected]']
  24. // the address to clone from over SSH: `git@host:slug.git` on port 22, else the ssh:// form with the port
  25. static sshUrl = (host, slug) => {
  26. h = sshHost != '' ? sshHost : host
  27. if (sshPort == '22') { return 'git@' + h + ':' + slug + '.git' }
  28. return 'ssh://git@' + h + ':' + sshPort + '/' + slug + '.git'
  29. }
  30. static rowOfKey = (k) => { return { @id = k.id name = k.name fingerprint = k.fingerprint created = localStamp(k.created) createdMs = k.created } }
  31. // a user's keys, newest first
  32. static keyRows = (userId) => {
  33. out = []
  34. all = sshKeysTable.find('user', userId)
  35. if (countOfList(all) == 0) { return out }
  36. for (k of all) { out.push(rowOfKey(k)) }
  37. return newestFirst(out)
  38. }
  39. // `ssh-keygen -l` on the key: → { bits, fingerprint } or null when it is not a key
  40. static inspectKey = (type, key) => {
  41. file = tmpDir() + '/' + randomBytes(12, 'hex') + '.pub'
  42. writeFile(file, type + ' ' + key + NL, 384)
  43. r = run(['ssh-keygen', '-l', '-f', file], { timeout = 10 })
  44. if (exists(file)) { remove(file) }
  45. if (r == null || r.exit != 0 || r.lines.length == 0) { return null }
  46. parts = r.lines[0].split(' ')
  47. if (parts.length < 2 || !parts[1].startsWith('SHA256:')) { return null }
  48. return { bits = toNumber(parts[0]) fingerprint = parts[1] }
  49. }
  50. // a new key: { row } or { error }. `text` is the line from the .pub file: `<type> <base64> [comment]`
  51. static addKey = (userId, name, text) => {
  52. u = userRecord(userId)
  53. if (u == null) { return { error = 'log in with ident (top right) first' } }
  54. n = name == null ? '' : ('' + name).trim()
  55. bad = plainError(n, 60, 'the key name')
  56. if (bad != null) { return { error = bad } }
  57. if (n == '') { return { error = 'give the key a name (for example the computer it is for)' } }
  58. if (text == null || hlTypeName(text) != 'String' || text.length > 2000) { return { error = 'paste the public key (the .pub file)' } }
  59. t = text.trim()
  60. if (t.startsWith('-----')) { return { error = 'that is a PRIVATE key — never paste it. Paste the .pub file: ssh-keygen -y -f ~/.ssh/id_ed25519' } }
  61. if (t.includes(NL)) { return { error = 'paste one public key, on one line' } }
  62. parts = t.split(' ')
  63. let type = parts[0]
  64. if (parts.length < 2 || !allowedTypes.includes(type)) { return { error = 'not a public key of a supported type (ssh-ed25519, ssh-rsa, ecdsa, sk-…)' } }
  65. let key = parts[1]
  66. if (!isBase64(key, 1200, 20)) { return { error = 'the key text is not valid' } }
  67. info = inspectKey(type, key)
  68. if (info == null) { return { error = 'the key text is not valid' } }
  69. if (type == 'ssh-rsa' && info.bits < 2048) { return { error = 'RSA keys need at least 2048 bits' } }
  70. if (countOfList(sshKeysTable.find('key', key)) > 0) { return { error = 'that key is already added' } }
  71. if (countOfList(sshKeysTable.find('user', u.id)) >= maxKeys) { return { error = 'you have ' + maxKeys + ' keys already — remove one first' } }
  72. id = sshKeysTable.put({ user = u.id name = n type = type key = key fingerprint = info.fingerprint created = now() })
  73. if (id == null) { return { error = 'could not store the key: ' + sshKeysTable.lastError() } }
  74. return { row = rowOfKey(sshKeysTable.fetch(id)) }
  75. }
  76. // remove one of the user's own keys (works at once: the sshd container asks again on every login)
  77. static revokeKey = (userId, keyId) => {
  78. if (userId == null || keyId == null || hlTypeName(keyId) != 'String') { return { error = 'no such key' } }
  79. k = sshKeysTable.fetch(keyId)
  80. if (k == null || k.user != userId) { return { error = 'no such key' } }
  81. sshKeysTable.delete(keyId)
  82. return { ok = true }
  83. }
  84. // the user a key belongs to (users @id) or null
  85. static userOfKey = (type, key) => {
  86. if (type == null || key == null || hlTypeName(type) != 'String' || hlTypeName(key) != 'String' || !isBase64(key, 1200, 20)) { return null }
  87. k = firstOf(sshKeysTable.find('key', key))
  88. if (k == null || k.type != type) { return null }
  89. return k.user
  90. }
  91. // what the sshd container's AuthorizedKeysCommand gets for a key (lib/api.hl gitKeys): the authorized_keys line of a known
  92. // user's key — forced command `gitoria-shell <user id>`, nothing else allowed — or '' for an unknown one
  93. static keyLine = (type, key) => {
  94. userId = userOfKey(type, key)
  95. if (userId == null || userRecord(userId) == null) { return '' }
  96. return 'restrict,command="/usr/local/bin/gitoria-shell ' + userId + '" ' + type + ' ' + key + NL
  97. }

Branches

Latest commits

  • e9f950a8reports: G25c soakmre
  • a7783686log: deploy 9f41ffcmre
  • 9f41ffc1log: W30b mergedmre
  • bef635c1Merge branch 'w30b'mre
  • 15b996a5Org list: keyed rows by @id, newest first, concat instead of hand loopmre
  • c4f6d9e3reports: G25c growth huntmre
  • b88e39f1log: privacy cleanup donemre
  • bfd7826ddocs: neutral example organisation namemre
  • 01b71c2edocs: neutral wording in missions, reports and logmre
  • 3eeea733log: 2026-11-09 PR flow plan and membersmre
  • e7c2ff1fdocs: neutral name for the apps-api reviewmre
  • 120b7abdlog: step 3 queuedmre
  • 358474bflog: P50 livemre
  • 477d7bdcMerge branch 'p50'mre
  • 00efc06fP50 reportmre
  • e3f85afblog: commit mention slipmre
  • 4d1a4cf4log: #25 numbersmre
  • 1af0a60ddocs: H25 run() correctionmre
  • eefa2dbcRepo setting: refuse direct pushes to the main branch (pre-receive over HTTPS and ssh)mre
  • 8c8ba2f1log: PR flow announcedmre