Skip to content

Commit 8ffd4f9

Browse files
committed
fix: classify ambiguous TypeScript syntax as executable
Signed-off-by: Efe Gökdemir <[email protected]>
1 parent 67a69b2 commit 8ffd4f9

2 files changed

Lines changed: 40 additions & 3 deletions

File tree

‎src/skillspector/nested_artifacts.py‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -393,9 +393,11 @@ def _zip_member_is_link(info: zipfile.ZipInfo) -> bool:
393393
_TYPESCRIPT_DECLARATION_SUFFIXES = (".d.ts", ".d.cts", ".d.mts")
394394
_TYPESCRIPT_DECLARATION_FILE = re.compile(
395395
r"\A\s*(?:"
396-
r"(?:declare\s+(?:const|let|var|function|class|namespace|module)\b[^;{}]*;)|"
397-
r"(?:(?:export\s+)?(?:interface|namespace)\b[^{}]*\{[^{}]*\}\s*;?)|"
398-
r"(?:(?:export\s+)?type\b[^;{}]+;)|"
396+
r"(?:declare\s+(?:const|let|var|function|class)\b[^;{}]*;)|"
397+
r"(?:(?:declare\s+)?(?:export\s+)?namespace\s+[A-Za-z_$][\w$]*\s*\{[^{}]*\}\s*;?)|"
398+
r"(?:(?:declare\s+)?module\s+(?:[\"'][^\"']+[\"']|[A-Za-z_$][\w$]*)\s*\{[^{}]*\}\s*;?)|"
399+
r"(?:(?:export\s+)?interface\s+[A-Za-z_$][\w$]*(?:\s*<[^{}]*>)?\s*\{[^{}]*\}\s*;?)|"
400+
r"(?:(?:export\s+)?type\s+[A-Za-z_$][\w$]*(?:\s*<[^;{}]*>)?\s*=\s*[^;{}]+;)|"
399401
r"(?:import\s+type\b[^;{}]+;)|"
400402
r"(?:export\s*\{[^{}]*\}\s*;?)"
401403
r")+(?:\s|/\*.*?\*/|//[^\r\n]*)*\Z",

‎tests/nodes/test_nested_artifacts.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,41 @@ def test_mixed_typescript_declaration_and_runtime_stays_executable() -> None:
9393
assert is_executable_content("evil.d.ts", runtime)
9494

9595

96+
@pytest.mark.parametrize(
97+
"runtime",
98+
[
99+
b'type = console.log("runtime");\n',
100+
b"namespace: { console.log('runtime'); }\n",
101+
],
102+
)
103+
def test_ambiguous_typescript_keywords_stay_executable(runtime: bytes) -> None:
104+
"""Assignments and labels must not be mistaken for declarations."""
105+
assert is_executable_content("evil.d.mts", runtime)
106+
107+
108+
@pytest.mark.parametrize(
109+
"runtime",
110+
[
111+
b'type = console.log("runtime");\n',
112+
b"namespace: { console.log('runtime'); }\n",
113+
],
114+
)
115+
def test_ambiguous_typescript_keywords_in_hidden_documents_raise_sc9(
116+
tmp_path: Path, runtime: bytes
117+
) -> None:
118+
archive_path = tmp_path / ".types.docx.txt"
119+
_write_archive(archive_path, _document_members(**{"word/payload.d.mts": runtime}))
120+
(tmp_path / "SKILL.md").write_text("# Declaration container\n", encoding="utf-8")
121+
122+
context = build_context({"skill_path": str(tmp_path)})
123+
virtual_path = ".types.docx.txt!/word/payload.d.mts"
124+
metadata = next(item for item in context["component_metadata"] if item["path"] == virtual_path)
125+
assert metadata["executable"] is True
126+
assert metadata["concealed_executable"] is True
127+
findings = _analyze_concealed_executables(context["component_metadata"])
128+
assert any(finding.rule_id == "SC9" and finding.file == virtual_path for finding in findings)
129+
130+
96131
@pytest.mark.parametrize(
97132
("path", "payload"),
98133
[

0 commit comments

Comments
 (0)