gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit9ced53c39ced53c3|||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)mre9ced53c3/lib/api-helpers.hl

7.4 KB

  1. // lib/api-helpers.hl — the plumbing of the function routes (lib/api.hl): JSON answers, redirects, the component error
  2. // page, the STRICT JSON body, the app's key + secret check of a call, the selector's CORS answers. Statics only.
  3. //
  4. // STRICT: a body must be a JSON object; an unknown field, a missing field and a field of
  5. // the wrong type are each a 400 whose `error` names the field. hl:json's JSON.parse
  6. // aborts the request (500) on invalid input and the language has no catch (hybriel #12),
  7. // so the body's syntax is checked first by jsoncheck.hl (copied from tickets).
  8. import { Response } from 'hl:http1'
  9. import ErrorPage from '../components/error.hl'
  10. import { jsonErrorAt } from './jsoncheck.hl'
  11. import { appByKey, appOfSecret } from './apps.hl'
  12. import { originAllowed } from './apps-helpers.hl'
  13. import { publicUrl } from './invites.hl'
  14. // SECURITY HEADERS of every answer these routes make (mission SI): no sniffing, no referrer, no framing, and a JSON / redirect answer may
  15. // load nothing at all. (A component page's headers are hl:web's, not ours: reports/SI-sec.md.)
  16. static noSniff = 'nosniff'
  17. static dataPolicy = "default-src 'none'; frame-ancestors 'none'"
  18. static pagePolicy = "default-src 'none'; style-src 'self' 'unsafe-inline'; img-src 'self'; manifest-src 'self'; form-action 'none'; base-uri 'none'; frame-ancestors 'none'"
  19. static jsonHeaders = { 'Content-Type' = 'application/json; charset=utf-8' 'Cache-Control' = 'no-store' 'X-Content-Type-Options' = noSniff 'Referrer-Policy' = 'no-referrer' 'X-Frame-Options' = 'DENY' 'Content-Security-Policy' = dataPolicy }
  20. static htmlHeaders = { 'Content-Type' = 'text/html; charset=utf-8' 'Cache-Control' = 'no-store' 'X-Content-Type-Options' = noSniff 'Referrer-Policy' = 'no-referrer' 'X-Frame-Options' = 'DENY' 'Content-Security-Policy' = pagePolicy }
  21. // the headers a framework-rendered page (srv.page: the code page, the 404 page) gets from us
  22. static hardenPage = (res) => {
  23. res.headers['X-Content-Type-Options'] = noSniff
  24. res.headers['Referrer-Policy'] = 'same-origin'
  25. res.headers['X-Frame-Options'] = 'DENY'
  26. res.headers['Content-Security-Policy'] = "frame-ancestors 'none'"
  27. return res
  28. }
  29. // a page that MAY be framed — by the origins given (an app's registered origins) and nobody else (no X-Frame-Options: it cannot name
  30. // more than one origin, and browsers that read frame-ancestors ignore it anyway)
  31. static framedBy = (res, origins) => {
  32. res.headers['X-Content-Type-Options'] = noSniff
  33. res.headers['Referrer-Policy'] = 'same-origin'
  34. res.headers['Cache-Control'] = 'no-store'
  35. res.headers['Content-Security-Policy'] = 'frame-ancestors ' + origins.join(' ')
  36. return res
  37. }
  38. static reply = (status, value) => { return new Response(JSON.stringify(value), { status = status headers = jsonHeaders }) }
  39. static fail = (status, message) => { return reply(status, { error = message }) }
  40. static redirect = (url) => { return new Response('', { status = 302 headers = { 'Location' = url 'Cache-Control' = 'no-store' 'X-Content-Type-Options' = noSniff 'Referrer-Policy' = 'no-referrer' 'X-Frame-Options' = 'DENY' } }) }
  41. // THE STRICT BODY: `spec` maps each allowed field to { type ('String' | 'Number' |
  42. // 'Boolean' | 'List'), required }. Answers { error } or { body }.
  43. static strictBody = (req, spec) => {
  44. raw = req.body
  45. if (raw == null || raw.trim() == '') { return { error = 'a JSON object body is required' } }
  46. at = jsonErrorAt(raw)
  47. if (at >= 0) { return { error = 'the body is not valid JSON (at character ' + at + ')' } }
  48. t = raw.trim()
  49. if (t[0] != '{') { return { error = 'the body must be a JSON object' } }
  50. b = JSON.parse(raw)
  51. for (k of b.keys()) {
  52. if (spec[k] == null) { return { error = 'unknown field: ' + k } }
  53. }
  54. for (k of spec.keys()) {
  55. let f = spec[k]
  56. let v = b[k]
  57. if (v == null) {
  58. if (f.required) { return { error = 'missing field: ' + k } }
  59. } else if (f.type == 'List') {
  60. if (hlTypeName(v) != 'Hybrid' || v.length == null) { return { error = 'field ' + k + ' must be a list' } }
  61. } else if (hlTypeName(v) != f.type) {
  62. return { error = 'field ' + k + ' must be a ' + f.type.toLowerCase() }
  63. }
  64. }
  65. return { body = b }
  66. }
  67. // ---- the HTML error page of the login button (never a redirect) ------------------------
  68. static errorPage = (route, req, &srv, status, title, message) => {
  69. m = { route = { pattern = route.route.pattern component = ErrorPage } kind = 'component' params = { title = title message = message } path = req.path }
  70. res = srv.page(m, req.session, srv.hostOf(req.headers['host']), srv.requestHeaders(req.headers))
  71. res.status = status
  72. for (k of htmlHeaders.keys()) { res.headers[k] = htmlHeaders[k] }
  73. return res
  74. }
  75. // the answer of a call that asked for profile fields (ident#11): the identity's short id, then one key per asked field
  76. static withProfile = (identity, profile) => {
  77. out = { identity = identity }
  78. for (k of profile.keys()) { out[k] = profile[k] }
  79. return out
  80. }
  81. // ---- THE APP'S CALLS (key + secret, strict JSON body) ------------------------------------------
  82. static field = (n, t) => { return { type = t required = n } }
  83. // POST only, the strict body `spec`, then the app of its key + secret: answers { res } (the refusal) or { app, body }
  84. static inviteCaller = (req, spec) => {
  85. if (req.method != 'POST') { return { res = fail(405, 'POST only') } }
  86. b = strictBody(req, spec)
  87. if (b.error != null) { return { res = fail(400, b.error) } }
  88. a = appOfSecret(b.body.key, b.body.secret)
  89. if (a == null) { return { res = fail(401, 'unknown API key or wrong secret') } }
  90. return { app = a body = b.body }
  91. }
  92. // where an invite link points: IDENT_PUBLIC_URL, else the request's own host
  93. static baseOf = (req) => {
  94. if (publicUrl != null) { return publicUrl }
  95. proto = req.headers['x-forwarded-proto'] != null ? req.headers['x-forwarded-proto'] : 'http'
  96. return proto + '://' + req.headers['host']
  97. }
  98. // a login request id is 32 hex (lib/apps.hl openRequest)
  99. static hexId = (v) => {
  100. if (v.length != 32) { return false }
  101. let i = 0
  102. while (i < v.length) {
  103. if (!'0123456789abcdef'.includes(v[i])) { return false }
  104. i = i + 1
  105. }
  106. return true
  107. }
  108. // ---- THE SELECTOR's CORS answers (lib/selector.hl) ----------------------------------------------
  109. static jsonType = 'application/json; charset=utf-8'
  110. // the refusal: no CORS headers → the browser gives the page nothing
  111. static refuse = (status, message) => {
  112. return new Response(JSON.stringify({ error = message }), { status = status headers = { 'Content-Type' = jsonType 'Cache-Control' = 'no-store' 'Vary' = 'Origin' 'X-Content-Type-Options' = noSniff 'Referrer-Policy' = 'no-referrer' } })
  113. }
  114. static corsHeaders = (origin) => {
  115. return { 'Content-Type' = jsonType 'Cache-Control' = 'no-store' 'Vary' = 'Origin' 'X-Content-Type-Options' = noSniff 'Referrer-Policy' = 'no-referrer' 'Access-Control-Allow-Origin' = origin 'Access-Control-Allow-Credentials' = 'true' }
  116. }
  117. static answer = (origin, status, value) => {
  118. return new Response(JSON.stringify(value), { status = status headers = corsHeaders(origin) })
  119. }
  120. // WHO ASKS: answers { app (record), origin } or { status, error }
  121. static caller = (req) => {
  122. let origin = req.headers['origin']
  123. if (origin == null || origin == '') { return { status = 403 error = 'the selector answers browsers only (no Origin header)' } }
  124. q = req.query != null ? req.query : {}
  125. a = appByKey(q.key)
  126. if (a == null) { return { status = 403 error = 'no app has this API key' } }
  127. if (!originAllowed(a, origin)) { return { status = 403 error = 'this origin is not registered for the app' } }
  128. return { app = a origin = origin }
  129. }

