tracker
All repositories: gitoria
2.1 KB
// lib/security.hl — THE SMALL DEFENCES the routes and faces share (mission SK, the security audit): a request budget (rate limit),// the caller's address as the proxy gives it, the headers every non-page answer carries.// hl:web answers the PAGES with no header hook (CSP / nosniff / frame-ancestors / referrer-policy there are hl:web's to add,// reports/SK-sec.md); the function routes and the files this app answers itself carry the headers below (imageHeaders; the text answers carry nosniff).import { now } from 'hl:time'import { envOr } from './util.hl'// imports of titles from TMDB (the search page's "add") per user and hour: TRACKER_IMPORT_PER_HOURstatic importPerHour = toNumber(envOr('TRACKER_IMPORT_PER_HOUR', '60'))// request budgets: `allowHit('login', ip, 20, 60000)` = at most 20 hits per key per window; true = go ahead. In memory, one// process per storage. The map is dropped when it holds too many keys (an address flood must not grow it forever).static budget = { counts = {} keys = 0 }static allowHit = (&cls, &key, &max, &windowMs) => {t = now()slot = cls + '|' + ((t - t % windowMs) / windowMs) + '|' + keyif (budget.keys > 20000) {budget.counts = {}budget.keys = 0}let c = budget.counts[slot]if (c == null) {c = 0budget.keys = budget.keys + 1}if (c >= max) { return false }budget.counts[slot] = c + 1return true}// the caller's address behind nginx (X-Client-IP, hybriel#25); 'direct' when there is none. Cut to 64 characters.static clientKey = (&req) => {h = req != null ? req.headers : nullif (h == null) { return 'direct' }let v = h['x-client-ip']if (v == null) { v = h['X-Client-IP'] }if (v == null || hlTypeName(v) != 'String' || v == '') { return 'direct' }return v.slice(0, 64)}// 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// opened as a page can run no script, load nothing and is sandboxedstatic imageHeaders = (&contentType, &cache) => {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" }}
Branches
Latest commits
- 07a5c883HM65 report measured field projection and complete validationmre
- 0eab858fRead only catalog date fields during the episode boot scanmre
- cb78a1a0HM65 report missing heap diagnostic package with repromre
- 89606a5cHM65 briefmre
- 096ce0d4log: DP69 live on day48mre
- b3d8a80bDP69 brief, RV48 mergedmre
- ce6d7d32Merge rv48: Hybriel day48mre
- 3da2ea86RV48: report day48 cold starts, gates and cleanupmre
- 8d9ecdfeRV48: re-vendor Hybriel set day48 (source b6a8f429, bin 660a9524)mre
- 4a802ae7status: RV48 runningmre
- 0a5412b6RV48 brief, day48 outmre
- 56665efelog: CB69 mergedmre
- 05448a98Merge cb69-imports: appTitle in a dependency-free topicmre
- e55b379dKeep shared app title independent of the route import graphmre
- 9a99659blog: hybriel#334, batch waits for day48mre
- 85fa6c9blog: CB68 result, CB69 startedmre
- 2f07c1b7log: DP68 boot gate failed, CB68mre
- 9652782dlog: HM59 merged, DP68 startedmre
- 29867a75Merge hm59-logic: tracker#59 page logic moved to libmre
- f7bfe90bMove remaining page builders into their topicsmre