Skip to content

Commit a9dd1df

Browse files
committed
fix: /dev/null redirect false positive, AST domain extractor @ bypass
- _bash_scanner: exclude /dev/null/zero/random/urandom/std*/tty from redirect CRITICAL detection (2>/dev/null is harmless). - _python_scanner: _extract_domain_from_url now strips userinfo:port same as _extract_url (fixes localhost:8080@evil.com AST bypass).
1 parent 9a11238 commit a9dd1df

2 files changed

Lines changed: 30 additions & 7 deletions

File tree

trpc_agent_sdk/tools/safety/_bash_scanner.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -438,13 +438,29 @@ def _check_rm(self, line_no: int, raw_line: str, tokens: List[str], args: List[s
438438
))
439439

440440
def _check_redirects(self, line_no: int, raw_line: str, tokens: List[str]) -> None:
441-
"""Detect write redirects to sensitive paths."""
442-
# Look for > or >> patterns
441+
"""Detect write redirects to sensitive paths.
442+
443+
Excludes harmless device files like ``/dev/null`` and ``/dev/zero``
444+
so that ``grep foo /etc/hosts 2>/dev/null`` is not falsely blocked.
445+
"""
446+
_SAFE_DEVS = frozenset({
447+
"/dev/null",
448+
"/dev/zero",
449+
"/dev/random",
450+
"/dev/urandom",
451+
"/dev/stdin",
452+
"/dev/stdout",
453+
"/dev/stderr",
454+
"/dev/fd",
455+
"/dev/tty",
456+
"/dev/pts",
457+
"/dev/console",
458+
})
443459
for i, t in enumerate(tokens):
444460
if t in (">", ">>", ">" + ">") and i + 1 < len(tokens):
445-
# Strip surrounding quotes so that "/etc/passwd" in
446-
# test expressions ([[ … ]]) is treated consistently.
447461
target = tokens[i + 1].strip("'\"")
462+
if target in _SAFE_DEVS:
463+
continue
448464
if _is_sensitive_path(target) or target.startswith("/dev/"):
449465
self._findings.append(
450466
BashScanFinding(
@@ -459,6 +475,8 @@ def _check_redirects(self, line_no: int, raw_line: str, tokens: List[str]) -> No
459475
if ">" in t and len(t) > 1:
460476
parts = t.split(">", 1)
461477
target = parts[1].strip().strip("'\"")
478+
if target in _SAFE_DEVS:
479+
continue
462480
if target and (_is_sensitive_path(target) or target.startswith("/dev/sd")):
463481
self._findings.append(
464482
BashScanFinding(

trpc_agent_sdk/tools/safety/_python_scanner.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -973,10 +973,15 @@ def _is_sensitive_env_key(key: str) -> bool:
973973

974974

975975
def _extract_domain_from_url(url: Optional[str]) -> Optional[str]:
976-
"""Extract domain from a URL string."""
976+
"""Extract bare hostname from a URL, stripping userinfo and port."""
977977
if not url:
978978
return None
979-
m = re.search(r"https?://([^\s/\"':]+)", url)
979+
m = re.search(r"https?://([^\s/\"']+)", url)
980980
if m:
981-
return m.group(1)
981+
host = m.group(1)
982+
if "@" in host:
983+
host = host.rsplit("@", 1)[-1]
984+
if ":" in host:
985+
host = host.rsplit(":", 1)[0]
986+
return host
982987
return None

0 commit comments

Comments
 (0)