Improve URI/query strings sanitization#26012
Merged
jhoeller merged 1 commit intospring-projects:masterfrom Nov 4, 2020
Merged
Conversation
rstoyanchev
requested changes
Nov 2, 2020
spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java
Outdated
Show resolved
Hide resolved
146f783 to
85131d2
Compare
rstoyanchev
reviewed
Nov 3, 2020
Contributor
rstoyanchev
left a comment
There was a problem hiding this comment.
Looks fine as far as I UriComponentsBuilder and UrlPathHelper are concerned.
Very minor, I would say getSanitizedPath and doubleToSingleSlash can be combined into one method. Also the same identical method could be created in UriComponentsBuilder. When there is identical logic it's useful to make it look as identical as possible.
Contributor
Author
|
@rstoyanchev done. I've also measured performance impact with simple benchmark @BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Fork(jvmArgsAppend = {"-Xms2g", "-Xmx2g"})
public class DropDoubleSlashBenchmark {
@Benchmark
public String ineffective(Data data) {
String path = data.path;
while (true) {
int index = path.indexOf("//");
if (index == -1) {
break;
}
path = path.substring(0, index) + path.substring(index + 1);
}
return path;
}
@Benchmark
public String effective(Data data) {
StringBuilder path = new StringBuilder(data.path);
while (true) {
int index = path.indexOf("//");
if (index == -1) {
break;
}
path.deleteCharAt(index);
}
return path.toString();
}
@State(Scope.Thread)
public static class Data {
private final String path = "/home/" + "/path";
}
}and got the following results on my machine On longer strings I think we'll have even better improvement. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Use
StringBuilder.deleteCharAt(int)andStringBuilder.delete(int, int)to handle sanitization more effectively