timecs 2.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- app.py +3014 -0
- ast_scanner.py +36187 -0
- banner.py +146 -0
- ci_reporter.py +570 -0
- cli.py +2347 -0
- config_loader.py +307 -0
- cross_file_engine.py +1781 -0
- delivery_models.py +145 -0
- delivery_repository.py +78 -0
- diff_scan.py +127 -0
- durable_delivery_sink.py +103 -0
- event_bus.py +155 -0
- event_publisher.py +345 -0
- event_taxonomy.py +313 -0
- function_summarizer.py +354 -0
- html_auditor.py +274 -0
- iac_auditor.py +400 -0
- in_app_adapter.py +115 -0
- js_scanner.py +895 -0
- manifest_parser.py +960 -0
- master_rules_bank.py +389 -0
- notification_models.py +346 -0
- notification_policy.py +309 -0
- notification_repository.py +118 -0
- notification_routes.py +394 -0
- notification_service.py +199 -0
- osv_client.py +437 -0
- remediation/__init__.py +39 -0
- remediation/base.py +102 -0
- remediation/contracts.py +196 -0
- remediation/cwe1188_binding.py +78 -0
- remediation/cwe1336_jinja.py +91 -0
- remediation/cwe22_path_traversal.py +348 -0
- remediation/cwe295_ssl.py +87 -0
- remediation/cwe327_weak_hash.py +78 -0
- remediation/cwe377_tempfile.py +77 -0
- remediation/cwe489_debug.py +80 -0
- remediation/cwe502_yaml.py +89 -0
- remediation/cwe614_cookie.py +91 -0
- remediation/cwe78_cmdi.py +415 -0
- remediation/cwe798_credentials.py +153 -0
- remediation/cwe89_sqli.py +425 -0
- remediation/cwe916_password_hash.py +131 -0
- remediation/cwe95_eval.py +77 -0
- remediation/dispatcher.py +114 -0
- remediation/orchestrator.py +541 -0
- remediation/patch_engine.py +213 -0
- remediation/verification.py +253 -0
- rule_engine.py +1535 -0
- sarif_adapter.py +719 -0
- sarif_exporter.py +88 -0
- sca_reachability/__init__.py +28 -0
- sca_reachability/ast_analyzer.py +366 -0
- sca_reachability/call_graph.py +203 -0
- sca_reachability/contracts.py +365 -0
- sca_reachability/dist_import_map.py +46 -0
- sca_reachability/engine.py +603 -0
- sca_reachability/resolver.py +169 -0
- scanners/__init__.py +70 -0
- scanners/container_scanner.py +323 -0
- scanners/cve_index.py +521 -0
- scanners/iac_scanner.py +328 -0
- scanners/sbom.py +183 -0
- scanners/sca_scanner.py +667 -0
- scanners/secret_scanner.py +345 -0
- scanners/yaml_lite.py +217 -0
- secret_filters.py +435 -0
- secret_scanner.py +943 -0
- sqlite_delivery_repository.py +277 -0
- sqlite_notification_repository.py +441 -0
- ssrf_guard.py +276 -0
- staged_scanner.py +492 -0
- suppression_resolver.py +282 -0
- symbol_indexer.py +424 -0
- tcs/__init__.py +1 -0
- tcs/analysis/__init__.py +1 -0
- tcs/analysis/def_use.py +408 -0
- tcs/analysis/function_contracts.py +290 -0
- tcs/parallel_analyzer.py +276 -0
- tcs/parallel_scanner.py +630 -0
- tcs/treesitter_packer.py +1446 -0
- tcs_cli.py +2086 -0
- tcs_config.py +218 -0
- tcs_gui.py +2123 -0
- timecs-2.0.0.dist-info/METADATA +142 -0
- timecs-2.0.0.dist-info/RECORD +94 -0
- timecs-2.0.0.dist-info/WHEEL +5 -0
- timecs-2.0.0.dist-info/entry_points.txt +4 -0
- timecs-2.0.0.dist-info/licenses/LICENSE +201 -0
- timecs-2.0.0.dist-info/top_level.txt +49 -0
- version_matcher.py +934 -0
- webhook_adapter.py +495 -0
- webhook_client.py +483 -0
- webhook_formatters.py +245 -0
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"""
|
|
2
|
+
TimeCodeSecurity - Vector D CWE-502 Unsafe YAML Deserialization Transformer.
|
|
3
|
+
Implements deterministic AST-guided remediation replacing yaml.load() with yaml.safe_load().
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from typing import Any, Dict, Optional, Tuple
|
|
7
|
+
import ast
|
|
8
|
+
|
|
9
|
+
from remediation.base import BaseRemediationTransformer
|
|
10
|
+
from remediation.contracts import RemediationRule, PatchStatus
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class Cwe502YamlSafeLoaderTransformer(BaseRemediationTransformer):
|
|
14
|
+
"""
|
|
15
|
+
Deterministic AST transformer for CWE-502 (Unsafe Deserialization).
|
|
16
|
+
Replaces yaml.load(data) with yaml.safe_load(data) or adds Loader=yaml.SafeLoader.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
@property
|
|
20
|
+
def rule(self) -> RemediationRule:
|
|
21
|
+
return RemediationRule.YAML_SAFE_LOADER
|
|
22
|
+
|
|
23
|
+
def can_transform(
|
|
24
|
+
self,
|
|
25
|
+
tree: ast.AST,
|
|
26
|
+
finding: Dict[str, Any],
|
|
27
|
+
source_code: str
|
|
28
|
+
) -> bool:
|
|
29
|
+
"""Check if finding is a yaml.load() call without safe loader."""
|
|
30
|
+
line_number = finding.get("line_number", 0)
|
|
31
|
+
call_node = self._find_yaml_load_call(tree, line_number)
|
|
32
|
+
return call_node is not None
|
|
33
|
+
|
|
34
|
+
def transform(
|
|
35
|
+
self,
|
|
36
|
+
tree: ast.AST,
|
|
37
|
+
finding: Dict[str, Any],
|
|
38
|
+
source_code: str
|
|
39
|
+
) -> Tuple[PatchStatus, Optional[str], Optional[str], Tuple[str, ...]]:
|
|
40
|
+
"""Replace yaml.load() with yaml.safe_load() or add SafeLoader."""
|
|
41
|
+
line_number = finding.get("line_number", 0)
|
|
42
|
+
|
|
43
|
+
call_node = self._find_yaml_load_call(tree, line_number)
|
|
44
|
+
if not call_node:
|
|
45
|
+
return (PatchStatus.UNSUPPORTED_CWE, None, None, ("yaml.load call not found",))
|
|
46
|
+
|
|
47
|
+
# Check if already using SafeLoader
|
|
48
|
+
existing_keywords = {kw.arg: kw.value for kw in call_node.keywords if kw.arg}
|
|
49
|
+
if "Loader" in existing_keywords:
|
|
50
|
+
loader_value = existing_keywords["Loader"]
|
|
51
|
+
if isinstance(loader_value, ast.Attribute):
|
|
52
|
+
if loader_value.attr == "SafeLoader":
|
|
53
|
+
return (PatchStatus.SUCCESS, source_code, "", ("Already using SafeLoader",))
|
|
54
|
+
|
|
55
|
+
# Strategy: Replace function name from load to safe_load
|
|
56
|
+
if isinstance(call_node.func, ast.Attribute):
|
|
57
|
+
call_node.func.attr = "safe_load"
|
|
58
|
+
|
|
59
|
+
# Remove Loader keyword if present (not needed for safe_load)
|
|
60
|
+
call_node.keywords = [kw for kw in call_node.keywords if kw.arg != "Loader"]
|
|
61
|
+
else:
|
|
62
|
+
return (PatchStatus.UNSUPPORTED_CWE, None, None, ("Unexpected AST structure",))
|
|
63
|
+
|
|
64
|
+
try:
|
|
65
|
+
patched_source = ast.unparse(tree)
|
|
66
|
+
snippet = self.patched_statement_snippet(tree, source_code, line_number) or ""
|
|
67
|
+
|
|
68
|
+
return (PatchStatus.SUCCESS, patched_source, snippet, ())
|
|
69
|
+
except Exception as e:
|
|
70
|
+
return (PatchStatus.FAILED_SYNTAX, None, None, (f"AST unparse failed: {e}",))
|
|
71
|
+
|
|
72
|
+
def _find_yaml_load_call(
|
|
73
|
+
self,
|
|
74
|
+
tree: ast.AST,
|
|
75
|
+
line_number: int
|
|
76
|
+
) -> Optional[ast.Call]:
|
|
77
|
+
"""Locate yaml.load() call at or near the given line."""
|
|
78
|
+
for node in ast.walk(tree):
|
|
79
|
+
if isinstance(node, ast.Call):
|
|
80
|
+
func = node.func
|
|
81
|
+
if isinstance(func, ast.Attribute):
|
|
82
|
+
if func.attr == "load":
|
|
83
|
+
# Verify it's yaml.load
|
|
84
|
+
if isinstance(func.value, ast.Name) and func.value.id == "yaml":
|
|
85
|
+
node_lineno = getattr(node, "lineno", 0)
|
|
86
|
+
node_end = getattr(node, "end_lineno", node_lineno)
|
|
87
|
+
if node_lineno <= line_number <= node_end or node_lineno == line_number:
|
|
88
|
+
return node
|
|
89
|
+
return None
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"""
|
|
2
|
+
TimeCodeSecurity - Vector D CWE-614/CWE-1275 Cookie Security Transformer.
|
|
3
|
+
Implements deterministic AST-guided remediation for insecure cookie configurations:
|
|
4
|
+
Ensures response.set_cookie() calls include secure=True, httponly=True, samesite='Lax'.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from typing import Any, Dict, List, Optional, Tuple
|
|
8
|
+
import ast
|
|
9
|
+
|
|
10
|
+
from remediation.base import BaseRemediationTransformer
|
|
11
|
+
from remediation.contracts import RemediationRule, PatchStatus
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class Cwe614CookieSecureTransformer(BaseRemediationTransformer):
|
|
15
|
+
"""
|
|
16
|
+
Deterministic AST transformer for CWE-614/CWE-1275 (Insecure Cookie Flags).
|
|
17
|
+
Appends missing security attributes to set_cookie() calls.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
@property
|
|
21
|
+
def rule(self) -> RemediationRule:
|
|
22
|
+
return RemediationRule.COOKIE_SECURE_FLAGS
|
|
23
|
+
|
|
24
|
+
def can_transform(
|
|
25
|
+
self,
|
|
26
|
+
tree: ast.AST,
|
|
27
|
+
finding: Dict[str, Any],
|
|
28
|
+
source_code: str
|
|
29
|
+
) -> bool:
|
|
30
|
+
"""Check if the finding is a set_cookie call that can be safely patched."""
|
|
31
|
+
line_number = finding.get("line_number", 0)
|
|
32
|
+
call_node = self._find_set_cookie_call(tree, line_number)
|
|
33
|
+
return call_node is not None
|
|
34
|
+
|
|
35
|
+
def transform(
|
|
36
|
+
self,
|
|
37
|
+
tree: ast.AST,
|
|
38
|
+
finding: Dict[str, Any],
|
|
39
|
+
source_code: str
|
|
40
|
+
) -> Tuple[PatchStatus, Optional[str], Optional[str], Tuple[str, ...]]:
|
|
41
|
+
"""Add missing security flags to set_cookie() calls."""
|
|
42
|
+
line_number = finding.get("line_number", 0)
|
|
43
|
+
|
|
44
|
+
# Find the set_cookie call
|
|
45
|
+
call_node = self._find_set_cookie_call(tree, line_number)
|
|
46
|
+
if not call_node:
|
|
47
|
+
return (PatchStatus.UNSUPPORTED_CWE, None, None, ("set_cookie call not found",))
|
|
48
|
+
|
|
49
|
+
# Check which flags are missing
|
|
50
|
+
existing_keywords = {kw.arg: kw.value for kw in call_node.keywords if kw.arg}
|
|
51
|
+
|
|
52
|
+
missing_flags = []
|
|
53
|
+
if "secure" not in existing_keywords:
|
|
54
|
+
missing_flags.append(("secure", ast.Constant(value=True)))
|
|
55
|
+
if "httponly" not in existing_keywords:
|
|
56
|
+
missing_flags.append(("httponly", ast.Constant(value=True)))
|
|
57
|
+
if "samesite" not in existing_keywords:
|
|
58
|
+
missing_flags.append(("samesite", ast.Constant(value="Lax")))
|
|
59
|
+
|
|
60
|
+
if not missing_flags:
|
|
61
|
+
return (PatchStatus.SUCCESS, source_code, "", ("Cookie already has all security flags",))
|
|
62
|
+
|
|
63
|
+
# Add missing keyword arguments
|
|
64
|
+
for arg_name, arg_value in missing_flags:
|
|
65
|
+
new_keyword = ast.keyword(arg=arg_name, value=arg_value)
|
|
66
|
+
call_node.keywords.append(new_keyword)
|
|
67
|
+
|
|
68
|
+
# Regenerate source code
|
|
69
|
+
try:
|
|
70
|
+
patched_source = ast.unparse(tree)
|
|
71
|
+
snippet = self.patched_statement_snippet(tree, source_code, line_number) or ""
|
|
72
|
+
|
|
73
|
+
return (PatchStatus.SUCCESS, patched_source, snippet, ())
|
|
74
|
+
except Exception as e:
|
|
75
|
+
return (PatchStatus.FAILED_SYNTAX, None, None, (f"AST unparse failed: {e}",))
|
|
76
|
+
|
|
77
|
+
def _find_set_cookie_call(
|
|
78
|
+
self,
|
|
79
|
+
tree: ast.AST,
|
|
80
|
+
line_number: int
|
|
81
|
+
) -> Optional[ast.Call]:
|
|
82
|
+
"""Locate set_cookie() call at or near the given line number."""
|
|
83
|
+
for node in ast.walk(tree):
|
|
84
|
+
if isinstance(node, ast.Call):
|
|
85
|
+
func_attr = getattr(node.func, "attr", None)
|
|
86
|
+
if func_attr == "set_cookie":
|
|
87
|
+
node_lineno = getattr(node, "lineno", 0)
|
|
88
|
+
node_end = getattr(node, "end_lineno", node_lineno)
|
|
89
|
+
if node_lineno <= line_number <= node_end or node_lineno == line_number:
|
|
90
|
+
return node
|
|
91
|
+
return None
|
|
@@ -0,0 +1,415 @@
|
|
|
1
|
+
"""
|
|
2
|
+
TimeCodeSecurity - Vector D CWE-78 Command Injection Transformer.
|
|
3
|
+
Implements deterministic, AST-guided remediation for command injection flaws:
|
|
4
|
+
Transforms subprocess calls (e.g. call, run, Popen, check_output) with shell=True
|
|
5
|
+
into tokenized argument lists (cmd_list) with shell=False.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from typing import Any, Dict, List, Optional, Tuple, Union
|
|
9
|
+
import ast
|
|
10
|
+
import re
|
|
11
|
+
import shlex
|
|
12
|
+
|
|
13
|
+
from remediation.base import BaseRemediationTransformer
|
|
14
|
+
from remediation.contracts import RemediationRule, PatchStatus
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class Cwe78CmdInjectionTransformer(BaseRemediationTransformer):
|
|
18
|
+
"""
|
|
19
|
+
Deterministic AST transformer for CWE-78 (Command Injection).
|
|
20
|
+
Transforms shell=True string commands into structured argument lists with shell=False.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
@property
|
|
24
|
+
def rule(self) -> RemediationRule:
|
|
25
|
+
return RemediationRule.CMD_INJECTION_SPLIT
|
|
26
|
+
|
|
27
|
+
def _find_subprocess_call(
|
|
28
|
+
self,
|
|
29
|
+
tree: ast.AST,
|
|
30
|
+
line_number: int
|
|
31
|
+
) -> Optional[Tuple[ast.Call, ast.AST, Optional[ast.AST]]]:
|
|
32
|
+
"""
|
|
33
|
+
Locates the target subprocess call at or covering line_number.
|
|
34
|
+
Returns (call_node, direct_stmt, parent_scope) or None.
|
|
35
|
+
"""
|
|
36
|
+
parent_map: Dict[ast.AST, ast.AST] = {}
|
|
37
|
+
for parent in ast.walk(tree):
|
|
38
|
+
for child in ast.iter_child_nodes(parent):
|
|
39
|
+
parent_map[child] = parent
|
|
40
|
+
|
|
41
|
+
found_call: Optional[ast.Call] = None
|
|
42
|
+
for node in ast.walk(tree):
|
|
43
|
+
if isinstance(node, ast.Call):
|
|
44
|
+
func_attr = getattr(node.func, "attr", None)
|
|
45
|
+
func_id = getattr(node.func, "id", None)
|
|
46
|
+
is_subp = False
|
|
47
|
+
if func_attr in ("call", "run", "Popen", "check_output", "check_call"):
|
|
48
|
+
is_subp = True
|
|
49
|
+
elif func_id in ("call", "run", "Popen", "check_output", "check_call"):
|
|
50
|
+
is_subp = True
|
|
51
|
+
|
|
52
|
+
if is_subp:
|
|
53
|
+
node_lineno = getattr(node, "lineno", 0)
|
|
54
|
+
node_end = getattr(node, "end_lineno", node_lineno)
|
|
55
|
+
if node_lineno <= line_number <= node_end or node_lineno == line_number:
|
|
56
|
+
found_call = node
|
|
57
|
+
break
|
|
58
|
+
|
|
59
|
+
if not found_call:
|
|
60
|
+
return None
|
|
61
|
+
|
|
62
|
+
# Find direct enclosing statement
|
|
63
|
+
curr: ast.AST = found_call
|
|
64
|
+
while curr in parent_map and not isinstance(curr, ast.stmt):
|
|
65
|
+
curr = parent_map[curr]
|
|
66
|
+
|
|
67
|
+
direct_stmt = curr
|
|
68
|
+
scope = parent_map.get(direct_stmt)
|
|
69
|
+
return (found_call, direct_stmt, scope)
|
|
70
|
+
|
|
71
|
+
def _find_preceding_assignment(
|
|
72
|
+
self,
|
|
73
|
+
scope: ast.AST,
|
|
74
|
+
var_name: str,
|
|
75
|
+
before_stmt: ast.AST
|
|
76
|
+
) -> Optional[ast.Assign]:
|
|
77
|
+
"""
|
|
78
|
+
Finds the most recent assignment to var_name in scope preceding before_stmt.
|
|
79
|
+
"""
|
|
80
|
+
body = getattr(scope, "body", None)
|
|
81
|
+
if not isinstance(body, list):
|
|
82
|
+
return None
|
|
83
|
+
|
|
84
|
+
candidates = []
|
|
85
|
+
for stmt in body:
|
|
86
|
+
if stmt == before_stmt:
|
|
87
|
+
break
|
|
88
|
+
if isinstance(stmt, ast.Assign):
|
|
89
|
+
for target in stmt.targets:
|
|
90
|
+
if isinstance(target, ast.Name) and target.id == var_name:
|
|
91
|
+
candidates.append(stmt)
|
|
92
|
+
|
|
93
|
+
if candidates:
|
|
94
|
+
return candidates[-1]
|
|
95
|
+
return None
|
|
96
|
+
|
|
97
|
+
def _flatten_binop_add(self, node: ast.AST) -> List[ast.AST]:
|
|
98
|
+
"""Flattens a binary addition tree into a linear list of operands."""
|
|
99
|
+
if isinstance(node, ast.BinOp) and isinstance(node.op, ast.Add):
|
|
100
|
+
return self._flatten_binop_add(node.left) + self._flatten_binop_add(node.right)
|
|
101
|
+
return [node]
|
|
102
|
+
|
|
103
|
+
SHELL_BUILTINS = {
|
|
104
|
+
"cd", "source", "export", "alias", "eval", "exec", "exit",
|
|
105
|
+
"history", "read", "type", "ulimit", "umask", "unalias",
|
|
106
|
+
"builtin", "set", "unset", "shopt", "bind", "jobs", "fg", "bg"
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
def _tokenize_command_expr(
|
|
110
|
+
self,
|
|
111
|
+
expr_node: ast.AST
|
|
112
|
+
) -> Tuple[bool, Optional[List[Union[str, ast.AST]]], Tuple[str, ...]]:
|
|
113
|
+
"""
|
|
114
|
+
Tokenizes an AST command expression (JoinedStr, BinOp(Add), Constant) into a list
|
|
115
|
+
of literal token strings or dynamic AST expression nodes.
|
|
116
|
+
Rejects any command requiring shell semantics (pipelines, redirections, substitutions,
|
|
117
|
+
expansions, globs, or built-ins).
|
|
118
|
+
"""
|
|
119
|
+
template = ""
|
|
120
|
+
var_map: Dict[str, ast.AST] = {}
|
|
121
|
+
|
|
122
|
+
if isinstance(expr_node, ast.JoinedStr):
|
|
123
|
+
for val in expr_node.values:
|
|
124
|
+
if isinstance(val, ast.Constant) and isinstance(val.value, str):
|
|
125
|
+
template += val.value
|
|
126
|
+
elif isinstance(val, ast.FormattedValue):
|
|
127
|
+
ph = f"__TCS_VAR_{len(var_map)}__"
|
|
128
|
+
var_map[ph] = val.value
|
|
129
|
+
template += ph
|
|
130
|
+
else:
|
|
131
|
+
return (False, None, ("Unsupported construct inside command f-string",))
|
|
132
|
+
|
|
133
|
+
elif isinstance(expr_node, ast.BinOp) and isinstance(expr_node.op, ast.Add):
|
|
134
|
+
elts = self._flatten_binop_add(expr_node)
|
|
135
|
+
for elt in elts:
|
|
136
|
+
if isinstance(elt, ast.Constant) and isinstance(elt.value, str):
|
|
137
|
+
template += elt.value
|
|
138
|
+
else:
|
|
139
|
+
unwrapped = elt
|
|
140
|
+
if isinstance(elt, ast.Call) and isinstance(elt.func, ast.Name) and elt.func.id == "str":
|
|
141
|
+
if len(elt.args) == 1:
|
|
142
|
+
unwrapped = elt.args[0]
|
|
143
|
+
ph = f"__TCS_VAR_{len(var_map)}__"
|
|
144
|
+
var_map[ph] = unwrapped
|
|
145
|
+
template += ph
|
|
146
|
+
|
|
147
|
+
elif isinstance(expr_node, ast.Constant) and isinstance(expr_node.value, str):
|
|
148
|
+
template = expr_node.value
|
|
149
|
+
|
|
150
|
+
else:
|
|
151
|
+
return (False, None, (f"Unsupported command expression type: {type(expr_node).__name__}",))
|
|
152
|
+
|
|
153
|
+
try:
|
|
154
|
+
raw_tokens = shlex.split(template)
|
|
155
|
+
except ValueError as exc:
|
|
156
|
+
return (False, None, (f"Malformed quotes or escape sequence in command string: {exc}",))
|
|
157
|
+
|
|
158
|
+
if not raw_tokens:
|
|
159
|
+
return (False, None, ("Command expression produced no argument tokens",))
|
|
160
|
+
|
|
161
|
+
# Check for shell built-ins
|
|
162
|
+
first_token = raw_tokens[0]
|
|
163
|
+
if isinstance(first_token, str) and first_token.lower() in self.SHELL_BUILTINS:
|
|
164
|
+
return (
|
|
165
|
+
False,
|
|
166
|
+
None,
|
|
167
|
+
(f"Shell built-in '{first_token}' requires a shell parser and has no standalone binary under shell=False",)
|
|
168
|
+
)
|
|
169
|
+
|
|
170
|
+
# Check for unsupported shell semantics
|
|
171
|
+
for tok in raw_tokens:
|
|
172
|
+
# Pipelines, redirections, chaining
|
|
173
|
+
if any(c in tok for c in ("|", ">", "<", ";", "&")):
|
|
174
|
+
return (
|
|
175
|
+
False,
|
|
176
|
+
None,
|
|
177
|
+
("Shell pipelines (|), redirections (>, <), and command separators (;, &) are unsupported for automatic splitting",)
|
|
178
|
+
)
|
|
179
|
+
# Command substitution: $(...) or `...`
|
|
180
|
+
if "$(" in tok or "`" in tok:
|
|
181
|
+
return (
|
|
182
|
+
False,
|
|
183
|
+
None,
|
|
184
|
+
("Command substitution ($(cmd) or `cmd`) is unsupported for automatic splitting",)
|
|
185
|
+
)
|
|
186
|
+
# Shell variable expansion: $VAR, ${VAR}
|
|
187
|
+
if any(tok.startswith(f"${prefix}") or f"${prefix}" in tok for prefix in ("", "{", "1", "2", "3", "4", "5", "6", "7", "8", "9")) and "$" in tok:
|
|
188
|
+
return (
|
|
189
|
+
False,
|
|
190
|
+
None,
|
|
191
|
+
("Shell variable expansion ($VAR or ${VAR}) is unsupported for automatic splitting",)
|
|
192
|
+
)
|
|
193
|
+
# Glob-dependent semantics (* or ? when not a flag like -?)
|
|
194
|
+
if "*" in tok or (tok.startswith("?") or (len(tok) > 1 and not tok.startswith("-") and "?" in tok)):
|
|
195
|
+
return (
|
|
196
|
+
False,
|
|
197
|
+
None,
|
|
198
|
+
("Glob-dependent semantics (* or ?) require a shell parser and are unsupported under shell=False",)
|
|
199
|
+
)
|
|
200
|
+
|
|
201
|
+
tokens: List[Union[str, ast.AST]] = []
|
|
202
|
+
for tok in raw_tokens:
|
|
203
|
+
if tok in var_map:
|
|
204
|
+
tokens.append(var_map[tok])
|
|
205
|
+
elif any(k in tok for k in var_map):
|
|
206
|
+
pieces = re.split(r"(__TCS_VAR_\d+__)", tok)
|
|
207
|
+
values: List[Union[ast.Constant, ast.FormattedValue]] = []
|
|
208
|
+
for p in pieces:
|
|
209
|
+
if not p:
|
|
210
|
+
continue
|
|
211
|
+
if p in var_map:
|
|
212
|
+
val = var_map[p]
|
|
213
|
+
if isinstance(val, ast.AST):
|
|
214
|
+
values.append(ast.FormattedValue(value=val, conversion=-1))
|
|
215
|
+
else:
|
|
216
|
+
values.append(ast.Constant(value=str(val)))
|
|
217
|
+
else:
|
|
218
|
+
values.append(ast.Constant(value=p))
|
|
219
|
+
tokens.append(ast.JoinedStr(values=values))
|
|
220
|
+
else:
|
|
221
|
+
tokens.append(tok)
|
|
222
|
+
|
|
223
|
+
return (True, tokens, ())
|
|
224
|
+
|
|
225
|
+
def _format_token_list(self, tokens: List[Union[str, ast.AST]]) -> str:
|
|
226
|
+
"""Formats the list of tokens and expressions as a Python list literal string."""
|
|
227
|
+
formatted_elts = []
|
|
228
|
+
for t in tokens:
|
|
229
|
+
if isinstance(t, str):
|
|
230
|
+
formatted_elts.append(f'"{t}"')
|
|
231
|
+
else:
|
|
232
|
+
formatted_elts.append(ast.unparse(t))
|
|
233
|
+
return f"[{', '.join(formatted_elts)}]"
|
|
234
|
+
|
|
235
|
+
def can_transform(
|
|
236
|
+
self,
|
|
237
|
+
tree: ast.AST,
|
|
238
|
+
finding: Dict[str, Any],
|
|
239
|
+
source_code: str
|
|
240
|
+
) -> bool:
|
|
241
|
+
line_no = int(finding.get("line_number", 0))
|
|
242
|
+
match = self._find_subprocess_call(tree, line_no)
|
|
243
|
+
if not match:
|
|
244
|
+
return False
|
|
245
|
+
|
|
246
|
+
call_node, parent_stmt, parent_scope = match
|
|
247
|
+
|
|
248
|
+
# Must have shell=True keyword argument
|
|
249
|
+
shell_kw = [k for k in call_node.keywords if k.arg == "shell"]
|
|
250
|
+
if not shell_kw:
|
|
251
|
+
return False
|
|
252
|
+
if not (isinstance(shell_kw[0].value, ast.Constant) and shell_kw[0].value.value is True):
|
|
253
|
+
return False
|
|
254
|
+
|
|
255
|
+
if not call_node.args:
|
|
256
|
+
return False
|
|
257
|
+
|
|
258
|
+
cmd_arg = call_node.args[0]
|
|
259
|
+
if isinstance(cmd_arg, (ast.JoinedStr, ast.BinOp, ast.Constant)):
|
|
260
|
+
ok, _, _ = self._tokenize_command_expr(cmd_arg)
|
|
261
|
+
return ok
|
|
262
|
+
elif isinstance(cmd_arg, ast.Name):
|
|
263
|
+
if not parent_scope:
|
|
264
|
+
return False
|
|
265
|
+
assign = self._find_preceding_assignment(parent_scope, cmd_arg.id, parent_stmt)
|
|
266
|
+
if not assign:
|
|
267
|
+
return False
|
|
268
|
+
ok, _, _ = self._tokenize_command_expr(assign.value)
|
|
269
|
+
return ok
|
|
270
|
+
|
|
271
|
+
return False
|
|
272
|
+
|
|
273
|
+
def transform(
|
|
274
|
+
self,
|
|
275
|
+
tree: ast.AST,
|
|
276
|
+
finding: Dict[str, Any],
|
|
277
|
+
source_code: str
|
|
278
|
+
) -> Tuple[PatchStatus, Optional[str], Optional[str], Tuple[str, ...]]:
|
|
279
|
+
line_no = int(finding.get("line_number", 0))
|
|
280
|
+
match = self._find_subprocess_call(tree, line_no)
|
|
281
|
+
if not match:
|
|
282
|
+
return (
|
|
283
|
+
PatchStatus.UNSUPPORTED_CWE,
|
|
284
|
+
None,
|
|
285
|
+
None,
|
|
286
|
+
(f"Could not locate matching subprocess call at line {line_no}",)
|
|
287
|
+
)
|
|
288
|
+
|
|
289
|
+
call_node, parent_stmt, parent_scope = match
|
|
290
|
+
|
|
291
|
+
# Must have shell=True
|
|
292
|
+
shell_kw = [k for k in call_node.keywords if k.arg == "shell"]
|
|
293
|
+
if not shell_kw:
|
|
294
|
+
return (
|
|
295
|
+
PatchStatus.UNSUPPORTED_CWE,
|
|
296
|
+
None,
|
|
297
|
+
None,
|
|
298
|
+
("Subprocess call does not specify shell keyword argument",)
|
|
299
|
+
)
|
|
300
|
+
if not (isinstance(shell_kw[0].value, ast.Constant) and shell_kw[0].value.value is True):
|
|
301
|
+
return (
|
|
302
|
+
PatchStatus.UNSUPPORTED_CWE,
|
|
303
|
+
None,
|
|
304
|
+
None,
|
|
305
|
+
("Subprocess call shell keyword argument is not set to True",)
|
|
306
|
+
)
|
|
307
|
+
|
|
308
|
+
if not call_node.args:
|
|
309
|
+
return (
|
|
310
|
+
PatchStatus.UNSUPPORTED_CWE,
|
|
311
|
+
None,
|
|
312
|
+
None,
|
|
313
|
+
("Subprocess call missing command argument",)
|
|
314
|
+
)
|
|
315
|
+
|
|
316
|
+
cmd_arg = call_node.args[0]
|
|
317
|
+
lines = source_code.splitlines(keepends=True)
|
|
318
|
+
|
|
319
|
+
if isinstance(cmd_arg, ast.Name):
|
|
320
|
+
var_name = cmd_arg.id
|
|
321
|
+
if not parent_scope:
|
|
322
|
+
return (
|
|
323
|
+
PatchStatus.UNSUPPORTED_CWE,
|
|
324
|
+
None,
|
|
325
|
+
None,
|
|
326
|
+
(f"Cannot resolve scope for variable {var_name}",)
|
|
327
|
+
)
|
|
328
|
+
|
|
329
|
+
assign_stmt = self._find_preceding_assignment(parent_scope, var_name, parent_stmt)
|
|
330
|
+
if not assign_stmt:
|
|
331
|
+
return (
|
|
332
|
+
PatchStatus.UNSUPPORTED_CWE,
|
|
333
|
+
None,
|
|
334
|
+
None,
|
|
335
|
+
(f"Could not locate preceding assignment to {var_name}",)
|
|
336
|
+
)
|
|
337
|
+
|
|
338
|
+
ok, tokens, limitations = self._tokenize_command_expr(assign_stmt.value)
|
|
339
|
+
if not ok or tokens is None:
|
|
340
|
+
return (PatchStatus.UNSUPPORTED_CWE, None, None, limitations)
|
|
341
|
+
|
|
342
|
+
list_var_name = f"{var_name}_list" if not var_name.endswith("_list") else var_name
|
|
343
|
+
token_list_str = self._format_token_list(tokens)
|
|
344
|
+
new_assign = f"{list_var_name} = {token_list_str}"
|
|
345
|
+
|
|
346
|
+
# Replace call
|
|
347
|
+
# Reconstruct call with list_var_name and shell=False
|
|
348
|
+
func_expr = ast.unparse(call_node.func)
|
|
349
|
+
new_call = f"{func_expr}({list_var_name}, shell=False)"
|
|
350
|
+
|
|
351
|
+
# Check if call was part of a return or assignment
|
|
352
|
+
call_start = parent_stmt.lineno - 1
|
|
353
|
+
call_end = getattr(parent_stmt, "end_lineno", parent_stmt.lineno)
|
|
354
|
+
call_orig = lines[call_start]
|
|
355
|
+
call_indent = call_orig[:len(call_orig) - len(call_orig.lstrip())]
|
|
356
|
+
call_ending = "\r\n" if call_orig.endswith("\r\n") else "\n"
|
|
357
|
+
|
|
358
|
+
if isinstance(parent_stmt, ast.Return):
|
|
359
|
+
call_line_repl = f"{call_indent}return {new_call}{call_ending}"
|
|
360
|
+
elif isinstance(parent_stmt, ast.Assign):
|
|
361
|
+
targets_str = " = ".join(ast.unparse(t) for t in parent_stmt.targets)
|
|
362
|
+
call_line_repl = f"{call_indent}{targets_str} = {new_call}{call_ending}"
|
|
363
|
+
else:
|
|
364
|
+
call_line_repl = f"{call_indent}{new_call}{call_ending}"
|
|
365
|
+
|
|
366
|
+
# Replace assign statement
|
|
367
|
+
assign_start = assign_stmt.lineno - 1
|
|
368
|
+
assign_end = getattr(assign_stmt, "end_lineno", assign_stmt.lineno)
|
|
369
|
+
assign_orig = lines[assign_start]
|
|
370
|
+
assign_indent = assign_orig[:len(assign_orig) - len(assign_orig.lstrip())]
|
|
371
|
+
assign_ending = "\r\n" if assign_orig.endswith("\r\n") else "\n"
|
|
372
|
+
assign_line_repl = f"{assign_indent}{new_assign}{assign_ending}"
|
|
373
|
+
|
|
374
|
+
patched_lines = list(lines)
|
|
375
|
+
patched_lines[call_start:call_end] = [call_line_repl]
|
|
376
|
+
patched_lines[assign_start:assign_end] = [assign_line_repl]
|
|
377
|
+
|
|
378
|
+
patched_source = "".join(patched_lines)
|
|
379
|
+
patched_snippet = f"{new_assign}\n{new_call}"
|
|
380
|
+
return (PatchStatus.SUCCESS, patched_source, patched_snippet, ())
|
|
381
|
+
|
|
382
|
+
elif isinstance(cmd_arg, (ast.JoinedStr, ast.BinOp, ast.Constant)):
|
|
383
|
+
ok, tokens, limitations = self._tokenize_command_expr(cmd_arg)
|
|
384
|
+
if not ok or tokens is None:
|
|
385
|
+
return (PatchStatus.UNSUPPORTED_CWE, None, None, limitations)
|
|
386
|
+
|
|
387
|
+
token_list_str = self._format_token_list(tokens)
|
|
388
|
+
func_expr = ast.unparse(call_node.func)
|
|
389
|
+
new_call = f"{func_expr}({token_list_str}, shell=False)"
|
|
390
|
+
|
|
391
|
+
start_l = parent_stmt.lineno - 1
|
|
392
|
+
end_l = getattr(parent_stmt, "end_lineno", parent_stmt.lineno)
|
|
393
|
+
orig_line = lines[start_l]
|
|
394
|
+
indent = orig_line[:len(orig_line) - len(orig_line.lstrip())]
|
|
395
|
+
line_ending = "\r\n" if orig_line.endswith("\r\n") else "\n"
|
|
396
|
+
|
|
397
|
+
if isinstance(parent_stmt, ast.Return):
|
|
398
|
+
replacement_line = f"{indent}return {new_call}{line_ending}"
|
|
399
|
+
elif isinstance(parent_stmt, ast.Assign):
|
|
400
|
+
targets_str = " = ".join(ast.unparse(t) for t in parent_stmt.targets)
|
|
401
|
+
replacement_line = f"{indent}{targets_str} = {new_call}{line_ending}"
|
|
402
|
+
else:
|
|
403
|
+
replacement_line = f"{indent}{new_call}{line_ending}"
|
|
404
|
+
|
|
405
|
+
patched_lines = list(lines)
|
|
406
|
+
patched_lines[start_l:end_l] = [replacement_line]
|
|
407
|
+
patched_source = "".join(patched_lines)
|
|
408
|
+
return (PatchStatus.SUCCESS, patched_source, new_call, ())
|
|
409
|
+
|
|
410
|
+
return (
|
|
411
|
+
PatchStatus.UNSUPPORTED_CWE,
|
|
412
|
+
None,
|
|
413
|
+
None,
|
|
414
|
+
(f"Unsupported command argument type: {type(cmd_arg).__name__}",)
|
|
415
|
+
)
|