Repository navigation
Expand file tree
/
Copy pathtest_tool_misuse_python_ast.py
More file actions
1664 lines (1368 loc) · 49.1 KB
/
Copy pathtest_tool_misuse_python_ast.py
File metadata and controls
1664 lines (1368 loc) · 49.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
"""Focused coverage for issue #475's ordinary-Python binding form."""
from __future__ import annotations
import pytest
from skillspector.nodes.analyzers import static_patterns_tool_misuse as tm_module
from skillspector.nodes.analyzers import static_python_shell_truthiness as python_tm_module
from skillspector.nodes.deduplicate import deduplicate
def _run(content: str, path: str = "run.py") -> dict:
return tm_module.node({"components": [path], "file_cache": {path: content}})
def _tm1(content: str, path: str = "run.py") -> list:
return [finding for finding in _run(content, path)["findings"] if finding.rule_id == "TM1"]
def _tm1_ast(content: str, path: str = "run.py") -> list:
return python_tm_module.analyze(content, path, "python")
@pytest.mark.parametrize(
"value",
[
pytest.param("True", id="issue-body-boolean"),
pytest.param("'True'", id="reporter-attachment-string"),
],
)
def test_issue_475_multiline_binding_matches_direct_tm1(value: str) -> None:
findings = _tm1(
"import subprocess\n"
"command = f'python a.py'\n"
f"enabled = {value}\n"
"result = subprocess.run(\n"
" command,\n"
" shell=enabled,\n"
" capture_output=True,\n"
" text=True,\n"
")\n"
)
assert len(findings) == 1
assert findings[0].start_line == 4
assert findings[0].severity == "HIGH"
assert findings[0].confidence == pytest.approx(0.9)
assert "shell=enabled" in findings[0].matched_text
assert not findings[0].evidence
@pytest.mark.parametrize(
"value",
[
pytest.param("1", id="integer"),
pytest.param("-1", id="negative-integer"),
pytest.param("(0,)", id="nonempty-tuple"),
pytest.param("not False", id="negation"),
],
)
def test_simple_immutable_truthy_values_are_tracked(value: str) -> None:
assert (
len(_tm1(f"import subprocess\nenabled = {value}\nsubprocess.run(cmd, shell=enabled)\n"))
== 1
)
@pytest.mark.parametrize(
"value",
[
pytest.param("False", id="boolean"),
pytest.param("0", id="integer"),
pytest.param("''", id="string"),
pytest.param("()", id="tuple"),
pytest.param("None", id="none"),
],
)
def test_definitely_false_values_are_not_tracked(value: str) -> None:
assert not _tm1(f"import subprocess\nenabled = {value}\nsubprocess.run(cmd, shell=enabled)\n")
def test_simple_alias_chain_and_bare_popen_are_tracked() -> None:
findings = _tm1(
"first = 'enabled'\nsecond = first\nthird = second\nPopen(command, shell=third)\n"
)
assert len(findings) == 1
assert findings[0].start_line == 4
@pytest.mark.parametrize(
"rebind",
[
pytest.param("enabled = False", id="false-assignment"),
pytest.param("enabled = dynamic", id="unknown-assignment"),
pytest.param("import pathlib as enabled", id="import"),
pytest.param("from settings import enabled", id="from-import"),
],
)
def test_rebinding_invalidates_truthy_fact(rebind: str) -> None:
assert not _tm1(
f"import subprocess\nenabled = True\n{rebind}\nsubprocess.run(command, shell=enabled)\n"
)
def test_import_side_effect_boundary_clears_truth_facts() -> None:
assert not _tm1_ast(
"import subprocess\nenabled = True\nimport attacker\n"
"subprocess.run(command, shell=enabled)\n"
)
@pytest.mark.parametrize(
"shadow",
[
pytest.param("subprocess = Proxy()", id="assignment"),
pytest.param("import other as subprocess", id="import-alias"),
pytest.param("subprocess.run = Proxy()", id="attribute-mutation"),
pytest.param("subprocess, other = pair", id="unpacking"),
],
)
def test_explicit_subprocess_shadow_rejects_bound_call(shadow: str) -> None:
assert not _tm1(f"{shadow}\nenabled = True\nsubprocess.run(cmd, shell=enabled)\n")
@pytest.mark.parametrize(
"native_rhs",
[
pytest.param("subprocess.check_call", id="native-cross-api"),
pytest.param("Popen", id="native-popen"),
pytest.param("functools.partial(Popen, command)", id="wrapped-popen"),
pytest.param("functools.partial(subprocess.run, command)", id="wrapped-native"),
pytest.param("Proxy(subprocess)", id="receiver-passed-to-constructor"),
],
)
def test_first_method_store_with_native_receiver_rhs_retains_bound_call(native_rhs: str) -> None:
findings = _tm1(
f"subprocess.run = {native_rhs}\nenabled = True\nsubprocess.run(command, shell=enabled)\n"
)
assert len(findings) == 1
assert findings[0].severity == "HIGH"
assert findings[0].start_line == 2
assert "shell=enabled" in findings[0].matched_text
def test_explicit_import_reestablishes_direct_receivers() -> None:
assert (
len(
_tm1(
"subprocess = None\n"
"Popen = None\n"
"import subprocess\n"
"from subprocess import Popen\n"
"enabled = True\n"
"subprocess.run('/usr/bin/true', shell=enabled)\n"
"Popen('/usr/bin/true', shell=enabled)\n"
)
)
== 2
)
def test_relative_import_does_not_establish_bare_popen() -> None:
assert not _tm1(
"Popen = proxy\nfrom .subprocess import Popen\nenabled = True\n"
"Popen(command, shell=enabled)\n"
)
def test_function_local_binding_and_outer_fact_are_independent() -> None:
findings = _tm1(
"outer = True\n"
"def execute(command):\n"
" enabled = 'True'\n"
" subprocess.run(command, shell=enabled)\n"
"subprocess.run('/usr/bin/true', shell=outer)\n"
)
assert [finding.start_line for finding in findings] == [4, 5]
def test_function_compile_time_receiver_shadow_rejects_earlier_lookup() -> None:
assert not _tm1(
"def execute(command):\n"
" enabled = True\n"
" subprocess.run(command, shell=enabled)\n"
" subprocess = Proxy()\n"
)
def test_later_global_receiver_mutation_does_not_suppress_earlier_function_call() -> None:
findings = _tm1(
"def execute(command):\n"
" enabled = True\n"
" subprocess.run(command, shell=enabled)\n"
"execute(command)\n"
"subprocess = Proxy()\n"
)
assert [finding.start_line for finding in findings] == [3]
def test_later_global_receiver_mutation_suppresses_unobserved_function_body() -> None:
assert not _tm1(
"def execute(command):\n"
" enabled = True\n"
" subprocess.run(command, shell=enabled)\n"
"subprocess = Proxy()\n"
)
def test_passive_function_definition_preserves_outer_fact() -> None:
assert (
len(
_tm1(
"enabled = True\n"
"def helper(value=1):\n"
" pass\n"
"subprocess.run(command, shell=enabled)\n"
)
)
== 1
)
@pytest.mark.parametrize(
"compound",
[
pytest.param("if condition:\n pass", id="if"),
pytest.param("for item in values:\n pass", id="for"),
pytest.param("with provider():\n pass", id="with"),
pytest.param("try:\n pass\nexcept Exception:\n pass", id="try"),
pytest.param("class Local:\n pass", id="class"),
],
)
def test_compound_statement_conservatively_clears_truth_facts(compound: str) -> None:
assert not _tm1_ast(f"enabled = True\n{compound}\nsubprocess.run(command, shell=enabled)\n")
def test_calls_inside_compound_statements_are_out_of_scope() -> None:
assert not _tm1_ast(
"if condition:\n enabled = True\n subprocess.run(command, shell=enabled)\n"
)
def test_class_body_same_scope_binding_is_tracked() -> None:
findings = _tm1(
"class Runner:\n enabled = True\n subprocess.run(command, shell=enabled)\n"
)
assert [finding.start_line for finding in findings] == [3]
def test_generic_call_invalidates_receiver_trust_in_class_body() -> None:
assert not _tm1_ast(
"class Runner:\n"
" replace_subprocess()\n"
" enabled = True\n"
" subprocess.run(command, shell=enabled)\n"
)
def test_class_method_does_not_close_over_class_binding() -> None:
assert not _tm1(
"class Runner:\n"
" enabled = True\n"
" def run(self):\n"
" subprocess.run(command, shell=enabled)\n"
)
def test_class_body_lookup_precedes_later_class_binding() -> None:
findings = _tm1(
"enabled = True\n"
"class Runner:\n"
" subprocess.run(command, shell=enabled)\n"
" enabled = False\n"
)
assert [finding.start_line for finding in findings] == [1]
@pytest.mark.parametrize(
"class_body",
[
pytest.param(
" enabled = subprocess.run(command, shell=enabled)\n",
id="assignment-rhs-before-store",
),
pytest.param(
" def enabled(value=subprocess.run(command, shell=enabled)):\n pass\n",
id="function-default-before-name-binding",
),
pytest.param(
" with manager(subprocess.run(command, shell=enabled)) as enabled:\n pass\n",
id="with-target-after-context",
),
pytest.param(
" try:\n"
" raise Error\n"
" except subprocess.run(command, shell=enabled) as enabled:\n"
" pass\n",
id="except-target-after-type",
),
],
)
def test_class_binding_is_installed_after_value_evaluation(class_body: str) -> None:
findings = _tm1("enabled = True\nclass Runner:\n" + class_body)
assert [finding.start_line for finding in findings] == [1]
def test_class_local_import_does_not_establish_method_receiver_trust() -> None:
assert not _tm1(
"subprocess = proxy\n"
"class Runner:\n"
" import subprocess\n"
" def run(self):\n"
" enabled = True\n"
" subprocess.run(command, shell=enabled)\n"
)
def test_class_local_shadow_does_not_hide_global_method_receiver() -> None:
findings = _tm1(
"import subprocess\n"
"class Runner:\n"
" subprocess = proxy\n"
" def run(self):\n"
" enabled = True\n"
" subprocess.run(command, shell=enabled)\n"
)
assert [finding.start_line for finding in findings] == [6]
def test_class_body_generic_call_invalidates_deferred_method_receiver() -> None:
assert not _tm1_ast(
"import subprocess\n"
"class Runner:\n"
" replace_subprocess()\n"
" def run(self):\n"
" enabled = True\n"
" subprocess.run(command, shell=enabled)\n"
)
def test_called_class_method_preserves_receiver_before_later_invalidation() -> None:
findings = _tm1(
"import subprocess\n"
"class Runner:\n"
" def run():\n"
" enabled = True\n"
" subprocess.run(command, shell=enabled)\n"
" run()\n"
" replace_subprocess()\n"
)
assert [finding.start_line for finding in findings] == [5]
def test_class_method_call_after_invalidation_is_rejected() -> None:
assert not _tm1_ast(
"import subprocess\n"
"class Runner:\n"
" def run():\n"
" enabled = True\n"
" subprocess.run(command, shell=enabled)\n"
" replace_subprocess()\n"
" run()\n"
)
@pytest.mark.parametrize(
"class_header",
[
pytest.param("@replace_subprocess()\nclass Runner:", id="decorator"),
pytest.param("class Runner(replace_subprocess()):", id="base"),
],
)
def test_effectful_class_header_invalidates_deferred_method_receiver(class_header: str) -> None:
assert not _tm1_ast(
"import subprocess\n"
f"{class_header}\n"
" def run(self):\n"
" enabled = True\n"
" subprocess.run(command, shell=enabled)\n"
)
def test_class_for_target_is_bound_after_iterable_evaluation() -> None:
findings = _tm1(
"enabled = True\n"
"class Runner:\n"
" for enabled in subprocess.run(command, shell=enabled):\n"
" pass\n"
)
assert [finding.start_line for finding in findings] == [1]
def test_comprehension_target_shadows_outer_shell_flag() -> None:
assert not _tm1(
"enabled = True\n"
"class Runner:\n"
" values = [\n"
" subprocess.run(command, shell=enabled)\n"
" for enabled in items\n"
" ]\n"
)
@pytest.mark.parametrize(
"argument",
[
pytest.param("disable()", id="call"),
pytest.param("mutator.command", id="attribute"),
pytest.param("holder[0]", id="subscript"),
pytest.param("left + right", id="operator"),
pytest.param("f'{value}'", id="formatted-string"),
pytest.param("[item for item in items]", id="comprehension"),
pytest.param("*commands", id="starred-expansion"),
],
)
def test_side_effect_capable_call_arguments_are_rejected(argument: str) -> None:
assert not _tm1_ast(f"enabled = True\nsubprocess.run({argument}, shell=enabled)\n")
@pytest.mark.parametrize(
"statement",
[
pytest.param(
"subprocess.run(command, shell=enabled, env=build_env())",
id="expression",
),
pytest.param(
"result = subprocess.run(command, shell=enabled, env=build_env())",
id="assignment",
),
pytest.param(
"result: object = subprocess.run(command, shell=enabled, env=build_env())",
id="annotated-assignment",
),
],
)
def test_later_keyword_effect_preserves_captured_shell_value(statement: str) -> None:
findings = _tm1(f"enabled = True\n{statement}\n")
literal_findings = _tm1(statement.replace("shell=enabled", "shell=True"))
assert len(findings) == len(literal_findings) == 1
assert findings[0].start_line == 2
assert findings[0].severity == literal_findings[0].severity
assert findings[0].confidence == literal_findings[0].confidence
@pytest.mark.parametrize(
"call",
[
pytest.param(
"subprocess.run(command, env=build_env(), shell=enabled)",
id="earlier-keyword",
),
pytest.param(
"subprocess.run(build_command(), shell=enabled)",
id="earlier-positional",
),
pytest.param(
"subprocess.run(shell=enabled, *build_args())",
id="starred-positional-written-later",
),
pytest.param(
"subprocess.run(command, **build_options(), shell=enabled)",
id="earlier-keyword-expansion",
),
],
)
def test_earlier_argument_effect_keeps_shell_value_uncertain(call: str) -> None:
assert not _tm1_ast(f"enabled = True\n{call}\n")
def test_later_keyword_expansion_preserves_captured_shell_value() -> None:
findings = _tm1("enabled = True\nsubprocess.run(command, shell=enabled, **build_options())\n")
assert len(findings) == 1
assert findings[0].start_line == 2
def test_later_argument_effect_invalidates_fact_after_captured_call() -> None:
findings = _tm1(
"enabled = True\n"
"subprocess.run(command, shell=enabled, env=build_env())\n"
"subprocess.run(command, shell=enabled)\n"
)
assert [finding.start_line for finding in findings] == [2]
@pytest.mark.parametrize(
"statement",
[
pytest.param(
"subprocess.run(command, shell=enabled, env=replace_subprocess())",
id="expression",
),
pytest.param(
"result = subprocess.run(command, shell=enabled, env=replace_subprocess())",
id="assignment",
),
pytest.param(
"result: object = subprocess.run(command, shell=enabled, env=replace_subprocess())",
id="annotated-assignment",
),
],
)
def test_later_argument_effect_invalidates_receiver_after_captured_call(statement: str) -> None:
findings = _tm1_ast(
"import subprocess\n"
"from helpers import replace_subprocess\n"
"enabled = True\n"
f"{statement}\n"
"later_enabled = True\n"
"subprocess.run(command, shell=later_enabled)\n"
)
assert [finding.location.start_line for finding in findings] == [4]
def test_later_argument_effect_invalidates_receiver_for_called_function() -> None:
findings = _tm1_ast(
"import subprocess\n"
"from helpers import replace_subprocess\n"
"enabled = True\n"
"subprocess.run(command, shell=enabled, env=replace_subprocess())\n"
"def execute():\n"
" later_enabled = True\n"
" subprocess.run(command, shell=later_enabled)\n"
"execute()\n"
)
assert [finding.location.start_line for finding in findings] == [4]
@pytest.mark.parametrize(
"statement",
[
pytest.param("replace_subprocess()", id="expression"),
pytest.param("result = replace_subprocess()", id="assignment"),
pytest.param("result: object = replace_subprocess()", id="annotated-assignment"),
],
)
def test_generic_call_invalidates_receiver_trust(statement: str) -> None:
findings = _tm1_ast(
"import subprocess\n"
"from helpers import replace_subprocess\n"
f"{statement}\n"
"enabled = True\n"
"subprocess.run(command, shell=enabled)\n"
)
assert not findings
@pytest.mark.parametrize(
"statement",
[
pytest.param("ignored = (replace_subprocess(),)", id="tuple-rhs"),
pytest.param("ignored = [replace_subprocess()]", id="list-rhs"),
pytest.param(
"ignored: tuple[object, ...] = (replace_subprocess(),)",
id="annotated-tuple-rhs",
),
],
)
def test_nested_generic_call_invalidates_receiver_trust(statement: str) -> None:
findings = _tm1_ast(
"import subprocess\n"
"from helpers import replace_subprocess\n"
f"{statement}\n"
"enabled = True\n"
"subprocess.run(command, shell=enabled)\n"
)
assert not findings
def test_nested_generic_call_invalidates_receiver_trust_for_called_function() -> None:
findings = _tm1_ast(
"import subprocess\n"
"from helpers import replace_subprocess\n"
"def execute():\n"
" enabled = True\n"
" subprocess.run(command, shell=enabled)\n"
"ignored = (replace_subprocess(),)\n"
"execute()\n"
)
assert not findings
def test_unsupported_eager_expression_invalidates_receiver_trust() -> None:
findings = _tm1_ast(
"import subprocess\n"
"from helpers import replace_subprocess\n"
"assert (replace_subprocess(),)\n"
"enabled = True\n"
"subprocess.run(command, shell=enabled)\n"
)
assert not findings
def test_unsupported_eager_expression_invalidates_called_function_trust() -> None:
findings = _tm1_ast(
"import subprocess\n"
"from helpers import replace_subprocess\n"
"def execute():\n"
" enabled = True\n"
" subprocess.run(command, shell=enabled)\n"
"assert (replace_subprocess(),)\n"
"execute()\n"
)
assert not findings
@pytest.mark.parametrize(
"statement",
[
pytest.param("(replace_subprocess(),)", id="tuple"),
pytest.param("[replace_subprocess()]", id="list"),
],
)
def test_eager_container_expression_invalidates_receiver_trust(statement: str) -> None:
findings = _tm1_ast(
"import subprocess\n"
"from helpers import replace_subprocess\n"
f"{statement}\n"
"enabled = True\n"
"subprocess.run(command, shell=enabled)\n"
)
assert not findings
@pytest.mark.parametrize(
"statement",
[
pytest.param("(replace_subprocess(),)", id="tuple"),
pytest.param("[replace_subprocess()]", id="list"),
],
)
def test_eager_container_expression_invalidates_called_function_trust(
statement: str,
) -> None:
findings = _tm1_ast(
"import subprocess\n"
"from helpers import replace_subprocess\n"
"def execute():\n"
" enabled = True\n"
" subprocess.run(command, shell=enabled)\n"
f"{statement}\n"
"execute()\n"
)
assert not findings
def test_protocol_consuming_direct_call_invalidates_called_function_trust() -> None:
findings = _tm1_ast(
"import subprocess\n"
"from helpers import mutator\n"
"def execute():\n"
" enabled = True\n"
" subprocess.run('/usr/bin/true', shell=enabled)\n"
"subprocess.run(mutator, shell=False)\n"
"execute()\n"
)
assert not findings
def test_unsafe_value_release_invalidates_called_function_trust() -> None:
findings = _tm1_ast(
"class Trigger:\n"
" def __del__(self):\n"
" global subprocess\n"
" subprocess = replacement\n"
"trigger = Trigger()\n"
"import subprocess\n"
"def execute():\n"
" enabled = True\n"
" subprocess.run('/usr/bin/true', shell=enabled)\n"
"trigger = 0\n"
"execute()\n"
)
assert not findings
@pytest.mark.parametrize(
"statement",
[
pytest.param("replace_subprocess()", id="direct-call"),
pytest.param("(replace_subprocess(),)", id="nested-call"),
],
)
def test_class_body_eager_call_invalidates_enclosing_receiver_trust(statement: str) -> None:
findings = _tm1_ast(
"import subprocess\n"
"from helpers import replace_subprocess\n"
"class Runner:\n"
f" {statement}\n"
"enabled = True\n"
"subprocess.run(command, shell=enabled)\n"
)
assert not findings
def test_class_body_outer_store_release_invalidates_receiver_trust() -> None:
findings = _tm1_ast(
"from helpers import trigger\n"
"import subprocess\n"
"class Runner:\n"
" global trigger\n"
" trigger = 0\n"
"enabled = True\n"
"subprocess.run('/bin/true', shell=enabled)\n"
)
assert not findings
def test_class_body_outer_store_release_invalidates_called_function_trust() -> None:
findings = _tm1_ast(
"from helpers import trigger\n"
"import subprocess\n"
"def execute():\n"
" enabled = True\n"
" subprocess.run('/bin/true', shell=enabled)\n"
"class Runner:\n"
" global trigger\n"
" trigger = 0\n"
"execute()\n"
)
assert not findings
def test_nested_class_outer_store_release_invalidates_receiver_trust() -> None:
findings = _tm1_ast(
"from helpers import trigger\n"
"import subprocess\n"
"class Outer:\n"
" class Inner:\n"
" global trigger\n"
" trigger = 0\n"
"enabled = True\n"
"subprocess.run('/bin/true', shell=enabled)\n"
)
assert not findings
def test_nested_class_outer_store_release_invalidates_called_function_trust() -> None:
findings = _tm1_ast(
"from helpers import trigger\n"
"import subprocess\n"
"def execute():\n"
" enabled = True\n"
" subprocess.run('/bin/true', shell=enabled)\n"
"class Outer:\n"
" class Inner:\n"
" global trigger\n"
" trigger = 0\n"
"execute()\n"
)
assert not findings
@pytest.mark.parametrize(
"definition",
[
pytest.param(" def trigger():\n pass\n", id="function"),
pytest.param(" class trigger:\n pass\n", id="class"),
],
)
def test_class_body_outer_definition_release_invalidates_receiver_trust(
definition: str,
) -> None:
findings = _tm1_ast(
"from helpers import trigger\n"
"import subprocess\n"
"class Outer:\n"
" global trigger\n"
+ definition
+ "enabled = True\n"
+ "subprocess.run('/bin/true', shell=enabled)\n"
)
assert not findings
@pytest.mark.parametrize(
"class_body",
[
pytest.param(
" @replace_subprocess\n def run():\n pass\n",
id="method-decorator",
),
pytest.param(
" class Inner(Base):\n pass\n",
id="nested-class-base",
),
pytest.param(
" @replace_subprocess\n class Inner:\n pass\n",
id="nested-class-decorator",
),
],
)
def test_class_implicit_eager_effect_invalidates_enclosing_receiver_trust(
class_body: str,
) -> None:
findings = _tm1_ast(
"from helpers import Base, replace_subprocess\n"
"import subprocess\n"
"class Runner:\n"
+ class_body
+ "enabled = True\n"
+ "subprocess.run('/bin/true', shell=enabled)\n"
)
assert not findings
@pytest.mark.parametrize(
"class_body",
[
pytest.param(" assert mutator\n", id="assert-truth"),
pytest.param(" target.attr = 1\n", id="attribute-store"),
pytest.param(" target += 1\n", id="augmented-assignment"),
pytest.param(
" with manager:\n pass\n",
id="context-manager",
),
],
)
def test_class_protocol_effect_invalidates_enclosing_receiver_trust(
class_body: str,
) -> None:
findings = _tm1_ast(
"from helpers import manager, mutator, target\n"
"import subprocess\n"
"class Runner:\n"
+ class_body
+ "enabled = True\n"
+ "subprocess.run('/bin/true', shell=enabled)\n"
)
assert not findings
def test_class_protocol_effect_invalidates_called_function_trust() -> None:
findings = _tm1_ast(
"from helpers import mutator\n"
"import subprocess\n"
"def execute():\n"
" enabled = True\n"
" subprocess.run('/bin/true', shell=enabled)\n"
"class Runner:\n"
" assert mutator\n"
"execute()\n"
)
assert not findings
@pytest.mark.parametrize(
"header",
[
pytest.param("@replace_subprocess\nclass Runner:", id="decorator-name"),
pytest.param("class Runner(Base):", id="base-name"),
],
)
def test_class_creation_invalidates_enclosing_receiver_trust(header: str) -> None:
findings = _tm1_ast(
"import subprocess\n"
"from helpers import Base, replace_subprocess\n"
f"{header}\n"
" pass\n"
"enabled = True\n"
"subprocess.run(command, shell=enabled)\n"
)
assert not findings
def test_import_binding_release_invalidates_receiver_trust() -> None:
findings = _tm1_ast(
"class Trigger:\n"
" def __del__(self):\n"
" global subprocess\n"
" subprocess = replacement\n"
"subprocess = Trigger()\n"
"import subprocess\n"
"enabled = True\n"
"subprocess.run('/bin/true', shell=enabled)\n"
)
assert not findings
def test_import_binding_release_invalidates_called_function_trust() -> None:
source = (
"class Trigger:\n"
" def __del__(self):\n"
" global subprocess\n"
" subprocess = replacement\n"
"subprocess = Trigger()\n"
"def execute():\n"
" enabled = True\n"
" subprocess.run('/bin/true', shell=enabled)\n"
"import subprocess\n"
"execute()\n"
)
# The release is an unknown effect: the companion abstains, and like the
# module-level layout the lexical signal stays because no store proves a
# replacement before the call.
assert not _tm1_ast(source)
findings = _tm1(source)
assert len(findings) == 1
assert findings[0].severity == "HIGH"
def test_class_binding_release_invalidates_receiver_trust() -> None:
findings = _tm1_ast(
"class Trigger:\n"
" def __del__(self):\n"
" global subprocess\n"
" subprocess = replacement\n"
"trigger = Trigger()\n"
"import subprocess\n"
"class trigger:\n"
" pass\n"
"enabled = True\n"
"subprocess.run('/bin/true', shell=enabled)\n"
)
assert not findings
def test_class_binding_release_invalidates_called_function_trust() -> None:
findings = _tm1_ast(
"class Trigger:\n"
" def __del__(self):\n"
" global subprocess\n"
" subprocess = replacement\n"
"trigger = Trigger()\n"
"import subprocess\n"
"def execute():\n"
" enabled = True\n"
" subprocess.run('/bin/true', shell=enabled)\n"
"class trigger:\n"
" pass\n"
"execute()\n"
)
assert not findings
def test_assert_truth_protocol_invalidates_receiver_trust() -> None:
findings = _tm1_ast(
"import subprocess\n"
"from helpers import mutator\n"
"assert mutator\n"
"enabled = True\n"
"subprocess.run(command, shell=enabled)\n"
)
assert not findings
def test_assert_truth_protocol_invalidates_called_function_trust() -> None:
findings = _tm1_ast(
"import subprocess\n"
"from helpers import mutator\n"
"def execute():\n"
" enabled = True\n"
" subprocess.run(command, shell=enabled)\n"
"assert mutator\n"
"execute()\n"
)
assert not findings