Details
Driver run (Python 3.9, sqlparse 0.5.5, threaded=False so one worker per request):
=== Baseline (benign payloads) ===
benign small SQL 8B wire= 8.8ms server= 0.2ms
benign 1 KB SQL 220B wire= 4.1ms server= 2.5ms
benign flat 500-cols 2902B wire= 91.7ms server= 90.2ms
=== Malicious payloads (within default caps) ===
nested-paren n=200 408B wire= 84.0ms server= 82.6ms ok=False
nested-paren n=500 1008B wire= 1371.9ms server= 1370.5ms ok=False
nested-paren n=1000 2008B wire=10335.3ms server=10333.7ms ok=False
nested-paren n=2000 4008B wire=10661.4ms server=10659.6ms ok=False
CASE-nested n=400 13905B wire= 5136.4ms server= 5134.7ms ok=False
IN-tuple-format n=1000 9922B wire= 3852.8ms server= 3851.2ms ok=True
A 2 KB payload (nested-paren n=1000) pins one worker for 10 seconds at 100% CPU. With gunicorn -w N deploying the same app, N concurrent malicious requests exhaust every worker and bring the service down. The cap SQLParseError exception is delivered to the caller, but only *after* the CPU work is already burnt.
Impact
- Single-threaded service: 1-2 KB payload locks the worker for 1-10 seconds (CWE-1333 / CWE-405 / CWE-400 — uncontrolled resource consumption).
- Multi-worker service: attacker sends
Nparallel requests, exhausts the worker pool. - Wire-to-CPU amplification on the worst vector: ~5000x (2 KB request → 10 seconds CPU).
- Downstream library impact:
sql-metadata.Parser(sql).columnscallssqlparse.parseinternally and inherits the exact same hang (nested-paren n=1000→ 11.3 s).
Suggested fix
Replace the eager str(self) materialization with a single-pass concatenation of children's already-cached value fields. The Token.value invariant value == str(self) at construction is preserved (children's value is itself built the same way bottom-up), but the per-node cost drops from O(subtree) to O(len(self.tokens)):
def __init__(self, tokens=None):
self.tokens = tokens or []
[setattr(token, 'parent', self) for token in self.tokens]
# Avoid materializing the full subtree via str(self): concatenating
# children's already-cached `value` is O(len(tokens)) per group,
# whereas str(self) recursively flattens the entire subtree which is
# O(subtree) per node and turns nested grouping into O(n * depth).
super().__init__(None, ''.join(token.value for token in self.tokens))
self.is_group = True
Measured against the 0.5.5 source tree with the patch applied locally and the full existing test-suite running (479 passed, 2 xfailed, 1 xpassed; the same baseline as unpatched 0d24023):
| Vector | Before fix | After fix | Speedup |
|---|---|---|---|
| nested-paren n=500 | 1336 ms | 11 ms | 121x |
| nested-paren n=1000 | 11206 ms | 22 ms | 509x |
| nested-paren n=2000 | TIMEOUT (>10 s) | 45 ms | 220x+ |
| CASE-nested n=200 | 559 ms | 25 ms | 22x |
| CASE-nested n=500 | TIMEOUT (>10 s) | 61 ms | 160x+ |
| benign 1 KB SQL | 3 ms | 3 ms | unchanged |
End-to-end Flask victim_app re-run against the patched library:
nested-paren n=1000 2008B server= 34.6ms
nested-paren n=2000 4008B server= 67.2ms
CASE-nested n=400 13905B server= 49.5ms
benign 1 KB SQL 220B server= 3.4ms
The IN-tuple format() vector observed at n=1000 (3.8 s for ~10 KB input) is a separate quadratic in the reindent filter (filters/reindent.py:_get_offset → _flatten_up_to_token) and is not covered by this advisory; please consider it as a follow-up if the maintainer would like a separate report.
Fix PR
A fix PR against the temp private fork, mirroring the diff above with a regression test (test_nested_paren_within_cap_under_50ms), is attached and linked from this advisory.
Credit
Reported by tonghuaroot.