Security audit #2
Loading…
Reference in a new issue
No description provided.
Delete branch "audit"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Lots of small bug fixes, nothing severe
Compiling a regular expression is covered by no timeout: (a{3001}){1000} takes about 0.76 s with the GIL held and leaves ~700 MB in regex's cache. A small tokenizer (escapes and character classes read as units, groups, quantifiers and braces followed) now refuses, before any compile, a pattern: - longer than 200 characters; - with a counted repeat ({n}, {n,}, {,m}, {n,m}) over 100; - with quantified groups nested more than 2 deep; - whose counted repeats nested inside each other multiply to more than 1000 (each counts by its largest number, *, + and ? as 1); without this ((a{100}){100}){100} still passes the other limits and costs 0.3 s and 270 MB to compile; - using fuzzy matching ({e<=1} and the like) or (?V1) set operations. /filter refuses such patterns with the reason, and checks and compiles what was typed on the filter workers. At match time, stored patterns over the limits are skipped (one warning per server) and never compiled; compiling happens only on the filter workers, with regex's own pattern cache off so the bounded LRU is the only one.str.isdigit() also accepts characters such as "²" that int() cannot read, so pattern_problem("a{²}") raised ValueError instead of answering. Only ASCII digits now make a counted repeat, as in the website's copy of the rules; regex treats such braces as literal text too.(?x) makes regex skip whitespace inside braces, so (?x)(a{3 001}){1 000} (~700 MB) and (?x)(?:(?:(?:a {100}) {100}) {100}) {100} (~24 s) passed the static rules, as did repeats split from their group by (?#...) comments. The rules are now the website's (webui/patterns.py), identical on a 3000-pattern differential run: - inline flags only i, m and s, global or scoped; x, V0/V1, a, L, a minus sign and anything else are refused; - (?#...) comments are refused; - a brace body of ASCII digits, commas and whitespace is a counted repeat, read with the whitespace removed, in any mode. Whatever the rules miss is caught by a probe: before this process compiles a pattern for the first time, a fresh isolated interpreter (python -I, spawned, never forked from the threaded bot) compiles it under RLIMIT_AS 256 MiB and RLIMIT_CPU 2 s with a 3 s wall clock; running out, crashing or timing out refuses the pattern. 256 MiB rather than 512 so the probe alone refuses the ~290 MB patterns from the re-test. Verdicts are cached per pattern (LRU of 4096). /filter probes what was typed before compiling it; stored patterns are probed on the filter workers the first time they are needed, and those refused are skipped with one warning per server.Compile probes (one python -I process each) ran on the 4 matching workers, outside the per-server slot, with no timeout and no sharing between callers: one server saving 100 new patterns and sending a burst queued thousands of spawns, every server's find_match timed out, and filters failed open. - Probes have their own pool of 2 threads, so matching never waits on them. - Concurrent callers share one in-flight probe per pattern (shielded, so a caller giving up does not cancel it for the others). - A server runs one probe at a time, so it cannot take the whole pool. - A message waits for unprobed patterns at most PROBE_WAIT (1 s); those not done are skipped for that message, and the verdict is kept when it lands. Probe verdicts now match the website's copy (webui/patterns.py, 1b33d36): - a bounded LRU of 16384 (was lru_cache of 4096, which 100 patterns in over 40 servers overflowed), with clear_probe_cache(); - only settled verdicts are remembered: OK, invalid, memory limit, and the CPU limit (SIGXCPU; the child now sets RLIMIT_CPU soft below hard, as with soft == hard the kernel sent SIGKILL and the CPU verdict was never seen); - the wall-clock timeout, an OSError at spawn (EAGAIN, ENOMEM, no fds), and any other exit without a verdict refuse the pattern for now only ("could not be checked"); an OSError no longer escapes into on_message or /filter. The compiled-pattern cache no longer keeps a for-now refusal either. The message handler caps to the first MAX_PATTERNS non-blank patterns before the static check, which is now cached, instead of checking every stored pattern on the event loop. /filter refuses more than MAX_PATTERNS lines before probing any. The swapped attribute docstrings of _filter_refused_warned and _filter_probe_warned are fixed.- /filter defers its modal answer (privately) before checking: probing about 40 or more new patterns took longer than Discord's 3 s, so the reply failed and the save was lost. Every later reply is a follow-up. - After startup a background task probes the patterns of every server the bot is in that has filters (capped and through the static rules, as for messages), one server at a time through the same per-server probe path, so warming keeps at most one probe worker busy and shares probes with live messages. One info line per server: how its patterns settled, or how many are still pending after PROBE_WARM_WAIT (120 s). - "Could not start the compile probe" is logged at most once a minute, with the number of failures since the last warning. - probe_patterns tells refused-for-good (too costly to compile) from refused-for-now ("could not be checked yet"); the message handler reports the latter as such, at most once a minute per server, instead of calling it too costly.