gitoria
All repositories: gitoria
4.8 KB
// lib/api-helpers.hl — the plumbing of the function routes (lib/api.hl): JSON / Markdown / plain answers, redirects,// the guard of the internal ssh routes, the repo of a request's host, where a login may return to. Statics only.import { Response } from 'hl:http1'import { NL, scheme, hostPort, slugOfHost } from './util.hl'import { slugOfRequest } from './orgs.hl'import { slugError, fullNameError } from './repos-helpers.hl'import { repoBySlug } from './repos.hl'import { sshSecret, sshEnabled } from './sshkeys.hl'static jsonHeaders = { 'Content-Type' = 'application/json; charset=utf-8' 'X-Content-Type-Options' = 'nosniff' }static reply = (status, value) => {return new Response(JSON.stringify(value), { status = status headers = jsonHeaders })}static fail = (status, message) => { return reply(status, { error = message }) }// THE APPS API'S ERROR SHAPE (docs/apps-api.md §6): { error: { code, message, details } }static apiError = (status, code, message, details) => {return reply(status, { error = { code = code message = message details = details } })}// THE ROUTE PATTERNS OF AN APPS API ROUTE (gitoria#41/#42; docs/apps-api.md §0, the API base): `path` (e.g. '/apps/token', '/pulls/:n') exists on the// repo's own address (`/api/…`) and on an organization's address in both spellings: `/api/<repo>/…` and `/<repo>/api/…`. project.hl lists a route once:// for (p of apiPatterns('/apps/token')) { { pattern = p function = appToken } }static apiPatterns = (path) => {return ['/api' + path, '/api/:orepo' + path, '/:orepo/api' + path]}// THE MARKDOWN READ VIEW: `Accept: text/markdown` (for LLM readers)static wantsMarkdown = (req) => {a = req.headers['accept']return a != null && hlTypeName(a) == 'String' && a.includes('text/markdown')}static markdownReply = (text) => {return new Response(text, { status = 200 headers = { 'Content-Type' = 'text/markdown; charset=utf-8' 'X-Content-Type-Options' = 'nosniff' } })}// a plain-text answer of the internal ssh routesstatic textReply = (status, text) => {return new Response(text, { status = status headers = { 'Content-Type' = 'text/plain; charset=utf-8' 'Cache-Control' = 'no-store' 'X-Content-Type-Options' = 'nosniff' } })}static goTo = (where) => {return new Response('redirect', { status = 302 headers = { 'Location' = where 'Cache-Control' = 'no-store' 'Content-Type' = 'text/plain; charset=utf-8' 'X-Content-Type-Options' = 'nosniff' } })}// THE INTERNAL SSH ROUTES' GUARD (docker/sshd asks them): null when the caller may ask, else the refusing answer. Without// GITORIA_SSH_SECRET they do not exist (404); every call needs `X-Gitoria-Secret` = that secret, and a request that came// through the public proxy (it carries X-Forwarded-For / X-Real-IP) is refused.static sshGuard = (req) => {if (!sshEnabled) { return textReply(404, 'not found' + NL) }if (req.method != 'GET') { return textReply(405, 'GET only' + NL) }if (req.headers['x-forwarded-for'] != null || req.headers['x-real-ip'] != null) { return textReply(403, 'internal only' + NL) }given = req.headers['x-gitoria-secret']if (given == null || hlTypeName(given) != 'String' || given != sshSecret) { return textReply(403, 'wrong secret' + NL) }return null}// the repo of a request (its host, and inside an organization the first path segment `:orepo`), or nullstatic repoOfRequest = (route, req) => {h = req.headers['host']orepo = route.params != null && route.params.orepo != null ? route.params.orepo : ''slug = slugOfRequest(h == null ? '' : h.split(':')[0], orepo)return slug == '' || fullNameError(slug) != null ? null : repoBySlug(slug)}// BACK TO THE PAGE after a login: /login.js puts `?next=` into the button's return URL at the click. Only a same-origin PATH// goes (one `/`, URL-safe characters, ≤ 500) — or a full URL of THIS system: <scheme>://<label>.<host>// with a valid, non-reserved repo label and such a path. Anything else → `/`.static nextChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~/?&=%+,;@!$()*:'static nextPath = (want) => {if (want == null || hlTypeName(want) != 'String' || want == '' || want.length > 500) { return '/' }if (want.slice(0, 1) != '/' || want.slice(0, 2) == '//' || want.slice(0, 7) == '/login/') { return '/' }let i = 0while (i < want.length) {if (!nextChars.includes(want[i])) { return '/' }i = i + 1}return want}static safeNext = (want) => {if (want == null || hlTypeName(want) != 'String' || want.length > 500) { return '/' }prefix = scheme + '://'if (!want.startsWith(prefix)) { return nextPath(want) }rest = want.slice(prefix.length)slash = rest.indexOf('/')hp = slash < 0 ? rest : rest.slice(0, slash)path = slash < 0 ? '/' : rest.slice(slash)dot = hp.indexOf('.')if (dot < 1 || hp.slice(dot + 1) != hostPort || slugError(hp.slice(0, dot)) != null) { return '/' }p = nextPath(path)if (p == '/' && path != '/') { return '/' }return prefix + hp + p}
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
- e5a0b64egitoria#46: PUT statuses/<sha>, required contexts block the merge on the servermre
- 49533a50gitoria#43: signed events to connected apps (merge w43-events)mre
- 1a1e87d5gitoria#43: the sender never writes the apps row; Settings shows the newest deliverymre
- cd442066gitoria#43: app-auth unit test reads the apps table from its new filemre
- 5dccbe38gitoria#43: in-flight rows are left alone by the other process; gate in deploy.sh; READMEmre
- 8c8805f3merge main (gitoria#42), delivery log and redelivery routes, sweep fixmre
- 5a996ea8gitoria#42: short-lived app tokens, read-only git fetch with them (merge w42-app-token)mre
- afdb0cbfgitoria#43: rename the moved helper (name clash in the browser build)mre
- 15e01e94gitoria#43: tests/events.mjs and its probemre
- 77829b73gitoria#43: signed events to connected apps (deliveries table, sender timer, pull_request events, ping)mre
- b5d696fegitoria#42: app tokens (gat_, one hour), appOfRequest, app token as the git read passwordmre
- 1997e77dgitoria#40: stable PR numbers, refs/pull/N/head, ssh pushes reach the app (merge w40-pr-number)mre
- 9364fb16gitoria#40: ssh pushes call /__git/pushed before and after (ensurePulls, notifyPush, syncPulls); gate over the sshd imagemre
- fdb9ed28gitoria#41: repo owners can connect an app (merge w41-apps-connect)mre
- 2ef23df0gitoria#40: the browser gate reads the title without the #Nmre
- 9af07ecegitoria#41: connect an app to a repo (apps table, connect flow, manual add, allow-list, settings)mre
- b9137ff0gitoria#40: pull requests are stored with a number, refs/pull/N/head, syncPulls and pullObject; /pulls shows #Nmre
- 06d622daown image tag gitoria-runtime:debian12 for gitoria and standby (shared tag lost git)mre
- 20caa33agitoria#37: media queries in the language's @media block (minWidth = '60rem') in list, codebrowser and repomre
- c4175e6dgitoria: a startup error prints its message before exit 1 (hybriel#247)mre