Skip to content

Commit 2263e13

Browse files
committed
Add scenarios with parameters without values
1 parent 4911e83 commit 2263e13

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/semconv/http/HttpClientAttributesExtractor.java

+14-4
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ private static String redactQueryParameters(String url) {
173173
StringBuilder redactedParameters = new StringBuilder();
174174
boolean inRedactedParamValue =
175175
false; // To be able to skip the characters of the parameters to redact
176-
boolean inParamValue = false;
176+
boolean paramWithValue = false;
177177

178178
// To build a parameter name until we reach the '=' character
179179
// If the parameter name is a one to redact, we will redact the value
@@ -182,24 +182,34 @@ private static String redactQueryParameters(String url) {
182182
for (int i = questionMarkIndex + 1; i < url.length(); i++) {
183183
char currentChar = url.charAt(i);
184184
if (currentChar == '=') {
185-
inParamValue = true;
185+
paramWithValue = true;
186186
redactedParameters.append(currentParamName);
187187
redactedParameters.append('=');
188188
if (PARAMS_TO_REDACT.contains(currentParamName.toString())) {
189189
redactedParameters.append("REDACTED");
190190
inRedactedParamValue = true;
191191
}
192192
} else if (currentChar == '&') { // New parameter delimiter
193+
if (!paramWithValue) { // Example: https://service.com?AWSAccessKeyId=AKIAIOSFODNN7&ab
194+
redactedParameters.append(currentParamName);
195+
}
193196
redactedParameters.append('&');
194-
inParamValue = false;
197+
paramWithValue = false;
195198
inRedactedParamValue = false;
196199
currentParamName.setLength(
197200
0); // To avoid creating a new StringBuilder for each new parameter
198201
} else if (currentChar == '#') { // Reference delimiter
202+
if (!paramWithValue) { // Example:
203+
// https://service.com?&&AWSAccessKeyId=AKIAIOSFODNN7&a&b#fragment
204+
redactedParameters.append(currentParamName);
205+
}
199206
redactedParameters.append(url.substring(i));
200207
break;
201-
} else if (!inParamValue) {
208+
} else if (!paramWithValue) {
202209
currentParamName.append(currentChar);
210+
if (i == url.length() - 1) { // Example: https://service.com?AWSAccessKeyId=AKIAIOSFODNN7&ab
211+
redactedParameters.append(currentParamName);
212+
}
203213
} else if (!inRedactedParamValue) {
204214
redactedParameters.append(currentChar);
205215
}

instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/semconv/http/HttpClientAttributesExtractorTest.java

+18-3
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ void normal() {
207207
}
208208

209209
@ParameterizedTest
210-
@ArgumentsSource(StripUrlArgumentSource.class)
210+
@ArgumentsSource(UrlSourceToRedact.class)
211211
void shouldRedactUserInfoAndQueryParameters(String url, String expectedResult) {
212212
Map<String, String> request = new HashMap<>();
213213
request.put("urlFull", url);
@@ -223,7 +223,7 @@ void shouldRedactUserInfoAndQueryParameters(String url, String expectedResult) {
223223
assertThat(attributes.build()).containsOnly(entry(URL_FULL, expectedResult));
224224
}
225225

226-
static final class StripUrlArgumentSource implements ArgumentsProvider {
226+
static final class UrlSourceToRedact implements ArgumentsProvider {
227227

228228
@Override
229229
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
@@ -274,7 +274,22 @@ public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
274274
"https://service.com?AWSAccessKeyId=REDACTED&AWSAccessKeyId=REDACTED"),
275275
arguments(
276276
"https://service.com?AWSAccessKeyId=AKIAIOSFODNN7#ref",
277-
"https://service.com?AWSAccessKeyId=REDACTED#ref"));
277+
"https://service.com?AWSAccessKeyId=REDACTED#ref"),
278+
arguments(
279+
"https://service.com?AWSAccessKeyId=AKIAIOSFODNN7&aa&bb",
280+
"https://service.com?AWSAccessKeyId=REDACTED&aa&bb"),
281+
arguments(
282+
"https://service.com?aa&bb&AWSAccessKeyId=AKIAIOSFODNN7",
283+
"https://service.com?aa&bb&AWSAccessKeyId=REDACTED"),
284+
arguments(
285+
"https://service.com?AWSAccessKeyId=AKIAIOSFODNN7&&",
286+
"https://service.com?AWSAccessKeyId=REDACTED&&"),
287+
arguments(
288+
"https://service.com?&&AWSAccessKeyId=AKIAIOSFODNN7",
289+
"https://service.com?&&AWSAccessKeyId=REDACTED"),
290+
arguments(
291+
"https://service.com?AWSAccessKeyId=AKIAIOSFODNN7&a&b#fragment",
292+
"https://service.com?AWSAccessKeyId=REDACTED&a&b#fragment"));
278293
}
279294
}
280295

0 commit comments

Comments
 (0)