Skip to content

Commit 4d55779

Browse files
Address review: fuzz the secret-masking scans against the regular expressions they replaced
The PR description relied on the hand-written scans agreeing with the old regular expressions on 200000 randomized strings, but that comparison lived in a throwaway harness - the committed `gtest_mask_uri_password.cpp` only iterated a fixed corpus, so the fuzz coverage would not survive future edits to the scans. The randomized cross-checks are now part of the test: `maskURIPassword` over 200000 strings of the characters its regular expression assigns meaning to, and `maskURIUserinfo` / `maskPresignedURLParameters` over 50000 strings built from the tokens theirs care about, all with a deterministic seed so a failure is reproducible. #111994 (comment)
1 parent efb8f9e commit 4d55779

1 file changed

Lines changed: 59 additions & 0 deletions

File tree

‎src/Common/tests/gtest_mask_uri_password.cpp‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <Common/maskURIPassword.h>
44
#include <Common/re2.h>
55

6+
#include <random>
67
#include <string>
78
#include <vector>
89

@@ -77,6 +78,32 @@ TEST(MaskURIPassword, MatchesTheRegularExpressionItReplaced)
7778
}
7879
}
7980

81+
TEST(MaskURIPassword, AgreesWithTheRegularExpressionOnRandomStrings)
82+
{
83+
/// The fixed corpus above documents the interesting cases; this is the safety net for the ones
84+
/// nobody thought of. The alphabet is the characters the regular expression assigns meaning to.
85+
constexpr std::string_view alphabet = "ab:@/.\n";
86+
std::mt19937_64 rng(20260731); /// NOLINT(cert-msc32-c,cert-msc51-cpp) deterministic seed, so a failure is reproducible
87+
std::uniform_int_distribution<size_t> length_dist(0, 32);
88+
std::uniform_int_distribution<size_t> char_dist(0, alphabet.size() - 1);
89+
90+
for (size_t round = 0; round < 200000; ++round)
91+
{
92+
std::string input(length_dist(rng), ' ');
93+
for (auto & c : input)
94+
c = alphabet[char_dist(rng)];
95+
96+
std::string with_scan = input;
97+
std::string with_re2 = input;
98+
99+
bool scanned = maskURIPassword(&with_scan);
100+
bool matched = maskURIPasswordWithRE2(&with_re2);
101+
102+
ASSERT_EQ(scanned, matched) << "return value differs for: " << input;
103+
ASSERT_EQ(with_scan, with_re2) << "result differs for: " << input;
104+
}
105+
}
106+
80107
TEST(MaskURIPassword, MasksThePassword)
81108
{
82109
std::string uri = "https://user:[email protected]:443/db";
@@ -171,6 +198,38 @@ TEST(MaskS3URLCredentials, MatchTheRegularExpressionsTheyReplaced)
171198
}
172199
}
173200

201+
TEST(MaskS3URLCredentials, AgreeWithTheRegularExpressionsOnRandomStrings)
202+
{
203+
/// Random characters would almost never spell a presigned parameter name, so the strings are
204+
/// built from the tokens the two regular expressions care about.
205+
const std::vector<std::string> tokens = {
206+
"https://", "s3://", "1://", "-x://", ":/", "user", "pass", ":", "@", "/", "?", "&", "=", "#",
207+
"Signature", "AWSAccessKeyId", "Expires", "GoogleAccessId", "X-Amz-", "X-Goog-", "Credential", "_", "a", "\n", "",
208+
};
209+
std::mt19937_64 rng(20260731); /// NOLINT(cert-msc32-c,cert-msc51-cpp) deterministic seed, so a failure is reproducible
210+
std::uniform_int_distribution<size_t> count_dist(0, 12);
211+
std::uniform_int_distribution<size_t> token_dist(0, tokens.size() - 1);
212+
213+
/// Fewer rounds than for `maskURIPassword`: each round runs two scans and two regular
214+
/// expressions, and `RE2::GlobalReplace` dominates - sanitizer builds multiply that.
215+
for (size_t round = 0; round < 50000; ++round)
216+
{
217+
std::string input;
218+
for (size_t i = 0, count = count_dist(rng); i < count; ++i)
219+
input += tokens[token_dist(rng)];
220+
221+
std::string with_scan = input;
222+
std::string with_re2 = input;
223+
224+
ASSERT_EQ(maskURIUserinfo(with_scan), maskURIUserinfoWithRE2(with_re2)) << "userinfo return value differs for: " << input;
225+
ASSERT_EQ(with_scan, with_re2) << "userinfo result differs for: " << input;
226+
227+
ASSERT_EQ(maskPresignedURLParameters(with_scan), maskPresignedURLParametersWithRE2(with_re2))
228+
<< "presign return value differs for: " << input;
229+
ASSERT_EQ(with_scan, with_re2) << "presign result differs for: " << input;
230+
}
231+
}
232+
174233
TEST(MaskS3URLCredentials, MasksUserinfoAndPresignedParameters)
175234
{
176235
std::string url = "https://key:secret@bucket/path?X-Amz-Signature=abcdef&format=CSV";

0 commit comments

Comments
 (0)