code-review-lab / demo /src /rules.py
alirezaaminzadeh's picture
Publish CodeReview Python lab diffs and versioned ruleset
32e0071 verified
Raw History Blame Contribute Delete
5.08 kB
from __future__ import annotations
from src.domain import RULESET_DATE, RULESET_ID, Severity
from src.schemas import RuleSpec
RULES: dict[str, RuleSpec] = {
"py-sql-concat": RuleSpec(
rule_id="py-sql-concat",
family="sql-injection",
cwe="CWE-89",
severity=Severity.HIGH,
title="SQL built by string concatenation",
title_fa="ساخت SQL با الحاق رشته",
message="A database execute call builds SQL from non-constant text. Use a parameter placeholder.",
message_fa="فراخوانی execute متن SQL را از رشته غیرثابت می‌سازد. از پارامتر استفاده کنید.",
references=["https://cwe.mitre.org/data/definitions/89.html"],
patch_hint="cursor.execute(sql_with_placeholder, (value,))",
),
"py-cmd-shell": RuleSpec(
rule_id="py-cmd-shell",
family="command-injection",
cwe="CWE-78",
severity=Severity.HIGH,
title="Shell command built from input",
title_fa="فرمان پوسته ساخته‌شده از ورودی",
message="A process is started with shell=True or a concatenated command string.",
message_fa="فرایند با shell=True یا رشته فرمان الحاق‌شده شروع می‌شود.",
references=["https://cwe.mitre.org/data/definitions/78.html"],
patch_hint="subprocess.run([executable, arg], check=False)",
),
"py-path-join": RuleSpec(
rule_id="py-path-join",
family="path-traversal",
cwe="CWE-22",
severity=Severity.HIGH,
title="File path built from input",
title_fa="مسیر فایل ساخته‌شده از ورودی",
message="A file is opened from a path joined with caller-controlled text without a base-directory check.",
message_fa="فایل از مسیر الحاق‌شده با متن کنترل‌شده باز می‌شود و بررسی پوشه پایه نیست.",
references=["https://cwe.mitre.org/data/definitions/22.html"],
patch_hint="resolved = (BASE / name).resolve(); resolved.is_relative_to(BASE)",
),
"py-pickle-load": RuleSpec(
rule_id="py-pickle-load",
family="insecure-deserialization",
cwe="CWE-502",
severity=Severity.HIGH,
title="Unsafe deserializer on input",
title_fa="بارگذاری ناامن شیء از ورودی",
message="pickle, marshal, or yaml.load is applied to data that is not a trusted constant.",
message_fa="pickle، marshal یا yaml.load روی داده غیرثابت اعمال شده است.",
references=["https://cwe.mitre.org/data/definitions/502.html"],
patch_hint="json.loads(text) for trusted JSON; do not unpickle request bodies",
),
"py-ssrf-url": RuleSpec(
rule_id="py-ssrf-url",
family="ssrf",
cwe="CWE-918",
severity=Severity.HIGH,
title="HTTP fetch of a caller-controlled URL",
title_fa="واکشی HTTP از URL کنترل‌شده",
message="urlopen or requests is called with a URL that is not a constant allowlisted host.",
message_fa="urlopen یا requests با URL غیرثابت صدا زده شده است.",
references=["https://cwe.mitre.org/data/definitions/918.html"],
patch_hint="parse the URL and allow only an explicit host list",
),
"py-xss-markup": RuleSpec(
rule_id="py-xss-markup",
family="xss-template",
cwe="CWE-79",
severity=Severity.MEDIUM,
title="Unescaped HTML built from input",
title_fa="HTML بدون escape از ورودی",
message="Markup or autoescape=False wraps text that is not a constant.",
message_fa="Markup یا autoescape=False متن غیرثابت را در HTML می‌گذارد.",
references=["https://cwe.mitre.org/data/definitions/79.html"],
patch_hint="html.escape(name) or a template with autoescape on",
),
"py-weak-password-hash": RuleSpec(
rule_id="py-weak-password-hash",
family="weak-crypto",
cwe="CWE-327",
severity=Severity.MEDIUM,
title="Weak hash used as a password digest",
title_fa="چکیده ضعیف برای گذرواژه",
message="MD5 or SHA1 is applied to a password-like value. This is not a password hasher.",
message_fa="MD5 یا SHA1 روی مقدار شبیه گذرواژه اعمال شده است.",
references=["https://cwe.mitre.org/data/definitions/327.html"],
patch_hint="use a password hasher such as hashlib.scrypt or a vetted library",
),
}
def rule_catalog() -> list[RuleSpec]:
return list(RULES.values())
def get_rule(rule_id: str) -> RuleSpec:
return RULES[rule_id]
def ruleset_meta() -> dict[str, str | int]:
return {
"ruleset_id": RULESET_ID,
"snapshot": RULESET_DATE,
"engine": "python-ast",
"semgrep_yaml": "rules/aria-codereview.v1.yml",
"n_rules": len(RULES),
}