gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitddc7d135ddc7d135Merge branch 'f7-hooks' into mainIdentddc7d135/lib/apps-helpers.hl

6.6 KB

  1. // lib/apps-helpers.hl — what lib/apps.hl (and lib/invites.hl) look up: the app form's strict input, an origin,
  2. // a return URL against the app's origins, new keys and secrets, the row a page shows. No table access. Statics only.
  3. import { randomBytes } from 'hl:crypto'
  4. import { hasControl, isList, isObject, urlChars } from './util.hl'
  5. static maxOrigins = 10
  6. static maxName = 60
  7. // ---- validation -----------------------------------------------------------------------
  8. static checkName = (name) => {
  9. if (name == null || hlTypeName(name) != 'String') { return { error = 'field name must be a string' field = 'name' } }
  10. t = name.trim()
  11. if (t == '') { return { error = 'the app needs a name' field = 'name' } }
  12. if (t.length > maxName) { return { error = 'field name is longer than ' + maxName + ' characters' field = 'name' } }
  13. if (hasControl(t)) { return { error = 'field name contains a control character' field = 'name' } }
  14. return { name = t }
  15. }
  16. // AN ORIGIN is `http(s)://host[:port]`: lowercase, no path, no user, no query. One
  17. // trailing slash is forgiven. Answers { origin } or { error }.
  18. static hostChars = 'abcdefghijklmnopqrstuvwxyz0123456789.-[]:'
  19. static checkOrigin = (o) => {
  20. if (o == null || hlTypeName(o) != 'String') { return { error = 'an origin must be a string' } }
  21. let t = o.trim().toLowerCase()
  22. if (t.length > 0 && t[t.length - 1] == '/') { t = t.slice(0, t.length - 1) }
  23. if (t.length > 200) { return { error = 'the origin ' + t + ' is too long' } }
  24. scheme = t.slice(0, 7) == 'http://' ? 'http://' : (t.slice(0, 8) == 'https://' ? 'https://' : null)
  25. if (scheme == null) { return { error = 'an origin starts with http:// or https:// (' + t + ')' } }
  26. let host = t.slice(scheme.length, t.length)
  27. // A WILDCARD ORIGIN `scheme://*.example.org[:port]` stands for every ONE-label subdomain of example.org (an app that runs on
  28. // <name>.example.org, e.g. gitoria's repository addresses): `*.` is only the first label, and what follows has a dot
  29. let wild = false
  30. if (host.slice(0, 2) == '*.') {
  31. wild = true
  32. host = host.slice(2, host.length)
  33. 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)' } }
  34. }
  35. if (host == '' || host[0] == '.' || host[0] == ':') { return { error = 'the origin ' + t + ' has no host' } }
  36. let i = 0
  37. while (i < host.length) {
  38. if (!hostChars.includes(host[i])) { return { error = 'an origin is only scheme://host[:port], no path (' + t + ')' } }
  39. i = i + 1
  40. }
  41. return { origin = t }
  42. }
  43. // the app form's input, STRICT: exactly { name, origins } — origins a list of 1-10
  44. // strings. Answers { error, field } or { name, origins (list, deduplicated) }.
  45. static appFields = ['name' 'origins']
  46. static checkAppInput = (input) => {
  47. if (!isObject(input)) { return { error = 'the app must be an object' field = '' } }
  48. for (k of input.keys()) {
  49. if (!appFields.includes(k)) { return { error = 'unknown field: ' + k field = k } }
  50. }
  51. for (k of appFields) {
  52. if (input[k] == null) { return { error = 'missing field: ' + k field = k } }
  53. }
  54. n = checkName(input.name)
  55. if (n.error != null) { return n }
  56. if (!isList(input.origins)) { return { error = 'field origins must be a list of strings' field = 'origins' } }
  57. out = []
  58. for (o of input.origins) {
  59. let c = checkOrigin(o)
  60. if (c.error != null) { return { error = c.error field = 'origins' } }
  61. if (!out.includes(c.origin)) { out.push(c.origin) }
  62. }
  63. if (out.length == 0) { return { error = 'give at least one origin the app runs on' field = 'origins' } }
  64. if (out.length > maxOrigins) { return { error = 'at most ' + maxOrigins + ' origins' field = 'origins' } }
  65. return { name = n.name origins = out }
  66. }
  67. // ---- apps -----------------------------------------------------------------------------
  68. static newKey = () => { return 'pk_' + randomBytes(16) } // 32 hex
  69. static newSecret = () => { return 'sk_' + randomBytes(24) } // 48 hex
  70. static originsOf = (rec) => { return rec.origins == '' ? [] : rec.origins.split(' ') }
  71. // DOES THE ORIGIN MATCH A REGISTERED ONE? Exactly, or a wildcard `scheme://*.tail` against `scheme://<one label>.tail`
  72. // (the label: letters, digits and hyphens, 1-63 characters; no dot, so a sub-subdomain, the bare domain and a lookalike
  73. // such as evil-gitoria.worldapi.org or gitoria.worldapi.org.evil.com do NOT match). Statics only.
  74. static labelChars = 'abcdefghijklmnopqrstuvwxyz0123456789-'
  75. static matchesOrigin = (registered, origin) => {
  76. if (registered == origin) { return true }
  77. at = registered.indexOf('://*.')
  78. if (at < 0) { return false }
  79. scheme = registered.slice(0, at + 3)
  80. tail = registered.slice(at + 4, registered.length)
  81. if (origin.slice(0, scheme.length) != scheme) { return false }
  82. rest = origin.slice(scheme.length, origin.length)
  83. if (rest.length <= tail.length || !rest.endsWith(tail)) { return false }
  84. label = rest.slice(0, rest.length - tail.length)
  85. if (label.length > 63 || label[0] == '-') { return false }
  86. let k = 0
  87. while (k < label.length) {
  88. if (!labelChars.includes(label[k])) { return false }
  89. k = k + 1
  90. }
  91. return true
  92. }
  93. static originAllowed = (rec, origin) => {
  94. for (r of originsOf(rec)) {
  95. if (matchesOrigin(r, origin)) { return true }
  96. }
  97. return false
  98. }
  99. static appRowOf = (rec) => {
  100. if (rec == null) { return null }
  101. let origins = originsOf(rec)
  102. return { id = rec.id name = rec.name origins = origins originsText = origins.join(' ') apiKey = rec.apiKey }
  103. }
  104. // ---- the login button ---------------------------------------------------------------
  105. // A RETURN URL is absolute http(s), printable, no fragment, at most 2000 chars, and its
  106. // origin is one of the app's. Answers { error } or { origin }.
  107. static checkReturn = (url, appRec) => {
  108. if (url == null || url == '') { return { error = 'the return parameter is missing' } }
  109. if (url.length > 2000) { return { error = 'the return URL is too long' } }
  110. let i = 0
  111. while (i < url.length) {
  112. if (!urlChars.includes(url[i])) { return { error = 'the return URL contains a character that is not allowed' } }
  113. i = i + 1
  114. }
  115. lower = url.toLowerCase()
  116. scheme = lower.slice(0, 7) == 'http://' ? 'http://' : (lower.slice(0, 8) == 'https://' ? 'https://' : null)
  117. if (scheme == null) { return { error = 'the return URL must start with http:// or https://' } }
  118. rest = lower.slice(scheme.length, lower.length)
  119. let end = rest.length
  120. for (ch of ['/' '?']) {
  121. let at = rest.indexOf(ch)
  122. if (at >= 0 && at < end) { end = at }
  123. }
  124. host = rest.slice(0, end)
  125. if (host == '') { return { error = 'the return URL has no host' } }
  126. let origin = scheme + host
  127. if (!originAllowed(appRec, origin)) { return { error = 'the return URL is on ' + origin + ', which is not one of the origins registered for ' + appRec.name } }
  128. return { origin = origin }
  129. }

