gitoria
All repositories: gitoria
4.3 KB
// lib/apps-helpers.hl — what lib/apps.hl looks up: the URL allow-list for an app's receiver (`hook`) and `return` address, the shapes// of keys and secrets, the address of a repo's API, the one-line host of a receiver. Statics only (docs/apps-api.md §1).// Config (environment, or `.env` beside project.hl): GITORIA_APP_HOSTS = the HyperConductor bases an app's URLs may start with, written// exactly as they are used (plain http and internal IP:port included), separated by commas or spaces. Default empty = only worldapi.org.import { envOr, isHex, repoOrigin, orgOrigin } from './util.hl'static appHostsRaw = envOr('GITORIA_APP_HOSTS', '')// the configured bases: one per entry, a trailing `/` dropped; an entry that is not http:// or https:// is ignoredstatic appBases = () => {out = []for (part of appHostsRaw.replaceAll(',', ' ').split(' ')) {p = part.trim()if (p != '' && (p.startsWith('http://') || p.startsWith('https://'))) {out.push(p.endsWith('/') ? p.slice(0, p.length - 1) : p)}}return out}static bases = appBases()// a URL is one line of visible ASCII (no space, no control character, no backslash), at most 500 charactersstatic plainUrl = (u) => {if (u == null || hlTypeName(u) != 'String' || u.length == 0 || u.length > 500) { return false }let i = 0while (i < u.length) {c = u.charCodeAt(i)if (c < 33 || c > 126 || c == 92) { return false }i = i + 1}return true}// a host name: letters, digits, `-` and `.` only (so no port, no user name, no IP-with-port trick)static hostChars = (h) => {if (h == '' || h.startsWith('.') || h.endsWith('.') || h.includes('..')) { return false }let i = 0while (i < h.length) {c = h.charCodeAt(i)if (!((c >= 48 && c <= 57) || (c >= 97 && c <= 122) || c == 45 || c == 46)) { return false }i = i + 1}return true}// the host of an https:// URL as written, lower case; null when there is no clean host (a user name, a port, an empty host)static httpsHost = (u) => {rest = u.slice(8)let end = rest.lengthfor (stop of ['/', '?', '#']) {k = rest.indexOf(stop)if (k >= 0 && k < end) { end = k }}h = rest.slice(0, end).toLowerCase()return hostChars(h) ? h : null}// MAY THIS URL BE AN APP'S `hook` OR `return`? (a) https:// with host worldapi.org or one of its subdomains, or (b) the URL IS one of the configured// bases or goes on after it with `/` (so http://100.77.141.84:8100.evil does not pass). Nothing else: no redirects are ever followed.static urlAllowed = (u) => {if (!plainUrl(u)) { return false }if (u.startsWith('https://')) {h = httpsHost(u)if (h != null && (h == 'worldapi.org' || h.endsWith('.worldapi.org'))) { return true }}for (b of bases) {if (u == b || u.startsWith(b + '/')) { return true }}return false}// the host (with port) of a receiver URL, for the list on the settings pagestatic hostOfUrl = (u) => {if (u == null || hlTypeName(u) != 'String') { return '' }i = u.indexOf('://')rest = i < 0 ? u : u.slice(i + 3)let end = rest.lengthfor (stop of ['/', '?', '#']) {k = rest.indexOf(stop)if (k >= 0 && k < end) { end = k }}return rest.slice(0, end)}// `return` with `code` and `state` added (the return URL may carry a query already)static withQuery = (u, pairs) => {let out = ulet hash = ''h = u.indexOf('#')if (h >= 0) { out = u.slice(0, h) hash = u.slice(h) }let sep = out.includes('?') ? '&' : '?'for (k of pairs.keys()) {out = out + sep + k + '=' + encodeURIComponent(pairs[k])sep = '&'}return out + hash}// a key (`gak_` + 40 hex) is shaped like one, or nothing is looked upstatic keyShaped = (k) => {return k != null && hlTypeName(k) == 'String' && k.length == 44 && k.startsWith('gak_') && isHex(k.slice(4), 40)}// the API base of a repo (docs/apps-api.md §0): <slug address>/api, inside an organization <org address>/api/<name>static apiOf = (slug) => {i = slug.indexOf('/')return i < 0 ? repoOrigin(slug) + '/api' : orgOrigin(slug.slice(0, i)) + '/api/' + slug.slice(i + 1)}// the form-encoded body of the confirm page's POST: { name = value } (hex values only travel in it, nothing is decoded)static formFields = (body) => {out = {}if (body == null || hlTypeName(body) != 'String') { return out }for (pair of body.split('&')) {k = pair.indexOf('=')if (k > 0) { out[pair.slice(0, k)] = pair.slice(k + 1) }}return out}
Branches
Latest commits
- fe209ffeMerge branch 'w56-owner-members' into mainGitoria
- fa0754c8|||PR repository owner transfer and members API (#55)mre
- 8862e537Merge branch 'w55-rename-org' into mainGitoria
- 014d2b7fOne immutable readers snapshot per move for the left-organization event (#53)mre
- 1859d506Decide who hears a repo left its organization by its readers before the move (#53)mre
- 499f25f2Review fixes for the Settings organization move (#53)mre
- ed3b922b|||PR move a repository into an organization in Settings (#53)mre
- 74ec2af8Merge branch 'w54-repo-rename-pr' into mainGitoria
- 9f7ce8d8Preserve rename compatibility and live page titlesmre
- c3d95b6e|||PR separate names and addresses, rename repositories in Settings (#53)mre
- ce08920dMerge branch 'w52c-header-phone' into mainGitoria
- 0bab9e46|||PR header: ident status wraps on phones instead of clippingmre
- e7af3eceMerge branch 'w52b-approval-followup' into mainGitoria
- c01e66c6|||PR approvals follow-up: live Merge after status writes, helpers (#52)mre
- 841da497Merge branch 'w53-pulls-speed-pr' into mainGitoria
- 80307ae2|||PR pulls page cache: live PR numbers, complete scans onlymre
- 69141f65|||PR pulls page: cache the git part per repo by branch tipsmre
- 155861f6Merge branch 'w52-person-approval' into mainGitoria
- b5435de8|||PR person approvals: second Kunibert round (#52)mre
- 3a8b2fb8|||PR person approvals: Kunibert review fixes (#52)mre