ident
All repositories: gitoria
6.6 KB
// lib/apps-helpers.hl — what lib/apps.hl (and lib/invites.hl) look up: the app form's strict input, an origin,// a return URL against the app's origins, new keys and secrets, the row a page shows. No table access. Statics only.import { randomBytes } from 'hl:crypto'import { hasControl, isList, isObject, urlChars } from './util.hl'static maxOrigins = 10static maxName = 60// ---- validation -----------------------------------------------------------------------static checkName = (name) => {if (name == null || hlTypeName(name) != 'String') { return { error = 'field name must be a string' field = 'name' } }t = name.trim()if (t == '') { return { error = 'the app needs a name' field = 'name' } }if (t.length > maxName) { return { error = 'field name is longer than ' + maxName + ' characters' field = 'name' } }if (hasControl(t)) { return { error = 'field name contains a control character' field = 'name' } }return { name = t }}// AN ORIGIN is `http(s)://host[:port]`: lowercase, no path, no user, no query. One// trailing slash is forgiven. Answers { origin } or { error }.static hostChars = 'abcdefghijklmnopqrstuvwxyz0123456789.-[]:'static checkOrigin = (o) => {if (o == null || hlTypeName(o) != 'String') { return { error = 'an origin must be a string' } }let t = o.trim().toLowerCase()if (t.length > 0 && t[t.length - 1] == '/') { t = t.slice(0, t.length - 1) }if (t.length > 200) { return { error = 'the origin ' + t + ' is too long' } }scheme = t.slice(0, 7) == 'http://' ? 'http://' : (t.slice(0, 8) == 'https://' ? 'https://' : null)if (scheme == null) { return { error = 'an origin starts with http:// or https:// (' + t + ')' } }let host = t.slice(scheme.length, t.length)// A WILDCARD ORIGIN `scheme://*.example.org[:port]` stands for every ONE-label subdomain of example.org (an app that runs on// <name>.example.org, e.g. gitoria's repository addresses): `*.` is only the first label, and what follows has a dotlet wild = falseif (host.slice(0, 2) == '*.') {wild = truehost = host.slice(2, host.length)if (!host.slice(0, host.indexOf(':') >= 0 ? host.indexOf(':') : host.length).includes('.')) { return { error = 'the wildcard origin ' + t + ' needs a domain after *. (e.g. https://*.example.org)' } }}if (host == '' || host[0] == '.' || host[0] == ':') { return { error = 'the origin ' + t + ' has no host' } }let i = 0while (i < host.length) {if (!hostChars.includes(host[i])) { return { error = 'an origin is only scheme://host[:port], no path (' + t + ')' } }i = i + 1}return { origin = t }}// the app form's input, STRICT: exactly { name, origins } — origins a list of 1-10// strings. Answers { error, field } or { name, origins (list, deduplicated) }.static appFields = ['name' 'origins']static checkAppInput = (input) => {if (!isObject(input)) { return { error = 'the app must be an object' field = '' } }for (k of input.keys()) {if (!appFields.includes(k)) { return { error = 'unknown field: ' + k field = k } }}for (k of appFields) {if (input[k] == null) { return { error = 'missing field: ' + k field = k } }}n = checkName(input.name)if (n.error != null) { return n }if (!isList(input.origins)) { return { error = 'field origins must be a list of strings' field = 'origins' } }out = []for (o of input.origins) {let c = checkOrigin(o)if (c.error != null) { return { error = c.error field = 'origins' } }if (!out.includes(c.origin)) { out.push(c.origin) }}if (out.length == 0) { return { error = 'give at least one origin the app runs on' field = 'origins' } }if (out.length > maxOrigins) { return { error = 'at most ' + maxOrigins + ' origins' field = 'origins' } }return { name = n.name origins = out }}// ---- apps -----------------------------------------------------------------------------static newKey = () => { return 'pk_' + randomBytes(16) } // 32 hexstatic newSecret = () => { return 'sk_' + randomBytes(24) } // 48 hexstatic originsOf = (rec) => { return rec.origins == '' ? [] : rec.origins.split(' ') }// DOES THE ORIGIN MATCH A REGISTERED ONE? Exactly, or a wildcard `scheme://*.tail` against `scheme://<one label>.tail`// (the label: letters, digits and hyphens, 1-63 characters; no dot, so a sub-subdomain, the bare domain and a lookalike// such as evil-gitoria.worldapi.org or gitoria.worldapi.org.evil.com do NOT match). Statics only.static labelChars = 'abcdefghijklmnopqrstuvwxyz0123456789-'static matchesOrigin = (registered, origin) => {if (registered == origin) { return true }at = registered.indexOf('://*.')if (at < 0) { return false }scheme = registered.slice(0, at + 3)tail = registered.slice(at + 4, registered.length)if (origin.slice(0, scheme.length) != scheme) { return false }rest = origin.slice(scheme.length, origin.length)if (rest.length <= tail.length || !rest.endsWith(tail)) { return false }label = rest.slice(0, rest.length - tail.length)if (label.length > 63 || label[0] == '-') { return false }let k = 0while (k < label.length) {if (!labelChars.includes(label[k])) { return false }k = k + 1}return true}static originAllowed = (rec, origin) => {for (r of originsOf(rec)) {if (matchesOrigin(r, origin)) { return true }}return false}static appRowOf = (rec) => {if (rec == null) { return null }let origins = originsOf(rec)return { id = rec.id name = rec.name origins = origins originsText = origins.join(' ') apiKey = rec.apiKey }}// ---- the login button ---------------------------------------------------------------// A RETURN URL is absolute http(s), printable, no fragment, at most 2000 chars, and its// origin is one of the app's. Answers { error } or { origin }.static checkReturn = (url, appRec) => {if (url == null || url == '') { return { error = 'the return parameter is missing' } }if (url.length > 2000) { return { error = 'the return URL is too long' } }let i = 0while (i < url.length) {if (!urlChars.includes(url[i])) { return { error = 'the return URL contains a character that is not allowed' } }i = i + 1}lower = url.toLowerCase()scheme = lower.slice(0, 7) == 'http://' ? 'http://' : (lower.slice(0, 8) == 'https://' ? 'https://' : null)if (scheme == null) { return { error = 'the return URL must start with http:// or https://' } }rest = lower.slice(scheme.length, lower.length)let end = rest.lengthfor (ch of ['/' '?']) {let at = rest.indexOf(ch)if (at >= 0 && at < end) { end = at }}host = rest.slice(0, end)if (host == '') { return { error = 'the return URL has no host' } }let origin = scheme + hostif (!originAllowed(appRec, origin)) { return { error = 'the return URL is on ' + origin + ', which is not one of the origins registered for ' + appRec.name } }return { origin = origin }}
Branches
Latest commits
- 59e8fcc1ident#40.3: the app's identity choice is one component (components/chooseidentity.hl); its face chooseIdentity moves with itmre
- e23efdfcident#40.3: the identity list is one component (components/identitylist.hl); its delete and face dropIdentity move with itmre
- e24b59a6ident#40.3: the time zone is one component (components/timezone.hl); its face changeTimeZone moves with itmre
- 8cbc5717ident#40.3: the email step is one component (components/emailstep.hl) placed by home.hl and embed.hlmre
- 6fad3bd2Merge branch 'f3-codestep' into mainIdent
- 51e387f2|||PR ident#40 the code step is one component (components/codestep.hl) placed by home.hl and embed.hl; its verify face, setCode, browserZone and verify call exist once (#40.2)mre
- c28bddeeMerge branch 'f2-shell' into mainIdent
- c3a40ff6|||PR ident#40 one shell for the app and /start (account part off on /start, #40.1); startshell.hl removed; unused signOut faces gone (#40.4)mre
- 060f3870ident#40.4: use the shell sign-out faces in regression gatesmre
- 9b94aa6aMerge branch 'f1b-cleanup' into mainIdent
- edcfce3f|||PR ident#41 ident#43 ident#44 login error page as a component (same status codes and text, pixel-identical), declarations and route wiring cleaned upmre
- 3ac85995ident#43 ident#44: plain declarations for never-reassigned lets, one envString helper, named route functions and audience rule out of project.hlmre
- c7eef14aMerge branch 'nv1-navigate' into mainIdent
- 644f9775|||PR ident#36 navigate() instead of window.location in the shell, start shell and homemre
- 4ca2f921deploy.sh: tar --hard-dereference so backups hold no hard linksmre
- 23971fb7ident#49: media queries in the language's @media formmre
- 73f4f97dident: re-vendor to hybriel day38b (mpackdb store-open fix)mre
- aa056c03ident: a startup error prints its message before exit 1 (hybriel#247)mre
- ee37a861ident#G48: gitoria's registration gets the wildcard origin of its repo addresses at load (gate tests/gitoria-origin.mjs)mre
- dc6d486fident#27: the docs gate expects the new Access entry in the main menumre