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
- 3356fcfclog: #52 blocked, HM53 startedmre
- 833dcfc9Merge hm52-search: report only, tracker#52 blocked by framework gapmre
- 73010cc0HM52: report replace-navigation search draft regressionmre
- 1742e92bstatus: DP64 and HM52 runningmre
- 29ad5b58log: #64 merged, DP64 and HM52 startedmre
- fafddbf9Merge hm64-catalog: tracker#64 catalog from packed entriesmre
- b2e2206dlog: #63 livemre
- 5831e0e0log: HM64 donemre
- 4042b923Flatten catalog membership rows into paired slotsmre
- ea8a759elog: #61 #62 live, DP63 and HM64 startedmre
- 40439456Merge hm63-searchtext: tracker#63 packed search textmre
- b7e6b48ePack normalized search text behind numeric offsetsmre
- b35f53e1log: DP62 stale runtime decisionmre
- f9593602log: Apollo deploy rule correctionmre
- 3c95f7f3log: DP62 and HM63 startedmre
- 9c4397c9mission HM63mre
- 8dfe42c8mission DP62mre
- e347fef4Merge rv44: tracker#61 #62 on Hybriel day44mre
- 8274bcf1log: RV44 done, deploy held for the usage windowmre
- 1ef7a3b1RV44: report day44 gates and migration/restart proofsmre