-
Notifications
You must be signed in to change notification settings - Fork 38.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve URI/query strings sanitization #26012
Conversation
spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java
Outdated
Show resolved
Hide resolved
146f783
to
85131d2
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
@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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Use
StringBuilder.deleteCharAt(int)
andStringBuilder.delete(int, int)
to handle sanitization more effectively