|
| 1 | + |
| 2 | +import sys |
| 3 | +import os |
| 4 | + |
| 5 | +sys.path.append(os.path.dirname(os.path.dirname((__file__)))) |
| 6 | +from testlib import expects |
| 7 | + |
| 8 | +# These are defined so that we can evaluate the test code. |
| 9 | +NONSOURCE = "not a source" |
| 10 | +SOURCE = "source" |
| 11 | + |
| 12 | + |
| 13 | +def is_source(x): |
| 14 | + return x == "source" or x == b"source" or x == 42 or x == 42.0 or x == 42j |
| 15 | + |
| 16 | + |
| 17 | +def SINK(x): |
| 18 | + if is_source(x): |
| 19 | + print("OK") |
| 20 | + else: |
| 21 | + print("Unexpected flow", x) |
| 22 | + |
| 23 | + |
| 24 | +def SINK_F(x): |
| 25 | + if is_source(x): |
| 26 | + print("Unexpected flow", x) |
| 27 | + else: |
| 28 | + print("OK") |
| 29 | + |
| 30 | +ensure_tainted = ensure_not_tainted = print |
| 31 | +TAINTED_STRING = "TAINTED_STRING" |
| 32 | + |
| 33 | +from foo import MS_identity, MS_apply_lambda, MS_reversed, MS_list_map, MS_append_to_list |
| 34 | + |
| 35 | +# Simple summary |
| 36 | +via_identity = MS_identity(SOURCE) |
| 37 | +SINK(via_identity) # $ flow="SOURCE, l:-1 -> via_identity" |
| 38 | + |
| 39 | +tainted = MS_identity(TAINTED_STRING) |
| 40 | +ensure_tainted(tainted) # $ tainted |
| 41 | + |
| 42 | + |
| 43 | +# Lambda summary |
| 44 | +via_lambda = MS_apply_lambda(lambda x: [x], SOURCE) |
| 45 | +SINK(via_lambda[0]) # $ flow="SOURCE, l:-1 -> via_lambda[0]" |
| 46 | + |
| 47 | +tainted_lambda = MS_apply_lambda(lambda x: [x], TAINTED_STRING) |
| 48 | +ensure_tainted(tainted_lambda) # $ tainted |
| 49 | + |
| 50 | + |
| 51 | +# A lambda that breaks the flow |
| 52 | +not_via_lambda = MS_apply_lambda(lambda x: 1, SOURCE) |
| 53 | +SINK_F(not_via_lambda) |
| 54 | + |
| 55 | +untainted_lambda = MS_apply_lambda(lambda x: 1, TAINTED_STRING) |
| 56 | +ensure_not_tainted(untainted_lambda) |
| 57 | + |
| 58 | +# Collection summaries |
| 59 | +via_reversed = MS_reversed([SOURCE]) |
| 60 | +SINK(via_reversed[0]) # $ flow="SOURCE, l:-1 -> via_reversed[0]" |
| 61 | + |
| 62 | +tainted_list = MS_reversed([TAINTED_STRING]) |
| 63 | +ensure_tainted(tainted_list[0]) # $ tainted |
| 64 | + |
| 65 | +# Complex summaries |
| 66 | +def box(x): |
| 67 | + return [x] |
| 68 | + |
| 69 | +via_map = MS_list_map(box, [SOURCE]) |
| 70 | +SINK(via_map[0][0]) # $ flow="SOURCE, l:-1 -> via_map[0][0]" |
| 71 | + |
| 72 | +tainted_mapped = MS_list_map(box, [TAINTED_STRING]) |
| 73 | +ensure_tainted(tainted_mapped[0][0]) # $ tainted |
| 74 | + |
| 75 | +def explicit_identity(x): |
| 76 | + return x |
| 77 | + |
| 78 | +via_map_explicit = MS_list_map(explicit_identity, [SOURCE]) |
| 79 | +SINK(via_map_explicit[0]) # $ flow="SOURCE, l:-1 -> via_map_explicit[0]" |
| 80 | + |
| 81 | +tainted_mapped_explicit = MS_list_map(explicit_identity, [TAINTED_STRING]) |
| 82 | +tainted_mapped_explicit_implicit = MS_list_map(explicit_identity, TAINTED_LIST) |
| 83 | +ensure_tainted( |
| 84 | + tainted_mapped_explicit, # $ tainted |
| 85 | + tainted_mapped_explicit[0], # $ tainted |
| 86 | + tainted_mapped_explicit_implicit, # $ tainted |
| 87 | + tainted_mapped_explicit_implicit[0] # $ tainted |
| 88 | + ) |
| 89 | + |
| 90 | +via_map_summary = MS_list_map(MS_identity, [SOURCE]) |
| 91 | +SINK(via_map_summary[0]) # $ flow="SOURCE, l:-1 -> via_map_summary[0]" |
| 92 | + |
| 93 | +tainted_mapped_summary = MS_list_map(MS_identity, [TAINTED_STRING]) |
| 94 | +tainted_mapped_summary_implicit = MS_list_map(MS_identity, TAINTED_LIST) |
| 95 | +ensure_tainted( |
| 96 | + tainted_mapped_summary, # $ tainted |
| 97 | + tainted_mapped_summary[0], # $ tainted |
| 98 | + tainted_mapped_summary_implicit, # $ tainted |
| 99 | + tainted_mapped_summary_implicit[0] # $ tainted |
| 100 | + ) |
| 101 | + |
| 102 | +via_append_el = MS_append_to_list([], SOURCE) |
| 103 | +SINK(via_append_el[0]) # $ flow="SOURCE, l:-1 -> via_append_el[0]" |
| 104 | + |
| 105 | +tainted_list_el = MS_append_to_list([], TAINTED_STRING) |
| 106 | +ensure_tainted( |
| 107 | + tainted_list_el, # $ tainted |
| 108 | + tainted_list_el[0] # $ tainted |
| 109 | + ) |
| 110 | + |
| 111 | +via_append = MS_append_to_list([SOURCE], NONSOURCE) |
| 112 | +SINK(via_append[0]) # $ flow="SOURCE, l:-1 -> via_append[0]" |
| 113 | + |
| 114 | +tainted_list = MS_append_to_list([TAINTED_STRING], NONSOURCE) |
| 115 | +tainted_list_implicit = MS_append_to_list(TAINTED_LIST, NONSOURCE) |
| 116 | +ensure_tainted( |
| 117 | + tainted_list, # $ tainted |
| 118 | + tainted_list[0], # $ tainted |
| 119 | + tainted_list_implicit, # $ tainted |
| 120 | + tainted_list_implicit[0] # $ tainted |
| 121 | + ) |
| 122 | + |
| 123 | +from json import MS_loads as json_loads |
| 124 | +tainted_resultlist = json_loads(TAINTED_STRING) |
| 125 | +ensure_tainted( |
| 126 | + tainted_resultlist, # $ tainted |
| 127 | + tainted_resultlist[0] # $ tainted |
| 128 | + ) |
0 commit comments