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
- 45163bd4log: person/tmdb error hypothesismre
- cb38de2blog: swap 503 causemre
- fd10c3d5log: CB livemre
- 7557c025log: CB deploy try 2mre
- 06a7534aMerge branch 'cb'mre
- 2f8030d6log: CB gates greenmre
- 498bf644log: CB gates startmre
- f59c2847gc1: bound the five request caches (shows.hl cacheLimit)mre
- 6ea2c6d6log: morning re-measure 11-04mre
- 446290f0log: GC1 livemre
- 8e6aa91dlog: GC1 deploy retrymre
- 87ad2dc1log: GC1 deployingmre
- 53f10fb8Merge branch 'gc1-deploy'mre
- 7fcbf1d2log: GC1 deploy GOmre
- 1fb6664bcompose: TRACKER_QSTATS=1 (GC1 measurement)mre
- 6576c1cdgc1: release the people-view scan lists once the view is builtmre
- 94f8a373gc1: back out the kept episode-id set (no measurable gain on /my/unwatched: 4.69 -> 4.61 GB per 150 loads)mre
- a1a88bd4gc1: search skips the stale-key string while no entry is stale; /my/unwatched asks a kept id set instead of building a key string per episode rowmre
- cd830744gc1: dbSearch: no split / map / list copy per candidate (scores from the stored text, top list touched only when the hit can enter)mre
- 451db7cdgc1: show page and check clicks read the kept watched set instead of building one from a by-value copy of every watchmre