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
js_scanner.py
ADDED
|
@@ -0,0 +1,895 @@
|
|
|
1
|
+
"""
|
|
2
|
+
js_scanner.py – Tree-sitter–based JS/TS/React Security Scanner for TimeCodeSecurity.
|
|
3
|
+
|
|
4
|
+
Supports: .js, .jsx, .ts, .tsx, .mjs, .cjs
|
|
5
|
+
Detects:
|
|
6
|
+
- CWE-79: dangerouslySetInnerHTML, innerHTML, outerHTML assignments
|
|
7
|
+
- CWE-95: eval(), Function(), new Function(), setTimeout (string), setInterval (string)
|
|
8
|
+
|
|
9
|
+
Returns standardized TimeCodeSecurity finding dictionaries fully compatible with:
|
|
10
|
+
- SARIF adapter (sarif_adapter.py)
|
|
11
|
+
- CLI table formatter (tcs_cli.py format_table)
|
|
12
|
+
|
|
13
|
+
Design: Defensive / Graceful – if tree-sitter or language grammars are absent,
|
|
14
|
+
the module logs a single warning and falls back to an empty result set without
|
|
15
|
+
raising any unhandled exception.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
from __future__ import annotations
|
|
19
|
+
|
|
20
|
+
import os
|
|
21
|
+
import sys
|
|
22
|
+
import time
|
|
23
|
+
from pathlib import Path
|
|
24
|
+
from typing import Any, Dict, List, Optional
|
|
25
|
+
|
|
26
|
+
# ---------------------------------------------------------------------------
|
|
27
|
+
# Graceful import of tree-sitter & grammars (Invariant 3: defensive fallback)
|
|
28
|
+
# ---------------------------------------------------------------------------
|
|
29
|
+
_TS_AVAILABLE = False
|
|
30
|
+
_TSX_LANGUAGE = None
|
|
31
|
+
_TS_LANGUAGE = None
|
|
32
|
+
_JS_LANGUAGE = None
|
|
33
|
+
_PARSER_TSX = None
|
|
34
|
+
_PARSER_TS = None
|
|
35
|
+
_PARSER_JS = None
|
|
36
|
+
_TS_IMPORT_ERROR = None
|
|
37
|
+
|
|
38
|
+
try:
|
|
39
|
+
from tree_sitter import Language, Parser, Query, QueryCursor # type: ignore
|
|
40
|
+
import tree_sitter_javascript as _tsj # type: ignore
|
|
41
|
+
import tree_sitter_typescript as _tst # type: ignore
|
|
42
|
+
|
|
43
|
+
_TSX_LANGUAGE = Language(_tst.language_tsx())
|
|
44
|
+
_TS_LANGUAGE = Language(_tst.language_typescript())
|
|
45
|
+
_JS_LANGUAGE = Language(_tsj.language())
|
|
46
|
+
_PARSER_TSX = Parser(_TSX_LANGUAGE)
|
|
47
|
+
_PARSER_TS = Parser(_TS_LANGUAGE)
|
|
48
|
+
_PARSER_JS = Parser(_JS_LANGUAGE)
|
|
49
|
+
_TS_AVAILABLE = True
|
|
50
|
+
except Exception as _e:
|
|
51
|
+
# Deferred: printing here warned about a missing grammar even when the scan was pure Python
|
|
52
|
+
# and `--include-js` was never asked for. `warn_js_ts_unavailable()` emits it on demand.
|
|
53
|
+
_TS_IMPORT_ERROR = _e
|
|
54
|
+
|
|
55
|
+
# ---------------------------------------------------------------------------
|
|
56
|
+
# Constants
|
|
57
|
+
# ---------------------------------------------------------------------------
|
|
58
|
+
JS_TS_EXTENSIONS = frozenset({".js", ".jsx", ".ts", ".tsx", ".mjs", ".cjs"})
|
|
59
|
+
|
|
60
|
+
# Directories to skip during file discovery (mirrors tcs_cli.py IGNORED_DIRS)
|
|
61
|
+
_SKIP_DIRS = frozenset({
|
|
62
|
+
"node_modules", ".next", "dist", "build", "coverage",
|
|
63
|
+
".git", ".github", "__pycache__", ".venv", "venv", "env",
|
|
64
|
+
".pytest_cache", ".mypy_cache", ".tox", ".idea", ".vscode",
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
# ---------------------------------------------------------------------------
|
|
68
|
+
# Pre-compiled Tree-sitter Queries (only built when grammars are available)
|
|
69
|
+
# ---------------------------------------------------------------------------
|
|
70
|
+
# Query: direct eval() or Function() call_expression / new_expression
|
|
71
|
+
_EVAL_FN_QUERY: Optional[Any] = None
|
|
72
|
+
# Query: JSX attribute dangerouslySetInnerHTML
|
|
73
|
+
_JSX_DSIHTML_QUERY: Optional[Any] = None
|
|
74
|
+
# Query: object pair with dangerouslySetInnerHTML key
|
|
75
|
+
_OBJ_DSIHTML_QUERY: Optional[Any] = None
|
|
76
|
+
# Query: assignment to .innerHTML / .outerHTML (x.innerHTML = ...)
|
|
77
|
+
_INNERHTML_QUERY: Optional[Any] = None
|
|
78
|
+
# Query: directives for context tagging only
|
|
79
|
+
_DIRECTIVE_QUERY: Optional[Any] = None
|
|
80
|
+
|
|
81
|
+
if _TS_AVAILABLE:
|
|
82
|
+
# We build queries for TSX (superset), reuse language reference where needed.
|
|
83
|
+
_LANG = _TSX_LANGUAGE
|
|
84
|
+
|
|
85
|
+
# (b) eval / Function / setTimeout / setInterval as identifiers in call position
|
|
86
|
+
_EVAL_FN_QUERY = Query(
|
|
87
|
+
_LANG,
|
|
88
|
+
"""
|
|
89
|
+
(call_expression
|
|
90
|
+
function: (identifier) @fn_id
|
|
91
|
+
arguments: (arguments) @fn_args)
|
|
92
|
+
(new_expression
|
|
93
|
+
constructor: (identifier) @new_id
|
|
94
|
+
arguments: (arguments) @new_args)
|
|
95
|
+
""",
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
# (a) dangerouslySetInnerHTML JSX attribute
|
|
99
|
+
_JSX_DSIHTML_QUERY = Query(
|
|
100
|
+
_LANG,
|
|
101
|
+
"""
|
|
102
|
+
(jsx_attribute
|
|
103
|
+
(property_identifier) @jsx_attr_name
|
|
104
|
+
(_) @jsx_attr_value)
|
|
105
|
+
""",
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
# (a2) object property pair with dangerouslySetInnerHTML
|
|
109
|
+
_OBJ_DSIHTML_QUERY = Query(
|
|
110
|
+
_LANG,
|
|
111
|
+
"""
|
|
112
|
+
(pair
|
|
113
|
+
key: (property_identifier) @prop_key
|
|
114
|
+
value: (_) @prop_val)
|
|
115
|
+
""",
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
# (c) member expression assignment: foo.innerHTML = ..., foo.outerHTML = ...
|
|
119
|
+
_INNERHTML_QUERY = Query(
|
|
120
|
+
_LANG,
|
|
121
|
+
"""
|
|
122
|
+
(assignment_expression
|
|
123
|
+
left: (member_expression
|
|
124
|
+
property: (property_identifier) @prop_name)
|
|
125
|
+
right: (_) @rhs)
|
|
126
|
+
""",
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
# directive detection
|
|
130
|
+
_DIRECTIVE_QUERY = Query(
|
|
131
|
+
_LANG,
|
|
132
|
+
"""
|
|
133
|
+
(expression_statement
|
|
134
|
+
(string) @dir_str)
|
|
135
|
+
""",
|
|
136
|
+
)
|
|
137
|
+
|
|
138
|
+
_EVAL_SINKS: frozenset[str] = frozenset({"eval", "Function", "setTimeout", "setInterval"})
|
|
139
|
+
_INNERHTML_SINKS: frozenset[str] = frozenset({"innerHTML", "outerHTML", "document.write", "document.writeln"})
|
|
140
|
+
_JSX_XSS_SINKS: frozenset[str] = frozenset({"dangerouslySetInnerHTML"})
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
# ---------------------------------------------------------------------------
|
|
144
|
+
# Internal helpers
|
|
145
|
+
# ---------------------------------------------------------------------------
|
|
146
|
+
def _get_parser_and_language(ext: str):
|
|
147
|
+
"""Return (parser, language) for the given file extension."""
|
|
148
|
+
if ext in (".tsx", ".jsx"):
|
|
149
|
+
return _PARSER_TSX, _TSX_LANGUAGE
|
|
150
|
+
if ext in (".ts",):
|
|
151
|
+
return _PARSER_TS, _TS_LANGUAGE
|
|
152
|
+
return _PARSER_JS, _JS_LANGUAGE
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def _safe_query_matches(query, cursor_cls, root_node) -> List:
|
|
156
|
+
"""Run query safely, return list of (pattern_idx, {capture_name: [nodes]}) tuples."""
|
|
157
|
+
try:
|
|
158
|
+
cursor = cursor_cls(query)
|
|
159
|
+
return cursor.matches(root_node)
|
|
160
|
+
except Exception:
|
|
161
|
+
return []
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
def _snippet(source_bytes: bytes, node, max_chars: int = 200) -> str:
|
|
165
|
+
"""Extract and truncate source text for a node."""
|
|
166
|
+
try:
|
|
167
|
+
text = source_bytes[node.start_byte: node.end_byte].decode("utf-8", errors="replace")
|
|
168
|
+
text = text.replace("\n", " ").strip()
|
|
169
|
+
return text[:max_chars] + ("…" if len(text) > max_chars else "")
|
|
170
|
+
except Exception:
|
|
171
|
+
return ""
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def _row_col_1indexed(node) -> tuple[int, int]:
|
|
175
|
+
"""Convert tree-sitter 0-indexed point to 1-indexed (line, col)."""
|
|
176
|
+
# node.start_point is a namedtuple-like: (row, column) – both 0-indexed
|
|
177
|
+
return node.start_point.row + 1, node.start_point.column + 1
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
# ---------------------------------------------------------------------------
|
|
181
|
+
# Core Scanner Class
|
|
182
|
+
# ---------------------------------------------------------------------------
|
|
183
|
+
class JsTsScanner:
|
|
184
|
+
"""
|
|
185
|
+
Tree-sitter–based JS/TS/React static security scanner.
|
|
186
|
+
|
|
187
|
+
Usage::
|
|
188
|
+
|
|
189
|
+
scanner = JsTsScanner()
|
|
190
|
+
findings = scanner.scan_directory(Path("apps/web"), base_dir=Path.cwd())
|
|
191
|
+
"""
|
|
192
|
+
|
|
193
|
+
def __init__(self) -> None:
|
|
194
|
+
self.available = _TS_AVAILABLE
|
|
195
|
+
|
|
196
|
+
# ------------------------------------------------------------------
|
|
197
|
+
# Public API
|
|
198
|
+
# ------------------------------------------------------------------
|
|
199
|
+
def scan_directory(
|
|
200
|
+
self,
|
|
201
|
+
target_path: Path,
|
|
202
|
+
base_dir: Optional[Path] = None,
|
|
203
|
+
skipped_files: Optional[List[str]] = None,
|
|
204
|
+
) -> List[Dict[str, Any]]:
|
|
205
|
+
"""
|
|
206
|
+
Recursively scan a directory (or single file) for JS/TS security issues.
|
|
207
|
+
|
|
208
|
+
Parameters
|
|
209
|
+
----------
|
|
210
|
+
target_path:
|
|
211
|
+
Resolved path to scan (file or directory).
|
|
212
|
+
base_dir:
|
|
213
|
+
Project root used for building relative paths (defaults to target_path
|
|
214
|
+
if it's a directory, else its parent).
|
|
215
|
+
skipped_files:
|
|
216
|
+
Optional list to append skip-warning strings to.
|
|
217
|
+
|
|
218
|
+
Returns
|
|
219
|
+
-------
|
|
220
|
+
List of standardized TimeCodeSecurity finding dicts.
|
|
221
|
+
"""
|
|
222
|
+
if not self.available:
|
|
223
|
+
return []
|
|
224
|
+
|
|
225
|
+
if base_dir is None:
|
|
226
|
+
base_dir = target_path if target_path.is_dir() else target_path.parent
|
|
227
|
+
|
|
228
|
+
js_files = self._discover_js_files(target_path, skipped_files)
|
|
229
|
+
findings: List[Dict[str, Any]] = []
|
|
230
|
+
counter = 1
|
|
231
|
+
|
|
232
|
+
for file_path in js_files:
|
|
233
|
+
try:
|
|
234
|
+
rel_path = file_path.relative_to(target_path if target_path.is_dir() else target_path.parent).as_posix()
|
|
235
|
+
except ValueError:
|
|
236
|
+
try:
|
|
237
|
+
rel_path = file_path.relative_to(base_dir).as_posix()
|
|
238
|
+
except ValueError:
|
|
239
|
+
rel_path = file_path.name
|
|
240
|
+
|
|
241
|
+
file_findings = self._scan_file(file_path, rel_path, counter)
|
|
242
|
+
for ff in file_findings:
|
|
243
|
+
ff["id"] = f"TimeCodeSecurity-JS-{counter:03d}"
|
|
244
|
+
counter += 1
|
|
245
|
+
findings.extend(file_findings)
|
|
246
|
+
|
|
247
|
+
def scan_file_content(
|
|
248
|
+
self,
|
|
249
|
+
code: str,
|
|
250
|
+
filename: str = "editor_snippet.tsx",
|
|
251
|
+
start_counter: int = 1,
|
|
252
|
+
) -> List[Dict[str, Any]]:
|
|
253
|
+
"""Parse source code in-memory and extract security findings."""
|
|
254
|
+
if not self.available:
|
|
255
|
+
return []
|
|
256
|
+
ext = Path(filename).suffix.lower() or ".tsx"
|
|
257
|
+
parser, language = _get_parser_and_language(ext)
|
|
258
|
+
if parser is None or language is None:
|
|
259
|
+
return []
|
|
260
|
+
source_bytes = code.encode("utf-8")
|
|
261
|
+
try:
|
|
262
|
+
tree = parser.parse(source_bytes)
|
|
263
|
+
except Exception:
|
|
264
|
+
return []
|
|
265
|
+
root = tree.root_node
|
|
266
|
+
findings: List[Dict[str, Any]] = []
|
|
267
|
+
if language is _JS_LANGUAGE:
|
|
268
|
+
findings.extend(self._extract_eval_sinks_js(root, source_bytes, filename, language))
|
|
269
|
+
findings.extend(self._extract_innerhtml_js(root, source_bytes, filename, language))
|
|
270
|
+
else:
|
|
271
|
+
findings.extend(self._extract_eval_sinks(root, source_bytes, filename))
|
|
272
|
+
findings.extend(self._extract_jsx_xss(root, source_bytes, filename))
|
|
273
|
+
findings.extend(self._extract_innerhtml(root, source_bytes, filename))
|
|
274
|
+
for i, ff in enumerate(findings):
|
|
275
|
+
ff["id"] = f"TimeCodeSecurity-JS-{start_counter + i:03d}"
|
|
276
|
+
return findings
|
|
277
|
+
|
|
278
|
+
def scan_files(
|
|
279
|
+
self,
|
|
280
|
+
file_paths: List[Path],
|
|
281
|
+
base_dir: Path,
|
|
282
|
+
) -> List[Dict[str, Any]]:
|
|
283
|
+
"""
|
|
284
|
+
Scan an explicit list of JS/TS file Paths (used when files are pre-discovered
|
|
285
|
+
by the CLI).
|
|
286
|
+
"""
|
|
287
|
+
if not self.available:
|
|
288
|
+
return []
|
|
289
|
+
|
|
290
|
+
findings: List[Dict[str, Any]] = []
|
|
291
|
+
counter = 1
|
|
292
|
+
for file_path in file_paths:
|
|
293
|
+
if file_path.suffix.lower() not in JS_TS_EXTENSIONS:
|
|
294
|
+
continue
|
|
295
|
+
try:
|
|
296
|
+
rel_path = file_path.relative_to(base_dir).as_posix()
|
|
297
|
+
except ValueError:
|
|
298
|
+
rel_path = file_path.name
|
|
299
|
+
|
|
300
|
+
file_findings = self._scan_file(file_path, rel_path, counter)
|
|
301
|
+
for ff in file_findings:
|
|
302
|
+
ff["id"] = f"TimeCodeSecurity-JS-{counter:03d}"
|
|
303
|
+
counter += 1
|
|
304
|
+
findings.extend(file_findings)
|
|
305
|
+
|
|
306
|
+
return findings
|
|
307
|
+
|
|
308
|
+
# ------------------------------------------------------------------
|
|
309
|
+
# Internal helpers
|
|
310
|
+
# ------------------------------------------------------------------
|
|
311
|
+
def _discover_js_files(
|
|
312
|
+
self,
|
|
313
|
+
target_path: Path,
|
|
314
|
+
skipped_files: Optional[List[str]],
|
|
315
|
+
) -> List[Path]:
|
|
316
|
+
"""Recursively collect JS/TS files, honoring skip directories."""
|
|
317
|
+
if target_path.is_file():
|
|
318
|
+
if target_path.suffix.lower() in JS_TS_EXTENSIONS:
|
|
319
|
+
return [target_path]
|
|
320
|
+
return []
|
|
321
|
+
|
|
322
|
+
discovered: List[Path] = []
|
|
323
|
+
visited_dirs: set[str] = set()
|
|
324
|
+
|
|
325
|
+
for root, dirs, files in os.walk(target_path, followlinks=False):
|
|
326
|
+
real_root = os.path.realpath(root)
|
|
327
|
+
if real_root in visited_dirs:
|
|
328
|
+
dirs[:] = []
|
|
329
|
+
continue
|
|
330
|
+
visited_dirs.add(real_root)
|
|
331
|
+
|
|
332
|
+
# Prune ignored dirs and symlinks
|
|
333
|
+
dirs[:] = [
|
|
334
|
+
d for d in dirs
|
|
335
|
+
if d not in _SKIP_DIRS
|
|
336
|
+
and not d.startswith(".")
|
|
337
|
+
and not os.path.islink(os.path.join(root, d))
|
|
338
|
+
]
|
|
339
|
+
|
|
340
|
+
for fname in files:
|
|
341
|
+
p = Path(root) / fname
|
|
342
|
+
if p.suffix.lower() in JS_TS_EXTENSIONS:
|
|
343
|
+
# Guard: skip oversized or binary files
|
|
344
|
+
try:
|
|
345
|
+
size = p.stat().st_size
|
|
346
|
+
if size > 5 * 1024 * 1024:
|
|
347
|
+
msg = f"Skipping JS/TS file exceeding size limit (5MB): {p}"
|
|
348
|
+
if skipped_files is not None:
|
|
349
|
+
skipped_files.append(msg)
|
|
350
|
+
continue
|
|
351
|
+
with open(p, "rb") as fh:
|
|
352
|
+
chunk = fh.read(8192)
|
|
353
|
+
if b"\x00" in chunk:
|
|
354
|
+
continue # binary
|
|
355
|
+
except OSError:
|
|
356
|
+
continue
|
|
357
|
+
discovered.append(p)
|
|
358
|
+
|
|
359
|
+
discovered.sort()
|
|
360
|
+
return discovered
|
|
361
|
+
|
|
362
|
+
def _scan_file(
|
|
363
|
+
self,
|
|
364
|
+
file_path: Path,
|
|
365
|
+
rel_path: str,
|
|
366
|
+
start_counter: int,
|
|
367
|
+
) -> List[Dict[str, Any]]:
|
|
368
|
+
"""Parse a single JS/TS file and extract security findings."""
|
|
369
|
+
ext = file_path.suffix.lower()
|
|
370
|
+
parser, language = _get_parser_and_language(ext)
|
|
371
|
+
if parser is None or language is None:
|
|
372
|
+
return []
|
|
373
|
+
|
|
374
|
+
try:
|
|
375
|
+
source_bytes = file_path.read_bytes()
|
|
376
|
+
except OSError:
|
|
377
|
+
return []
|
|
378
|
+
|
|
379
|
+
# Determine if we need to re-build queries for JS (non-TSX) language
|
|
380
|
+
# For JS files, re-use TSX queries but run them against JS parser output.
|
|
381
|
+
# Tree-sitter queries are language-specific; for JS we must build per-language queries.
|
|
382
|
+
try:
|
|
383
|
+
tree = parser.parse(source_bytes)
|
|
384
|
+
except Exception:
|
|
385
|
+
return []
|
|
386
|
+
|
|
387
|
+
root = tree.root_node
|
|
388
|
+
findings: List[Dict[str, Any]] = []
|
|
389
|
+
|
|
390
|
+
# Determine which query-set to use based on language
|
|
391
|
+
if language is _JS_LANGUAGE:
|
|
392
|
+
findings.extend(self._extract_eval_sinks_js(root, source_bytes, rel_path, language))
|
|
393
|
+
findings.extend(self._extract_innerhtml_js(root, source_bytes, rel_path, language))
|
|
394
|
+
else:
|
|
395
|
+
# TSX / TS
|
|
396
|
+
findings.extend(self._extract_eval_sinks(root, source_bytes, rel_path))
|
|
397
|
+
findings.extend(self._extract_jsx_xss(root, source_bytes, rel_path))
|
|
398
|
+
findings.extend(self._extract_innerhtml(root, source_bytes, rel_path))
|
|
399
|
+
|
|
400
|
+
return findings
|
|
401
|
+
|
|
402
|
+
# ------------------------------------------------------------------
|
|
403
|
+
# TSX / TS extractors
|
|
404
|
+
# ------------------------------------------------------------------
|
|
405
|
+
def _extract_eval_sinks(
|
|
406
|
+
self, root, source_bytes: bytes, rel_path: str
|
|
407
|
+
) -> List[Dict[str, Any]]:
|
|
408
|
+
"""Detect eval(), Function(), new Function(), setTimeout, setInterval in TSX/TS."""
|
|
409
|
+
findings: List[Dict[str, Any]] = []
|
|
410
|
+
if _EVAL_FN_QUERY is None:
|
|
411
|
+
return findings
|
|
412
|
+
|
|
413
|
+
for _, match_dict in _safe_query_matches(_EVAL_FN_QUERY, QueryCursor, root):
|
|
414
|
+
# call_expression pattern
|
|
415
|
+
if "fn_id" in match_dict:
|
|
416
|
+
for fn_node in match_dict["fn_id"]:
|
|
417
|
+
name = (fn_node.text or b"").decode("utf-8", errors="replace")
|
|
418
|
+
if name not in _EVAL_SINKS:
|
|
419
|
+
continue
|
|
420
|
+
args_nodes = match_dict.get("fn_args", [])
|
|
421
|
+
args_node = args_nodes[0] if args_nodes else None
|
|
422
|
+
line, col = _row_col_1indexed(fn_node)
|
|
423
|
+
findings.append(self._make_finding(
|
|
424
|
+
rel_path=rel_path,
|
|
425
|
+
line=line,
|
|
426
|
+
col=col,
|
|
427
|
+
cwe="CWE-95",
|
|
428
|
+
severity="HIGH",
|
|
429
|
+
sink=name,
|
|
430
|
+
kind="call_expression",
|
|
431
|
+
source_bytes=source_bytes,
|
|
432
|
+
sink_node=fn_node,
|
|
433
|
+
args_node=args_node,
|
|
434
|
+
))
|
|
435
|
+
|
|
436
|
+
# new_expression pattern
|
|
437
|
+
if "new_id" in match_dict:
|
|
438
|
+
for fn_node in match_dict["new_id"]:
|
|
439
|
+
name = (fn_node.text or b"").decode("utf-8", errors="replace")
|
|
440
|
+
if name != "Function":
|
|
441
|
+
continue
|
|
442
|
+
args_nodes = match_dict.get("new_args", [])
|
|
443
|
+
args_node = args_nodes[0] if args_nodes else None
|
|
444
|
+
line, col = _row_col_1indexed(fn_node)
|
|
445
|
+
findings.append(self._make_finding(
|
|
446
|
+
rel_path=rel_path,
|
|
447
|
+
line=line,
|
|
448
|
+
col=col,
|
|
449
|
+
cwe="CWE-95",
|
|
450
|
+
severity="HIGH",
|
|
451
|
+
sink="Function",
|
|
452
|
+
kind="new_expression",
|
|
453
|
+
source_bytes=source_bytes,
|
|
454
|
+
sink_node=fn_node,
|
|
455
|
+
args_node=args_node,
|
|
456
|
+
))
|
|
457
|
+
|
|
458
|
+
return findings
|
|
459
|
+
|
|
460
|
+
def _extract_jsx_xss(
|
|
461
|
+
self, root, source_bytes: bytes, rel_path: str
|
|
462
|
+
) -> List[Dict[str, Any]]:
|
|
463
|
+
"""Detect dangerouslySetInnerHTML in JSX attributes and object properties."""
|
|
464
|
+
findings: List[Dict[str, Any]] = []
|
|
465
|
+
if _JSX_DSIHTML_QUERY is None:
|
|
466
|
+
return findings
|
|
467
|
+
|
|
468
|
+
# JSX attribute pattern
|
|
469
|
+
for _, match_dict in _safe_query_matches(_JSX_DSIHTML_QUERY, QueryCursor, root):
|
|
470
|
+
if "jsx_attr_name" in match_dict:
|
|
471
|
+
for attr_node in match_dict["jsx_attr_name"]:
|
|
472
|
+
name = (attr_node.text or b"").decode("utf-8", errors="replace")
|
|
473
|
+
if name not in _JSX_XSS_SINKS:
|
|
474
|
+
continue
|
|
475
|
+
val_nodes = match_dict.get("jsx_attr_value", [])
|
|
476
|
+
val_node = val_nodes[0] if val_nodes else None
|
|
477
|
+
line, col = _row_col_1indexed(attr_node)
|
|
478
|
+
findings.append(self._make_finding(
|
|
479
|
+
rel_path=rel_path,
|
|
480
|
+
line=line,
|
|
481
|
+
col=col,
|
|
482
|
+
cwe="CWE-79",
|
|
483
|
+
severity="CRITICAL",
|
|
484
|
+
sink=name,
|
|
485
|
+
kind="jsx_attribute",
|
|
486
|
+
source_bytes=source_bytes,
|
|
487
|
+
sink_node=attr_node,
|
|
488
|
+
args_node=val_node,
|
|
489
|
+
))
|
|
490
|
+
|
|
491
|
+
# Object pair pattern
|
|
492
|
+
if _OBJ_DSIHTML_QUERY is not None:
|
|
493
|
+
for _, match_dict in _safe_query_matches(_OBJ_DSIHTML_QUERY, QueryCursor, root):
|
|
494
|
+
if "prop_key" in match_dict:
|
|
495
|
+
for key_node in match_dict["prop_key"]:
|
|
496
|
+
name = (key_node.text or b"").decode("utf-8", errors="replace")
|
|
497
|
+
if name not in _JSX_XSS_SINKS:
|
|
498
|
+
continue
|
|
499
|
+
val_nodes = match_dict.get("prop_val", [])
|
|
500
|
+
val_node = val_nodes[0] if val_nodes else None
|
|
501
|
+
line, col = _row_col_1indexed(key_node)
|
|
502
|
+
findings.append(self._make_finding(
|
|
503
|
+
rel_path=rel_path,
|
|
504
|
+
line=line,
|
|
505
|
+
col=col,
|
|
506
|
+
cwe="CWE-79",
|
|
507
|
+
severity="CRITICAL",
|
|
508
|
+
sink=name,
|
|
509
|
+
kind="object_property",
|
|
510
|
+
source_bytes=source_bytes,
|
|
511
|
+
sink_node=key_node,
|
|
512
|
+
args_node=val_node,
|
|
513
|
+
))
|
|
514
|
+
|
|
515
|
+
return findings
|
|
516
|
+
|
|
517
|
+
def _extract_innerhtml(
|
|
518
|
+
self, root, source_bytes: bytes, rel_path: str
|
|
519
|
+
) -> List[Dict[str, Any]]:
|
|
520
|
+
"""Detect direct DOM innerHTML/outerHTML assignment expressions."""
|
|
521
|
+
findings: List[Dict[str, Any]] = []
|
|
522
|
+
if _INNERHTML_QUERY is None:
|
|
523
|
+
return findings
|
|
524
|
+
|
|
525
|
+
for _, match_dict in _safe_query_matches(_INNERHTML_QUERY, QueryCursor, root):
|
|
526
|
+
if "prop_name" in match_dict:
|
|
527
|
+
for prop_node in match_dict["prop_name"]:
|
|
528
|
+
name = (prop_node.text or b"").decode("utf-8", errors="replace")
|
|
529
|
+
if name not in _INNERHTML_SINKS:
|
|
530
|
+
continue
|
|
531
|
+
rhs_nodes = match_dict.get("rhs", [])
|
|
532
|
+
rhs_node = rhs_nodes[0] if rhs_nodes else None
|
|
533
|
+
line, col = _row_col_1indexed(prop_node)
|
|
534
|
+
findings.append(self._make_finding(
|
|
535
|
+
rel_path=rel_path,
|
|
536
|
+
line=line,
|
|
537
|
+
col=col,
|
|
538
|
+
cwe="CWE-79",
|
|
539
|
+
severity="CRITICAL",
|
|
540
|
+
sink=name,
|
|
541
|
+
kind="assignment_expression",
|
|
542
|
+
source_bytes=source_bytes,
|
|
543
|
+
sink_node=prop_node,
|
|
544
|
+
args_node=rhs_node,
|
|
545
|
+
))
|
|
546
|
+
|
|
547
|
+
return findings
|
|
548
|
+
|
|
549
|
+
# ------------------------------------------------------------------
|
|
550
|
+
# JS (non-TSX) extractors – use per-language queries
|
|
551
|
+
# ------------------------------------------------------------------
|
|
552
|
+
def _extract_eval_sinks_js(
|
|
553
|
+
self, root, source_bytes: bytes, rel_path: str, language
|
|
554
|
+
) -> List[Dict[str, Any]]:
|
|
555
|
+
"""Eval/Function sink detection for pure JS files."""
|
|
556
|
+
try:
|
|
557
|
+
q = Query(language, """
|
|
558
|
+
(call_expression
|
|
559
|
+
function: (identifier) @fn_id
|
|
560
|
+
arguments: (arguments) @fn_args)
|
|
561
|
+
(new_expression
|
|
562
|
+
constructor: (identifier) @new_id
|
|
563
|
+
arguments: (arguments) @new_args)
|
|
564
|
+
""")
|
|
565
|
+
cursor = QueryCursor(q)
|
|
566
|
+
matches = cursor.matches(root)
|
|
567
|
+
except Exception:
|
|
568
|
+
return []
|
|
569
|
+
|
|
570
|
+
findings: List[Dict[str, Any]] = []
|
|
571
|
+
for _, match_dict in matches:
|
|
572
|
+
if "fn_id" in match_dict:
|
|
573
|
+
for fn_node in match_dict["fn_id"]:
|
|
574
|
+
name = (fn_node.text or b"").decode("utf-8", errors="replace")
|
|
575
|
+
if name not in _EVAL_SINKS:
|
|
576
|
+
continue
|
|
577
|
+
args_nodes = match_dict.get("fn_args", [])
|
|
578
|
+
args_node = args_nodes[0] if args_nodes else None
|
|
579
|
+
line, col = _row_col_1indexed(fn_node)
|
|
580
|
+
findings.append(self._make_finding(
|
|
581
|
+
rel_path=rel_path,
|
|
582
|
+
line=line, col=col,
|
|
583
|
+
cwe="CWE-95", severity="HIGH",
|
|
584
|
+
sink=name, kind="call_expression",
|
|
585
|
+
source_bytes=source_bytes,
|
|
586
|
+
sink_node=fn_node, args_node=args_node,
|
|
587
|
+
))
|
|
588
|
+
if "new_id" in match_dict:
|
|
589
|
+
for fn_node in match_dict["new_id"]:
|
|
590
|
+
name = (fn_node.text or b"").decode("utf-8", errors="replace")
|
|
591
|
+
if name != "Function":
|
|
592
|
+
continue
|
|
593
|
+
args_nodes = match_dict.get("new_args", [])
|
|
594
|
+
args_node = args_nodes[0] if args_nodes else None
|
|
595
|
+
line, col = _row_col_1indexed(fn_node)
|
|
596
|
+
findings.append(self._make_finding(
|
|
597
|
+
rel_path=rel_path,
|
|
598
|
+
line=line, col=col,
|
|
599
|
+
cwe="CWE-95", severity="HIGH",
|
|
600
|
+
sink="Function", kind="new_expression",
|
|
601
|
+
source_bytes=source_bytes,
|
|
602
|
+
sink_node=fn_node, args_node=args_node,
|
|
603
|
+
))
|
|
604
|
+
return findings
|
|
605
|
+
|
|
606
|
+
def _extract_innerhtml_js(
|
|
607
|
+
self, root, source_bytes: bytes, rel_path: str, language
|
|
608
|
+
) -> List[Dict[str, Any]]:
|
|
609
|
+
"""innerHTML/outerHTML detection for pure JS files."""
|
|
610
|
+
try:
|
|
611
|
+
q = Query(language, """
|
|
612
|
+
(assignment_expression
|
|
613
|
+
left: (member_expression
|
|
614
|
+
property: (property_identifier) @prop_name)
|
|
615
|
+
right: (_) @rhs)
|
|
616
|
+
""")
|
|
617
|
+
cursor = QueryCursor(q)
|
|
618
|
+
matches = cursor.matches(root)
|
|
619
|
+
except Exception:
|
|
620
|
+
return []
|
|
621
|
+
|
|
622
|
+
findings: List[Dict[str, Any]] = []
|
|
623
|
+
for _, match_dict in matches:
|
|
624
|
+
if "prop_name" in match_dict:
|
|
625
|
+
for prop_node in match_dict["prop_name"]:
|
|
626
|
+
name = (prop_node.text or b"").decode("utf-8", errors="replace")
|
|
627
|
+
if name not in _INNERHTML_SINKS:
|
|
628
|
+
continue
|
|
629
|
+
rhs_nodes = match_dict.get("rhs", [])
|
|
630
|
+
rhs_node = rhs_nodes[0] if rhs_nodes else None
|
|
631
|
+
line, col = _row_col_1indexed(prop_node)
|
|
632
|
+
findings.append(self._make_finding(
|
|
633
|
+
rel_path=rel_path,
|
|
634
|
+
line=line, col=col,
|
|
635
|
+
cwe="CWE-79", severity="CRITICAL",
|
|
636
|
+
sink=name, kind="assignment_expression",
|
|
637
|
+
source_bytes=source_bytes,
|
|
638
|
+
sink_node=prop_node, args_node=rhs_node,
|
|
639
|
+
))
|
|
640
|
+
return findings
|
|
641
|
+
|
|
642
|
+
# ------------------------------------------------------------------
|
|
643
|
+
# Finding factory
|
|
644
|
+
# ------------------------------------------------------------------
|
|
645
|
+
def _make_finding(
|
|
646
|
+
self,
|
|
647
|
+
rel_path: str,
|
|
648
|
+
line: int,
|
|
649
|
+
col: int,
|
|
650
|
+
cwe: str,
|
|
651
|
+
severity: str,
|
|
652
|
+
sink: str,
|
|
653
|
+
kind: str,
|
|
654
|
+
source_bytes: bytes,
|
|
655
|
+
sink_node,
|
|
656
|
+
args_node,
|
|
657
|
+
) -> Dict[str, Any]:
|
|
658
|
+
"""Build a standardized TimeCodeSecurity finding dict."""
|
|
659
|
+
# Build snippet: prefer argument text; fall back to full node line
|
|
660
|
+
if args_node is not None:
|
|
661
|
+
raw_snippet = _snippet(source_bytes, args_node)
|
|
662
|
+
else:
|
|
663
|
+
raw_snippet = _snippet(source_bytes, sink_node)
|
|
664
|
+
|
|
665
|
+
# Inline code snippet: the line of the finding
|
|
666
|
+
lines = source_bytes.decode("utf-8", errors="replace").splitlines()
|
|
667
|
+
line_text = lines[line - 1].strip() if 1 <= line <= len(lines) else ""
|
|
668
|
+
|
|
669
|
+
if cwe == "CWE-79":
|
|
670
|
+
message = (
|
|
671
|
+
"Unsanitized input passed to dangerous DOM/React rendering sink "
|
|
672
|
+
f"({sink}). Sanitize all HTML content before rendering."
|
|
673
|
+
)
|
|
674
|
+
remediation = (
|
|
675
|
+
"Sanitize all user-supplied HTML with a trusted library (e.g., DOMPurify) "
|
|
676
|
+
"before passing to dangerouslySetInnerHTML or innerHTML. "
|
|
677
|
+
"Prefer React's default JSX escaping by passing content as children, not HTML."
|
|
678
|
+
)
|
|
679
|
+
category = "CROSS_SITE_SCRIPTING"
|
|
680
|
+
else:
|
|
681
|
+
message = (
|
|
682
|
+
"Dynamic code evaluation via eval() or Function() constructor can lead to "
|
|
683
|
+
"arbitrary JavaScript execution. Avoid dynamic code evaluation."
|
|
684
|
+
)
|
|
685
|
+
remediation = (
|
|
686
|
+
"Avoid eval() and new Function(). Use JSON.parse() for JSON data, "
|
|
687
|
+
"or refactor to static function dispatch tables."
|
|
688
|
+
)
|
|
689
|
+
category = "CODE_INJECTION"
|
|
690
|
+
|
|
691
|
+
flow_summary = f"[User Input] -> [Tainted Dataflow] -> [{sink} (L{line})]"
|
|
692
|
+
flow_trace = [
|
|
693
|
+
"Source: User Input",
|
|
694
|
+
f"Sink: {sink} ({rel_path}:{line})",
|
|
695
|
+
]
|
|
696
|
+
|
|
697
|
+
return {
|
|
698
|
+
# NOTE: id is assigned by the caller after counter is known
|
|
699
|
+
"id": "TimeCodeSecurity-JS-000",
|
|
700
|
+
"category": category,
|
|
701
|
+
"cwe": cwe,
|
|
702
|
+
"severity": severity,
|
|
703
|
+
"confidence": 1.0,
|
|
704
|
+
"confidence_label": "CONFIRMED",
|
|
705
|
+
"file": rel_path,
|
|
706
|
+
"line_number": line,
|
|
707
|
+
"column": col,
|
|
708
|
+
"sink_symbol": sink,
|
|
709
|
+
"source_symbol": "USER_INPUT",
|
|
710
|
+
"source_line": None,
|
|
711
|
+
"source_file": None,
|
|
712
|
+
"code_snippet": line_text or raw_snippet,
|
|
713
|
+
"flow_trace": flow_trace,
|
|
714
|
+
"flow_trace_summary": flow_summary,
|
|
715
|
+
"remediation": remediation,
|
|
716
|
+
"message": message,
|
|
717
|
+
"proof_graph": None,
|
|
718
|
+
"proof_graph_ascii": None,
|
|
719
|
+
"discovery_mode": "JS_TS_SCAN",
|
|
720
|
+
"status": "ACTIVE",
|
|
721
|
+
"suppressed": False,
|
|
722
|
+
"suppression_justification": None,
|
|
723
|
+
"suppression_kind": None,
|
|
724
|
+
"active": True,
|
|
725
|
+
# Metadata
|
|
726
|
+
"_js_kind": kind,
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
|
|
730
|
+
# ---------------------------------------------------------------------------
|
|
731
|
+
# Convenience functions (mirrors the public surface used by tcs_cli.py)
|
|
732
|
+
# ---------------------------------------------------------------------------
|
|
733
|
+
def discover_js_ts_files(
|
|
734
|
+
target_path: Path,
|
|
735
|
+
skipped_files: Optional[List[str]] = None,
|
|
736
|
+
) -> List[Path]:
|
|
737
|
+
"""
|
|
738
|
+
Standalone helper: discover JS/TS files under target_path.
|
|
739
|
+
Returns sorted list of Path objects.
|
|
740
|
+
"""
|
|
741
|
+
scanner = JsTsScanner()
|
|
742
|
+
return scanner._discover_js_files(target_path, skipped_files)
|
|
743
|
+
|
|
744
|
+
|
|
745
|
+
def run_js_ts_scan(
|
|
746
|
+
target_path: Path,
|
|
747
|
+
base_dir: Optional[Path] = None,
|
|
748
|
+
skipped_files: Optional[List[str]] = None,
|
|
749
|
+
) -> List[Dict[str, Any]]:
|
|
750
|
+
"""
|
|
751
|
+
Convenience function: run a full JS/TS security scan and return findings.
|
|
752
|
+
Returns empty list if tree-sitter is unavailable.
|
|
753
|
+
"""
|
|
754
|
+
scanner = JsTsScanner()
|
|
755
|
+
return scanner.scan_directory(target_path, base_dir=base_dir, skipped_files=skipped_files)
|
|
756
|
+
|
|
757
|
+
|
|
758
|
+
def js_ts_available() -> bool:
|
|
759
|
+
"""Returns True if tree-sitter JS/TS scanning is available."""
|
|
760
|
+
return _TS_AVAILABLE
|
|
761
|
+
|
|
762
|
+
|
|
763
|
+
def warn_js_ts_unavailable() -> None:
|
|
764
|
+
"""Emit the grammar-missing warning now that the caller knows JS/TS was wanted.
|
|
765
|
+
|
|
766
|
+
No-op when the grammar imported fine; the message is the one the module used to print at
|
|
767
|
+
import time, unchanged so existing CI log greps still match.
|
|
768
|
+
"""
|
|
769
|
+
if _TS_AVAILABLE:
|
|
770
|
+
return
|
|
771
|
+
print(
|
|
772
|
+
f"[TimeCodeSecurity-JS] WARNING: tree-sitter or JS/TS grammar not available ({_TS_IMPORT_ERROR}). "
|
|
773
|
+
"JS/TS scanning is disabled. Run: pip install tree-sitter tree-sitter-javascript tree-sitter-typescript",
|
|
774
|
+
file=sys.stderr,
|
|
775
|
+
)
|
|
776
|
+
|
|
777
|
+
|
|
778
|
+
def has_js_ts_files(target_path) -> bool:
|
|
779
|
+
"""True as soon as one scannable JS/TS file exists under `target_path`.
|
|
780
|
+
|
|
781
|
+
Early exit and no `stat()`, because the only caller wants a yes/no before deciding whether
|
|
782
|
+
to warn -- not a file list. Skips the same ignored directories `_discover_js_files` prunes.
|
|
783
|
+
"""
|
|
784
|
+
try:
|
|
785
|
+
path = Path(target_path)
|
|
786
|
+
if path.is_file():
|
|
787
|
+
return path.suffix.lower() in JS_TS_EXTENSIONS
|
|
788
|
+
if not path.is_dir():
|
|
789
|
+
return False
|
|
790
|
+
for root, dirs, files in os.walk(path, followlinks=False):
|
|
791
|
+
dirs[:] = [
|
|
792
|
+
d for d in dirs
|
|
793
|
+
if d not in _SKIP_DIRS
|
|
794
|
+
and not d.startswith(".")
|
|
795
|
+
and not os.path.islink(os.path.join(root, d))
|
|
796
|
+
]
|
|
797
|
+
if any(Path(name).suffix.lower() in JS_TS_EXTENSIONS for name in files):
|
|
798
|
+
return True
|
|
799
|
+
except OSError:
|
|
800
|
+
return False
|
|
801
|
+
return False
|
|
802
|
+
|
|
803
|
+
|
|
804
|
+
# ---------------------------------------------------------------------------
|
|
805
|
+
# Crash-isolated runner
|
|
806
|
+
# ---------------------------------------------------------------------------
|
|
807
|
+
# py-tree-sitter can take a native access violation on some grammars/files. That
|
|
808
|
+
# is not a Python exception: it kills the interpreter process, so the whole scan
|
|
809
|
+
# dies and no artefact is written. Running the grammar in a child process turns
|
|
810
|
+
# an unrecoverable crash into a recoverable per-file loss.
|
|
811
|
+
_JS_WORKER_TIMEOUT_SECONDS = 900
|
|
812
|
+
|
|
813
|
+
|
|
814
|
+
def _scan_batch_in_child(files: List[str], base_dir: str) -> Optional[List[Dict[str, Any]]]:
|
|
815
|
+
"""Scan one batch of JS/TS files in a child process; None when the child died."""
|
|
816
|
+
import json
|
|
817
|
+
import subprocess
|
|
818
|
+
|
|
819
|
+
try:
|
|
820
|
+
proc = subprocess.run(
|
|
821
|
+
[sys.executable, os.path.abspath(__file__), "--tcs-js-worker"],
|
|
822
|
+
input=json.dumps({"files": files, "base_dir": base_dir}),
|
|
823
|
+
cwd=os.getcwd(), capture_output=True, text=True,
|
|
824
|
+
encoding="utf-8", errors="replace",
|
|
825
|
+
timeout=_JS_WORKER_TIMEOUT_SECONDS,
|
|
826
|
+
)
|
|
827
|
+
except (OSError, subprocess.TimeoutExpired):
|
|
828
|
+
return None
|
|
829
|
+
if proc.returncode != 0:
|
|
830
|
+
print(f"[TimeCodeSecurity-JS] worker exited {proc.returncode}: {(proc.stderr or '')[-300:]}",
|
|
831
|
+
file=sys.stderr)
|
|
832
|
+
return None
|
|
833
|
+
try:
|
|
834
|
+
payload = json.loads(proc.stdout)
|
|
835
|
+
except ValueError:
|
|
836
|
+
return None
|
|
837
|
+
return payload if isinstance(payload, list) else None
|
|
838
|
+
|
|
839
|
+
|
|
840
|
+
def _scan_files_isolated(files: List[Path], base_dir: str,
|
|
841
|
+
dropped: List[str]) -> List[Dict[str, Any]]:
|
|
842
|
+
"""Bisect around a crashing file so only the files that break the grammar are lost."""
|
|
843
|
+
if not files:
|
|
844
|
+
return []
|
|
845
|
+
batch = _scan_batch_in_child([str(f) for f in files], base_dir)
|
|
846
|
+
if batch is not None:
|
|
847
|
+
return batch
|
|
848
|
+
if len(files) == 1:
|
|
849
|
+
dropped.append(str(files[0]))
|
|
850
|
+
return []
|
|
851
|
+
mid = len(files) // 2
|
|
852
|
+
return (_scan_files_isolated(files[:mid], base_dir, dropped)
|
|
853
|
+
+ _scan_files_isolated(files[mid:], base_dir, dropped))
|
|
854
|
+
|
|
855
|
+
|
|
856
|
+
def run_js_ts_scan_isolated(
|
|
857
|
+
target_path: Path,
|
|
858
|
+
base_dir: Optional[Path] = None,
|
|
859
|
+
skipped_files: Optional[List[str]] = None,
|
|
860
|
+
) -> List[Dict[str, Any]]:
|
|
861
|
+
"""JS/TS scan that cannot take the caller down with a native crash."""
|
|
862
|
+
if not _TS_AVAILABLE:
|
|
863
|
+
return []
|
|
864
|
+
base = str(Path(base_dir) if base_dir else Path.cwd())
|
|
865
|
+
files = JsTsScanner()._discover_js_files(Path(target_path), skipped_files)
|
|
866
|
+
if not files:
|
|
867
|
+
return []
|
|
868
|
+
dropped: List[str] = []
|
|
869
|
+
findings = _scan_files_isolated(files, base, dropped)
|
|
870
|
+
for path in dropped:
|
|
871
|
+
print(f"[TimeCodeSecurity-JS] WARNING: tree-sitter crashed on {path}; file skipped.",
|
|
872
|
+
file=sys.stderr)
|
|
873
|
+
for index, finding in enumerate(findings, start=1):
|
|
874
|
+
finding["id"] = f"TimeCodeSecurity-JS-{index:03d}"
|
|
875
|
+
return findings
|
|
876
|
+
|
|
877
|
+
|
|
878
|
+
def _worker_main() -> int:
|
|
879
|
+
import json
|
|
880
|
+
|
|
881
|
+
request = json.loads(sys.stdin.read() or "{}")
|
|
882
|
+
findings = JsTsScanner().scan_files(
|
|
883
|
+
[Path(f) for f in request.get("files", [])],
|
|
884
|
+
base_dir=Path(request.get("base_dir") or os.getcwd()),
|
|
885
|
+
)
|
|
886
|
+
sys.stdout.write(json.dumps(findings))
|
|
887
|
+
return 0
|
|
888
|
+
|
|
889
|
+
|
|
890
|
+
if __name__ == "__main__":
|
|
891
|
+
if "--tcs-js-worker" in sys.argv:
|
|
892
|
+
sys.exit(_worker_main())
|
|
893
|
+
print("js_scanner.py is a library; use tcs_cli.py --include-js to scan JS/TS.",
|
|
894
|
+
file=sys.stderr)
|
|
895
|
+
sys.exit(2)
|