@@ -68,6 +68,129 @@ def _document_members(**extra: bytes) -> dict[str, bytes]:
6868 }
6969
7070
71+ def test_typescript_declaration_files_are_not_executable_by_name_alone () -> None :
72+ declaration = b"export interface Options { retries?: number; }\n export type Result = string;\n "
73+
74+ assert not is_executable_content ("types.d.cts" , declaration , complete_content = True )
75+ assert not is_executable_content ("types.d.mts" , declaration , complete_content = True )
76+
77+
78+ def test_runtime_code_in_typescript_declaration_named_file_stays_executable () -> None :
79+ runtime = b'declare const marker: string;\n require("child_process").execSync(marker);\n '
80+
81+ assert is_executable_content ("evil.d.cts" , runtime )
82+
83+
84+ def test_declaration_exemption_requires_complete_content () -> None :
85+ declaration = b"export {}"
86+
87+ assert is_executable_content ("types.d.mts" , declaration )
88+ assert not is_executable_content ("types.d.mts" , declaration , complete_content = True )
89+
90+
91+ def test_runtime_default_import_named_type_is_not_a_type_only_import () -> None :
92+ runtime = b'import type from "./payload.mjs";\n '
93+
94+ assert is_executable_content ("evil.d.mts" , runtime , complete_content = True )
95+
96+
97+ def test_typescript_declaration_span_cannot_cross_asi_line_break () -> None :
98+ runtime = b'export type Config = string\n console.log("RUNTIME")\n ;'
99+
100+ assert is_executable_content ("evil.d.cts" , runtime , complete_content = True )
101+
102+
103+ @pytest .mark .parametrize (
104+ "runtime" ,
105+ [
106+ b'type A = "/*";\n console.log("RUNTIME");\n type B = "*/";\n ' ,
107+ b'type A = "//"; console.log("RUNTIME")\n type B = string;\n ' ,
108+ b'type A = `unterminated;\n console.log("RUNTIME");' ,
109+ ],
110+ )
111+ def test_comment_markers_inside_literals_cannot_hide_runtime (runtime : bytes ) -> None :
112+ assert is_executable_content ("evil.d.ts" , runtime , complete_content = True )
113+
114+
115+ def test_typical_typescript_emitted_declaration_file_is_inert () -> None :
116+ declaration = (
117+ b"export interface Options { retries?: number; }\n "
118+ b"export declare function load(options: Options): Promise<void>;\n "
119+ b"export type Result = string;\n "
120+ )
121+
122+ assert not is_executable_content ("types.d.ts" , declaration , complete_content = True )
123+
124+
125+ def test_typescript_side_effect_import_in_declaration_named_file_stays_executable () -> None :
126+ runtime = b"import './payload.js';\n export type Result = string;\n "
127+
128+ assert is_executable_content ("evil.d.mts" , runtime )
129+
130+
131+ def test_mixed_typescript_declaration_and_runtime_stays_executable () -> None :
132+ runtime = b"export type Result = string;\n console.log('runtime');\n "
133+
134+ assert is_executable_content ("evil.d.ts" , runtime )
135+
136+
137+ @pytest .mark .parametrize ("path" , ["types.d.ts" , "types.d.cts" , "types.d.mts" ])
138+ def test_ambient_typescript_namespaces_and_modules_are_declarations (path : str ) -> None :
139+ declaration = (
140+ b"declare namespace Payload { interface Value { name: string; } }\n "
141+ b'declare module "payload" { export function load(): string; }\n '
142+ )
143+
144+ assert not is_executable_content (path , declaration , complete_content = True )
145+
146+
147+ @pytest .mark .parametrize (
148+ "runtime" ,
149+ [
150+ b'namespace Payload { console.log("runtime"); }\n ' ,
151+ b'export namespace Payload { console.log("runtime"); }\n ' ,
152+ b'declare namespace Payload { console.log("runtime"); }\n ' ,
153+ ],
154+ )
155+ def test_typescript_namespace_bodies_must_be_proven_ambient (runtime : bytes ) -> None :
156+ assert is_executable_content ("evil.d.ts" , runtime )
157+
158+
159+ @pytest .mark .parametrize (
160+ "runtime" ,
161+ [
162+ b'type = console.log("runtime");\n ' ,
163+ b"namespace: { console.log('runtime'); }\n " ,
164+ ],
165+ )
166+ def test_ambiguous_typescript_keywords_stay_executable (runtime : bytes ) -> None :
167+ """Assignments and labels must not be mistaken for declarations."""
168+ assert is_executable_content ("evil.d.mts" , runtime )
169+
170+
171+ @pytest .mark .parametrize (
172+ "runtime" ,
173+ [
174+ b'type = console.log("runtime");\n ' ,
175+ b"namespace: { console.log('runtime'); }\n " ,
176+ ],
177+ )
178+ def test_ambiguous_typescript_keywords_in_hidden_documents_raise_sc9 (
179+ tmp_path : Path , runtime : bytes
180+ ) -> None :
181+ archive_path = tmp_path / ".types.docx.txt"
182+ _write_archive (archive_path , _document_members (** {"word/payload.d.mts" : runtime }))
183+ (tmp_path / "SKILL.md" ).write_text ("# Declaration container\n " , encoding = "utf-8" )
184+
185+ context = build_context ({"skill_path" : str (tmp_path )})
186+ virtual_path = ".types.docx.txt!/word/payload.d.mts"
187+ metadata = next (item for item in context ["component_metadata" ] if item ["path" ] == virtual_path )
188+ assert metadata ["executable" ] is True
189+ assert metadata ["concealed_executable" ] is True
190+ findings = _analyze_concealed_executables (context ["component_metadata" ])
191+ assert any (finding .rule_id == "SC9" and finding .file == virtual_path for finding in findings )
192+
193+
71194@pytest .mark .parametrize (
72195 ("path" , "payload" ),
73196 [
0 commit comments