3030
3131from __future__ import annotations
3232
33+ import ast
3334import codecs
3435import functools
3536import io
3637import json
3738import os
3839import re
40+ import shlex
3941import sys
4042import time
4143import tomllib
@@ -1753,7 +1755,12 @@ def line_number(start: int) -> int:
17531755 file_type ,
17541756 line_starts ,
17551757 )
1756- if _is_safe_supply_chain_pattern (mt ):
1758+ data_consumer = _interpreter_reads_stdin_as_data (
1759+ content ,
1760+ match .start (),
1761+ line_starts ,
1762+ )
1763+ if _is_safe_supply_chain_pattern (mt ) or data_consumer :
17571764 adj = min (confidence , 0.15 )
17581765 sev = Severity .LOW
17591766 else :
@@ -1762,31 +1769,43 @@ def line_number(start: int) -> int:
17621769 finding_tags = list (tag )
17631770 if warned_internal_installer :
17641771 finding_tags .extend (["contextual-triage" , "explicit-risk-warning" ])
1772+ if data_consumer :
1773+ finding_tags .append ("data-only-stdin-consumer" )
1774+ if warned_internal_installer :
1775+ message = "Warned Pipe-to-Shell Installer"
1776+ remediation = (
1777+ "Keep the warning adjacent to this command. Prefer a checksum, signature, "
1778+ "or inspect-before-execute flow instead of piping fetched content directly "
1779+ "to a shell."
1780+ )
1781+ explanation = (
1782+ "The matched documentation explicitly warns that an internal installer "
1783+ "is fetched and piped directly to a shell. The warning provides context, "
1784+ "but the command still executes remote code without an inspection step."
1785+ )
1786+ elif data_consumer :
1787+ message = "Inline Program Reads Download as Data"
1788+ remediation = (
1789+ "Prefer structured data formats and validate the fetched content before "
1790+ "using it in a program."
1791+ )
1792+ explanation = (
1793+ "The fetched content is parsed as data by the interpreter program supplied "
1794+ "on the command line; the download is not executed as a script."
1795+ )
1796+ else :
1797+ message = "External Script Fetching"
1798+ remediation = None
1799+ explanation = None
17651800 findings .append (
17661801 AnalyzerFinding (
17671802 rule_id = "SC2" ,
1768- message = (
1769- "Warned Pipe-to-Shell Installer"
1770- if warned_internal_installer
1771- else "External Script Fetching"
1772- ),
1803+ message = message ,
17731804 severity = sev ,
17741805 location = loc (line_num ),
17751806 confidence = adj ,
1776- remediation = (
1777- "Keep the warning adjacent to this command. Prefer a checksum, signature, "
1778- "or inspect-before-execute flow instead of piping fetched content directly "
1779- "to a shell."
1780- if warned_internal_installer
1781- else None
1782- ),
1783- explanation = (
1784- "The matched documentation explicitly warns that an internal installer "
1785- "is fetched and piped directly to a shell. The warning provides context, "
1786- "but the command still executes remote code without an inspection step."
1787- if warned_internal_installer
1788- else None
1789- ),
1807+ remediation = remediation ,
1808+ explanation = explanation ,
17901809 tags = finding_tags ,
17911810 context = ctx (match .start ()),
17921811 matched_text = mt [:200 ],
@@ -1934,6 +1953,238 @@ def _is_safe_supply_chain_pattern(text: str) -> bool:
19341953 return _is_trusted_source (text ) or bool (_SAFE_INSTALL_PATTERN .search (text ))
19351954
19361955
1956+ # A fetched program supplied on the interpreter command line leaves the
1957+ # download as data on stdin. Every other shape stays HIGH.
1958+ _MAX_SC2_LOGICAL_LINE_CHARS = 4_096
1959+ _PYTHON_SAFE_CALLS = frozenset (
1960+ {
1961+ "bool" ,
1962+ "dict" ,
1963+ "float" ,
1964+ "int" ,
1965+ "json.dump" ,
1966+ "json.dumps" ,
1967+ "json.load" ,
1968+ "json.loads" ,
1969+ "len" ,
1970+ "list" ,
1971+ "print" ,
1972+ "str" ,
1973+ "sys.stderr.write" ,
1974+ "sys.stdin.buffer.read" ,
1975+ "sys.stdin.buffer.readline" ,
1976+ "sys.stdin.read" ,
1977+ "sys.stdin.readline" ,
1978+ "sys.stdout.write" ,
1979+ }
1980+ )
1981+ _PYTHON_SAFE_CALL_ROOTS = frozenset (name .split ("." , 1 )[0 ] for name in _PYTHON_SAFE_CALLS )
1982+ _PYTHON_SAFE_ATTRIBUTES = frozenset (
1983+ {
1984+ "buffer" ,
1985+ "dump" ,
1986+ "dumps" ,
1987+ "load" ,
1988+ "loads" ,
1989+ "read" ,
1990+ "readline" ,
1991+ "stderr" ,
1992+ "stdin" ,
1993+ "stdout" ,
1994+ "write" ,
1995+ }
1996+ )
1997+ _PYTHON_DANGEROUS = re .compile (
1998+ r"\b(?:exec|eval|compile|execfile|__import__|getattr|setattr|delattr|"
1999+ r"globals|locals|vars|open|input|system|popen|spawn\w*|runpy|pickle|"
2000+ r"marshal|dill|shelve|importlib|subprocess|os)\b|__"
2001+ )
2002+ _PYTHON_BINDING_NODES = (
2003+ ast .AsyncFunctionDef ,
2004+ ast .ClassDef ,
2005+ ast .FunctionDef ,
2006+ ast .Global ,
2007+ ast .Lambda ,
2008+ ast .Nonlocal ,
2009+ )
2010+
2011+
2012+ def _sc2_logical_command (
2013+ content : str ,
2014+ match_start : int ,
2015+ line_starts : tuple [int , ...],
2016+ ) -> str | None :
2017+ """Return the shell logical line containing *match_start*, if it starts it."""
2018+ line_index = bisect_right (line_starts , match_start ) - 1
2019+ if line_index < 0 :
2020+ return None
2021+ line_start = line_starts [line_index ]
2022+
2023+ # Walk back only across escaped shell newlines after locating the current
2024+ # logical line in O(log n). Any other logical break is not a shell
2025+ # command boundary and makes the exemption unsafe.
2026+ while line_index > 0 :
2027+ separator_start = (
2028+ line_start - 2
2029+ if line_start >= 2 and content [line_start - 2 : line_start ] == "\r \n "
2030+ else line_start - 1
2031+ )
2032+ separator = LOGICAL_LINE_BREAK .match (content , separator_start )
2033+ if separator is None :
2034+ return None
2035+ if separator .group (0 ) not in {"\n " , "\r \n " }:
2036+ return None
2037+ if separator_start == 0 or content [separator_start - 1 ] != "\\ " :
2038+ break
2039+ line_index -= 1
2040+ line_start = line_starts [line_index ]
2041+ if match_start - line_start > _MAX_SC2_LOGICAL_LINE_CHARS :
2042+ return None
2043+
2044+ if match_start - line_start > _MAX_SC2_LOGICAL_LINE_CHARS :
2045+ return None
2046+
2047+ line_end = len (content )
2048+ search_pos = match_start
2049+ search_end = min (len (content ), line_start + _MAX_SC2_LOGICAL_LINE_CHARS + 1 )
2050+ while search_pos <= len (content ):
2051+ separator = LOGICAL_LINE_BREAK .search (content , search_pos , search_end )
2052+ if separator is None :
2053+ if len (content ) - line_start > _MAX_SC2_LOGICAL_LINE_CHARS :
2054+ return None
2055+ break
2056+ if separator .group (0 ) not in {"\n " , "\r \n " }:
2057+ return None
2058+ if separator .start () > 0 and content [separator .start () - 1 ] == "\\ " :
2059+ search_pos = separator .end ()
2060+ if search_pos - line_start > _MAX_SC2_LOGICAL_LINE_CHARS :
2061+ return None
2062+ continue
2063+ line_end = separator .start ()
2064+ break
2065+
2066+ if line_end - line_start > _MAX_SC2_LOGICAL_LINE_CHARS :
2067+ return None
2068+
2069+ # Only lower a pipeline that begins the logical line. This rejects
2070+ # command substitutions, wrappers such as ``bash -c``, and ``eval``.
2071+ if content [line_start :match_start ].strip ():
2072+ return None
2073+ command = content [line_start :line_end ]
2074+ return re .sub (r"\\\r?\n" , " " , command )
2075+
2076+
2077+ def _shell_tokens (command : str ) -> list [str ] | None :
2078+ """Tokenize a conservative subset of shell syntax, or return None."""
2079+ if re .search (r"[`$]" , command ):
2080+ return None
2081+ lexer = shlex .shlex (command , posix = True , punctuation_chars = "|&;<>()" )
2082+ lexer .whitespace_split = True
2083+ lexer .commenters = ""
2084+ try :
2085+ return list (lexer )
2086+ except ValueError :
2087+ return None
2088+
2089+
2090+ def _dotted_name (node : ast .expr ) -> str | None :
2091+ """Return a dotted name for ``a.b.c`` expressions, otherwise None."""
2092+ parts : list [str ] = []
2093+ current = node
2094+ while isinstance (current , ast .Attribute ):
2095+ parts .append (current .attr )
2096+ current = current .value
2097+ if not isinstance (current , ast .Name ):
2098+ return None
2099+ parts .append (current .id )
2100+ return "." .join (reversed (parts ))
2101+
2102+
2103+ def _python_stdin_script_is_data_only (script : str ) -> bool :
2104+ """Return whether a Python ``-c`` script only treats stdin as data.
2105+
2106+ This is deliberately a structural allowlist rather than a name-based
2107+ denylist. Rebinding an allowlisted call root, any attribute or subscript
2108+ store, and any attribute outside the small data-processing allowlist makes
2109+ the script ambiguous and therefore ineligible for the SC2 downgrade.
2110+ """
2111+ if not script .strip () or len (script ) > _MAX_SC2_LOGICAL_LINE_CHARS :
2112+ return False
2113+ try :
2114+ tree = ast .parse (script , mode = "exec" )
2115+ except (SyntaxError , ValueError ):
2116+ return False
2117+
2118+ for node in ast .walk (tree ):
2119+ if isinstance (node , _PYTHON_BINDING_NODES ):
2120+ return False
2121+ if isinstance (node , ast .Import ):
2122+ if any (alias .name not in {"json" , "sys" } or alias .asname for alias in node .names ):
2123+ return False
2124+ elif isinstance (node , ast .ImportFrom ):
2125+ return False
2126+ elif isinstance (node , ast .Name ):
2127+ if isinstance (node .ctx , (ast .Store , ast .Del )):
2128+ if node .id in _PYTHON_SAFE_CALL_ROOTS or node .id .startswith ("__" ):
2129+ return False
2130+ elif _PYTHON_DANGEROUS .search (node .id ):
2131+ return False
2132+ elif isinstance (node , ast .Attribute ):
2133+ if node .attr not in _PYTHON_SAFE_ATTRIBUTES :
2134+ return False
2135+ if isinstance (node .ctx , (ast .Store , ast .Del )):
2136+ return False
2137+ elif isinstance (node , ast .Subscript ):
2138+ if isinstance (node .ctx , (ast .Store , ast .Del )):
2139+ return False
2140+ elif isinstance (node , ast .Call ):
2141+ name = _dotted_name (node .func )
2142+ if name is None or name not in _PYTHON_SAFE_CALLS :
2143+ return False
2144+ return True
2145+
2146+
2147+ def _interpreter_reads_stdin_as_data (
2148+ content : str ,
2149+ match_start : int ,
2150+ line_starts : tuple [int , ...],
2151+ ) -> bool :
2152+ """Return whether the piped-to interpreter receives a command-line program."""
2153+ command = _sc2_logical_command (content , match_start , line_starts )
2154+ if command is None :
2155+ return False
2156+ tokens = _shell_tokens (command )
2157+ if tokens is None :
2158+ return False
2159+ if any (
2160+ token in {"|&" , "||" , "&&" , ";" , "&" , "<" , ">" , ">>" , "<<" , "(" , ")" } for token in tokens
2161+ ):
2162+ return False
2163+ pipes = [index for index , token in enumerate (tokens ) if token == "|" ]
2164+ if len (pipes ) != 1 :
2165+ return False
2166+
2167+ pipe_index = pipes [0 ]
2168+ fetch = tokens [:pipe_index ]
2169+ tail = tokens [pipe_index + 1 :]
2170+ if not fetch or fetch [0 ].lower () not in {"curl" , "wget" }:
2171+ return False
2172+ if tail [:1 ] == ["sudo" ]:
2173+ tail = tail [1 :]
2174+ if not tail :
2175+ return False
2176+
2177+ interpreter = tail [0 ].lower ()
2178+ args = tail [1 :]
2179+ if interpreter in {"python" , "python3" }:
2180+ return args == ["-m" , "json.tool" ] or (
2181+ len (args ) == 2 and args [0 ] == "-c" and _python_stdin_script_is_data_only (args [1 ])
2182+ )
2183+ # Node remains HIGH: arbitrary JavaScript cannot be proven data-only by
2184+ # this lightweight allowlist (computed properties can recover Function).
2185+ return False
2186+
2187+
19372188# ---------------------------------------------------------------------------
19382189# SC4–SC6: Dependency-level analysis (runs per dependency file)
19392190# ---------------------------------------------------------------------------
0 commit comments