gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Branchf15-navigate3b3e451f|||PR ident#36 navigate to the app instead of location.assignmref15-navigate/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

  • 3b3e451f|||PR ident#36 navigate to the app instead of location.assignmre
  • e36f4516Merge branch 'f14-online' into mainIdent
  • 2c5eac91|||PR ident#38 online state without probe timermre
  • bf1980f2Merge branch 'v56-day56' into mainIdent
  • 62ae0adf|||PR ident: vendor Hybriel day56mre
  • 0d18abc4Merge branch 'f13-avatar' into mainIdent
  • 2a99730a|||PR ident#37 avatar in hlmre
  • fa4ed25bMerge branch 'f11-upsert' into mainIdent
  • 8f3d9bc9|||PR ident#42 keyed upsert and removemre
  • 675493b1ident#42: editing an identity upserts the one edited row (face editIdentity answers { identity, row }); a create or delete keeps the whole list because the rows' position-dependent parts (default, Delete, Identity n) changemre
  • 45e55303ident#42: an app write patches one row: upsert on create, edit and new secret, one-argument remove on delete; the faces answer the changed app (or the deleted id), not the whole listmre
  • 991641d4Merge branch 'f10-access' into mainIdent
  • e56d8273|||PR ident#34 part 4: access and inbox fixed classesmre
  • df7696dbident#34: tests: access ticks selector quoting fixmre
  • 7215903eident#34: access: the unstyled classes all, app, tick, apptick, plus are dropped; the gate finds the app ticks by input[name=app]mre
  • 69b0a0ceident#34: access: the pick buttons use Style.pickCard instead of class pickmre
  • dc702419Merge branch 'f9-choose' into mainIdent
  • 8caec771|||PR ident#34 restore .choose hookmre
  • 0dbff960Merge branch 'f8-shared' into mainIdent
  • d69665f6|||PR ident#34 part 3: shared look classes (wide, avatar, chip/used, conn, choose, who, inboxlink) moved to statics or droppedmre