Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_text/tests/scrub.rs

19.6 KiB, 9 runs

created by r1870400018:65666, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1//! `secret::scrub`, the content scrubber of Daimond's debug feed.
2//!
3//! The answers it must give are the JavaScript's, and `fe2o3_net/tests/lens.rs` holds it to the
4//! JavaScript over a table of 130 cases. These tests are the edges of that table, one rule at a
5//! time: the minimum length of each shape and one byte short of it, the word boundary, the
6//! places where JavaScript's `\s` and its code units differ from Rust's, the entropy catch at its
7//! threshold, and strings built to make a rule slow.
8//!
9//! Every credential is built at run time from a generator, never written out, so that the
10//! scanners reading this file find nothing in it to refuse.
11
12use oxedyne_fe2o3_core::prelude::*;
13use oxedyne_fe2o3_text::secret::{
14 self,
15 scrub,
16};
17
18use std::time::Instant;
19
20
21const ALNUM: &str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
22const B64: &str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
23const STD: &str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789/+";
24const UPPER: &str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
25const HEX: &str = "0123456789abcdef";
26
27/// `n` characters of an alphabet, from a linear congruential generator seeded by `seed`.
28fn run(n: usize, seed: u32, alpha: &str) -> String {
29 let a: Vec<char> = alpha.chars().collect();
30 let mut h = (seed as u64 * 2_654_435_761u64) as u32;
31 let mut s = String::new();
32 for _ in 0..n {
33 h = h.wrapping_mul(1_103_515_245).wrapping_add(12_345);
34 s.push(a[((h >> 16) as usize) % a.len()]);
35 }
36 s
37}
38
39/// The scrub of `v`, written in place, which is what a marker is.
40fn marked(kind: &str, v: &str) -> String {
41 scrub::mark(kind, v)
42}
43
44/// Does the scrub of `text` carry a marker of this shape?
45fn has(text: &str, kind: &str) -> bool {
46 scrub::text(text, false).contains(&fmt!("[redacted {} #", kind))
47}
48
49// An opening, what may follow it, how many of those at least, and the marker's name.
50const SHAPES: &[(&str, &str, &str, usize)] = &[
51 ("gh", "ghp_", ALNUM, 16),
52 ("gh", "gho_", ALNUM, 16),
53 ("gh", "ghu_", ALNUM, 16),
54 ("gh", "ghs_", ALNUM, 16),
55 ("gh", "ghr_", ALNUM, 16),
56 ("ghpat", "github_pat_", ALNUM, 20),
57 ("stripe", "sk_live_", ALNUM, 10),
58 ("stripe", "sk_test_", ALNUM, 10),
59 ("stripe", "rk_live_", ALNUM, 10),
60 ("stripe", "rk_test_", ALNUM, 10),
61 ("whsec", "whsec_", ALNUM, 16),
62 ("sk", "sk-", B64, 16),
63 ("aws", "AKIA", UPPER, 12),
64 ("aws", "ASIA", UPPER, 12),
65 ("aws", "ABIA", UPPER, 12),
66 ("aws", "ACCA", UPPER, 12),
67 ("aws", "AGPA", UPPER, 12),
68 ("aws", "AIDA", UPPER, 12),
69 ("aws", "AIPA", UPPER, 12),
70 ("aws", "ANPA", UPPER, 12),
71 ("aws", "ANVA", UPPER, 12),
72 ("aws", "AROA", UPPER, 12),
73 ("aws", "APKA", UPPER, 12),
74 ("gcp", "AIza", B64, 30),
75 ("slack", "xoxa-", ALNUM, 10),
76 ("slack", "xoxb-", ALNUM, 10),
77 ("slack", "xoxe-", ALNUM, 10),
78 ("slack", "xoxp-", ALNUM, 10),
79 ("slack", "xoxr-", ALNUM, 10),
80 ("slack", "xoxs-", ALNUM, 10),
81];
82
83#[test]
84fn a_marker_is_the_shape_a_hash_and_the_length_in_utf16_units() {
85 // djb2 over "abc" is 0xb885c8b, the figure `fingerprint` gives the same three characters.
86 assert_eq!(marked("sk", "abc"), "[redacted sk #b885c8b/3]");
87 assert_eq!(marked("x", ""), "[redacted x #1505/0]");
88 // An astral character is two UTF-16 units, so it counts two and hashes as two.
89 assert!(marked("x", "\u{1F600}").ends_with("/2]"), "{}", marked("x", "\u{1F600}"));
90 assert_ne!(marked("x", "\u{1F600}"), marked("x", "\u{1F601}"));
91 assert_ne!(marked("x", "ab"), marked("x", "ba"), "the hash separates two orders");
92}
93
94#[test]
95fn each_shape_is_replaced_whole_at_its_minimum_and_not_one_byte_short() -> Outcome<()> {
96 for (i, (kind, lead, alpha, min)) in SHAPES.iter().enumerate() {
97 let at = run(*min, 1000 + i as u32, alpha);
98 let cred = fmt!("{}{}", lead, at);
99 let text = fmt!("note {} end", cred);
100 req!(scrub::text(&text, false), fmt!("note {} end", marked(kind, &cred)), "at the minimum, for {}", lead);
101 // A longer run is taken whole.
102 let long = fmt!("{}{}", cred, run(9, 2000 + i as u32, alpha));
103 req!(scrub::text(&fmt!("<{}>", long), false), fmt!("<{}>", marked(kind, &long)), "longer, for {}", lead);
104 // One short is not the shape. A run that long may still be a high-entropy blob, so what is
105 // asked is that this shape's marker is absent.
106 let short = fmt!("note {}{} end", lead, run(*min - 1, 3000 + i as u32, alpha));
107 req!(has(&short, kind), false, "one short, for {}", lead);
108 }
109 Ok(())
110}
111
112#[test]
113fn a_tune_key_is_exactly_thirty_two_hex_digits_and_a_jwt_is_three_runs() -> Outcome<()> {
114 let key = fmt!("tune-{}", run(32, 41, HEX));
115 let more = run(8, 42, HEX);
116 // `{32}` takes 32 and no more: what stands after it is left where it was.
117 req!(scrub::text(&fmt!("a {}{} b", key, more), false), fmt!("a {}{} b", marked("tune", &key), more));
118 req!(scrub::text(&fmt!("a tune-{} b", run(31, 43, HEX)), false), fmt!("a tune-{} b", run(31, 43, HEX)));
119 // A JWT: the first run opens with `eyJ` and holds six more, the second six, the third four.
120 for (a, b, c, hit) in [(6, 6, 4, true), (5, 6, 4, false), (6, 5, 4, false), (6, 6, 3, false), (30, 90, 43, true)] {
121 let jwt = fmt!("eyJ{}.{}.{}", run(a, 51, B64), run(b, 52, B64), run(c, 53, B64));
122 let text = fmt!("x {} y", jwt);
123 if hit {
124 req!(scrub::text(&text, false), fmt!("x {} y", marked("jwt", &jwt)), "for {:?}", (a, b, c));
125 } else {
126 req!(scrub::hit(&text, false), false, "for {:?}", (a, b, c));
127 }
128 }
129 Ok(())
130}
131
132#[test]
133fn a_shape_needs_a_word_boundary_before_it_and_no_other_character_will_do() -> Outcome<()> {
134 for (i, (kind, lead, alpha, min)) in SHAPES.iter().enumerate() {
135 let cred = fmt!("{}{}", lead, run(*min, 4000 + i as u32, alpha));
136 // A word character before it, a letter, a digit or an underscore: no boundary.
137 for pre in ["x", "9", "_"] {
138 let text = fmt!("{}{}", pre, cred);
139 req!(has(&text, kind), false, "after {:?}, for {}", pre, lead);
140 }
141 // A hyphen, a slash or a quote before it is a boundary, however word-like it looks.
142 for pre in ["-", "/", "\"", " ", "="] {
143 let text = fmt!("a{}{}", pre, cred);
144 req!(scrub::text(&text, false), fmt!("a{}{}", pre, marked(kind, &cred)), "after {:?}, for {}", pre, lead);
145 }
146 }
147 Ok(())
148}
149
150#[test]
151fn a_pem_private_key_goes_to_its_footer_or_to_the_end_of_the_string() -> Outcome<()> {
152 let head = |algo: &str| fmt!("{}{}{}{}", "-----BEGIN ", algo, "PRIVATE KEY", "-----");
153 let foot = |algo: &str| fmt!("{}{}{}{}", "-----END ", algo, "PRIVATE KEY", "-----");
154 let body = fmt!("\n{}\n{}\n", run(64, 61, STD), run(64, 62, STD));
155 for algo in ["", "RSA ", "EC ", "OPENSSH ", "ENCRYPTED "] {
156 let whole = fmt!("{}{}{}", head(algo), body, foot(algo));
157 req!(scrub::text(&fmt!("before {} after", whole), false),
158 fmt!("before {} after", marked("pem", &whole)), "closed, for {:?}", algo);
159 // No footer, as when a log line was clipped: the rest of the string goes.
160 let open = fmt!("{}{}", head(algo), body);
161 req!(scrub::text(&fmt!("log {}", open), false), fmt!("log {}", marked("pem", &open)), "open, for {:?}", algo);
162 }
163 // The first footer ends it, and a second key is a second marker.
164 let one = fmt!("{}{}{}", head(""), body, foot(""));
165 let two = fmt!("{}\nmid\n{}", one, one);
166 req!(scrub::text(&two, false), fmt!("{}\nmid\n{}", marked("pem", &one), marked("pem", &one)));
167 // A certificate is no private key.
168 let cert = fmt!("{}{}{}\n{}\n{}\n{}\n{}{}{}", "-----BEGIN ", "CERTIFICATE", "-----",
169 run(16, 63, STD), run(16, 64, STD), run(8, 65, STD), "-----END ", "CERTIFICATE", "-----");
170 req!(scrub::text(&cert, false), cert.clone());
171 // The header's words are capitals, digits and spaces only.
172 req!(scrub::hit(&fmt!("{}{}{}{}", "-----BEGIN ", "rsa ", "PRIVATE KEY", "-----"), false), false);
173 Ok(())
174}
175
176#[test]
177fn a_pair_keeps_its_name_and_marks_only_the_value() -> Outcome<()> {
178 // Bearer, Basic and Token, one or more white spaces, then eight or more of the value's alphabet.
179 for word in ["Bearer", "Basic", "Token"] {
180 let v = run(20, 71, B64);
181 req!(scrub::text(&fmt!("Authorization: {} {}", word, v), false),
182 fmt!("Authorization: {} {}", word, marked("bearer", &v)), "for {}", word);
183 }
184 let v = run(8, 72, B64);
185 req!(scrub::text(&fmt!("Bearer \t {}", v), false), fmt!("Bearer \t {}", marked("bearer", &v)));
186 let seven = fmt!("Bearer {}", run(7, 73, B64));
187 req!(scrub::text(&seven, false), seven.clone());
188 // `bearer` is case-free, as a scheme is on the wire (SCRUB-ONE); `Basic` and `Token` are not,
189 // since prose uses both words lower case. The boundary before the word holds for all three.
190 let v = run(20, 74, B64);
191 req!(scrub::text(&fmt!("bearer {}", v), false), fmt!("bearer {}", marked("bearer", &v)));
192 req!(scrub::text(&fmt!("BEARER {}", v), false), fmt!("BEARER {}", marked("bearer", &v)));
193 req!(scrub::hit(&fmt!("basic {}", v), false), false);
194 req!(scrub::hit(&fmt!("token {}", v), false), false);
195 req!(scrub::hit(&fmt!("xBearer {}", run(20, 74, B64)), false), false);
196 Ok(())
197}
198
199#[test]
200fn a_url_argument_is_matched_without_regard_to_case_and_counts_utf16_units() -> Outcome<()> {
201 for name in ["access_token", "refresh_token", "id_token", "token", "api_key", "api-key", "apikey", "key",
202 "auth", "secret", "password", "passwd", "sig", "signature"]
203 {
204 for case in [name.to_string(), name.to_uppercase()] {
205 let v = run(12, 81, ALNUM);
206 for lead in ["?", "&", "#"] {
207 req!(scrub::text(&fmt!("https://h.example/p{}{}={}&z=1", lead, case, v), false),
208 fmt!("https://h.example/p{}{}={}&z=1", lead, case, marked("urlarg", &v)), "for {} {}", lead, case);
209 }
210 }
211 }
212 // Four units or more. An astral character is two, so two of them make four and one makes two.
213 let two = "\u{1F600}\u{1F600}";
214 req!(scrub::text(&fmt!("u?key={}&z", two), false), fmt!("u?key={}&z", marked("urlarg", two)));
215 req!(marked("urlarg", two).ends_with("/4]"), true);
216 req!(scrub::hit("u?key=\u{1F600}&z", false), false);
217 // The value stops at a quote, an angle bracket, a bracket or any JavaScript white space.
218 for stop in ["\"", "'", "<", ">", "[", "]", "#", "&", " ", "\u{00A0}", "\u{FEFF}", "\u{3000}"] {
219 let v = run(6, 82, ALNUM);
220 req!(scrub::text(&fmt!("?token={}{}tail", v, stop), false), fmt!("?token={}{}tail", marked("urlarg", &v), stop), "stops at {:?}", stop);
221 }
222 // And a name that is only part of a longer one is not the name.
223 req!(scrub::hit(&fmt!("?monkey={}", run(12, 83, ALNUM)), false), false);
224 req!(scrub::hit(&fmt!("?keys={}", run(12, 83, ALNUM)), false), false);
225 Ok(())
226}
227
228#[test]
229fn javascripts_white_space_is_not_unicodes() -> Outcome<()> {
230 // U+FEFF is white space to JavaScript's `\s`, and U+0085 is not; Rust's `char::is_whitespace`
231 // has it the other way round, so the port cannot lean on it.
232 let v = run(12, 91, B64);
233 req!(scrub::text(&fmt!("Bearer\u{FEFF}{}", v), false), fmt!("Bearer\u{FEFF}{}", marked("bearer", &v)));
234 req!(scrub::text(&fmt!("Bearer\u{2028}{}", v), false), fmt!("Bearer\u{2028}{}", marked("bearer", &v)));
235 req!(scrub::hit(&fmt!("Bearer\u{0085}{}", v), false), false);
236 Ok(())
237}
238
239#[test]
240fn a_secret_name_against_a_value_is_matched_through_up_to_three_quotes_and_spaces() -> Outcome<()> {
241 for name in ["apikey", "api_key", "api-key", "auth_token", "auth-token", "authtoken", "access_token",
242 "accesstoken", "token", "secret", "passphrase", "password", "master_key", "masterkey",
243 "private-key", "privatekey", "mnemonic", "seed_phrase", "seedphrase", "salt", "sealed", "wrapped",
244 "API_KEY", "Password"]
245 {
246 let v = run(16, 101, ALNUM);
247 // A typed name's value is the rest of its line, since a passphrase is words (SCRUB-ONE).
248 let typed = ["passphrase", "password", "mnemonic", "seed_phrase", "seedphrase", "Password"].contains(&name);
249 let want = if typed {
250 fmt!("cfg {}: {}", name, marked("named", &fmt!("{} end", v)))
251 } else {
252 fmt!("cfg {}: {} end", name, marked("named", &v))
253 };
254 req!(scrub::text(&fmt!("cfg {}: {} end", name, v), false), want, "for {}", name);
255 req!(scrub::text(&fmt!("cfg {}=\"{}\"", name, v), false),
256 fmt!("cfg {}=\"{}\"", name, marked("named", &v)), "quoted, for {}", name);
257 }
258 let v = run(10, 102, ALNUM);
259 // Three quotes or spaces either side of the colon, and not four.
260 req!(scrub::text(&fmt!("token\" : \"'{}", v), false), fmt!("token\" : \"'{}", marked("named", &v)));
261 req!(scrub::hit(&fmt!("token\" : {}", v), false), false);
262 // A quoted value is taken whole at any length, so a lone quote mark between two is one, and
263 // the run after it is not the value (the JavaScript answers the same).
264 req!(scrub::text(&fmt!("token: \"'\" {}", v), false), fmt!("token: \"{}\" {}", marked("named", "'"), v));
265 // Eight value characters, the alphabet with `.`, `/`, `+` and `=` in it.
266 req!(scrub::text("token=ab.cd/e+=", false), fmt!("token={}", marked("named", "ab.cd/e+=")));
267 req!(scrub::hit("token=abcdefg", false), false);
268 // A name is not a name in the middle of a word it does not end: there is no boundary rule on
269 // the front of this one, so a suffix counts.
270 req!(scrub::text(&fmt!("mytoken={}", v), false), fmt!("mytoken={}", marked("named", &v)));
271 Ok(())
272}
273
274#[test]
275fn an_aws_secret_is_found_beside_a_key_id_the_shapes_did_not_take_or_beside_its_name() -> Outcome<()> {
276 let id = fmt!("AKIA{}", run(16, 111, UPPER));
277 let sec = run(40, 112, STD);
278 // With a word boundary the id is scrubbed first, and nothing is left for the pair to stand
279 // beside: the secret keeps its own bytes, as it does in the JavaScript.
280 let plain = fmt!("{} and its secret {} here", id, sec);
281 req!(scrub::text(&plain, false), fmt!("{} and its secret {} here", marked("aws", &id), sec));
282 // Without one, the shape passes the id by and the pair takes the secret.
283 let bound = fmt!("x{} and its secret {} here", id, sec);
284 req!(scrub::text(&bound, false), fmt!("x{} and its secret {} here", id, marked("awssec", &sec)));
285 // The gap between them is at most 200 units, and the separator is one of a quote, a space, a
286 // colon, an equals sign or a comma.
287 for (gap, ok) in [(0usize, true), (150, true), (199, true), (200, true), (201, false)] {
288 let text = fmt!("x{}{}:{}", id, "-".repeat(gap), sec);
289 if ok {
290 req!(scrub::text(&text, false), fmt!("x{}{}:{}", id, "-".repeat(gap), marked("awssec", &sec)), "gap {}", gap);
291 } else {
292 req!(scrub::text(&text, false), text.clone(), "gap {}", gap);
293 }
294 }
295 // Forty exactly: a forty-first character of the value's alphabet means it is no secret.
296 let long = fmt!("x{} {}{}", id, sec, "a");
297 req!(scrub::hit(&long, false), false);
298 // And by its name, with or without `aws`, in any case.
299 for name in ["aws_secret_access_key", "AWS_SECRET_ACCESS_KEY", "secret_access_key", "awssecretaccesskey",
300 "SecretAccessKey"]
301 {
302 req!(scrub::text(&fmt!("{} = {}", name, sec), false), fmt!("{} = {}", name, marked("awssec", &sec)), "for {}", name);
303 req!(scrub::text(&fmt!("{}: \"{}\"", name, sec), false), fmt!("{}: \"{}\"", name, marked("awssec", &sec)), "quoted, for {}", name);
304 }
305 req!(scrub::hit(&fmt!("aws_secret_access_key = {}", run(39, 113, STD)), false), true,
306 "thirty-nine is no secret to this rule, but it is a long high-entropy run");
307 Ok(())
308}
309
310#[test]
311fn the_entropy_catch_stops_at_its_threshold_and_spares_what_a_feed_carries() -> Outcome<()> {
312 req!(scrub::entropy("aaaa"), 0.0);
313 req!(scrub::entropy("abab"), 1.0);
314 req!(scrub::entropy("abcd"), 2.0);
315 req!(scrub::entropy("0123456789abcdef"), 4.0);
316 // Sixteen symbols, each twice, is exactly 4.0 bits and is not above it. They are not all hex,
317 // all digits or all letters, so the safe-run rules do not decide it.
318 let sixteen = "ghijklmn12345678ghijklmn12345678";
319 req!(sixteen.len(), 32);
320 req!(scrub::entropy(sixteen), 4.0);
321 req!(scrub::safe_run(sixteen), false);
322 req!(scrub::text(&fmt!("x {} y", sixteen), false), fmt!("x {} y", sixteen));
323 // Seventeen, each twice, is above it.
324 let seventeen = "ghijklmno12345678ghijklmno12345678";
325 req!(scrub::text(&fmt!("x {} y", seventeen), false), fmt!("x {} y", marked("hi", seventeen)));
326 // A run of 31 is under the minimum however random it is, and 32 is not.
327 req!(scrub::hit(&run(31, 121, B64), false), false);
328 req!(scrub::hit(&run(32, 121, B64), false), true);
329 // The correlation-id fields are spared the catch alone.
330 let r = run(40, 122, B64);
331 req!(scrub::text(&r, true), r.clone());
332 req!(scrub::text(&r, false), marked("hi", &r));
333 let key = fmt!("sk-{}", run(20, 123, B64));
334 req!(scrub::text(&key, true), marked("sk", &key), "a shape is not spared");
335 Ok(())
336}
337
338#[test]
339fn what_a_feed_is_meant_to_carry_is_a_safe_run() {
340 for s in [
341 "0a1b2c3d4e5f60718293a4b5c6d7e8f9", // hex
342 "0A1B-2C3D_4E5F-6071-8293", // hex joined by separators, case unimportant
343 "fb9ba89bc9a6abe785f300ee_83dec58094a8", // the same with a separator in the middle
344 "12345678901234567890123456789012", // decimal
345 "abcdefghijklmnopqrstuvwxyzabcdef", // letters
346 "x_y_zz", // letters once the separators are gone
347 "worker_pool_seat_000412_alpha", // words and numbers
348 ] {
349 assert!(scrub::safe_run(s), "{:?}", s);
350 }
351 for s in [
352 "build-6911be85c0f7-seq-312-stamp", // a segment that is hex and no word or number
353 "gh_c1_zz", // `c1` is neither
354 "gh--12", // an empty segment is neither
355 "g_1", // a single letter is not a word
356 "",
357 "Zm9vYmFy-Z2hpamts_bW5vcA", // base64url
358 ] {
359 assert!(!scrub::safe_run(s), "{:?}", s);
360 }
361}
362
363#[test]
364fn nothing_is_changed_that_has_nothing_in_it() {
365 for s in ["", "short", " ", "plain prose with no credential in it, only words and 12345",
366 "/usr/local/bin/some_tool --flag=value --other 12", "caf\u{e9} \u{65e5}\u{672c}\u{8a9e} \u{1F600}"]
367 {
368 assert_eq!(scrub::text(s, false), s);
369 assert!(!scrub::hit(s, false), "{:?}", s);
370 }
371}
372
373#[test]
374fn scrubbing_twice_changes_nothing_the_second_time() -> Outcome<()> {
375 let text = fmt!("first sk_test_{} then eyJ{}.{}.{} then AKIA{}\nAuthorization: Bearer {}\n?token={}&k=1\nblob {}",
376 run(24, 131, ALNUM), run(10, 132, B64), run(10, 133, B64), run(10, 134, B64), run(16, 135, UPPER),
377 run(20, 136, B64), run(12, 137, ALNUM), run(48, 138, B64));
378 let once = scrub::text(&text, false);
379 assert!(once != text, "the text held credentials and was not changed");
380 req!(scrub::text(&once, false), once.clone());
381 req!(scrub::hit(&once, false), false);
382 Ok(())
383}
384
385#[test]
386fn the_hooks_scan_reports_what_it_reported_before() -> Outcome<()> {
387 // Daimond's shapes are a table of their own. A shape the scrubber has and the hook has not
388 // is no business of a commit.
389 let cases = [
390 fmt!("eyJ{}.{}.{}", run(8, 141, B64), run(12, 142, B64), run(6, 143, B64)),
391 fmt!("whsec_{}", run(20, 144, ALNUM)),
392 fmt!("sk_test_{}", run(14, 145, ALNUM)),
393 fmt!("tune-{}", run(32, 146, HEX)),
394 fmt!("ASIA{}", run(14, 147, UPPER)),
395 fmt!("Authorization: Bearer {}", run(20, 148, B64)),
396 fmt!("?token={}", run(12, 149, ALNUM)),
397 fmt!("blob {}", run(40, 150, B64)),
398 ];
399 for c in &cases {
400 req!(scrub::hit(c, false), true, "{:?}", c);
401 req!(secret::scan(c.as_bytes()).is_empty(), true, "scan, for {:?}", c);
402 req!(secret::holds(c), false, "holds, for {:?}", c);
403 }
404 req!(secret::leads_are_covered(), true);
405 Ok(())
406}
407
408#[test]
409fn a_hostile_string_does_not_make_a_rule_slow() -> Outcome<()> {
410 // A rule that restarts a long run from every opening is quadratic, and a feed's author may be
411 // anyone. Each of these is a long run with an opening at every few bytes.
412 let n = 30_000;
413 let hostile = [
414 "eyJ".repeat(n),
415 "AKIA".repeat(n),
416 "ASIA".repeat(n / 2) + " ,",
417 "sk-".repeat(n) + "!",
418 "-----BEGIN ".repeat(n / 4),
419 fmt!("{}{}", "-----BEGIN ", "PRIVATE KEY-----").repeat(n / 8),
420 "token=".repeat(n),
421 "?key=".repeat(n),
422 "Bearer ".repeat(n),
423 "aws_secret_access_key=".repeat(n / 4),
424 "a".repeat(n * 4),
425 "a_".repeat(n * 2),
426 "x-".repeat(n * 2) + &run(40, 160, B64),
427 "AKIA".repeat(n) + &" ".repeat(300) + &run(40, 161, STD),
428 ];
429 for h in &hostile {
430 let t = Instant::now();
431 let _ = scrub::text(h, false);
432 let _ = scrub::text(h, true);
433 let ms = t.elapsed().as_millis();
434 assert!(ms < 10_000, "{} ms on a {}-byte string opening {:?}", ms, h.len(), &h[..h.len().min(24)]);
435 }
436 Ok(())
437}