|
6 | 6 | describe GraphQL::Tracing::PerfettoTrace do |
7 | 7 | include PerfettoSnapshot |
8 | 8 |
|
| 9 | + def trace_includes?(json_str, test_str) |
| 10 | + json_str.include?(Base64.encode64(test_str).strip) || |
| 11 | + json_str.include?(test_str) |
| 12 | + end |
| 13 | + |
9 | 14 | class PerfettoSchema < GraphQL::Schema |
10 | 15 | class BaseObject < GraphQL::Schema::Object |
11 | 16 | end |
@@ -110,14 +115,21 @@ class SecretInput < GraphQL::Schema::InputObject |
110 | 115 | argument :password, String |
111 | 116 | end |
112 | 117 |
|
113 | | - field :secret_field, String do |
| 118 | + class SecretThing < GraphQL::Schema::Object |
| 119 | + field :greeting, String |
| 120 | + end |
| 121 | + field :secret_field, SecretThing do |
114 | 122 | argument :cipher, String, required: false |
115 | 123 | argument :password, String, required: false |
116 | 124 | argument :input, [[SecretInput]], required: false |
117 | 125 | end |
118 | 126 |
|
119 | 127 | def secret_field(cipher: nil, password: nil, input: nil) |
120 | | - cipher || password || input[0][0][:password] |
| 128 | + { |
| 129 | + greeting: "Hello!", |
| 130 | + cipher: cipher || "FALLBACK_CIPHER", |
| 131 | + password: password || (input ? input[0][0][:password] : "FALLBACK_PASSWORD"), |
| 132 | + } |
121 | 133 | end |
122 | 134 | end |
123 | 135 |
|
@@ -174,40 +186,43 @@ def self.detailed_trace?(q) |
174 | 186 | end |
175 | 187 |
|
176 | 188 | it "filters params with ActiveSupport" do |
177 | | - query_str = 'query getStuff { secretField(cipher: "abcdef") }' |
| 189 | + query_str = 'query getStuff { secretField(cipher: "abcdef") { greeting } }' |
178 | 190 | res = PerfettoSchema.execute(query_str, validate: false) |
179 | 191 | json = res.context.query.current_trace.write(file: nil, debug_json: true) |
180 | | - assert_includes json, "abcdef" |
181 | | - refute_includes json, "FILTERED" |
| 192 | + assert trace_includes?(json, "abcdef") |
| 193 | + refute trace_includes?(json, "FILTERED") |
182 | 194 |
|
183 | 195 | prev_fp = ActiveSupport.filter_parameters |
184 | 196 | ActiveSupport.filter_parameters = ["cipher"] |
185 | 197 | res = PerfettoSchema.execute(query_str) |
186 | 198 | json = res.context.query.current_trace.write(file: nil, debug_json: true) |
187 | | - refute_includes json, "abcdef" |
188 | | - assert_includes json, "[FILTERED]" |
| 199 | + refute trace_includes?(json, "abcdef") |
| 200 | + assert trace_includes?(json, "[FILTERED]") |
189 | 201 |
|
190 | 202 | ActiveSupport.filter_parameters = ["password"] |
191 | | - res = PerfettoSchema.execute('query getStuff { secretField(input: [[{ password: "jklmn" }]]) }') |
| 203 | + res = PerfettoSchema.execute('query getStuff { secretField(input: [[{ password: "jklmn" }]]) { greeting } }') |
192 | 204 | json = res.context.query.current_trace.write(file: nil, debug_json: true) |
193 | | - refute json.include?("jklmn"), "Value is removed" |
| 205 | + assert trace_includes?(json, "password"), "Name is retained" |
| 206 | + refute trace_includes?(json, "jklmn"), "Value is removed" |
194 | 207 | assert_includes json, "[FILTERED]" |
195 | 208 | ensure |
196 | 209 | ActiveSupport.filter_parameters = prev_fp |
197 | 210 | end |
198 | 211 |
|
199 | 212 | it "filters params without ActiveSupport" do |
200 | | - query_str = 'query getStuff { secretField(password: "qrstuv") }' |
| 213 | + query_str = 'query getStuff { secretField(password: "qrstuv") { greeting } }' |
201 | 214 | res = PerfettoSchema.execute(query_str, context: { detailed_trace_filter: GraphQL::Tracing::PerfettoTrace::ArgumentsFilter.new }) |
202 | 215 | json = res.context.query.current_trace.write(file: nil, debug_json: true) |
203 | | - assert_includes json, "[FILTERED]" |
204 | | - refute_includes json, "qrstuv" |
| 216 | + assert trace_includes?(json, "FILTERED"), "The replacement string is present" |
| 217 | + assert trace_includes?(json, "FALLBACK_CIPHER"), "Unfiltered values are present" |
| 218 | + refute trace_includes?(json, "qrstuv"), "The password is obscured" |
205 | 219 |
|
206 | | - query_str = 'query getStuff { secretField(input: [[{ password: "lmnop" }]]) }' |
| 220 | + query_str = 'query getStuff { secretField(input: [[{ password: "lmnop" }]]) { greeting } }' |
207 | 221 | res = PerfettoSchema.execute(query_str, context: { detailed_trace_filter: GraphQL::Tracing::PerfettoTrace::ArgumentsFilter.new }) |
208 | 222 | json = res.context.query.current_trace.write(file: nil, debug_json: true) |
209 | | - refute json.include?("lmnop"), "The password is obscured" |
210 | | - assert json.include?("[FILTERED]"), "The replacement string is present" |
| 223 | + assert trace_includes?(json, "password"), "Name is retained" |
| 224 | + refute trace_includes?(json, "lmnop"), "The password is obscured" |
| 225 | + assert trace_includes?(json, "[FILTERED]"), "The replacement string is present" |
211 | 226 | end |
212 | 227 |
|
213 | 228 | it "provides an error when google-protobuf isn't available" do |
|
0 commit comments