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
- ef949994conductor: neutral ticket rulemre
- 89e91ef3log: RV39 livemre
- ee2b677alog: RV39 deployingmre
- c1c4178cMerge branch 'rv39'mre
- a016e9abReapply "Merge branch 'rv39'"mre
- f29e2502log: RV39 gate fixmre
- d9fce111browser gate #26: exact frame count incl. SL1 people step (hybriel#308)mre
- ad529ac1log: m572 verdictmre
- 1b37d404RV39 on hold: hybriel#308mre
- f9127c68Revert "Merge branch 'rv39'"mre
- cd39f3d3log: hybriel#308mre
- 51341d6elog: RV39 deploy startedmre
- c9e29e06Merge branch 'rv39'mre
- 0f8d1fdclog: RV39 report, browser 387/2mre
- 50249b0bRV39: reportmre
- 10ed9581compose: HL_GC_STATS=1mre
- 955da19bRV39: re-vendor Hybriel set day39 (source 51e921d7, bin 6b481aa7)mre
- 64b8d1dclog: RV39 startedmre
- 1f2df72amission RV39mre
- 6c1936f9log+status: GC planmre