Skip to content

Commit 6db2db9

Browse files
authored
Merge pull request #5085 from wubin01/normalize_http_url
feat(net): normalize http urls to prevent attacks
2 parents 4339688 + bfa2ffe commit 6db2db9

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

framework/src/main/java/org/tron/core/services/filter/HttpApiAccessFilter.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.tron.core.services.filter;
22

33
import com.alibaba.fastjson.JSONObject;
4+
import java.net.URI;
45
import java.util.List;
56
import javax.servlet.Filter;
67
import javax.servlet.FilterChain;
@@ -58,6 +59,7 @@ private boolean isDisabled(String endpoint) {
5859
boolean disabled = false;
5960

6061
try {
62+
endpoint = URI.create(endpoint).normalize().toString();
6163
List<String> disabledApiList = CommonParameter.getInstance().getDisabledApiList();
6264
if (!disabledApiList.isEmpty()) {
6365
disabled = disabledApiList.contains(endpoint.split("/")[2].toLowerCase());

framework/src/test/java/org/tron/core/services/filter/HttpApiAccessFilterTest.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import java.io.File;
55
import java.io.IOException;
66
import java.io.InputStreamReader;
7+
import java.lang.reflect.Method;
78
import java.util.ArrayList;
9+
import java.util.Collection;
810
import java.util.Collections;
911
import java.util.List;
1012
import org.apache.http.HttpResponse;
@@ -21,10 +23,13 @@
2123
import org.tron.common.application.Application;
2224
import org.tron.common.application.ApplicationFactory;
2325
import org.tron.common.application.TronApplicationContext;
26+
import org.tron.common.parameter.CommonParameter;
2427
import org.tron.common.utils.FileUtil;
28+
import org.tron.common.utils.ReflectUtils;
2529
import org.tron.core.Constant;
2630
import org.tron.core.config.DefaultConfig;
2731
import org.tron.core.config.args.Args;
32+
import org.tron.core.net.peer.PeerConnection;
2833
import org.tron.core.services.http.FullNodeHttpApiService;
2934
import org.tron.core.services.interfaceOnPBFT.http.PBFT.HttpApiOnPBFTService;
3035
import org.tron.core.services.interfaceOnSolidity.http.solidity.HttpApiOnSolidityService;
@@ -37,6 +42,7 @@ public class HttpApiAccessFilterTest {
3742
private static Application appTest;
3843
private static CloseableHttpClient httpClient = HttpClients.createDefault();
3944
private static String dbPath = "output_http_api_access_filter_test";
45+
private static HttpApiAccessFilter httpApiAccessFilter;
4046

4147
/**
4248
* init dependencies.
@@ -47,7 +53,7 @@ public static void init() {
4753
Args.getInstance().setFullNodeAllowShieldedTransactionArgs(false);
4854
context = new TronApplicationContext(DefaultConfig.class);
4955
appTest = ApplicationFactory.create(context);
50-
56+
httpApiAccessFilter = context.getBean(HttpApiAccessFilter.class);
5157
FullNodeHttpApiService httpApiService = context
5258
.getBean(FullNodeHttpApiService.class);
5359
HttpApiOnSolidityService httpApiOnSolidityService = context
@@ -153,4 +159,31 @@ private int getReuqestCode(String url) {
153159

154160
return 0;
155161
}
162+
163+
@Test
164+
public void testIsDisabled() throws Exception {
165+
List<String> list = new ArrayList<>();
166+
list.add("getnowblock");
167+
CommonParameter.getInstance().setDisabledApiList(list);
168+
Method privateMethod = httpApiAccessFilter.getClass()
169+
.getDeclaredMethod("isDisabled", String.class);
170+
privateMethod.setAccessible(true);
171+
172+
String url = "/wallet/getnowblock";
173+
boolean f = (boolean) privateMethod.invoke(httpApiAccessFilter,url);
174+
Assert.assertTrue(f);
175+
176+
url = "/wallet/a/../b/../getnowblock";
177+
f = (boolean) privateMethod.invoke(httpApiAccessFilter,url);
178+
Assert.assertTrue(f);
179+
180+
url = "/wallet/a/b/../getnowblock";
181+
f = (boolean) privateMethod.invoke(httpApiAccessFilter,url);
182+
Assert.assertTrue(!f);
183+
184+
url = "/wallet/getblock";
185+
f = (boolean) privateMethod.invoke(httpApiAccessFilter,url);
186+
Assert.assertTrue(!f);
187+
}
188+
156189
}

0 commit comments

Comments
 (0)