Security auditing #1
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
Cancelling deleted an announcement whether it was waiting or already being posted ("sending"), so the tasks service could post it anyway and its record and result were lost. The delete now matches only status "active" in one find-and-delete; one that is being sent or was sent stays, and the person is sent back with "Too late to cancel". The page shows "Posting now" instead of the Cancel button for those.jarvis-core owns the shared collections, so the website cannot add unique indexes there. It now has its own small web_locks collection, whose documents are keyed by _id (always unique, so no index is needed beyond a TTL on expires_at): - claim("appeal:{guild}:{user}"), claim("tag:{guild}:{name}") and claim("rolegroup:{guild}:{name}") are held while checking that the thing does not exist yet and creating it, so a burst of identical submissions makes exactly one. A second request waits up to five seconds for the claim and then gets a 409; a claim whose holder died lapses after a minute and is taken over. This replaces the appeal de-duplication after insert. - The ten-requests-a-minute action limit was count-then-insert, so a burst got 13 to 17 through. It is now one atomic find-and-update counter per person ("rate:{user}"), whose window starts with the first request; menu refreshes still do not count.A few characters were enough to hurt the site: (a{3001}){1000} adds about 700 MB that stays and holds the event loop for most of a second, and (?:(?:(?:a{100}){100}){100}){100} takes about 24 seconds to compile. Saving compiled every pattern on the event loop before the cap was even checked, and the tester's time budget did not cover compiling. webui/patterns.py reads each pattern with a small tokenizer (escapes and classes skipped as units, groups and quantifiers followed) and refuses it, without compiling, when it is longer than MAX_PATTERN_LENGTH (200), has a counted repeat over MAX_REPEAT (100), nests quantified groups more than MAX_QUANTIFIED_NESTING (2) deep, multiplies nested counted repeats past MAX_REPEAT_PRODUCT (1000), or uses fuzzy matching or (?V1) set operations. These are the bot's rules and limits from jarvis/utils/filters.py. Saving now checks every pattern first and names the one refused and why, then, holding a per-server claim, checks the 100-pattern cap, and only then compiles, in a worker thread under the same semaphore as the tester. The tester refuses such patterns as "not allowed" and matches with concurrent=True so the GIL is released. When a server is already at or over the cap, the error says how many patterns the other filters hold and how far to cut them.(?x) made the regex module skip whitespace inside braces, so (?x)(a{3 001}){1 000} (about 700 MB) and (?x)(?:(?:(?:a {100}) {100}) {100}) {100} (about 24 s to compile) read as harmless to the validator, and (?#...) comments could split a repeat from its group the same way. The static rules, identical to the bot's jarvis/utils/filters.py at bed79de: - inline flags may only be i, m and s, globally ((?i), (?im)) or scoped ((?i:...)); x, V0/V1, a, L, u, a minus and anything else are refused, as are (?1) and (?R); - (?#...) comments are refused; - a brace body of ASCII digits, commas and whitespace is a counted repeat, read with the whitespace removed, and every number in it must be at most 100; - unchanged: at most 200 characters, quantified groups nested at most 2 deep, nested counted repeats multiplying to at most 1000, no fuzzy matching. Behind them, a pattern is compiled once in a separate, freshly spawned interpreter (python -I, never a fork of this threaded process) that limits itself to 256 MiB of address space (RLIMIT_AS) and 2 s of CPU (RLIMIT_CPU), and is killed after 3 s of wall-clock time. Running out of any of them, or dying, refuses the pattern with the reason; the verdict is kept in a 4096-entry LRU per pattern string. 256 MiB, not 512, so the probe alone also refuses patterns such as ((a{100})(?#c){100})(?#c){100}, which need about 290 MB. Saving probes every pattern before compiling it here; the tester probes within a 5 s budget, compiles with cache_pattern=False, and calls regex.purge() afterwards, as saving does, so nothing tried stays in memory.