GuardBench / items.jsonl
Soaperloafidksum's picture
GuardBench: 120 items, 20 patterns, 6 guard shapes
82c37ae verified
Raw
History Blame Contribute Delete
76.5 kB
{"id": "xxe_none", "pattern": "xxe", "shape": "none", "label": "unsafe", "vuln_class": "xml external entity", "why": "no guard is applied on this path", "code": "import defusedxml.ElementTree as SAFEET\nimport xml.etree.ElementTree as ET\n\ndef _parse_trusted(blob):\n \"\"\"Internal feeds only; these come from our own signed bundles.\"\"\"\n return ET.fromstring(blob)\n\ndef import_feed(request):\n return ET.fromstring(request.body)"}
{"id": "xxe_covers", "pattern": "xxe", "shape": "covers", "label": "safe", "vuln_class": null, "why": "the guard is applied to the tainted value and addresses the weakness", "code": "import defusedxml.ElementTree as SAFEET\nimport xml.etree.ElementTree as ET\n\ndef _parse_trusted(blob):\n \"\"\"Internal feeds only; these come from our own signed bundles.\"\"\"\n return ET.fromstring(blob)\n\ndef import_feed(request):\n return SAFEET.fromstring(request.body)"}
{"id": "xxe_wrong_value", "pattern": "xxe", "shape": "wrong_value", "label": "unsafe", "vuln_class": "xml external entity", "why": "the guard is applied to a sibling value, not the one that reaches the sink", "code": "import defusedxml.ElementTree as SAFEET\nimport xml.etree.ElementTree as ET\n\ndef _parse_trusted(blob):\n \"\"\"Internal feeds only; these come from our own signed bundles.\"\"\"\n return ET.fromstring(blob)\n\ndef import_feed(request):\n SAFEET.fromstring(request.headers.get(\"X-Manifest\", \"<m/>\"))\n return ET.fromstring(request.body)"}
{"id": "xxe_irrelevant", "pattern": "xxe", "shape": "irrelevant", "label": "unsafe", "vuln_class": "xml external entity", "why": "a guard is applied to the right value but does not address this weakness", "code": "import defusedxml.ElementTree as SAFEET\nimport xml.etree.ElementTree as ET\n\ndef _parse_trusted(blob):\n \"\"\"Internal feeds only; these come from our own signed bundles.\"\"\"\n return ET.fromstring(blob)\n\ndef import_feed(request):\n if len(request.body) > 2_000_000:\n raise ValueError(\"feed too large\")\n return ET.fromstring(request.body)"}
{"id": "xxe_elsewhere", "pattern": "xxe", "shape": "elsewhere", "label": "unsafe", "vuln_class": "xml external entity", "why": "the guard exists and is used by a neighbouring function, not on this path", "code": "import defusedxml.ElementTree as SAFEET\nimport xml.etree.ElementTree as ET\n\ndef _parse_trusted(blob):\n \"\"\"Internal feeds only; these come from our own signed bundles.\"\"\"\n return ET.fromstring(blob)\n\ndef import_manifest(request):\n return SAFEET.fromstring(request.body)\n\ndef import_feed(request):\n return _parse_trusted(request.body)"}
{"id": "xxe_covers_alt", "pattern": "xxe", "shape": "covers_alt", "label": "safe", "vuln_class": null, "why": "a second correct implementation, defended a different way", "code": "import defusedxml.ElementTree as SAFEET\nimport xml.etree.ElementTree as ET\n\ndef _parse_trusted(blob):\n \"\"\"Internal feeds only; these come from our own signed bundles.\"\"\"\n return ET.fromstring(blob)\n\ndef import_feed(request):\n parser = ET.XMLParser()\n parser.parser.DefaultHandlerExpand = None\n parser.entity = {}\n return ET.fromstring(request.body, parser=parser)"}
{"id": "ssti_none", "pattern": "ssti", "shape": "none", "label": "unsafe", "vuln_class": "server-side template injection", "why": "no guard is applied on this path", "code": "from jinja2 import Environment, select_autoescape\nfrom jinja2.sandbox import SandboxedEnvironment\n\nSANDBOX = SandboxedEnvironment(autoescape=select_autoescape())\nLOOSE = Environment(autoescape=select_autoescape())\n\ndef render_signature(user):\n return LOOSE.from_string(user.signature_template).render(user=user)"}
{"id": "ssti_covers", "pattern": "ssti", "shape": "covers", "label": "safe", "vuln_class": null, "why": "the guard is applied to the tainted value and addresses the weakness", "code": "from jinja2 import Environment, select_autoescape\nfrom jinja2.sandbox import SandboxedEnvironment\n\nSANDBOX = SandboxedEnvironment(autoescape=select_autoescape())\nLOOSE = Environment(autoescape=select_autoescape())\n\ndef render_signature(user):\n return SANDBOX.from_string(user.signature_template).render(user=user)"}
{"id": "ssti_wrong_value", "pattern": "ssti", "shape": "wrong_value", "label": "unsafe", "vuln_class": "server-side template injection", "why": "the guard is applied to a sibling value, not the one that reaches the sink", "code": "from jinja2 import Environment, select_autoescape\nfrom jinja2.sandbox import SandboxedEnvironment\n\nSANDBOX = SandboxedEnvironment(autoescape=select_autoescape())\nLOOSE = Environment(autoescape=select_autoescape())\n\ndef render_signature(user):\n SANDBOX.from_string(user.display_name).render()\n return LOOSE.from_string(user.signature_template).render(user=user)"}
{"id": "ssti_irrelevant", "pattern": "ssti", "shape": "irrelevant", "label": "unsafe", "vuln_class": "server-side template injection", "why": "a guard is applied to the right value but does not address this weakness", "code": "from jinja2 import Environment, select_autoescape\nfrom jinja2.sandbox import SandboxedEnvironment\n\nSANDBOX = SandboxedEnvironment(autoescape=select_autoescape())\nLOOSE = Environment(autoescape=select_autoescape())\n\ndef render_signature(user):\n if \"<script\" in user.signature_template.lower():\n raise ValueError(\"no script tags\")\n return LOOSE.from_string(user.signature_template).render(user=user)"}
{"id": "ssti_elsewhere", "pattern": "ssti", "shape": "elsewhere", "label": "unsafe", "vuln_class": "server-side template injection", "why": "the guard exists and is used by a neighbouring function, not on this path", "code": "from jinja2 import Environment, select_autoescape\nfrom jinja2.sandbox import SandboxedEnvironment\n\nSANDBOX = SandboxedEnvironment(autoescape=select_autoescape())\nLOOSE = Environment(autoescape=select_autoescape())\n\ndef render_footer(org):\n return SANDBOX.from_string(org.footer_template).render(org=org)\n\ndef render_signature(user):\n return LOOSE.from_string(user.signature_template).render(user=user)"}
{"id": "ssti_covers_alt", "pattern": "ssti", "shape": "covers_alt", "label": "safe", "vuln_class": null, "why": "a second correct implementation, defended a different way", "code": "from jinja2 import Environment, select_autoescape\nfrom jinja2.sandbox import SandboxedEnvironment\n\nSANDBOX = SandboxedEnvironment(autoescape=select_autoescape())\nLOOSE = Environment(autoescape=select_autoescape())\n\nALLOWED_VARS = (\"name\", \"org\", \"title\")\n\ndef render_signature(user):\n tmpl = user.signature_template\n out = tmpl\n for v in ALLOWED_VARS:\n out = out.replace(\"{{%s}}\" % v, escape(getattr(user, v, \"\")))\n return out"}
{"id": "redos_none", "pattern": "redos", "shape": "none", "label": "unsafe", "vuln_class": "regular expression denial of service", "why": "no guard is applied on this path", "code": "import re, regex\n\nMAX_PATTERN_LEN = 200\n\ndef _compile_bounded(p):\n \"\"\"regex module supports a match timeout; re does not.\"\"\"\n return regex.compile(p)\n\ndef search_logs(lines, user_pattern):\n rx = re.compile(user_pattern)\n return [l for l in lines if rx.search(l)]"}
{"id": "redos_covers", "pattern": "redos", "shape": "covers", "label": "safe", "vuln_class": null, "why": "the guard is applied to the tainted value and addresses the weakness", "code": "import re, regex\n\nMAX_PATTERN_LEN = 200\n\ndef _compile_bounded(p):\n \"\"\"regex module supports a match timeout; re does not.\"\"\"\n return regex.compile(p)\n\ndef search_logs(lines, user_pattern):\n rx = _compile_bounded(user_pattern)\n return [l for l in lines if rx.search(l, timeout=0.25)]"}
{"id": "redos_wrong_value", "pattern": "redos", "shape": "wrong_value", "label": "unsafe", "vuln_class": "regular expression denial of service", "why": "the guard is applied to a sibling value, not the one that reaches the sink", "code": "import re, regex\n\nMAX_PATTERN_LEN = 200\n\ndef _compile_bounded(p):\n \"\"\"regex module supports a match timeout; re does not.\"\"\"\n return regex.compile(p)\n\ndef search_logs(lines, user_pattern, highlight):\n _compile_bounded(highlight)\n rx = re.compile(user_pattern)\n return [l for l in lines if rx.search(l)]"}
{"id": "redos_irrelevant", "pattern": "redos", "shape": "irrelevant", "label": "unsafe", "vuln_class": "regular expression denial of service", "why": "a guard is applied to the right value but does not address this weakness", "code": "import re, regex\n\nMAX_PATTERN_LEN = 200\n\ndef _compile_bounded(p):\n \"\"\"regex module supports a match timeout; re does not.\"\"\"\n return regex.compile(p)\n\ndef search_logs(lines, user_pattern):\n if len(user_pattern) > MAX_PATTERN_LEN:\n raise ValueError(\"pattern too long\")\n rx = re.compile(user_pattern)\n return [l for l in lines if rx.search(l)]"}
{"id": "redos_elsewhere", "pattern": "redos", "shape": "elsewhere", "label": "unsafe", "vuln_class": "regular expression denial of service", "why": "the guard exists and is used by a neighbouring function, not on this path", "code": "import re, regex\n\nMAX_PATTERN_LEN = 200\n\ndef _compile_bounded(p):\n \"\"\"regex module supports a match timeout; re does not.\"\"\"\n return regex.compile(p)\n\ndef search_alerts(rows, user_pattern):\n rx = _compile_bounded(user_pattern)\n return [r for r in rows if rx.search(r, timeout=0.25)]\n\ndef search_logs(lines, user_pattern):\n rx = re.compile(user_pattern)\n return [l for l in lines if rx.search(l)]"}
{"id": "redos_covers_alt", "pattern": "redos", "shape": "covers_alt", "label": "safe", "vuln_class": null, "why": "a second correct implementation, defended a different way", "code": "import re, regex\n\nMAX_PATTERN_LEN = 200\n\ndef _compile_bounded(p):\n \"\"\"regex module supports a match timeout; re does not.\"\"\"\n return regex.compile(p)\n\ndef search_logs(lines, user_pattern):\n rx = regex.compile(regex.escape(user_pattern))\n return [l for l in lines if rx.search(l)]"}
{"id": "host_header_none", "pattern": "host_header", "shape": "none", "label": "unsafe", "vuln_class": "host header injection", "why": "no guard is applied on this path", "code": "TRUSTED_HOSTS = {\"app.example.com\", \"www.example.com\"}\n\ndef _origin(request):\n host = request.headers.get(\"Host\", \"\")\n if host not in TRUSTED_HOSTS:\n raise ValueError(\"untrusted host\")\n return \"https://\" + host\n\ndef send_reset(user, request, token):\n link = \"https://\" + request.headers[\"Host\"] + \"/reset?t=\" + token\n mail(user.email, link)"}
{"id": "host_header_covers", "pattern": "host_header", "shape": "covers", "label": "safe", "vuln_class": null, "why": "the guard is applied to the tainted value and addresses the weakness", "code": "TRUSTED_HOSTS = {\"app.example.com\", \"www.example.com\"}\n\ndef _origin(request):\n host = request.headers.get(\"Host\", \"\")\n if host not in TRUSTED_HOSTS:\n raise ValueError(\"untrusted host\")\n return \"https://\" + host\n\ndef send_reset(user, request, token):\n mail(user.email, _origin(request) + \"/reset?t=\" + token)"}
{"id": "host_header_wrong_value", "pattern": "host_header", "shape": "wrong_value", "label": "unsafe", "vuln_class": "host header injection", "why": "the guard is applied to a sibling value, not the one that reaches the sink", "code": "TRUSTED_HOSTS = {\"app.example.com\", \"www.example.com\"}\n\ndef _origin(request):\n host = request.headers.get(\"Host\", \"\")\n if host not in TRUSTED_HOSTS:\n raise ValueError(\"untrusted host\")\n return \"https://\" + host\n\ndef send_reset(user, request, token):\n _origin(request)\n link = \"https://\" + request.headers.get(\"X-Forwarded-Host\", \"\") + \"/reset?t=\" + token\n mail(user.email, link)"}
{"id": "host_header_irrelevant", "pattern": "host_header", "shape": "irrelevant", "label": "unsafe", "vuln_class": "host header injection", "why": "a guard is applied to the right value but does not address this weakness", "code": "TRUSTED_HOSTS = {\"app.example.com\", \"www.example.com\"}\n\ndef _origin(request):\n host = request.headers.get(\"Host\", \"\")\n if host not in TRUSTED_HOSTS:\n raise ValueError(\"untrusted host\")\n return \"https://\" + host\n\ndef send_reset(user, request, token):\n host = request.headers.get(\"Host\", \"\")\n if len(host) > 253 or \"\\n\" in host:\n raise ValueError(\"malformed host\")\n mail(user.email, \"https://\" + host + \"/reset?t=\" + token)"}
{"id": "host_header_elsewhere", "pattern": "host_header", "shape": "elsewhere", "label": "unsafe", "vuln_class": "host header injection", "why": "the guard exists and is used by a neighbouring function, not on this path", "code": "TRUSTED_HOSTS = {\"app.example.com\", \"www.example.com\"}\n\ndef _origin(request):\n host = request.headers.get(\"Host\", \"\")\n if host not in TRUSTED_HOSTS:\n raise ValueError(\"untrusted host\")\n return \"https://\" + host\n\ndef send_invite(user, request, token):\n mail(user.email, _origin(request) + \"/invite?t=\" + token)\n\ndef send_reset(user, request, token):\n mail(user.email, \"https://\" + request.headers[\"Host\"] + \"/reset?t=\" + token)"}
{"id": "host_header_covers_alt", "pattern": "host_header", "shape": "covers_alt", "label": "safe", "vuln_class": null, "why": "a second correct implementation, defended a different way", "code": "TRUSTED_HOSTS = {\"app.example.com\", \"www.example.com\"}\n\ndef _origin(request):\n host = request.headers.get(\"Host\", \"\")\n if host not in TRUSTED_HOSTS:\n raise ValueError(\"untrusted host\")\n return \"https://\" + host\n\nCANONICAL_ORIGIN = \"https://app.example.com\"\n\ndef send_reset(user, request, token):\n mail(user.email, CANONICAL_ORIGIN + \"/reset?t=\" + token)"}
{"id": "weak_token_none", "pattern": "weak_token", "shape": "none", "label": "unsafe", "vuln_class": "insecure randomness", "why": "no guard is applied on this path", "code": "import random, secrets\n\nTOKEN_BYTES = 32\n\ndef _strong_token():\n return secrets.token_urlsafe(TOKEN_BYTES)\n\ndef new_reset_token(user):\n t = \"\".join(random.choice(\"0123456789abcdef\") for _ in range(32))\n store(user, t); return t"}
{"id": "weak_token_covers", "pattern": "weak_token", "shape": "covers", "label": "safe", "vuln_class": null, "why": "the guard is applied to the tainted value and addresses the weakness", "code": "import random, secrets\n\nTOKEN_BYTES = 32\n\ndef _strong_token():\n return secrets.token_urlsafe(TOKEN_BYTES)\n\ndef new_reset_token(user):\n t = _strong_token()\n store(user, t); return t"}
{"id": "weak_token_wrong_value", "pattern": "weak_token", "shape": "wrong_value", "label": "unsafe", "vuln_class": "insecure randomness", "why": "the guard is applied to a sibling value, not the one that reaches the sink", "code": "import random, secrets\n\nTOKEN_BYTES = 32\n\ndef _strong_token():\n return secrets.token_urlsafe(TOKEN_BYTES)\n\ndef new_reset_token(user):\n _strong_token()\n t = \"%d%d\" % (int(time.time()), random.randint(0, 10**9))\n store(user, t); return t"}
{"id": "weak_token_irrelevant", "pattern": "weak_token", "shape": "irrelevant", "label": "unsafe", "vuln_class": "insecure randomness", "why": "a guard is applied to the right value but does not address this weakness", "code": "import random, secrets\n\nTOKEN_BYTES = 32\n\ndef _strong_token():\n return secrets.token_urlsafe(TOKEN_BYTES)\n\ndef new_reset_token(user):\n t = \"\".join(random.choice(\"0123456789abcdef\") for _ in range(64))\n if len(t) < 32:\n raise ValueError(\"token too short\")\n store(user, t); return t"}
{"id": "weak_token_elsewhere", "pattern": "weak_token", "shape": "elsewhere", "label": "unsafe", "vuln_class": "insecure randomness", "why": "the guard exists and is used by a neighbouring function, not on this path", "code": "import random, secrets\n\nTOKEN_BYTES = 32\n\ndef _strong_token():\n return secrets.token_urlsafe(TOKEN_BYTES)\n\ndef new_session_token(user):\n t = _strong_token()\n store(user, t); return t\n\ndef new_reset_token(user):\n t = \"\".join(random.choice(\"0123456789abcdef\") for _ in range(32))\n store(user, t); return t"}
{"id": "weak_token_covers_alt", "pattern": "weak_token", "shape": "covers_alt", "label": "safe", "vuln_class": null, "why": "a second correct implementation, defended a different way", "code": "import random, secrets\n\nTOKEN_BYTES = 32\n\ndef _strong_token():\n return secrets.token_urlsafe(TOKEN_BYTES)\n\ndef new_reset_token(user):\n t = secrets.token_hex(TOKEN_BYTES)\n store(user, t); return t"}
{"id": "csv_injection_none", "pattern": "csv_injection", "shape": "none", "label": "unsafe", "vuln_class": "formula injection", "why": "no guard is applied on this path", "code": "DANGEROUS_PREFIX = (\"=\", \"+\", \"-\", \"@\", \"\\t\", \"\\r\")\n\ndef _neutralise(v):\n s = \"\" if v is None else str(v)\n return \"'\" + s if s.startswith(DANGEROUS_PREFIX) else s\n\ndef export_rows(rows, out):\n w = csv.writer(out)\n for r in rows:\n w.writerow([r.name, r.note])"}
{"id": "csv_injection_covers", "pattern": "csv_injection", "shape": "covers", "label": "safe", "vuln_class": null, "why": "the guard is applied to the tainted value and addresses the weakness", "code": "DANGEROUS_PREFIX = (\"=\", \"+\", \"-\", \"@\", \"\\t\", \"\\r\")\n\ndef _neutralise(v):\n s = \"\" if v is None else str(v)\n return \"'\" + s if s.startswith(DANGEROUS_PREFIX) else s\n\ndef export_rows(rows, out):\n w = csv.writer(out)\n for r in rows:\n w.writerow([_neutralise(r.name), _neutralise(r.note)])"}
{"id": "csv_injection_wrong_value", "pattern": "csv_injection", "shape": "wrong_value", "label": "unsafe", "vuln_class": "formula injection", "why": "the guard is applied to a sibling value, not the one that reaches the sink", "code": "DANGEROUS_PREFIX = (\"=\", \"+\", \"-\", \"@\", \"\\t\", \"\\r\")\n\ndef _neutralise(v):\n s = \"\" if v is None else str(v)\n return \"'\" + s if s.startswith(DANGEROUS_PREFIX) else s\n\ndef export_rows(rows, out):\n w = csv.writer(out)\n for r in rows:\n w.writerow([_neutralise(r.name), r.note])"}
{"id": "csv_injection_irrelevant", "pattern": "csv_injection", "shape": "irrelevant", "label": "unsafe", "vuln_class": "formula injection", "why": "a guard is applied to the right value but does not address this weakness", "code": "DANGEROUS_PREFIX = (\"=\", \"+\", \"-\", \"@\", \"\\t\", \"\\r\")\n\ndef _neutralise(v):\n s = \"\" if v is None else str(v)\n return \"'\" + s if s.startswith(DANGEROUS_PREFIX) else s\n\ndef export_rows(rows, out):\n w = csv.writer(out)\n for r in rows:\n w.writerow([html.escape(r.name), html.escape(r.note)])"}
{"id": "csv_injection_elsewhere", "pattern": "csv_injection", "shape": "elsewhere", "label": "unsafe", "vuln_class": "formula injection", "why": "the guard exists and is used by a neighbouring function, not on this path", "code": "DANGEROUS_PREFIX = (\"=\", \"+\", \"-\", \"@\", \"\\t\", \"\\r\")\n\ndef _neutralise(v):\n s = \"\" if v is None else str(v)\n return \"'\" + s if s.startswith(DANGEROUS_PREFIX) else s\n\ndef export_summary(rows, out):\n w = csv.writer(out)\n for r in rows:\n w.writerow([_neutralise(r.label)])\n\ndef export_rows(rows, out):\n w = csv.writer(out)\n for r in rows:\n w.writerow([r.name, r.note])"}
{"id": "csv_injection_covers_alt", "pattern": "csv_injection", "shape": "covers_alt", "label": "safe", "vuln_class": null, "why": "a second correct implementation, defended a different way", "code": "DANGEROUS_PREFIX = (\"=\", \"+\", \"-\", \"@\", \"\\t\", \"\\r\")\n\ndef _neutralise(v):\n s = \"\" if v is None else str(v)\n return \"'\" + s if s.startswith(DANGEROUS_PREFIX) else s\n\ndef export_rows(rows, out):\n w = csv.writer(out, quoting=csv.QUOTE_ALL)\n for r in rows:\n w.writerow([_neutralise(x) for x in (r.name, r.note)])"}
{"id": "zip_bomb_none", "pattern": "zip_bomb", "shape": "none", "label": "unsafe", "vuln_class": "resource exhaustion", "why": "no guard is applied on this path", "code": "MAX_TOTAL = 200 * 1024 * 1024\n\ndef _checked_members(z):\n total = 0\n for info in z.infolist():\n total += info.file_size\n if total > MAX_TOTAL:\n raise ValueError(\"archive expands too large\")\n yield info\n\ndef unpack(path, dest):\n with zipfile.ZipFile(path) as z:\n z.extractall(dest)"}
{"id": "zip_bomb_covers", "pattern": "zip_bomb", "shape": "covers", "label": "safe", "vuln_class": null, "why": "the guard is applied to the tainted value and addresses the weakness", "code": "MAX_TOTAL = 200 * 1024 * 1024\n\ndef _checked_members(z):\n total = 0\n for info in z.infolist():\n total += info.file_size\n if total > MAX_TOTAL:\n raise ValueError(\"archive expands too large\")\n yield info\n\ndef unpack(path, dest):\n with zipfile.ZipFile(path) as z:\n for info in _checked_members(z):\n z.extract(info, dest)"}
{"id": "zip_bomb_wrong_value", "pattern": "zip_bomb", "shape": "wrong_value", "label": "unsafe", "vuln_class": "resource exhaustion", "why": "the guard is applied to a sibling value, not the one that reaches the sink", "code": "MAX_TOTAL = 200 * 1024 * 1024\n\ndef _checked_members(z):\n total = 0\n for info in z.infolist():\n total += info.file_size\n if total > MAX_TOTAL:\n raise ValueError(\"archive expands too large\")\n yield info\n\ndef unpack(path, dest):\n with zipfile.ZipFile(path) as z:\n list(_checked_members(z))\n z.extractall(dest)"}
{"id": "zip_bomb_irrelevant", "pattern": "zip_bomb", "shape": "irrelevant", "label": "unsafe", "vuln_class": "resource exhaustion", "why": "a guard is applied to the right value but does not address this weakness", "code": "MAX_TOTAL = 200 * 1024 * 1024\n\ndef _checked_members(z):\n total = 0\n for info in z.infolist():\n total += info.file_size\n if total > MAX_TOTAL:\n raise ValueError(\"archive expands too large\")\n yield info\n\ndef unpack(path, dest):\n if os.path.getsize(path) > 20 * 1024 * 1024:\n raise ValueError(\"archive file too large\")\n with zipfile.ZipFile(path) as z:\n z.extractall(dest)"}
{"id": "zip_bomb_elsewhere", "pattern": "zip_bomb", "shape": "elsewhere", "label": "unsafe", "vuln_class": "resource exhaustion", "why": "the guard exists and is used by a neighbouring function, not on this path", "code": "MAX_TOTAL = 200 * 1024 * 1024\n\ndef _checked_members(z):\n total = 0\n for info in z.infolist():\n total += info.file_size\n if total > MAX_TOTAL:\n raise ValueError(\"archive expands too large\")\n yield info\n\ndef unpack_theme(path, dest):\n with zipfile.ZipFile(path) as z:\n for info in _checked_members(z):\n z.extract(info, dest)\n\ndef unpack(path, dest):\n with zipfile.ZipFile(path) as z:\n z.extractall(dest)"}
{"id": "zip_bomb_covers_alt", "pattern": "zip_bomb", "shape": "covers_alt", "label": "safe", "vuln_class": null, "why": "a second correct implementation, defended a different way", "code": "MAX_TOTAL = 200 * 1024 * 1024\n\ndef _checked_members(z):\n total = 0\n for info in z.infolist():\n total += info.file_size\n if total > MAX_TOTAL:\n raise ValueError(\"archive expands too large\")\n yield info\n\ndef unpack(path, dest):\n written = 0\n with zipfile.ZipFile(path) as z:\n for info in z.infolist():\n written += info.file_size\n if written > MAX_TOTAL:\n raise ValueError(\"archive expands too large\")\n z.extract(info, dest)"}
{"id": "ldap_injection_none", "pattern": "ldap_injection", "shape": "none", "label": "unsafe", "vuln_class": "ldap injection", "why": "no guard is applied on this path", "code": "from ldap3.utils.conv import escape_filter_chars\n\nBASE_DN = \"ou=people,dc=example,dc=com\"\n\ndef _filter_for(uid):\n return \"(uid=%s)\" % escape_filter_chars(uid)\n\ndef find_user(conn, uid):\n conn.search(BASE_DN, \"(uid=%s)\" % uid)\n return conn.entries"}
{"id": "ldap_injection_covers", "pattern": "ldap_injection", "shape": "covers", "label": "safe", "vuln_class": null, "why": "the guard is applied to the tainted value and addresses the weakness", "code": "from ldap3.utils.conv import escape_filter_chars\n\nBASE_DN = \"ou=people,dc=example,dc=com\"\n\ndef _filter_for(uid):\n return \"(uid=%s)\" % escape_filter_chars(uid)\n\ndef find_user(conn, uid):\n conn.search(BASE_DN, _filter_for(uid))\n return conn.entries"}
{"id": "ldap_injection_wrong_value", "pattern": "ldap_injection", "shape": "wrong_value", "label": "unsafe", "vuln_class": "ldap injection", "why": "the guard is applied to a sibling value, not the one that reaches the sink", "code": "from ldap3.utils.conv import escape_filter_chars\n\nBASE_DN = \"ou=people,dc=example,dc=com\"\n\ndef _filter_for(uid):\n return \"(uid=%s)\" % escape_filter_chars(uid)\n\ndef find_user(conn, uid, dept):\n _filter_for(dept)\n conn.search(BASE_DN, \"(&(uid=%s)(ou=%s))\" % (uid, escape_filter_chars(dept)))\n return conn.entries"}
{"id": "ldap_injection_irrelevant", "pattern": "ldap_injection", "shape": "irrelevant", "label": "unsafe", "vuln_class": "ldap injection", "why": "a guard is applied to the right value but does not address this weakness", "code": "from ldap3.utils.conv import escape_filter_chars\n\nBASE_DN = \"ou=people,dc=example,dc=com\"\n\ndef _filter_for(uid):\n return \"(uid=%s)\" % escape_filter_chars(uid)\n\ndef find_user(conn, uid):\n if not uid or len(uid) > 64:\n raise ValueError(\"bad uid length\")\n conn.search(BASE_DN, \"(uid=%s)\" % uid)\n return conn.entries"}
{"id": "ldap_injection_elsewhere", "pattern": "ldap_injection", "shape": "elsewhere", "label": "unsafe", "vuln_class": "ldap injection", "why": "the guard exists and is used by a neighbouring function, not on this path", "code": "from ldap3.utils.conv import escape_filter_chars\n\nBASE_DN = \"ou=people,dc=example,dc=com\"\n\ndef _filter_for(uid):\n return \"(uid=%s)\" % escape_filter_chars(uid)\n\ndef find_group(conn, gid):\n conn.search(BASE_DN, \"(cn=%s)\" % escape_filter_chars(gid))\n return conn.entries\n\ndef find_user(conn, uid):\n conn.search(BASE_DN, \"(uid=%s)\" % uid)\n return conn.entries"}
{"id": "ldap_injection_covers_alt", "pattern": "ldap_injection", "shape": "covers_alt", "label": "safe", "vuln_class": null, "why": "a second correct implementation, defended a different way", "code": "from ldap3.utils.conv import escape_filter_chars\n\nBASE_DN = \"ou=people,dc=example,dc=com\"\n\ndef _filter_for(uid):\n return \"(uid=%s)\" % escape_filter_chars(uid)\n\ndef find_user(conn, uid):\n conn.search(BASE_DN, \"(uid=%s)\" % escape_filter_chars(uid))\n return conn.entries"}
{"id": "ssrf_redirect_none", "pattern": "ssrf_redirect", "shape": "none", "label": "unsafe", "vuln_class": "server-side request forgery", "why": "no guard is applied on this path", "code": "import requests\nfrom urllib.parse import urlparse\n\nALLOWED = {\"api.partner.com\", \"cdn.partner.com\"}\n\ndef _checked(url):\n if urlparse(url).hostname not in ALLOWED:\n raise ValueError(\"host not allowed\")\n return url\n\ndef fetch_avatar(url):\n return requests.get(url, timeout=5).content"}
{"id": "ssrf_redirect_covers", "pattern": "ssrf_redirect", "shape": "covers", "label": "safe", "vuln_class": null, "why": "the guard is applied to the tainted value and addresses the weakness", "code": "import requests\nfrom urllib.parse import urlparse\n\nALLOWED = {\"api.partner.com\", \"cdn.partner.com\"}\n\ndef _checked(url):\n if urlparse(url).hostname not in ALLOWED:\n raise ValueError(\"host not allowed\")\n return url\n\ndef fetch_avatar(url):\n return requests.get(_checked(url), timeout=5, allow_redirects=False).content"}
{"id": "ssrf_redirect_covers_alt", "pattern": "ssrf_redirect", "shape": "covers_alt", "label": "safe", "vuln_class": null, "why": "a second correct implementation, defended a different way", "code": "import requests\nfrom urllib.parse import urlparse\n\nALLOWED = {\"api.partner.com\", \"cdn.partner.com\"}\n\ndef _checked(url):\n if urlparse(url).hostname not in ALLOWED:\n raise ValueError(\"host not allowed\")\n return url\n\ndef fetch_avatar(url):\n r = requests.get(_checked(url), timeout=5, allow_redirects=True)\n _checked(r.url)\n return r.content"}
{"id": "ssrf_redirect_wrong_value", "pattern": "ssrf_redirect", "shape": "wrong_value", "label": "unsafe", "vuln_class": "server-side request forgery", "why": "the guard is applied to a sibling value, not the one that reaches the sink", "code": "import requests\nfrom urllib.parse import urlparse\n\nALLOWED = {\"api.partner.com\", \"cdn.partner.com\"}\n\ndef _checked(url):\n if urlparse(url).hostname not in ALLOWED:\n raise ValueError(\"host not allowed\")\n return url\n\ndef fetch_avatar(url, fallback):\n _checked(fallback)\n return requests.get(url, timeout=5).content"}
{"id": "ssrf_redirect_irrelevant", "pattern": "ssrf_redirect", "shape": "irrelevant", "label": "unsafe", "vuln_class": "server-side request forgery", "why": "a guard is applied to the right value but does not address this weakness", "code": "import requests\nfrom urllib.parse import urlparse\n\nALLOWED = {\"api.partner.com\", \"cdn.partner.com\"}\n\ndef _checked(url):\n if urlparse(url).hostname not in ALLOWED:\n raise ValueError(\"host not allowed\")\n return url\n\ndef fetch_avatar(url):\n if not url.startswith(\"https://\"):\n raise ValueError(\"https only\")\n return requests.get(url, timeout=5).content"}
{"id": "ssrf_redirect_elsewhere", "pattern": "ssrf_redirect", "shape": "elsewhere", "label": "unsafe", "vuln_class": "server-side request forgery", "why": "the guard exists and is used by a neighbouring function, not on this path", "code": "import requests\nfrom urllib.parse import urlparse\n\nALLOWED = {\"api.partner.com\", \"cdn.partner.com\"}\n\ndef _checked(url):\n if urlparse(url).hostname not in ALLOWED:\n raise ValueError(\"host not allowed\")\n return url\n\ndef fetch_manifest(url):\n return requests.get(_checked(url), timeout=5, allow_redirects=False).json()\n\ndef fetch_avatar(url):\n return requests.get(url, timeout=5).content"}
{"id": "csrf_missing_none", "pattern": "csrf_missing", "shape": "none", "label": "unsafe", "vuln_class": "cross-site request forgery", "why": "no guard is applied on this path", "code": "from functools import wraps\n\ndef require_csrf(fn):\n @wraps(fn)\n def inner(request, *a, **kw):\n if request.form.get(\"csrf\") != request.session.get(\"csrf\"):\n abort(403)\n return fn(request, *a, **kw)\n return inner\n\n@app.post(\"/account/email\")\ndef change_email(request):\n request.user.email = request.form[\"email\"]\n request.user.save()"}
{"id": "csrf_missing_covers", "pattern": "csrf_missing", "shape": "covers", "label": "safe", "vuln_class": null, "why": "the guard is applied to the tainted value and addresses the weakness", "code": "from functools import wraps\n\ndef require_csrf(fn):\n @wraps(fn)\n def inner(request, *a, **kw):\n if request.form.get(\"csrf\") != request.session.get(\"csrf\"):\n abort(403)\n return fn(request, *a, **kw)\n return inner\n\n@app.post(\"/account/email\")\n@require_csrf\ndef change_email(request):\n request.user.email = request.form[\"email\"]\n request.user.save()"}
{"id": "csrf_missing_covers_alt", "pattern": "csrf_missing", "shape": "covers_alt", "label": "safe", "vuln_class": null, "why": "a second correct implementation, defended a different way", "code": "from functools import wraps\n\ndef require_csrf(fn):\n @wraps(fn)\n def inner(request, *a, **kw):\n if request.form.get(\"csrf\") != request.session.get(\"csrf\"):\n abort(403)\n return fn(request, *a, **kw)\n return inner\n\n@app.post(\"/account/email\")\ndef change_email(request):\n if request.headers.get(\"Sec-Fetch-Site\") != \"same-origin\":\n abort(403)\n if request.form.get(\"csrf\") != request.session.get(\"csrf\"):\n abort(403)\n request.user.email = request.form[\"email\"]\n request.user.save()"}
{"id": "csrf_missing_wrong_value", "pattern": "csrf_missing", "shape": "wrong_value", "label": "unsafe", "vuln_class": "cross-site request forgery", "why": "the guard is applied to a sibling value, not the one that reaches the sink", "code": "from functools import wraps\n\ndef require_csrf(fn):\n @wraps(fn)\n def inner(request, *a, **kw):\n if request.form.get(\"csrf\") != request.session.get(\"csrf\"):\n abort(403)\n return fn(request, *a, **kw)\n return inner\n\n@app.post(\"/account/email\")\ndef change_email(request):\n if request.form.get(\"csrf\") != request.form.get(\"csrf_echo\"):\n abort(403)\n request.user.email = request.form[\"email\"]\n request.user.save()"}
{"id": "csrf_missing_irrelevant", "pattern": "csrf_missing", "shape": "irrelevant", "label": "unsafe", "vuln_class": "cross-site request forgery", "why": "a guard is applied to the right value but does not address this weakness", "code": "from functools import wraps\n\ndef require_csrf(fn):\n @wraps(fn)\n def inner(request, *a, **kw):\n if request.form.get(\"csrf\") != request.session.get(\"csrf\"):\n abort(403)\n return fn(request, *a, **kw)\n return inner\n\n@app.post(\"/account/email\")\ndef change_email(request):\n if \"@\" not in request.form[\"email\"]:\n abort(400)\n request.user.email = request.form[\"email\"]\n request.user.save()"}
{"id": "csrf_missing_elsewhere", "pattern": "csrf_missing", "shape": "elsewhere", "label": "unsafe", "vuln_class": "cross-site request forgery", "why": "the guard exists and is used by a neighbouring function, not on this path", "code": "from functools import wraps\n\ndef require_csrf(fn):\n @wraps(fn)\n def inner(request, *a, **kw):\n if request.form.get(\"csrf\") != request.session.get(\"csrf\"):\n abort(403)\n return fn(request, *a, **kw)\n return inner\n\n@app.post(\"/account/password\")\n@require_csrf\ndef change_password(request):\n request.user.set_password(request.form[\"pw\"])\n\n@app.post(\"/account/email\")\ndef change_email(request):\n request.user.email = request.form[\"email\"]\n request.user.save()"}
{"id": "toctou_symlink_none", "pattern": "toctou_symlink", "shape": "none", "label": "unsafe", "vuln_class": "time-of-check time-of-use", "why": "no guard is applied on this path", "code": "import os\n\nSPOOL = \"/var/spool/uploads\"\n\ndef _create_exclusive(path):\n fd = os.open(path, os.O_CREAT | os.O_EXCL | os.O_WRONLY | os.O_NOFOLLOW, 0o600)\n return os.fdopen(fd, \"wb\")\n\ndef save_upload(name, data):\n path = os.path.join(SPOOL, name)\n if os.path.exists(path):\n raise FileExistsError(name)\n with open(path, \"wb\") as fh:\n fh.write(data)"}
{"id": "toctou_symlink_covers", "pattern": "toctou_symlink", "shape": "covers", "label": "safe", "vuln_class": null, "why": "the guard is applied to the tainted value and addresses the weakness", "code": "import os\n\nSPOOL = \"/var/spool/uploads\"\n\ndef _create_exclusive(path):\n fd = os.open(path, os.O_CREAT | os.O_EXCL | os.O_WRONLY | os.O_NOFOLLOW, 0o600)\n return os.fdopen(fd, \"wb\")\n\ndef save_upload(name, data):\n with _create_exclusive(os.path.join(SPOOL, name)) as fh:\n fh.write(data)"}
{"id": "toctou_symlink_covers_alt", "pattern": "toctou_symlink", "shape": "covers_alt", "label": "safe", "vuln_class": null, "why": "a second correct implementation, defended a different way", "code": "import os\n\nSPOOL = \"/var/spool/uploads\"\n\ndef _create_exclusive(path):\n fd = os.open(path, os.O_CREAT | os.O_EXCL | os.O_WRONLY | os.O_NOFOLLOW, 0o600)\n return os.fdopen(fd, \"wb\")\n\ndef save_upload(name, data):\n fd = os.open(os.path.join(SPOOL, name),\n os.O_CREAT | os.O_EXCL | os.O_WRONLY | os.O_NOFOLLOW, 0o600)\n with os.fdopen(fd, \"wb\") as fh:\n fh.write(data)"}
{"id": "toctou_symlink_wrong_value", "pattern": "toctou_symlink", "shape": "wrong_value", "label": "unsafe", "vuln_class": "time-of-check time-of-use", "why": "the guard is applied to a sibling value, not the one that reaches the sink", "code": "import os\n\nSPOOL = \"/var/spool/uploads\"\n\ndef _create_exclusive(path):\n fd = os.open(path, os.O_CREAT | os.O_EXCL | os.O_WRONLY | os.O_NOFOLLOW, 0o600)\n return os.fdopen(fd, \"wb\")\n\ndef save_upload(name, data, tmpname):\n _create_exclusive(os.path.join(SPOOL, tmpname)).close()\n with open(os.path.join(SPOOL, name), \"wb\") as fh:\n fh.write(data)"}
{"id": "toctou_symlink_irrelevant", "pattern": "toctou_symlink", "shape": "irrelevant", "label": "unsafe", "vuln_class": "time-of-check time-of-use", "why": "a guard is applied to the right value but does not address this weakness", "code": "import os\n\nSPOOL = \"/var/spool/uploads\"\n\ndef _create_exclusive(path):\n fd = os.open(path, os.O_CREAT | os.O_EXCL | os.O_WRONLY | os.O_NOFOLLOW, 0o600)\n return os.fdopen(fd, \"wb\")\n\ndef save_upload(name, data):\n if \"/\" in name or name.startswith(\".\"):\n raise ValueError(\"bad name\")\n path = os.path.join(SPOOL, name)\n with open(path, \"wb\") as fh:\n fh.write(data)"}
{"id": "toctou_symlink_elsewhere", "pattern": "toctou_symlink", "shape": "elsewhere", "label": "unsafe", "vuln_class": "time-of-check time-of-use", "why": "the guard exists and is used by a neighbouring function, not on this path", "code": "import os\n\nSPOOL = \"/var/spool/uploads\"\n\ndef _create_exclusive(path):\n fd = os.open(path, os.O_CREAT | os.O_EXCL | os.O_WRONLY | os.O_NOFOLLOW, 0o600)\n return os.fdopen(fd, \"wb\")\n\ndef save_temp(name, data):\n with _create_exclusive(os.path.join(SPOOL, name)) as fh:\n fh.write(data)\n\ndef save_upload(name, data):\n with open(os.path.join(SPOOL, name), \"wb\") as fh:\n fh.write(data)"}
{"id": "unicode_bypass_none", "pattern": "unicode_bypass", "shape": "none", "label": "unsafe", "vuln_class": "input validation bypass", "why": "no guard is applied on this path", "code": "import unicodedata, re\n\nBLOCKED = re.compile(r\"(?i)\\b(admin|root|system)\\b\")\n\ndef _canonical(s):\n return unicodedata.normalize(\"NFKC\", s).casefold()\n\ndef register(username):\n return create_user(username)"}
{"id": "unicode_bypass_covers", "pattern": "unicode_bypass", "shape": "covers", "label": "safe", "vuln_class": null, "why": "the guard is applied to the tainted value and addresses the weakness", "code": "import unicodedata, re\n\nBLOCKED = re.compile(r\"(?i)\\b(admin|root|system)\\b\")\n\ndef _canonical(s):\n return unicodedata.normalize(\"NFKC\", s).casefold()\n\ndef register(username):\n canon = _canonical(username)\n if BLOCKED.search(canon):\n raise ValueError(\"reserved name\")\n return create_user(canon)"}
{"id": "unicode_bypass_covers_alt", "pattern": "unicode_bypass", "shape": "covers_alt", "label": "safe", "vuln_class": null, "why": "a second correct implementation, defended a different way", "code": "import unicodedata, re\n\nBLOCKED = re.compile(r\"(?i)\\b(admin|root|system)\\b\")\n\ndef _canonical(s):\n return unicodedata.normalize(\"NFKC\", s).casefold()\n\ndef register(username):\n canon = unicodedata.normalize(\"NFKC\", username).casefold()\n if BLOCKED.search(canon):\n raise ValueError(\"reserved name\")\n return create_user(canon)"}
{"id": "unicode_bypass_wrong_value", "pattern": "unicode_bypass", "shape": "wrong_value", "label": "unsafe", "vuln_class": "input validation bypass", "why": "the guard is applied to a sibling value, not the one that reaches the sink", "code": "import unicodedata, re\n\nBLOCKED = re.compile(r\"(?i)\\b(admin|root|system)\\b\")\n\ndef _canonical(s):\n return unicodedata.normalize(\"NFKC\", s).casefold()\n\ndef register(username, display):\n if BLOCKED.search(_canonical(display)):\n raise ValueError(\"reserved name\")\n return create_user(_canonical(username))"}
{"id": "unicode_bypass_irrelevant", "pattern": "unicode_bypass", "shape": "irrelevant", "label": "unsafe", "vuln_class": "input validation bypass", "why": "a guard is applied to the right value but does not address this weakness", "code": "import unicodedata, re\n\nBLOCKED = re.compile(r\"(?i)\\b(admin|root|system)\\b\")\n\ndef _canonical(s):\n return unicodedata.normalize(\"NFKC\", s).casefold()\n\ndef register(username):\n if len(username) > 32:\n raise ValueError(\"too long\")\n return create_user(_canonical(username))"}
{"id": "unicode_bypass_elsewhere", "pattern": "unicode_bypass", "shape": "elsewhere", "label": "unsafe", "vuln_class": "input validation bypass", "why": "the guard exists and is used by a neighbouring function, not on this path", "code": "import unicodedata, re\n\nBLOCKED = re.compile(r\"(?i)\\b(admin|root|system)\\b\")\n\ndef _canonical(s):\n return unicodedata.normalize(\"NFKC\", s).casefold()\n\ndef register_org(name):\n canon = _canonical(name)\n if BLOCKED.search(canon):\n raise ValueError(\"reserved name\")\n return create_org(canon)\n\ndef register(username):\n return create_user(_canonical(username))"}
{"id": "cache_poisoning_none", "pattern": "cache_poisoning", "shape": "none", "label": "unsafe", "vuln_class": "web cache poisoning", "why": "no guard is applied on this path", "code": "KEYED_HEADERS = (\"Host\", \"Accept-Language\")\n\ndef _cache_key(request):\n parts = [request.path] + [request.headers.get(h, \"\") for h in KEYED_HEADERS]\n return \"|\".join(parts)\n\ndef render_page(request):\n key = request.path\n if key in CACHE:\n return CACHE[key]\n body = build(request, base=request.headers.get(\"X-Forwarded-Host\", \"\"))\n CACHE[key] = body\n return body"}
{"id": "cache_poisoning_covers", "pattern": "cache_poisoning", "shape": "covers", "label": "safe", "vuln_class": null, "why": "the guard is applied to the tainted value and addresses the weakness", "code": "KEYED_HEADERS = (\"Host\", \"Accept-Language\")\n\ndef _cache_key(request):\n parts = [request.path] + [request.headers.get(h, \"\") for h in KEYED_HEADERS]\n return \"|\".join(parts)\n\ndef render_page(request):\n key = _cache_key(request)\n if key in CACHE:\n return CACHE[key]\n body = build(request, base=request.headers.get(\"Host\", \"\"))\n CACHE[key] = body\n return body"}
{"id": "cache_poisoning_covers_alt", "pattern": "cache_poisoning", "shape": "covers_alt", "label": "safe", "vuln_class": null, "why": "a second correct implementation, defended a different way", "code": "KEYED_HEADERS = (\"Host\", \"Accept-Language\")\n\ndef _cache_key(request):\n parts = [request.path] + [request.headers.get(h, \"\") for h in KEYED_HEADERS]\n return \"|\".join(parts)\n\ndef render_page(request):\n key = request.path + \"|\" + request.headers.get(\"Host\", \"\")\n if key in CACHE:\n return CACHE[key]\n body = build(request, base=request.headers.get(\"Host\", \"\"))\n CACHE[key] = body\n return body"}
{"id": "cache_poisoning_wrong_value", "pattern": "cache_poisoning", "shape": "wrong_value", "label": "unsafe", "vuln_class": "web cache poisoning", "why": "the guard is applied to a sibling value, not the one that reaches the sink", "code": "KEYED_HEADERS = (\"Host\", \"Accept-Language\")\n\ndef _cache_key(request):\n parts = [request.path] + [request.headers.get(h, \"\") for h in KEYED_HEADERS]\n return \"|\".join(parts)\n\ndef render_page(request):\n key = _cache_key(request)\n if key in CACHE:\n return CACHE[key]\n body = build(request, base=request.headers.get(\"X-Forwarded-Host\", \"\"))\n CACHE[key] = body\n return body"}
{"id": "cache_poisoning_irrelevant", "pattern": "cache_poisoning", "shape": "irrelevant", "label": "unsafe", "vuln_class": "web cache poisoning", "why": "a guard is applied to the right value but does not address this weakness", "code": "KEYED_HEADERS = (\"Host\", \"Accept-Language\")\n\ndef _cache_key(request):\n parts = [request.path] + [request.headers.get(h, \"\") for h in KEYED_HEADERS]\n return \"|\".join(parts)\n\ndef render_page(request):\n if len(request.path) > 512:\n raise ValueError(\"path too long\")\n key = request.path\n if key in CACHE:\n return CACHE[key]\n body = build(request, base=request.headers.get(\"X-Forwarded-Host\", \"\"))\n CACHE[key] = body\n return body"}
{"id": "cache_poisoning_elsewhere", "pattern": "cache_poisoning", "shape": "elsewhere", "label": "unsafe", "vuln_class": "web cache poisoning", "why": "the guard exists and is used by a neighbouring function, not on this path", "code": "KEYED_HEADERS = (\"Host\", \"Accept-Language\")\n\ndef _cache_key(request):\n parts = [request.path] + [request.headers.get(h, \"\") for h in KEYED_HEADERS]\n return \"|\".join(parts)\n\ndef render_asset(request):\n key = _cache_key(request)\n return CACHE.setdefault(key, build_asset(request))\n\ndef render_page(request):\n key = request.path\n if key in CACHE:\n return CACHE[key]\n body = build(request, base=request.headers.get(\"X-Forwarded-Host\", \"\"))\n CACHE[key] = body\n return body"}
{"id": "int_overflow_none", "pattern": "int_overflow", "shape": "none", "label": "unsafe", "vuln_class": "integer overflow in size check", "why": "no guard is applied on this path", "code": "MAX_TOTAL = 50 * 1024 * 1024\n\ndef _fits(count, unit):\n if count < 0 or unit < 0:\n raise ValueError(\"negative size\")\n if count > MAX_TOTAL // max(unit, 1):\n raise ValueError(\"too large\")\n return True\n\ndef allocate_frames(count, unit):\n return bytearray(count * unit)"}
{"id": "int_overflow_covers", "pattern": "int_overflow", "shape": "covers", "label": "safe", "vuln_class": null, "why": "the guard is applied to the tainted value and addresses the weakness", "code": "MAX_TOTAL = 50 * 1024 * 1024\n\ndef _fits(count, unit):\n if count < 0 or unit < 0:\n raise ValueError(\"negative size\")\n if count > MAX_TOTAL // max(unit, 1):\n raise ValueError(\"too large\")\n return True\n\ndef allocate_frames(count, unit):\n _fits(count, unit)\n return bytearray(count * unit)"}
{"id": "int_overflow_covers_alt", "pattern": "int_overflow", "shape": "covers_alt", "label": "safe", "vuln_class": null, "why": "a second correct implementation, defended a different way", "code": "MAX_TOTAL = 50 * 1024 * 1024\n\ndef _fits(count, unit):\n if count < 0 or unit < 0:\n raise ValueError(\"negative size\")\n if count > MAX_TOTAL // max(unit, 1):\n raise ValueError(\"too large\")\n return True\n\ndef allocate_frames(count, unit):\n if count < 0 or unit < 0 or count > MAX_TOTAL // max(unit, 1):\n raise ValueError(\"bad size\")\n return bytearray(count * unit)"}
{"id": "int_overflow_wrong_value", "pattern": "int_overflow", "shape": "wrong_value", "label": "unsafe", "vuln_class": "integer overflow in size check", "why": "the guard is applied to a sibling value, not the one that reaches the sink", "code": "MAX_TOTAL = 50 * 1024 * 1024\n\ndef _fits(count, unit):\n if count < 0 or unit < 0:\n raise ValueError(\"negative size\")\n if count > MAX_TOTAL // max(unit, 1):\n raise ValueError(\"too large\")\n return True\n\ndef allocate_frames(count, unit, stride):\n _fits(count, stride)\n return bytearray(count * unit)"}
{"id": "int_overflow_irrelevant", "pattern": "int_overflow", "shape": "irrelevant", "label": "unsafe", "vuln_class": "integer overflow in size check", "why": "a guard is applied to the right value but does not address this weakness", "code": "MAX_TOTAL = 50 * 1024 * 1024\n\ndef _fits(count, unit):\n if count < 0 or unit < 0:\n raise ValueError(\"negative size\")\n if count > MAX_TOTAL // max(unit, 1):\n raise ValueError(\"too large\")\n return True\n\ndef allocate_frames(count, unit):\n if count * unit > MAX_TOTAL:\n raise ValueError(\"too large\")\n return bytearray(count * unit)"}
{"id": "int_overflow_elsewhere", "pattern": "int_overflow", "shape": "elsewhere", "label": "unsafe", "vuln_class": "integer overflow in size check", "why": "the guard exists and is used by a neighbouring function, not on this path", "code": "MAX_TOTAL = 50 * 1024 * 1024\n\ndef _fits(count, unit):\n if count < 0 or unit < 0:\n raise ValueError(\"negative size\")\n if count > MAX_TOTAL // max(unit, 1):\n raise ValueError(\"too large\")\n return True\n\ndef allocate_tiles(count, unit):\n _fits(count, unit)\n return bytearray(count * unit)\n\ndef allocate_frames(count, unit):\n return bytearray(count * unit)"}
{"id": "sig_skip_branch_none", "pattern": "sig_skip_branch", "shape": "none", "label": "unsafe", "vuln_class": "missing signature verification", "why": "no guard is applied on this path", "code": "import hmac, hashlib, base64\n\nSCHEME = \"v2\"\n\ndef _verify(payload, header, secret):\n \"\"\"Header is \"v2,<ts>,<b64 mac>\"; the MAC covers the timestamp and the payload.\"\"\"\n try:\n scheme, ts, mac_b64 = header.split(\",\", 2)\n except ValueError:\n return False\n if scheme != SCHEME:\n return False\n signed = ts.encode() + b\".\" + payload\n want = base64.b64decode(mac_b64 + \"==\")\n got = hmac.new(secret, signed, hashlib.sha512).digest()\n return hmac.compare_digest(got, want)\n\ndef handle_hook(request, secret):\n return process(json.loads(request.body))"}
{"id": "sig_skip_branch_covers", "pattern": "sig_skip_branch", "shape": "covers", "label": "safe", "vuln_class": null, "why": "the guard is applied to the tainted value and addresses the weakness", "code": "import hmac, hashlib, base64\n\nSCHEME = \"v2\"\n\ndef _verify(payload, header, secret):\n \"\"\"Header is \"v2,<ts>,<b64 mac>\"; the MAC covers the timestamp and the payload.\"\"\"\n try:\n scheme, ts, mac_b64 = header.split(\",\", 2)\n except ValueError:\n return False\n if scheme != SCHEME:\n return False\n signed = ts.encode() + b\".\" + payload\n want = base64.b64decode(mac_b64 + \"==\")\n got = hmac.new(secret, signed, hashlib.sha512).digest()\n return hmac.compare_digest(got, want)\n\ndef handle_hook(request, secret):\n if not _verify(request.body, request.headers.get(\"X-Sig\", \"\"), secret):\n abort(401)\n return process(json.loads(request.body))"}
{"id": "sig_skip_branch_covers_alt", "pattern": "sig_skip_branch", "shape": "covers_alt", "label": "safe", "vuln_class": null, "why": "a second correct implementation, defended a different way", "code": "import hmac, hashlib, base64\n\nSCHEME = \"v2\"\n\ndef _verify(payload, header, secret):\n \"\"\"Header is \"v2,<ts>,<b64 mac>\"; the MAC covers the timestamp and the payload.\"\"\"\n try:\n scheme, ts, mac_b64 = header.split(\",\", 2)\n except ValueError:\n return False\n if scheme != SCHEME:\n return False\n signed = ts.encode() + b\".\" + payload\n want = base64.b64decode(mac_b64 + \"==\")\n got = hmac.new(secret, signed, hashlib.sha512).digest()\n return hmac.compare_digest(got, want)\n\ndef handle_hook(request, secret):\n header = request.headers.get(\"X-Sig\", \"\")\n scheme, ts, mac_b64 = (header.split(\",\", 2) + [\"\", \"\", \"\"])[:3]\n signed = ts.encode() + b\".\" + request.body\n got = hmac.new(secret, signed, hashlib.sha512).digest()\n if scheme != SCHEME or not hmac.compare_digest(got, base64.b64decode(mac_b64 + \"==\")):\n abort(401)\n return process(json.loads(request.body))"}
{"id": "sig_skip_branch_wrong_value", "pattern": "sig_skip_branch", "shape": "wrong_value", "label": "unsafe", "vuln_class": "missing signature verification", "why": "the guard is applied to a sibling value, not the one that reaches the sink", "code": "import hmac, hashlib, base64\n\nSCHEME = \"v2\"\n\ndef _verify(payload, header, secret):\n \"\"\"Header is \"v2,<ts>,<b64 mac>\"; the MAC covers the timestamp and the payload.\"\"\"\n try:\n scheme, ts, mac_b64 = header.split(\",\", 2)\n except ValueError:\n return False\n if scheme != SCHEME:\n return False\n signed = ts.encode() + b\".\" + payload\n want = base64.b64decode(mac_b64 + \"==\")\n got = hmac.new(secret, signed, hashlib.sha512).digest()\n return hmac.compare_digest(got, want)\n\ndef handle_hook(request, secret):\n if not _verify(request.headers.get(\"X-Meta\", b\"\"), request.headers.get(\"X-Sig\", \"\"), secret):\n abort(401)\n return process(json.loads(request.body))"}
{"id": "sig_skip_branch_irrelevant", "pattern": "sig_skip_branch", "shape": "irrelevant", "label": "unsafe", "vuln_class": "missing signature verification", "why": "a guard is applied to the right value but does not address this weakness", "code": "import hmac, hashlib, base64\n\nSCHEME = \"v2\"\n\ndef _verify(payload, header, secret):\n \"\"\"Header is \"v2,<ts>,<b64 mac>\"; the MAC covers the timestamp and the payload.\"\"\"\n try:\n scheme, ts, mac_b64 = header.split(\",\", 2)\n except ValueError:\n return False\n if scheme != SCHEME:\n return False\n signed = ts.encode() + b\".\" + payload\n want = base64.b64decode(mac_b64 + \"==\")\n got = hmac.new(secret, signed, hashlib.sha512).digest()\n return hmac.compare_digest(got, want)\n\ndef handle_hook(request, secret):\n if request.headers.get(\"Content-Type\") != \"application/json\":\n abort(415)\n return process(json.loads(request.body))"}
{"id": "sig_skip_branch_elsewhere", "pattern": "sig_skip_branch", "shape": "elsewhere", "label": "unsafe", "vuln_class": "missing signature verification", "why": "the guard exists and is used by a neighbouring function, not on this path", "code": "import hmac, hashlib, base64\n\nSCHEME = \"v2\"\n\ndef _verify(payload, header, secret):\n \"\"\"Header is \"v2,<ts>,<b64 mac>\"; the MAC covers the timestamp and the payload.\"\"\"\n try:\n scheme, ts, mac_b64 = header.split(\",\", 2)\n except ValueError:\n return False\n if scheme != SCHEME:\n return False\n signed = ts.encode() + b\".\" + payload\n want = base64.b64decode(mac_b64 + \"==\")\n got = hmac.new(secret, signed, hashlib.sha512).digest()\n return hmac.compare_digest(got, want)\n\ndef handle_billing_hook(request, secret):\n if not _verify(request.body, request.headers.get(\"X-Sig\", \"\"), secret):\n abort(401)\n return process(json.loads(request.body))\n\ndef handle_hook(request, secret):\n return process(json.loads(request.body))"}
{"id": "reset_token_reuse_none", "pattern": "reset_token_reuse", "shape": "none", "label": "unsafe", "vuln_class": "authentication bypass", "why": "no guard is applied on this path", "code": "def _consume(token):\n \"\"\"Single-use: returns the user only if the row was still unused.\"\"\"\n rows = db.execute(\n \"UPDATE resets SET used_at = now() WHERE token = %s AND used_at IS NULL RETURNING user_id\",\n (token,))\n return rows[0][0] if rows else None\n\ndef apply_reset(token, new_password):\n uid = db.query(\"SELECT user_id FROM resets WHERE token = %s\", (token,))[0][0]\n set_password(uid, new_password)"}
{"id": "reset_token_reuse_covers", "pattern": "reset_token_reuse", "shape": "covers", "label": "safe", "vuln_class": null, "why": "the guard is applied to the tainted value and addresses the weakness", "code": "def _consume(token):\n \"\"\"Single-use: returns the user only if the row was still unused.\"\"\"\n rows = db.execute(\n \"UPDATE resets SET used_at = now() WHERE token = %s AND used_at IS NULL RETURNING user_id\",\n (token,))\n return rows[0][0] if rows else None\n\ndef apply_reset(token, new_password):\n uid = _consume(token)\n if uid is None:\n abort(400)\n set_password(uid, new_password)"}
{"id": "reset_token_reuse_covers_alt", "pattern": "reset_token_reuse", "shape": "covers_alt", "label": "safe", "vuln_class": null, "why": "a second correct implementation, defended a different way", "code": "def _consume(token):\n \"\"\"Single-use: returns the user only if the row was still unused.\"\"\"\n rows = db.execute(\n \"UPDATE resets SET used_at = now() WHERE token = %s AND used_at IS NULL RETURNING user_id\",\n (token,))\n return rows[0][0] if rows else None\n\ndef apply_reset(token, new_password):\n rows = db.execute(\n \"UPDATE resets SET used_at = now() WHERE token = %s AND used_at IS NULL RETURNING user_id\",\n (token,))\n if not rows:\n abort(400)\n set_password(rows[0][0], new_password)"}
{"id": "reset_token_reuse_wrong_value", "pattern": "reset_token_reuse", "shape": "wrong_value", "label": "unsafe", "vuln_class": "authentication bypass", "why": "the guard is applied to a sibling value, not the one that reaches the sink", "code": "def _consume(token):\n \"\"\"Single-use: returns the user only if the row was still unused.\"\"\"\n rows = db.execute(\n \"UPDATE resets SET used_at = now() WHERE token = %s AND used_at IS NULL RETURNING user_id\",\n (token,))\n return rows[0][0] if rows else None\n\ndef apply_reset(token, confirm_token, new_password):\n _consume(confirm_token)\n uid = db.query(\"SELECT user_id FROM resets WHERE token = %s\", (token,))[0][0]\n set_password(uid, new_password)"}
{"id": "reset_token_reuse_irrelevant", "pattern": "reset_token_reuse", "shape": "irrelevant", "label": "unsafe", "vuln_class": "authentication bypass", "why": "a guard is applied to the right value but does not address this weakness", "code": "def _consume(token):\n \"\"\"Single-use: returns the user only if the row was still unused.\"\"\"\n rows = db.execute(\n \"UPDATE resets SET used_at = now() WHERE token = %s AND used_at IS NULL RETURNING user_id\",\n (token,))\n return rows[0][0] if rows else None\n\ndef apply_reset(token, new_password):\n row = db.query(\"SELECT user_id, created_at FROM resets WHERE token = %s\", (token,))[0]\n if (now() - row[1]).total_seconds() > 3600:\n abort(400)\n set_password(row[0], new_password)"}
{"id": "reset_token_reuse_elsewhere", "pattern": "reset_token_reuse", "shape": "elsewhere", "label": "unsafe", "vuln_class": "authentication bypass", "why": "the guard exists and is used by a neighbouring function, not on this path", "code": "def _consume(token):\n \"\"\"Single-use: returns the user only if the row was still unused.\"\"\"\n rows = db.execute(\n \"UPDATE resets SET used_at = now() WHERE token = %s AND used_at IS NULL RETURNING user_id\",\n (token,))\n return rows[0][0] if rows else None\n\ndef apply_invite(token):\n uid = _consume(token)\n if uid is None:\n abort(400)\n activate(uid)\n\ndef apply_reset(token, new_password):\n uid = db.query(\"SELECT user_id FROM resets WHERE token = %s\", (token,))[0][0]\n set_password(uid, new_password)"}
{"id": "dict_merge_none", "pattern": "dict_merge", "shape": "none", "label": "unsafe", "vuln_class": "mass assignment", "why": "no guard is applied on this path", "code": "PROTECTED = {\"is_admin\", \"org_id\", \"plan\", \"__class__\"}\n\ndef _merge_safe(target, patch):\n for k, v in patch.items():\n if k in PROTECTED:\n continue\n if isinstance(v, dict) and isinstance(target.get(k), dict):\n _merge_safe(target[k], v)\n else:\n target[k] = v\n return target\n\ndef update_settings(user, patch):\n return _deep_merge(user.settings, patch)"}
{"id": "dict_merge_covers", "pattern": "dict_merge", "shape": "covers", "label": "safe", "vuln_class": null, "why": "the guard is applied to the tainted value and addresses the weakness", "code": "PROTECTED = {\"is_admin\", \"org_id\", \"plan\", \"__class__\"}\n\ndef _merge_safe(target, patch):\n for k, v in patch.items():\n if k in PROTECTED:\n continue\n if isinstance(v, dict) and isinstance(target.get(k), dict):\n _merge_safe(target[k], v)\n else:\n target[k] = v\n return target\n\ndef update_settings(user, patch):\n return _merge_safe(user.settings, patch)"}
{"id": "dict_merge_covers_alt", "pattern": "dict_merge", "shape": "covers_alt", "label": "safe", "vuln_class": null, "why": "a second correct implementation, defended a different way", "code": "PROTECTED = {\"is_admin\", \"org_id\", \"plan\", \"__class__\"}\n\ndef _merge_safe(target, patch):\n for k, v in patch.items():\n if k in PROTECTED:\n continue\n if isinstance(v, dict) and isinstance(target.get(k), dict):\n _merge_safe(target[k], v)\n else:\n target[k] = v\n return target\n\ndef update_settings(user, patch):\n clean = {k: v for k, v in patch.items() if k not in PROTECTED}\n return _merge_safe(user.settings, clean)"}
{"id": "dict_merge_wrong_value", "pattern": "dict_merge", "shape": "wrong_value", "label": "unsafe", "vuln_class": "mass assignment", "why": "the guard is applied to a sibling value, not the one that reaches the sink", "code": "PROTECTED = {\"is_admin\", \"org_id\", \"plan\", \"__class__\"}\n\ndef _merge_safe(target, patch):\n for k, v in patch.items():\n if k in PROTECTED:\n continue\n if isinstance(v, dict) and isinstance(target.get(k), dict):\n _merge_safe(target[k], v)\n else:\n target[k] = v\n return target\n\ndef update_settings(user, patch, defaults):\n _merge_safe({}, defaults)\n return _deep_merge(user.settings, patch)"}
{"id": "dict_merge_irrelevant", "pattern": "dict_merge", "shape": "irrelevant", "label": "unsafe", "vuln_class": "mass assignment", "why": "a guard is applied to the right value but does not address this weakness", "code": "PROTECTED = {\"is_admin\", \"org_id\", \"plan\", \"__class__\"}\n\ndef _merge_safe(target, patch):\n for k, v in patch.items():\n if k in PROTECTED:\n continue\n if isinstance(v, dict) and isinstance(target.get(k), dict):\n _merge_safe(target[k], v)\n else:\n target[k] = v\n return target\n\ndef update_settings(user, patch):\n if len(patch) > 50:\n raise ValueError(\"too many keys\")\n return _deep_merge(user.settings, patch)"}
{"id": "dict_merge_elsewhere", "pattern": "dict_merge", "shape": "elsewhere", "label": "unsafe", "vuln_class": "mass assignment", "why": "the guard exists and is used by a neighbouring function, not on this path", "code": "PROTECTED = {\"is_admin\", \"org_id\", \"plan\", \"__class__\"}\n\ndef _merge_safe(target, patch):\n for k, v in patch.items():\n if k in PROTECTED:\n continue\n if isinstance(v, dict) and isinstance(target.get(k), dict):\n _merge_safe(target[k], v)\n else:\n target[k] = v\n return target\n\ndef update_org_settings(org, patch):\n return _merge_safe(org.settings, patch)\n\ndef update_settings(user, patch):\n return _deep_merge(user.settings, patch)"}
{"id": "arg_injection_none", "pattern": "arg_injection", "shape": "none", "label": "unsafe", "vuln_class": "argument injection", "why": "no guard is applied on this path", "code": "import subprocess\n\ndef _as_operand(value):\n \"\"\"Stops a value beginning with '-' from being read as a flag.\"\"\"\n if value.startswith(\"-\"):\n raise ValueError(\"operand may not start with a dash\")\n return value\n\ndef count_matches(pattern, path):\n return subprocess.run([\"grep\", \"-c\", pattern, path], capture_output=True).stdout"}
{"id": "arg_injection_covers", "pattern": "arg_injection", "shape": "covers", "label": "safe", "vuln_class": null, "why": "the guard is applied to the tainted value and addresses the weakness", "code": "import subprocess\n\ndef _as_operand(value):\n \"\"\"Stops a value beginning with '-' from being read as a flag.\"\"\"\n if value.startswith(\"-\"):\n raise ValueError(\"operand may not start with a dash\")\n return value\n\ndef count_matches(pattern, path):\n return subprocess.run([\"grep\", \"-c\", \"-e\", pattern, \"--\", path],\n capture_output=True).stdout"}
{"id": "arg_injection_covers_alt", "pattern": "arg_injection", "shape": "covers_alt", "label": "safe", "vuln_class": null, "why": "a second correct implementation, defended a different way", "code": "import subprocess\n\ndef _as_operand(value):\n \"\"\"Stops a value beginning with '-' from being read as a flag.\"\"\"\n if value.startswith(\"-\"):\n raise ValueError(\"operand may not start with a dash\")\n return value\n\ndef count_matches(pattern, path):\n return subprocess.run([\"grep\", \"-c\", \"--\", _as_operand(pattern), _as_operand(path)],\n capture_output=True).stdout"}
{"id": "arg_injection_wrong_value", "pattern": "arg_injection", "shape": "wrong_value", "label": "unsafe", "vuln_class": "argument injection", "why": "the guard is applied to a sibling value, not the one that reaches the sink", "code": "import subprocess\n\ndef _as_operand(value):\n \"\"\"Stops a value beginning with '-' from being read as a flag.\"\"\"\n if value.startswith(\"-\"):\n raise ValueError(\"operand may not start with a dash\")\n return value\n\ndef count_matches(pattern, path):\n _as_operand(path)\n return subprocess.run([\"grep\", \"-c\", pattern, path], capture_output=True).stdout"}
{"id": "arg_injection_irrelevant", "pattern": "arg_injection", "shape": "irrelevant", "label": "unsafe", "vuln_class": "argument injection", "why": "a guard is applied to the right value but does not address this weakness", "code": "import subprocess\n\ndef _as_operand(value):\n \"\"\"Stops a value beginning with '-' from being read as a flag.\"\"\"\n if value.startswith(\"-\"):\n raise ValueError(\"operand may not start with a dash\")\n return value\n\ndef count_matches(pattern, path):\n if \";\" in pattern or \"|\" in pattern or \"$\" in pattern:\n raise ValueError(\"shell metacharacter\")\n return subprocess.run([\"grep\", \"-c\", pattern, path], capture_output=True).stdout"}
{"id": "arg_injection_elsewhere", "pattern": "arg_injection", "shape": "elsewhere", "label": "unsafe", "vuln_class": "argument injection", "why": "the guard exists and is used by a neighbouring function, not on this path", "code": "import subprocess\n\ndef _as_operand(value):\n \"\"\"Stops a value beginning with '-' from being read as a flag.\"\"\"\n if value.startswith(\"-\"):\n raise ValueError(\"operand may not start with a dash\")\n return value\n\ndef list_matches(pattern, path):\n return subprocess.run([\"grep\", \"-n\", \"-e\", pattern, \"--\", path],\n capture_output=True).stdout\n\ndef count_matches(pattern, path):\n return subprocess.run([\"grep\", \"-c\", pattern, path], capture_output=True).stdout"}
{"id": "tar_symlink_none", "pattern": "tar_symlink", "shape": "none", "label": "unsafe", "vuln_class": "path traversal", "why": "no guard is applied on this path", "code": "import tarfile, os\n\ndef _safe_members(tar, root):\n root = os.path.realpath(root)\n for m in tar.getmembers():\n if m.issym() or m.islnk():\n continue\n dest = os.path.realpath(os.path.join(root, m.name))\n if os.path.commonpath([dest, root]) != root:\n raise ValueError(\"entry escapes destination\")\n yield m\n\ndef unpack_bundle(path, dest):\n with tarfile.open(path) as t:\n t.extractall(dest)"}
{"id": "tar_symlink_covers", "pattern": "tar_symlink", "shape": "covers", "label": "safe", "vuln_class": null, "why": "the guard is applied to the tainted value and addresses the weakness", "code": "import tarfile, os\n\ndef _safe_members(tar, root):\n root = os.path.realpath(root)\n for m in tar.getmembers():\n if m.issym() or m.islnk():\n continue\n dest = os.path.realpath(os.path.join(root, m.name))\n if os.path.commonpath([dest, root]) != root:\n raise ValueError(\"entry escapes destination\")\n yield m\n\ndef unpack_bundle(path, dest):\n with tarfile.open(path) as t:\n t.extractall(dest, members=_safe_members(t, dest))"}
{"id": "tar_symlink_covers_alt", "pattern": "tar_symlink", "shape": "covers_alt", "label": "safe", "vuln_class": null, "why": "a second correct implementation, defended a different way", "code": "import tarfile, os\n\ndef _safe_members(tar, root):\n root = os.path.realpath(root)\n for m in tar.getmembers():\n if m.issym() or m.islnk():\n continue\n dest = os.path.realpath(os.path.join(root, m.name))\n if os.path.commonpath([dest, root]) != root:\n raise ValueError(\"entry escapes destination\")\n yield m\n\ndef unpack_bundle(path, dest):\n with tarfile.open(path) as t:\n t.extractall(dest, filter=\"data\")"}
{"id": "tar_symlink_wrong_value", "pattern": "tar_symlink", "shape": "wrong_value", "label": "unsafe", "vuln_class": "path traversal", "why": "the guard is applied to a sibling value, not the one that reaches the sink", "code": "import tarfile, os\n\ndef _safe_members(tar, root):\n root = os.path.realpath(root)\n for m in tar.getmembers():\n if m.issym() or m.islnk():\n continue\n dest = os.path.realpath(os.path.join(root, m.name))\n if os.path.commonpath([dest, root]) != root:\n raise ValueError(\"entry escapes destination\")\n yield m\n\ndef unpack_bundle(path, dest, staging):\n with tarfile.open(path) as t:\n list(_safe_members(t, staging))\n t.extractall(dest)"}
{"id": "tar_symlink_irrelevant", "pattern": "tar_symlink", "shape": "irrelevant", "label": "unsafe", "vuln_class": "path traversal", "why": "a guard is applied to the right value but does not address this weakness", "code": "import tarfile, os\n\ndef _safe_members(tar, root):\n root = os.path.realpath(root)\n for m in tar.getmembers():\n if m.issym() or m.islnk():\n continue\n dest = os.path.realpath(os.path.join(root, m.name))\n if os.path.commonpath([dest, root]) != root:\n raise ValueError(\"entry escapes destination\")\n yield m\n\ndef unpack_bundle(path, dest):\n with tarfile.open(path) as t:\n if sum(m.size for m in t.getmembers()) > 100 * 1024 * 1024:\n raise ValueError(\"bundle too large\")\n t.extractall(dest)"}
{"id": "tar_symlink_elsewhere", "pattern": "tar_symlink", "shape": "elsewhere", "label": "unsafe", "vuln_class": "path traversal", "why": "the guard exists and is used by a neighbouring function, not on this path", "code": "import tarfile, os\n\ndef _safe_members(tar, root):\n root = os.path.realpath(root)\n for m in tar.getmembers():\n if m.issym() or m.islnk():\n continue\n dest = os.path.realpath(os.path.join(root, m.name))\n if os.path.commonpath([dest, root]) != root:\n raise ValueError(\"entry escapes destination\")\n yield m\n\ndef unpack_theme(path, dest):\n with tarfile.open(path) as t:\n t.extractall(dest, members=_safe_members(t, dest))\n\ndef unpack_bundle(path, dest):\n with tarfile.open(path) as t:\n t.extractall(dest)"}
{"id": "jwt_audience_none", "pattern": "jwt_audience", "shape": "none", "label": "unsafe", "vuln_class": "authentication bypass", "why": "no guard is applied on this path", "code": "import jwt\n\nISSUER = \"https://auth.example.com/\"\nAUDIENCE = \"api.example.com\"\n\ndef _claims(token, key):\n return jwt.decode(token, key, algorithms=[\"RS256\"],\n audience=AUDIENCE, issuer=ISSUER,\n options={\"require\": [\"exp\", \"iss\", \"aud\", \"sub\"]})\n\ndef current_user(token, key):\n claims = jwt.decode(token, key, algorithms=[\"RS256\"])\n return User.get(claims[\"sub\"])"}
{"id": "jwt_audience_covers", "pattern": "jwt_audience", "shape": "covers", "label": "safe", "vuln_class": null, "why": "the guard is applied to the tainted value and addresses the weakness", "code": "import jwt\n\nISSUER = \"https://auth.example.com/\"\nAUDIENCE = \"api.example.com\"\n\ndef _claims(token, key):\n return jwt.decode(token, key, algorithms=[\"RS256\"],\n audience=AUDIENCE, issuer=ISSUER,\n options={\"require\": [\"exp\", \"iss\", \"aud\", \"sub\"]})\n\ndef current_user(token, key):\n return User.get(_claims(token, key)[\"sub\"])"}
{"id": "jwt_audience_covers_alt", "pattern": "jwt_audience", "shape": "covers_alt", "label": "safe", "vuln_class": null, "why": "a second correct implementation, defended a different way", "code": "import jwt\n\nISSUER = \"https://auth.example.com/\"\nAUDIENCE = \"api.example.com\"\n\ndef _claims(token, key):\n return jwt.decode(token, key, algorithms=[\"RS256\"],\n audience=AUDIENCE, issuer=ISSUER,\n options={\"require\": [\"exp\", \"iss\", \"aud\", \"sub\"]})\n\ndef current_user(token, key):\n claims = jwt.decode(token, key, algorithms=[\"RS256\"],\n audience=AUDIENCE, issuer=ISSUER,\n options={\"require\": [\"exp\", \"iss\", \"aud\", \"sub\"]})\n return User.get(claims[\"sub\"])"}
{"id": "jwt_audience_wrong_value", "pattern": "jwt_audience", "shape": "wrong_value", "label": "unsafe", "vuln_class": "authentication bypass", "why": "the guard is applied to a sibling value, not the one that reaches the sink", "code": "import jwt\n\nISSUER = \"https://auth.example.com/\"\nAUDIENCE = \"api.example.com\"\n\ndef _claims(token, key):\n return jwt.decode(token, key, algorithms=[\"RS256\"],\n audience=AUDIENCE, issuer=ISSUER,\n options={\"require\": [\"exp\", \"iss\", \"aud\", \"sub\"]})\n\ndef current_user(token, refresh_token, key):\n _claims(refresh_token, key)\n claims = jwt.decode(token, key, algorithms=[\"RS256\"])\n return User.get(claims[\"sub\"])"}
{"id": "jwt_audience_irrelevant", "pattern": "jwt_audience", "shape": "irrelevant", "label": "unsafe", "vuln_class": "authentication bypass", "why": "a guard is applied to the right value but does not address this weakness", "code": "import jwt\n\nISSUER = \"https://auth.example.com/\"\nAUDIENCE = \"api.example.com\"\n\ndef _claims(token, key):\n return jwt.decode(token, key, algorithms=[\"RS256\"],\n audience=AUDIENCE, issuer=ISSUER,\n options={\"require\": [\"exp\", \"iss\", \"aud\", \"sub\"]})\n\ndef current_user(token, key):\n claims = jwt.decode(token, key, algorithms=[\"RS256\"])\n if claims.get(\"exp\", 0) < time.time():\n abort(401)\n return User.get(claims[\"sub\"])"}
{"id": "jwt_audience_elsewhere", "pattern": "jwt_audience", "shape": "elsewhere", "label": "unsafe", "vuln_class": "authentication bypass", "why": "the guard exists and is used by a neighbouring function, not on this path", "code": "import jwt\n\nISSUER = \"https://auth.example.com/\"\nAUDIENCE = \"api.example.com\"\n\ndef _claims(token, key):\n return jwt.decode(token, key, algorithms=[\"RS256\"],\n audience=AUDIENCE, issuer=ISSUER,\n options={\"require\": [\"exp\", \"iss\", \"aud\", \"sub\"]})\n\ndef service_account(token, key):\n return Service.get(_claims(token, key)[\"sub\"])\n\ndef current_user(token, key):\n claims = jwt.decode(token, key, algorithms=[\"RS256\"])\n return User.get(claims[\"sub\"])"}