@@ -170,51 +170,43 @@ private static String redactQueryParameters(String url) {
170
170
return url ;
171
171
}
172
172
173
- StringBuilder redactedParameters = new StringBuilder ();
174
- boolean inRedactedParamValue =
175
- false ; // To be able to skip the characters of the parameters to redact
176
- boolean paramWithValue = false ;
173
+ StringBuilder urAfterQuestionMark = new StringBuilder ();
177
174
178
175
// To build a parameter name until we reach the '=' character
179
176
// If the parameter name is a one to redact, we will redact the value
180
177
StringBuilder currentParamName = new StringBuilder ();
181
178
182
179
for (int i = questionMarkIndex + 1 ; i < url .length (); i ++) {
183
180
char currentChar = url .charAt (i );
181
+
184
182
if (currentChar == '=' ) {
185
- paramWithValue = true ;
186
- redactedParameters .append (currentParamName );
187
- redactedParameters .append ('=' );
183
+ urAfterQuestionMark .append ('=' );
188
184
if (PARAMS_TO_REDACT .contains (currentParamName .toString ())) {
189
- redactedParameters .append ("REDACTED" );
190
- inRedactedParamValue = true ;
185
+ urAfterQuestionMark .append ("REDACTED" );
186
+ // skip over parameter value
187
+ for (; i + 1 < url .length (); i ++) {
188
+ char c = url .charAt (i + 1 );
189
+ if (c == '&' || c == '#' ) {
190
+ break ;
191
+ }
192
+ }
191
193
}
192
194
} else if (currentChar == '&' ) { // New parameter delimiter
193
- if (!paramWithValue ) { // Example: https://service.com?AWSAccessKeyId=AKIAIOSFODNN7&a&
194
- redactedParameters .append (currentParamName );
195
- }
196
- redactedParameters .append ('&' );
197
- paramWithValue = false ;
198
- inRedactedParamValue = false ;
199
- currentParamName .setLength (
200
- 0 ); // To avoid creating a new StringBuilder for each new parameter
195
+ urAfterQuestionMark .append (currentChar );
196
+ // To avoid creating a new StringBuilder for each new parameter
197
+ currentParamName .setLength (0 );
201
198
} else if (currentChar == '#' ) { // Reference delimiter
202
- if (!paramWithValue ) { // Example:
203
- // https://service.com?&&AWSAccessKeyId=AKIAIOSFODNN7&a&b#fragment
204
- redactedParameters .append (currentParamName );
205
- }
206
- redactedParameters .append (url .substring (i ));
199
+ urAfterQuestionMark .append (url .substring (i ));
207
200
break ;
208
- } else if (!paramWithValue ) {
209
- currentParamName .append (currentChar );
210
- if (i == url .length () - 1 ) { // Example: https://service.com?AWSAccessKeyId=AKIAIOSFODNN7&a
211
- redactedParameters .append (currentParamName );
212
- }
213
- } else if (!inRedactedParamValue ) {
214
- redactedParameters .append (currentChar );
201
+ } else {
202
+ currentParamName .append (
203
+ currentChar ); // param values can be appended to currentParamName here but it's not an
204
+ // issue
205
+ urAfterQuestionMark .append (currentChar );
215
206
}
216
207
}
217
- return url .substring (0 , questionMarkIndex ) + "?" + redactedParameters ;
208
+
209
+ return url .substring (0 , questionMarkIndex ) + "?" + urAfterQuestionMark ;
218
210
}
219
211
220
212
private static boolean containsParamToRedact (String urlpart ) {
0 commit comments