Skip to content

Commit 1c99347

Browse files
1nhannigr
authored andcommitted
fix security issues
1 parent 1472345 commit 1c99347

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

src/main/java/jodd/http/HttpRequest.java

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.io.InputStreamReader;
3838
import java.io.OutputStream;
3939
import java.io.UnsupportedEncodingException;
40+
import java.net.URLEncoder;
4041
import java.nio.charset.StandardCharsets;
4142
import java.util.Map;
4243
import java.util.concurrent.CompletableFuture;
@@ -338,35 +339,38 @@ public String path() {
338339
* Previous query is discarded.
339340
* @see #query()
340341
*/
341-
public HttpRequest path(String path) {
342+
public HttpRequest path(String path){
342343
// this must be the only place that sets the path
343344

344345
if (!path.startsWith(StringPool.SLASH)) {
345346
path = StringPool.SLASH + path;
346347
}
347348

348-
// remove fragment
349+
try {
350+
// remove fragment
351+
final int fragmentIndex = path.indexOf('#');
352+
if (path.indexOf('#') != -1) {
353+
this.fragment = URLEncoder.encode(path.substring(fragmentIndex + 1), StandardCharsets.UTF_8.name());
354+
path = path.substring(0, fragmentIndex);
355+
}
349356

350-
final int fragmentIndex = path.indexOf('#');
351-
if (path.indexOf('#') != -1) {
352-
this.fragment = path.substring(fragmentIndex + 1);
353-
path = path.substring(0, fragmentIndex);
354-
}
357+
final int ndx = path.indexOf('?');
355358

356-
final int ndx = path.indexOf('?');
359+
if (ndx != -1) {
360+
final String queryString = path.substring(ndx + 1);
357361

358-
if (ndx != -1) {
359-
final String queryString = path.substring(ndx + 1);
362+
path = URLEncoder.encode(path.substring(0, ndx), StandardCharsets.UTF_8.name());
360363

361-
path = path.substring(0, ndx);
364+
query = HttpUtil.parseQuery(queryString, true);
365+
} else {
366+
query = HttpMultiMap.newCaseInsensitiveMap();
367+
}
362368

363-
query = HttpUtil.parseQuery(queryString, true);
364-
} else {
365-
query = HttpMultiMap.newCaseInsensitiveMap();
369+
this.path = URLEncoder.encode(path, StandardCharsets.UTF_8.name());
370+
;
371+
}catch (UnsupportedEncodingException e) {
372+
return null;
366373
}
367-
368-
this.path = path;
369-
370374
return this;
371375
}
372376

0 commit comments

Comments
 (0)