Branches

Latest commits

  • ddc7d135Merge branch 'f7-hooks' into mainIdent
  • eee0b513|||PR ident#34 part: test-hook classes replaced by name/element/attribute selectors (tests adjusted), the shared lead/message/notice looks are statics in styles.hlmre
  • ccd6d0c4ident#34: browser gate: rowButton finds the row's button by namemre
  • 24dd414eident#34: the shared lead, message and notice looks are statics in components/styles.hl, imported by the components (lib/looks.hl gone)mre
  • 03ed26c7ident#34: access.hl: the remaining lead classes droppedmre
  • 1d066c42ident#34: access.hl drops its unstyled lead class (the shared lead rule would otherwise mute it)mre
  • 1e1dd7b4ident#34: the access page keeps its lead class string (the page has no ident-card, so its lead line was never muted; a shared rule would change the look)mre
  • 89ac6553ident#34: the lead, message and notice lines are named with Style.pLead / pMessage / pNotice (shared bodies in lib/looks.hl) instead of class stringsmre
  • e84be3deident#34: browser gate lists the Edit/Delete buttons by namemre
  • fb7111faident#34: access page: unstyled test-hook classes (idrow, never, who, whoname, appname, neverrevoke) dropped; the tests use element/name selectorsmre
  • 9f2ad803ident#34: notification switches: push/email by name=..., locked by aria-disabled, the switch look is the role=switch button (no switch/push/email/locked classes)mre
  • e0f0ddaaident#34: inbox: the Mark read/unread button is styled as the button of its note-foot, the icon is found as the row's img (no toggle/icon classes)mre
  • 16b49756ident#34: unused row classes (choice, usedrow, kind, shortid) droppedmre
  • f9d6fb38ident#34: apps and identity rows: the Edit/New secret/Delete buttons carry name=..., the API key is found by its element; unused row classes (app, identity, shortid) droppedmre
  • b323265aMerge branch 'f5-styles' into mainIdent
  • 9ced53c3|||PR ident#33 ident#35 every component's styles live in its own Style block; styles.hl keeps the page-wide rest; the account bar is phone first (min-width: 60rem block); the avatar preview is hidden with the hidden attribute (ident#34 in part)mre
  • f28333e4ident#33 ident#35: styles.hl keeps only the accent, the page column and the shared patterns; the account bar is written phone first (base rule + one min-width: 60rem block) instead of a max-width overridemre
  • 5b4541fcident#33: the selector sign-in frame's rules move into embed.hl's Stylemre
  • 00235f21ident#33: the per-app notification switches move into appsettings.hl's Stylemre
  • 2249945cident#33: the inbox and connection list rules move into inbox.hl's Stylemre