gitoria
All repositories: gitoria
6.3 KB
// lib/sshkeys.hl — SSH KEYS for git over SSH (ticket gitoria#7; docs/git-backend.md "Credentials"). A logged-in user pastes a// 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// key works at once and no authorized_keys file is edited. Only the public key is stored — nothing secret.// sshKeysTable pk @id index !key, user { user (users @id), name, type, key (base64), fingerprint, created } storage/mpackdb/sshkeys.db// 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.import { MPackDB } from 'hl:mpackdb'import { run } from 'hl:proc'import { now } from 'hl:time'import { writeFile, remove, exists } from 'hl:fs'import { randomBytes } from 'hl:crypto'import { NL, storageDir, countOfList, firstOf, plainError, envOr, newestFirst, localStamp } from './util.hl'import { userRecord } from './users.hl'import { tmpDir } from './git.hl'static sshKeysTable = new MPackDB(file = storageDir + '/sshkeys.db', primaryKey = '@id', indexes = ['!key', 'user'])static maxKeys = 20// SSH is offered only when the sshd container is set up: the shared secret it uses to ask this app is in the environmentstatic sshSecret = envOr('GITORIA_SSH_SECRET', '')static sshPort = envOr('GITORIA_SSH_PORT', '2222')// the host name in SSH clone addresses: repo addresses go through Cloudflare's proxy, which does not pass SSH, so// production sets a DNS-only name (GITORIA_SSH_HOST=ssh.gitoria.worldapi.org); empty = the page's own host (dev, gates)static sshHost = envOr('GITORIA_SSH_HOST', '')static sshEnabled = sshSecret != ''static allowedTypes = ['ssh-ed25519', 'ecdsa-sha2-nistp256', 'ecdsa-sha2-nistp384', 'ecdsa-sha2-nistp521', 'ssh-rsa', '[email protected]', '[email protected]']// the address to clone from over SSH: `git@host:slug.git` on port 22, else the ssh:// form with the portstatic sshUrl = (host, slug) => {h = sshHost != '' ? sshHost : hostif (sshPort == '22') { return 'git@' + h + ':' + slug + '.git' }return 'ssh://git@' + h + ':' + sshPort + '/' + slug + '.git'}static isKeyBase64 = (s) => {if (s.length < 20 || s.length > 1200) { return false }let i = 0while (i < s.length) {let c = s.charCodeAt(i)if (!((c >= 48 && c <= 57) || (c >= 65 && c <= 90) || (c >= 97 && c <= 122) || c == 43 || c == 47 || c == 61)) { return false }i = i + 1}return true}static rowOfKey = (k) => { return { id = k.id name = k.name fingerprint = k.fingerprint created = localStamp(k.created) createdMs = k.created } }// a user's keys, newest firststatic keyRows = (userId) => {out = []all = sshKeysTable.find('user', userId)if (countOfList(all) == 0) { return out }for (k of all) { out.push(rowOfKey(k)) }return newestFirst(out)}// `ssh-keygen -l` on the key: → { bits, fingerprint } or null when it is not a keystatic inspectKey = (type, key) => {file = tmpDir() + '/' + randomBytes(12, 'hex') + '.pub'writeFile(file, type + ' ' + key + NL, 384)r = run(['ssh-keygen', '-l', '-f', file], { timeout = 10 })if (exists(file)) { remove(file) }if (r == null || r.exit != 0 || r.lines.length == 0) { return null }parts = r.lines[0].split(' ')if (parts.length < 2 || !parts[1].startsWith('SHA256:')) { return null }return { bits = toNumber(parts[0]) fingerprint = parts[1] }}// a new key: { row } or { error }. `text` is the line from the .pub file: `<type> <base64> [comment]`static addKey = (userId, name, text) => {u = userRecord(userId)if (u == null) { return { error = 'log in with ident (top right) first' } }n = name == null ? '' : ('' + name).trim()bad = plainError(n, 60, 'the key name')if (bad != null) { return { error = bad } }if (n == '') { return { error = 'give the key a name (for example the computer it is for)' } }if (text == null || hlTypeName(text) != 'String' || text.length > 2000) { return { error = 'paste the public key (the .pub file)' } }t = text.trim()if (t.startsWith('-----')) { return { error = 'that is a PRIVATE key — never paste it. Paste the .pub file: ssh-keygen -y -f ~/.ssh/id_ed25519' } }if (t.includes(NL)) { return { error = 'paste one public key, on one line' } }parts = t.split(' ')let type = parts[0]if (parts.length < 2 || !allowedTypes.includes(type)) { return { error = 'not a public key of a supported type (ssh-ed25519, ssh-rsa, ecdsa, sk-…)' } }let key = parts[1]if (!isKeyBase64(key)) { return { error = 'the key text is not valid' } }info = inspectKey(type, key)if (info == null) { return { error = 'the key text is not valid' } }if (type == 'ssh-rsa' && info.bits < 2048) { return { error = 'RSA keys need at least 2048 bits' } }if (countOfList(sshKeysTable.find('key', key)) > 0) { return { error = 'that key is already added' } }if (countOfList(sshKeysTable.find('user', u.id)) >= maxKeys) { return { error = 'you have ' + maxKeys + ' keys already — remove one first' } }id = sshKeysTable.put({ user = u.id name = n type = type key = key fingerprint = info.fingerprint created = now() })if (id == null) { return { error = 'could not store the key: ' + sshKeysTable.lastError() } }return { row = rowOfKey(sshKeysTable.fetch(id)) }}// remove one of the user's own keys (works at once: the sshd container asks again on every login)static revokeKey = (userId, keyId) => {if (userId == null || keyId == null || hlTypeName(keyId) != 'String') { return { error = 'no such key' } }k = sshKeysTable.fetch(keyId)if (k == null || k.user != userId) { return { error = 'no such key' } }sshKeysTable.delete(keyId)return { ok = true }}// the user a key belongs to (users @id) or nullstatic userOfKey = (type, key) => {if (type == null || key == null || hlTypeName(type) != 'String' || hlTypeName(key) != 'String' || !isKeyBase64(key)) { return null }k = firstOf(sshKeysTable.find('key', key))if (k == null || k.type != type) { return null }return k.user}// what the sshd container's AuthorizedKeysCommand gets for a key (lib/api.hl gitKeys): the authorized_keys line of a known// user's key — forced command `gitoria-shell <user id>`, nothing else allowed — or '' for an unknown onestatic keyLine = (type, key) => {userId = userOfKey(type, key)if (userId == null || userRecord(userId) == null) { return '' }return 'restrict,command="/usr/local/bin/gitoria-shell ' + userId + '" ' + type + ' ' + key + NL}
Branches
- f1-broken-json
- f2-api-bugs
- gp-gate-ports
- heap-revert
- hl25-records
- ident-chooser-selector
- mainmain branch
- p51-public-pr-reads
- records-1112
- s25-no-session-on-login-error
- v59
- w52-person-approval
- w52b-approval-followup
- w52c-header-phone
- w53-pulls-speed-pr
- w54-repo-rename-pr
- w55-rename-org
- w56-owner-members
- w62-pr-close
- w63-force-push
- w64-org-slash
Latest commits
- 5b0b4b9ereadme-cache gate: use the security gate's ports when no own ports are givenmre
- bc8f20d9gitoria#25: re-vendor Hybriel c09538bb with the W141 render cache, cache the homepage per commit, add the readme-cache gatemre
- 9feebbd3Re-vendor Hybriel to ded47c13 (W145: Firefox start script fix)mre
- 28ed41e5gitoria#24: one sign-in comes from hl:web batch 20, the /session/share workaround is gonemre
- 3b2669e4gs: drop the unbuildable legacy check, fix pwa fetchmre
- 2a2fdd77gs: C2 check before cookie experimentsmre
- 76e0587aWIP gs 2mre
- a80da7bdWIP gs: security gate for lazy sessions, g24 browser checksmre
- 23fb05d8re-vendor Hybriel aaa39e69 (hl:web batch 20: login emit and Sessions.mint, tab follows a login, signed-in session cookie wins among duplicates and is re-issued with the Domain, W85 faster render, lazy sessions)mre
- a0727ca3ident#31: the selector's sign-in returns to the page the user started on (return attribute = the login callback with next), gate checkmre
- a06975f9gitoria: a gap between your name and Log out in the phone header (they touched); gate checks itmre
- 32ba9d59gitoria: leave the host-only cookie experiment out of the browser gate (gitoria#24 is a separate mission)mre
- 43ad2bcfgitoria header: the selector alone plus your name and Log out as before the theme missions; tests follow (WIP: host-only cookie experiment fails)mre
- ac2c4e3fident selector back in the header (creator 2026-10-24): selector top right signed in and out on phone and desktop, no second Log in button, new gate checks (WIP: gates not re-run after the last edits)mre
- b279f536theme: sync from layouts 4994037 (list rows stack, td word-keep, heading code, edited tap target)mre
- 2b01515ctests/ticketskit: a failed start kills the tickets server (own process group); stop() kills the groupmre
- 109cfc5ctests/ticketskit: keep the rotated session cookie of the tickets login (tickets ST2 session rotation)mre
- 7eb6be27sessions last 99 years (creator 2026-10-23: logins never expire; only sign out ends them)mre
- b3911918reports/MG-mobile.md: 404 fixed, rebased gatesMG
- 9da65b42tests/browser.mjs: 404 checks (host, no hydration wait)MG