File size: 1,635 Bytes
c8e832f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"""Example Python snippets for exercising the review environment."""

EXAMPLE_SNIPPETS = {
    "unsafe_eval": "\n".join(
        [
            "def load_settings(config_text):",
            "    return eval(config_text)",
        ]
    ),
    "mutable_default": "\n".join(
        [
            "def append_name(name, names=[]):",
            "    names.append(name)",
            "    return names",
        ]
    ),
    "bare_except": "\n".join(
        [
            "def publish_report(report):",
            "    try:",
            '        return report[\"summary\"]',
            "    except:",
            "        return None",
        ]
    ),
    "shell_injection": "\n".join(
        [
            "import subprocess",
            "",
            "def run_script(script_path, user_input):",
            '    cmd = f\"python {script_path} {user_input}\"',
            "    return subprocess.check_output(cmd, shell=True, text=True)",
        ]
    ),
    "syntax_error": "\n".join(
        [
            "def broken_function(",
            "    return 42",
        ]
    ),
    "clean_function": "\n".join(
        [
            "def normalize_name(name: str) -> str:",
            "    cleaned = name.strip().lower()",
            "    return cleaned.replace(\"  \", \" \")",
        ]
    ),
}


EXPECTED_RULE_IDS = {
    "unsafe_eval": {"avoid-eval"},
    "mutable_default": {"mutable-default-list"},
    "bare_except": {"bare-except"},
    "shell_injection": {"shell-true-command-injection"},
    "syntax_error": {"syntax-error"},
    "clean_function": set(),
}