Branches

Latest commits

  • 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
  • f2d9f79fident#33: the apps list and the secret box move into apps.hl's Stylemre
  • 8370b748ident#33: the app request box moves into home.hl's Stylemre
  • 40bf3b97ident#33: the 404 page's home link rule moves into notfound.hlmre
  • 40627a89ident#33: the time zone section's spacing moves into timezone.hlmre
  • d389db08ident#33: the identity page's rules and the Notif. chip move into identity.hl's Stylemre
  • ddda0b5bident#33: the identity row's parts move into identitylist.hl's Stylemre
  • 47e516afident#33 ident#34: the identity form's avatar rules move into identityform.hl; the preview and Remove button are hidden with the hidden attribute instead of built class strings (avatarPreviewClass, avatarRemoveClass gone)mre
  • 5cb5cf0eident#33: the code field's look moves into codestep.hl's Stylemre
  • f421cd60ident#33: the shell's own rules (user menu address, offline note, network probe) move into main.hl's Stylemre
  • c512fb96Merge branch 'f4-homesplit' into mainIdent
  • 07d1717c|||PR ident#40 home.hl is split into components, one per area: email step, identity form, identity list, app identity choice, time zone; each moves its faces with it, the email step is shared with embed.hl (#40.3, rest of #40.2)mre
  • 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