Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,12 @@ public void requestProxyGateway(final ProxyGatewayDTO proxyGatewayDTO, final Htt
throw new ShenyuException(proxyHostPort + " is not allowed.");
}

String signContent = null;
String sign = null;
String timestamp = String.valueOf(Instant.now().toEpochMilli());
if (StringUtils.isNotEmpty(appKey)) {
String timestamp = String.valueOf(Instant.now().toEpochMilli());
String secureKey = getSecureKey(appKey);
Assert.notBlack(secureKey, Constants.SIGN_APP_KEY_IS_NOT_EXIST);
signContent = ShenyuSignatureUtils.getSignContent(secureKey, timestamp, uriComponents.getPath());
String signContent = ShenyuSignatureUtils.getSignContent(secureKey, timestamp, uriComponents.getPath());
sign = ShenyuSignatureUtils.generateSign(signContent);
header.put("timestamp", timestamp);
header.put("appKey", appKey);
Expand All @@ -114,7 +113,10 @@ public void requestProxyGateway(final ProxyGatewayDTO proxyGatewayDTO, final Htt
return;
}
if (StringUtils.isNotEmpty(appKey)) {
response.addHeader("sandbox-beforesign", UriUtils.encode(signContent, StandardCharsets.UTF_8));
String signContentWithoutSecret = "path" + uriComponents.getPath()
+ "timestamp" + timestamp
+ "version" + ShenyuSignatureUtils.VERSION;
response.addHeader("sandbox-beforesign", UriUtils.encode(signContentWithoutSecret, StandardCharsets.UTF_8));
response.addHeader("sandbox-sign", UriUtils.encode(sign, StandardCharsets.UTF_8));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Collections;

import org.apache.shenyu.admin.model.dto.ProxyGatewayDTO;
import org.apache.shenyu.admin.model.entity.AppAuthDO;
import org.apache.shenyu.admin.model.vo.ShenyuDictVO;
import org.apache.shenyu.admin.service.AppAuthService;
import org.apache.shenyu.admin.service.ShenyuDictService;
Expand Down Expand Up @@ -69,6 +70,35 @@ void requestProxyGatewayShouldCopyBody() throws IOException {
}
}

@Test
void requestProxyGatewayShouldNotExposeAppSecretInBeforeSignHeader() throws IOException {
HttpServer server = startHttpServer("proxied response");
int port = server.getAddress().getPort();
SandboxServiceImpl sandboxService = new SandboxServiceImpl(appAuthService, shenyuDictService);

when(shenyuDictService.list(anyString())).thenReturn(Collections.singletonList(buildDict(port)));

String testAppKey = "testAppKey";
String testAppSecret = "testSecret123";
AppAuthDO appAuthDO = new AppAuthDO(testAppKey, testAppSecret, true, true, null, null, null, null);
when(appAuthService.findByAppKey(testAppKey)).thenReturn(appAuthDO);

try {
MockHttpServletResponse response = new MockHttpServletResponse();
ProxyGatewayDTO dto = buildProxyGatewayDTO(port);
dto.setAppKey(testAppKey);
sandboxService.requestProxyGateway(dto, new MockHttpServletRequest(), response);

String beforesign = response.getHeader("sandbox-beforesign");
assertThat(beforesign).doesNotContain(testAppSecret);

String sign = response.getHeader("sandbox-sign");
assertThat(sign).isNotNull();
} finally {
server.stop(0);
}
}

private ProxyGatewayDTO buildProxyGatewayDTO(final int port) {
ProxyGatewayDTO proxyGatewayDTO = new ProxyGatewayDTO();
proxyGatewayDTO.setRequestUrl("http://localhost:" + port + "/proxy");
Expand Down
Loading