Skip to content

Commit f2884a9

Browse files
committed
Merge branch '1.2.x'
2 parents 070468d + 9eb7efc commit f2884a9

File tree

3 files changed

+74
-8
lines changed

3 files changed

+74
-8
lines changed

app-builder/plugins/aipp-http-call/src/main/java/modelengine/fit/jade/aipp/http/call/AippHttpCallService.java

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
import modelengine.fit.jade.aipp.http.call.command.HttpCallResult;
1515
import modelengine.fitframework.annotation.Component;
1616
import modelengine.fitframework.annotation.Fitable;
17+
import modelengine.fitframework.annotation.Value;
18+
import modelengine.fitframework.log.Logger;
19+
import modelengine.fitframework.util.StringUtils;
20+
21+
import java.util.List;
1722

1823
/**
1924
* 表示 {@link HttpCallService} 的 aipp 实现。
@@ -23,16 +28,34 @@
2328
*/
2429
@Component
2530
public class AippHttpCallService implements HttpCallService {
31+
private static final Logger log = Logger.get(AippHttpCallService.class);
32+
2633
private final HttpCallCommandHandler handler;
2734

28-
public AippHttpCallService(HttpCallCommandHandler handler) {
35+
private List<String> blacklistHttpEndpoints;
36+
37+
public AippHttpCallService(HttpCallCommandHandler handler,
38+
@Value("${blacklist.httpEndpoints:[]}") List<String> blacklistHttpEndpoints) {
2939
this.handler = handler;
40+
this.blacklistHttpEndpoints = blacklistHttpEndpoints;
3041
}
3142

3243
@Override
3344
@Fitable("aipp")
3445
public HttpResult httpCall(HttpRequest request) throws HttpClientException {
3546
notNull(request, "Http request cannot be null.");
47+
48+
String url = request.getUrl();
49+
if (StringUtils.isBlank(url)) {
50+
log.error("Blocked: URL is null or empty.");
51+
return createErrorResponse();
52+
}
53+
if (this.isInBlacklist(url)) {
54+
String baseOnly = this.getBaseUrlSafely(url);
55+
log.error("Blocked: URL is in the blacklist. Base URL: {}", baseOnly);
56+
return createErrorResponse();
57+
}
58+
3659
HttpCallCommand command = new HttpCallCommand();
3760
command.setMethod(request.getHttpMethod());
3861
command.setUrl(request.getUrl());
@@ -49,4 +72,23 @@ public HttpResult httpCall(HttpRequest request) throws HttpClientException {
4972
result.setData(httpCallResult.getData());
5073
return result;
5174
}
75+
76+
private String getBaseUrlSafely(String url) {
77+
int queryOrFragmentIndex = Math.min(url.indexOf('?'), url.indexOf('#'));
78+
if (queryOrFragmentIndex < 0) {
79+
queryOrFragmentIndex = url.length();
80+
}
81+
return url.substring(0, queryOrFragmentIndex);
82+
}
83+
84+
private boolean isInBlacklist(String url) {
85+
return blacklistHttpEndpoints.stream().anyMatch(url::contains);
86+
}
87+
88+
private HttpResult createErrorResponse() {
89+
HttpResult result = new HttpResult();
90+
result.setStatus(-1);
91+
result.setErrorMsg("Invalid request.");
92+
return result;
93+
}
5294
}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
fit:
22
beans:
33
packages:
4-
- 'modelengine.fit.jade.aipp.http.call'
4+
- 'modelengine.fit.jade.aipp.http.call'
5+
6+
blacklist:
7+
httpEndpoints: []

app-builder/plugins/aipp-http-call/src/test/java/modelengine/fit/jade/aipp/http/call/AippHttpCallServiceTest.java

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import org.junit.jupiter.api.DisplayName;
2020
import org.junit.jupiter.api.Test;
2121

22+
import java.util.List;
23+
2224
/**
2325
* {@link AippHttpCallService}测试集。
2426
*
@@ -33,7 +35,7 @@ public class AippHttpCallServiceTest {
3335
@BeforeEach
3436
void setUp() {
3537
this.handler = mock(HttpCallCommandHandler.class);
36-
this.httpCallService = new AippHttpCallService(this.handler);
38+
this.httpCallService = new AippHttpCallService(this.handler, List.of("blacklist.com"));
3739
}
3840

3941
@Test
@@ -44,16 +46,35 @@ void shouldOk() {
4446
when(this.handler.handle(any())).thenReturn(httpResult);
4547
when(httpResult.getStatus()).thenReturn(200);
4648

47-
HttpRequest request = new HttpRequest();
48-
request.setHttpMethod("GET");
49-
request.setUrl("http://examples.com");
50-
request.setTimeout(1000);
51-
request.setArgs(MapBuilder.<String, Object>get().put("111", "2222").build());
49+
HttpRequest request = constructHttpRequest("http://examples.com");
5250

5351
// when
5452
HttpResult result = this.httpCallService.httpCall(request);
5553

5654
// then
5755
Assertions.assertEquals(200, result.getStatus());
5856
}
57+
58+
@Test
59+
@DisplayName("黑名单网站调用失败")
60+
void blackListShouldBeBlocked() {
61+
// given
62+
HttpRequest request = constructHttpRequest("http://blacklist.com");
63+
64+
// when
65+
HttpResult result = this.httpCallService.httpCall(request);
66+
67+
// then
68+
Assertions.assertEquals(-1, result.getStatus());
69+
Assertions.assertEquals("The URL is in the blacklist.", result.getErrorMsg());
70+
}
71+
72+
private static HttpRequest constructHttpRequest(String url) {
73+
HttpRequest request = new HttpRequest();
74+
request.setHttpMethod("GET");
75+
request.setUrl(url);
76+
request.setTimeout(1000);
77+
request.setArgs(MapBuilder.<String, Object>get().put("111", "2222").build());
78+
return request;
79+
}
5980
}

0 commit comments

Comments
 (0)