gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commite070e16fe070e16fMerge branch 'records-dp75' into mainTrackere070e16f/lib/security.hl

2.1 KB

  1. // lib/security.hl — THE SMALL DEFENCES the routes and faces share (mission SK, the security audit): a request budget (rate limit),
  2. // the caller's address as the proxy gives it, the headers every non-page answer carries.
  3. // hl:web answers the PAGES with no header hook (CSP / nosniff / frame-ancestors / referrer-policy there are hl:web's to add,
  4. // reports/SK-sec.md); the function routes and the files this app answers itself carry the headers below (imageHeaders; the text answers carry nosniff).
  5. import { now } from 'hl:time'
  6. import { envOr } from './util.hl'
  7. // imports of titles from TMDB (the search page's "add") per user and hour: TRACKER_IMPORT_PER_HOUR
  8. static importPerHour = toNumber(envOr('TRACKER_IMPORT_PER_HOUR', '60'))
  9. // request budgets: `allowHit('login', ip, 20, 60000)` = at most 20 hits per key per window; true = go ahead. In memory, one
  10. // process per storage. The map is dropped when it holds too many keys (an address flood must not grow it forever).
  11. static budget = { counts = {} keys = 0 }
  12. static allowHit = (&cls, &key, &max, &windowMs) => {
  13. t = now()
  14. slot = cls + '|' + ((t - t % windowMs) / windowMs) + '|' + key
  15. if (budget.keys > 20000) {
  16. budget.counts = {}
  17. budget.keys = 0
  18. }
  19. let c = budget.counts[slot]
  20. if (c == null) {
  21. c = 0
  22. budget.keys = budget.keys + 1
  23. }
  24. if (c >= max) { return false }
  25. budget.counts[slot] = c + 1
  26. return true
  27. }
  28. // the caller's address behind nginx (X-Client-IP, hybriel#25); 'direct' when there is none. Cut to 64 characters.
  29. static clientKey = (&req) => {
  30. h = req != null ? req.headers : null
  31. if (h == null) { return 'direct' }
  32. let v = h['x-client-ip']
  33. if (v == null) { v = h['X-Client-IP'] }
  34. if (v == null || hlTypeName(v) != 'String' || v == '') { return 'direct' }
  35. return v.slice(0, 64)
  36. }
  37. // an image file's headers: its type, its caching, nosniff, and a policy that lets a browser show it and nothing else — a stored .svg
  38. // opened as a page can run no script, load nothing and is sandboxed
  39. static imageHeaders = (&contentType, &cache) => {
  40. return { 'Content-Type' = contentType 'Cache-Control' = cache 'X-Content-Type-Options' = 'nosniff' 'Content-Security-Policy' = "default-src 'none'; style-src 'unsafe-inline'; img-src data:; sandbox" }
  41. }

Branches

Latest commits

  • e070e16fMerge branch 'records-dp75' into mainTracker
  • 642b4fa8|||PR records: DP75 day62 live, AP62 briefTracker
  • bfc4ea4dMerge branch 'ap62' into mainTracker
  • 2c95ac46|||PR AP62: re-vendor Hybriel set day62 (source 70341ad2, bin a54f5f08)Tracker
  • b9c7c3c8Merge branch 'records-dp74' into mainTracker
  • 85cfb4ae|||PR records: AP61 day61, DP74 liveTracker
  • b1496147Merge branch 'ap61' into mainTracker
  • 2047955bMerge branch 'u12-gateports' into mainTracker
  • c8da40f3|||PR AP61: re-vendor Hybriel set day61 (source a05af47c, bin a11042e5)Tracker
  • 4e018678|||PR deploy.sh: gates use Tracker's ports 8716-8719 and LC_ALL=C; README: 2 backups keptTracker
  • 0b86324aMerge branch 'records-dp73' into mainTracker
  • 9ce5f59b|||PR records: DP73 live 613ae0e; deploy.sh keeps 2 backupsTracker
  • 613ae0ecMerge branch 'bs3' into mainTracker
  • 1383264b|||PR BS3: keep shared synchronous sorting fastTracker
  • 63e5ba1a|||PR BS3: guard boot imports and share resumable sort buffersTracker
  • 310613f1Merge branch 'records-dp72' into mainTracker
  • 6f7b61ba|||PR records: deploy-swap fix live, Kunibert findingsTracker
  • c41587d2Merge branch 'bs2' into mainTracker
  • 0dd538a6|||PR BS2: finish the boot index in slices so a booting instance keeps answeringTracker
  • 019d5cedMerge branch 'records-1110' into mainTracker