Skip to content

Commit cd9c406

Browse files
committed
fix(taint): align urllib opener sinks with existing resolution
Signed-off-by: Whj9283 <[email protected]>
1 parent d3dcd81 commit cd9c406

2 files changed

Lines changed: 84 additions & 192 deletions

File tree

‎src/skillspector/nodes/analyzers/behavioral_taint_tracking.py‎

Lines changed: 13 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
import ast
2626
import time
27-
from collections.abc import Callable, Iterator
27+
from collections.abc import Callable
2828
from dataclasses import dataclass, field
2929
from typing import NamedTuple
3030

@@ -111,8 +111,6 @@
111111
_CREDENTIAL_SOURCES | _FILE_READ_SOURCES | _NETWORK_INPUT_SOURCES | _USER_INPUT_SOURCES
112112
)
113113

114-
_INLINE_URLLIB_OPENER_SINK = "urllib.request.build_opener().open"
115-
116114
_NETWORK_OUTPUT_SINKS = frozenset(
117115
{
118116
"requests.post",
@@ -124,7 +122,7 @@
124122
"httpx.patch",
125123
"httpx.get",
126124
"urllib.request.urlopen",
127-
_INLINE_URLLIB_OPENER_SINK,
125+
"urllib.request.build_opener.open",
128126
"socket.socket.send",
129127
"socket.socket.sendall",
130128
"socket.socket.sendto",
@@ -288,108 +286,6 @@ def analyzer_exhausted(self) -> bool:
288286
]
289287

290288

291-
def _inline_urllib_opener_calls(
292-
tree: ast.Module, check_runtime: Callable[[], None] | None = None
293-
) -> set[ast.Call]:
294-
"""Recognize inline ``build_opener(...).open(...)`` with conservative bindings.
295-
296-
Only absolute, module-level imports preceding the call are accepted. Any
297-
rebinding of the import root anywhere in the file disqualifies it. This
298-
deliberately conservative gate does not infer stored opener objects or
299-
attempt closure/call-site binding analysis.
300-
"""
301-
imports: dict[str, list[tuple[str, tuple[int, int]]]] = {}
302-
module_imports: set[ast.AST] = set()
303-
for statement in tree.body:
304-
if check_runtime is not None:
305-
check_runtime()
306-
if isinstance(statement, ast.Import):
307-
bindings = [
308-
(
309-
item.asname or item.name.split(".")[0],
310-
item.name if item.asname else item.name.split(".")[0],
311-
)
312-
for item in statement.names
313-
]
314-
elif isinstance(statement, ast.ImportFrom):
315-
bindings = (
316-
[
317-
(item.asname or item.name, f"{statement.module}.{item.name}")
318-
for item in statement.names
319-
]
320-
if statement.level == 0
321-
else [(item.asname or item.name, "") for item in statement.names]
322-
)
323-
else:
324-
continue
325-
module_imports.add(statement)
326-
for local, canonical in bindings:
327-
imports.setdefault(local, []).append(
328-
(canonical, (statement.lineno, statement.col_offset))
329-
)
330-
331-
allowed = {"urllib", "urllib.request", "urllib.request.build_opener"}
332-
roots = {
333-
local: entries[0]
334-
for local, entries in imports.items()
335-
if entries[0][0] in allowed and all(entry[0] == entries[0][0] for entry in entries)
336-
}
337-
if not roots:
338-
return set()
339-
340-
invalid: set[str] = set()
341-
candidates: list[tuple[ast.Call, str]] = []
342-
for node in ast.walk(tree):
343-
if check_runtime is not None:
344-
check_runtime()
345-
bound: str | None = None
346-
if isinstance(node, (ast.Name, ast.Attribute)) and isinstance(
347-
node.ctx, (ast.Store, ast.Del)
348-
):
349-
name = resolve_dotted_name(node)
350-
bound = name.split(".")[0] if name else None
351-
elif isinstance(node, ast.arg):
352-
bound = node.arg
353-
elif isinstance(
354-
node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef, ast.ExceptHandler)
355-
):
356-
bound = node.name
357-
elif isinstance(node, (ast.MatchAs, ast.MatchStar)):
358-
bound = node.name
359-
elif isinstance(node, ast.MatchMapping):
360-
bound = node.rest
361-
elif isinstance(node, (ast.Import, ast.ImportFrom)):
362-
if any(item.name == "*" for item in node.names):
363-
invalid.update(roots)
364-
elif node not in module_imports:
365-
invalid.update(item.asname or item.name.split(".")[0] for item in node.names)
366-
if bound in roots:
367-
assert bound is not None
368-
invalid.add(bound)
369-
370-
if not isinstance(node, ast.Call) or not isinstance(node.func, ast.Attribute):
371-
continue
372-
if node.func.attr != "open" or not isinstance(node.func.value, ast.Call):
373-
continue
374-
factory = node.func.value
375-
name = resolve_dotted_name(factory.func)
376-
if name is None:
377-
continue
378-
root, _, suffix = name.partition(".")
379-
imported = roots.get(root)
380-
if imported is None:
381-
continue
382-
canonical, position = imported
383-
factory_name = f"{canonical}.{suffix}" if suffix else canonical
384-
if factory_name == "urllib.request.build_opener" and position < (
385-
factory.lineno,
386-
factory.col_offset,
387-
):
388-
candidates.append((node, root))
389-
390-
return {call for call, root in candidates if root not in invalid}
391-
392-
393289
def _resolve_sink_name(
394290
node: ast.Call,
395291
type_map: dict[str, str] | None = None,
@@ -402,6 +298,14 @@ def _resolve_sink_name(
402298
``importlib.import_module('subprocess').run(...)`` resolves to ``'subprocess.run'``
403299
and re-enters ``_EXEC_SINKS`` like the statically-imported form would.
404300
"""
301+
if (
302+
isinstance(node.func, ast.Attribute)
303+
and node.func.attr == "open"
304+
and isinstance(node.func.value, ast.Call)
305+
and resolve_call_name_typed(node.func.value, type_map, aliases)
306+
== "urllib.request.build_opener"
307+
):
308+
return "urllib.request.build_opener.open"
405309
name = resolve_call_name_typed(node, type_map, aliases)
406310
if name is None:
407311
name = resolve_dynamic_import_call(node, aliases)
@@ -473,28 +377,15 @@ def _find_source_in_expr(
473377
return None
474378

475379

476-
def _walk_call_payload(node: ast.Call, arguments_only: bool) -> Iterator[ast.AST]:
477-
"""Keep inline opener construction separate from data passed to ``open``."""
478-
if arguments_only:
479-
for argument in node.args:
480-
yield from ast.walk(argument)
481-
for keyword in node.keywords:
482-
yield from ast.walk(keyword.value)
483-
else:
484-
yield from ast.walk(node)
485-
486-
487380
def _find_nested_sources(
488381
node: ast.Call,
489382
type_map: dict[str, str] | None = None,
490383
aliases: dict[str, str] | None = None,
491384
check_runtime: Callable[[], None] | None = None,
492-
*,
493-
arguments_only: bool = False,
494385
) -> list[tuple[str, ast.Call]]:
495386
"""Walk children to find source calls nested inside a sink call."""
496387
results: list[tuple[str, ast.Call]] = []
497-
for child in _walk_call_payload(node, arguments_only):
388+
for child in ast.walk(node):
498389
if check_runtime is not None:
499390
check_runtime()
500391
if child is node:
@@ -511,13 +402,11 @@ def _find_tainted_names_in_args(
511402
node: ast.Call,
512403
tainted: dict[str, _TaintedVar],
513404
check_runtime: Callable[[], None] | None = None,
514-
*,
515-
arguments_only: bool = False,
516405
) -> list[_TaintedVar]:
517406
"""Find references to tainted variables in a call's arguments and keywords."""
518407
seen: set[str] = set()
519408
hits: list[_TaintedVar] = []
520-
for child in _walk_call_payload(node, arguments_only):
409+
for child in ast.walk(node):
521410
if check_runtime is not None:
522411
check_runtime()
523412
if child is node:
@@ -582,9 +471,6 @@ def _analyze_python(
582471

583472
aliases = python_ast.import_aliases
584473
type_map = build_type_map(tree, aliases)
585-
inline_openers = _inline_urllib_opener_calls(
586-
tree, budget.check_runtime if budget is not None else None
587-
)
588474
lines = python_ast.lines
589475
findings: list[AnalyzerFinding] = []
590476
tainted: dict[str, _TaintedVar] = {}
@@ -681,11 +567,7 @@ def _emit(
681567
if not isinstance(ast_node, ast.Call):
682568
continue
683569

684-
sink_name = (
685-
_INLINE_URLLIB_OPENER_SINK
686-
if ast_node in inline_openers
687-
else _resolve_sink_name(ast_node, type_map, aliases)
688-
)
570+
sink_name = _resolve_sink_name(ast_node, type_map, aliases)
689571
if not sink_name or sink_name not in _ALL_SINKS:
690572
continue
691573

@@ -697,7 +579,6 @@ def _emit(
697579
type_map,
698580
aliases,
699581
budget.check_runtime if budget is not None else None,
700-
arguments_only=ast_node in inline_openers,
701582
):
702583
if src_name == "open" and _is_open_for_write(src_node):
703584
continue
@@ -714,7 +595,6 @@ def _emit(
714595
ast_node,
715596
tainted,
716597
budget.check_runtime if budget is not None else None,
717-
arguments_only=ast_node in inline_openers,
718598
):
719599
rule = _pick_rule(tv.source_call, sink_name, is_direct=False)
720600
src_cat = _classify(tv.source_call, _SOURCE_CATEGORIES, "data source")

0 commit comments

Comments
 (0)