Skip to content

Commit 34521cc

Browse files
authored
Feedback from Java Integration (#8272)
* ensure we get the exact same version of the list. this is probably paranoia but nothing telling we can't do it * make it so that a crashing body key sanitizer is logged, but doesn't kill the sanitization session * handle when the targeted path actually exists before attempting to secret scan it * remove duplicate sanitizer
1 parent 74f5758 commit 34521cc

4 files changed

Lines changed: 45 additions & 31 deletions

File tree

tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/ModifiableRecordSession.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,20 @@ public class ModifiableRecordSession
1414

1515
public ModifiableRecordSession(SanitizerDictionary sanitizerRegistry, string sessionId)
1616
{
17-
this.AppliedSanitizers = sanitizerRegistry.SessionSanitizers.ToList();
17+
lock(sanitizerRegistry.SessionSanitizerLock)
18+
{
19+
this.AppliedSanitizers = sanitizerRegistry.SessionSanitizers.ToList();
20+
}
1821
this.SessionId = sessionId;
1922
}
2023

2124
public ModifiableRecordSession(RecordSession session, SanitizerDictionary sanitizerRegistry, string sessionId)
2225
{
2326
Session = session;
24-
this.AppliedSanitizers = sanitizerRegistry.SessionSanitizers.ToList();
27+
lock (sanitizerRegistry.SessionSanitizerLock)
28+
{
29+
this.AppliedSanitizers = sanitizerRegistry.SessionSanitizers.ToList();
30+
}
2531
this.SessionId = sessionId;
2632
}
2733

tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/SanitizerDictionary.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -607,10 +607,6 @@ public SanitizerDictionary() {
607607
new BodyKeySanitizer("$..apiKey"),
608608
"AZSDK3480"
609609
),
610-
new RegisteredSanitizer(
611-
new BodyKeySanitizer("$..connectionString"),
612-
"AZSDK3481"
613-
),
614610
new RegisteredSanitizer(
615611
new BodyKeySanitizer("$..password"),
616612
"AZSDK3482"

tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/SecretScanner.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,25 @@ public List<Tuple<string, Detection>> DiscoverSecrets(string assetRepoRoot, IEnu
3838

3939
Parallel.ForEach(relativePaths, options, (filePath) =>
4040
{
41-
var content = File.ReadAllText(Path.Combine(assetRepoRoot, filePath));
42-
var fileDetections = DetectSecrets(content);
41+
var path = Path.Combine(assetRepoRoot, filePath);
4342

44-
if (fileDetections != null && fileDetections.Count > 0)
43+
if (File.Exists(path))
4544
{
46-
foreach (Detection detection in fileDetections)
45+
var content = File.ReadAllText(path);
46+
var fileDetections = DetectSecrets(content);
47+
48+
if (fileDetections != null && fileDetections.Count > 0)
4749
{
48-
detectedSecrets.Add(Tuple.Create(filePath, detection));
50+
foreach (Detection detection in fileDetections)
51+
{
52+
detectedSecrets.Add(Tuple.Create(filePath, detection));
53+
}
4954
}
50-
}
5155

52-
Interlocked.Increment(ref seen);
56+
Interlocked.Increment(ref seen);
5357

54-
Console.Write($"\r\u001b[2KScanned {seen}/{total}.");
58+
Console.Write($"\r\u001b[2KScanned {seen}/{total}.");
59+
}
5560
});
5661

5762
Console.WriteLine(string.Empty);

tools/test-proxy/Azure.Sdk.Tools.TestProxy/Sanitizers/BodyKeySanitizer.cs

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -64,33 +64,40 @@ public override string SanitizeTextBody(string contentType, string body)
6464
return body;
6565
}
6666

67-
6867
if (jsonO != null)
6968
{
70-
foreach (JToken token in jsonO.SelectTokens(_jsonPath))
69+
try
7170
{
72-
// HasValues is false for tokens with children. We will not apply sanitization if that is the case.
73-
if (!token.HasValues)
71+
foreach (JToken token in jsonO.SelectTokens(_jsonPath))
7472
{
75-
var originalValue = token.Value<string>();
76-
77-
// regex replacement does not support null
78-
if (originalValue == null)
73+
// HasValues is false for tokens with children. We will not apply sanitization if that is the case.
74+
if (!token.HasValues)
7975
{
80-
continue;
81-
}
76+
var originalValue = token.Value<string>();
8277

83-
var replacement = StringSanitizer.SanitizeValue(originalValue, _newValue, _regexValue, _groupForReplace);
78+
// regex replacement does not support null
79+
if (originalValue == null)
80+
{
81+
continue;
82+
}
8483

85-
// this sanitizer should only apply to actual values
86-
// if we attempt to apply a regex update to a jtoken that has a more complex type, throw
87-
token.Replace(JToken.FromObject(replacement));
84+
var replacement = StringSanitizer.SanitizeValue(originalValue, _newValue, _regexValue, _groupForReplace);
8885

89-
if (originalValue != replacement)
90-
{
91-
sanitized = true;
86+
// this sanitizer should only apply to actual values
87+
// if we attempt to apply a regex update to a jtoken that has a more complex type, throw
88+
token.Replace(JToken.FromObject(replacement));
89+
90+
if (originalValue != replacement)
91+
{
92+
sanitized = true;
93+
}
9294
}
9395
}
96+
}
97+
catch(Exception e)
98+
{
99+
DebugLogger.LogError($"Ran into exception \"{e.Message}\" while attempting to run regex \"{_regexValue}\" against body value \"{body}\"");
100+
return body;
94101
}
95102
}
96103

0 commit comments

Comments
 (0)