gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Main branchmainfe209ffeMerge branch 'w56-owner-members' into mainGitoriamain/lib/apps-helpers.hl

4.3 KB

  1. // 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
  2. // 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).
  3. // Config (environment, or `.env` beside project.hl): GITORIA_APP_HOSTS = the HyperConductor bases an app's URLs may start with, written
  4. // exactly as they are used (plain http and internal IP:port included), separated by commas or spaces. Default empty = only worldapi.org.
  5. import { envOr, isHex, repoOrigin, orgOrigin } from './util.hl'
  6. static appHostsRaw = envOr('GITORIA_APP_HOSTS', '')
  7. // the configured bases: one per entry, a trailing `/` dropped; an entry that is not http:// or https:// is ignored
  8. static appBases = () => {
  9. out = []
  10. for (part of appHostsRaw.replaceAll(',', ' ').split(' ')) {
  11. p = part.trim()
  12. if (p != '' && (p.startsWith('http://') || p.startsWith('https://'))) {
  13. out.push(p.endsWith('/') ? p.slice(0, p.length - 1) : p)
  14. }
  15. }
  16. return out
  17. }
  18. static bases = appBases()
  19. // a URL is one line of visible ASCII (no space, no control character, no backslash), at most 500 characters
  20. static plainUrl = (u) => {
  21. if (u == null || hlTypeName(u) != 'String' || u.length == 0 || u.length > 500) { return false }
  22. let i = 0
  23. while (i < u.length) {
  24. c = u.charCodeAt(i)
  25. if (c < 33 || c > 126 || c == 92) { return false }
  26. i = i + 1
  27. }
  28. return true
  29. }
  30. // a host name: letters, digits, `-` and `.` only (so no port, no user name, no IP-with-port trick)
  31. static hostChars = (h) => {
  32. if (h == '' || h.startsWith('.') || h.endsWith('.') || h.includes('..')) { return false }
  33. let i = 0
  34. while (i < h.length) {
  35. c = h.charCodeAt(i)
  36. if (!((c >= 48 && c <= 57) || (c >= 97 && c <= 122) || c == 45 || c == 46)) { return false }
  37. i = i + 1
  38. }
  39. return true
  40. }
  41. // 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)
  42. static httpsHost = (u) => {
  43. rest = u.slice(8)
  44. let end = rest.length
  45. for (stop of ['/', '?', '#']) {
  46. k = rest.indexOf(stop)
  47. if (k >= 0 && k < end) { end = k }
  48. }
  49. h = rest.slice(0, end).toLowerCase()
  50. return hostChars(h) ? h : null
  51. }
  52. // 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
  53. // 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.
  54. static urlAllowed = (u) => {
  55. if (!plainUrl(u)) { return false }
  56. if (u.startsWith('https://')) {
  57. h = httpsHost(u)
  58. if (h != null && (h == 'worldapi.org' || h.endsWith('.worldapi.org'))) { return true }
  59. }
  60. for (b of bases) {
  61. if (u == b || u.startsWith(b + '/')) { return true }
  62. }
  63. return false
  64. }
  65. // the host (with port) of a receiver URL, for the list on the settings page
  66. static hostOfUrl = (u) => {
  67. if (u == null || hlTypeName(u) != 'String') { return '' }
  68. i = u.indexOf('://')
  69. rest = i < 0 ? u : u.slice(i + 3)
  70. let end = rest.length
  71. for (stop of ['/', '?', '#']) {
  72. k = rest.indexOf(stop)
  73. if (k >= 0 && k < end) { end = k }
  74. }
  75. return rest.slice(0, end)
  76. }
  77. // `return` with `code` and `state` added (the return URL may carry a query already)
  78. static withQuery = (u, pairs) => {
  79. let out = u
  80. let hash = ''
  81. h = u.indexOf('#')
  82. if (h >= 0) { out = u.slice(0, h) hash = u.slice(h) }
  83. let sep = out.includes('?') ? '&' : '?'
  84. for (k of pairs.keys()) {
  85. out = out + sep + k + '=' + encodeURIComponent(pairs[k])
  86. sep = '&'
  87. }
  88. return out + hash
  89. }
  90. // a key (`gak_` + 40 hex) is shaped like one, or nothing is looked up
  91. static keyShaped = (k) => {
  92. return k != null && hlTypeName(k) == 'String' && k.length == 44 && k.startsWith('gak_') && isHex(k.slice(4), 40)
  93. }
  94. // the API base of a repo (docs/apps-api.md §0): <slug address>/api, inside an organization <org address>/api/<name>
  95. static apiOf = (slug) => {
  96. i = slug.indexOf('/')
  97. return i < 0 ? repoOrigin(slug) + '/api' : orgOrigin(slug.slice(0, i)) + '/api/' + slug.slice(i + 1)
  98. }
  99. // the form-encoded body of the confirm page's POST: { name = value } (hex values only travel in it, nothing is decoded)
  100. static formFields = (body) => {
  101. out = {}
  102. if (body == null || hlTypeName(body) != 'String') { return out }
  103. for (pair of body.split('&')) {
  104. k = pair.indexOf('=')
  105. if (k > 0) { out[pair.slice(0, k)] = pair.slice(k + 1) }
  106. }
  107. return out
  108. }

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