Skip to content

Commit 5bbef27

Browse files
committed
fix issue
1 parent 5ee5859 commit 5bbef27

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

dgrv4_Gateway_serv/src/main/java/tpi/dgrv4/gateway/service/GtwIdPApproveService.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package tpi.dgrv4.gateway.service;
22

33
import java.io.IOException;
4+
import java.net.MalformedURLException;
5+
import java.net.URL;
46
import java.util.ArrayList;
57
import java.util.Date;
68
import java.util.List;
@@ -217,8 +219,9 @@ private ResponseEntity<?> handleSendRedirect(HttpServletResponse httpResp, Strin
217219
return getResponseEntity(errMsg); // 400
218220
}
219221

220-
if (allowedHosts.contains(redirectUrl.substring(0, flag))) {
221-
// [ZH] 若 redirect URL 在合法清單中, 則轉導
222+
boolean isValidRedirectUrl = isValidRedirectUrl(redirectUrl, allowedHosts);
223+
if (isValidRedirectUrl) {
224+
// [ZH] 若重定向 URL 在合法清單中, 則轉導
222225
// [EN] If the redirect URL is in the allow list, then redirect.
223226
httpResp.sendRedirect(redirectUrl);
224227
return null;
@@ -233,6 +236,37 @@ private ResponseEntity<?> handleSendRedirect(HttpServletResponse httpResp, Strin
233236
}
234237
}
235238

239+
/*
240+
* [ZH] 驗證重定向 URL 是否在許可清單中
241+
* [EN] Verify that the redirect URL is in the allowed list
242+
*/
243+
private boolean isValidRedirectUrl(String redirectUrl, List<String> allowedHosts) {
244+
if (redirectUrl == null || redirectUrl.trim().isEmpty()) {
245+
return false;
246+
}
247+
248+
try {
249+
// [ZH] 使用 URL 類處理 URL
250+
// [EN] Handling URLs with the URL class
251+
URL url = new URL(redirectUrl);
252+
String host = url.getHost();
253+
254+
// [ZH] 嚴格檢查完整主機名
255+
// [EN] Strictly check the full host name
256+
for (String allowedHost : allowedHosts) {
257+
if (host.equals(allowedHost)) {
258+
return true;
259+
}
260+
}
261+
262+
return false;
263+
} catch (MalformedURLException e) {
264+
// [ZH] 無效 URL
265+
// [EN] Invalid URL
266+
return false;
267+
}
268+
}
269+
236270
private ResponseEntity<?> getResponseEntity(String errMsg) {
237271
return new ResponseEntity<OAuthTokenErrorResp2>(
238272
getTokenHelper().getOAuthTokenErrorResp2(TokenHelper.INVALID_REQUEST, errMsg), HttpStatus.BAD_REQUEST);// 400

0 commit comments

Comments
 (0)