Просмотр исходного кода

fix: 兼容账单下载地址的标准端口并显示汇付地址校验原因

yonge 1 неделя назад
Родитель
Сommit
93324624c2

+ 18 - 5
mec-biz/src/main/java/com/ym/mec/biz/service/impl/receipt/gateway/BillHttp.java

@@ -31,11 +31,24 @@ class BillHttp implements Closeable {
     }
 
     static URI download(String value, String... allowedHosts) throws Failure {
-        URI uri = URI.create(value);
-        if (uri.getHost() == null || !Arrays.asList(allowedHosts).contains(uri.getHost()) ||
-                uri.getRawUserInfo() != null || uri.getPort() != -1 || uri.getRawFragment() != null ||
-                !("https".equals(uri.getScheme()) || "http".equals(uri.getScheme()))) throw new Failure("非受信账单下载地址");
-        return URI.create("https" + value.substring(value.indexOf(':')));
+        URI uri;
+        try {
+            uri = URI.create(value);
+        } catch (IllegalArgumentException invalid) {
+            throw new Failure("下载地址格式无效");
+        }
+        boolean https = "https".equalsIgnoreCase(uri.getScheme());
+        if (!https && !"http".equalsIgnoreCase(uri.getScheme())) throw new Failure("下载地址协议无效");
+        if (uri.getRawUserInfo() != null) throw new Failure("下载地址含用户信息");
+        if (uri.getRawFragment() != null) throw new Failure("下载地址含片段标识");
+        if (uri.getHost() == null) throw new Failure("下载地址缺少有效域名");
+        String host = uri.getHost().toLowerCase(Locale.ROOT);
+        if (!Arrays.asList(allowedHosts).contains(host)) throw new Failure("未登记的账单下载域名(" + host + ")");
+        if (uri.getPort() != -1 && uri.getPort() != (https ? 443 : 80))
+            throw new Failure("账单下载端口不受支持(" + uri.getPort() + ")");
+        // 保留原始路径和查询串的转义形式,避免改变临时下载签名。
+        return URI.create("https://" + host + uri.getRawPath() +
+                (uri.getRawQuery() == null ? "" : "?" + uri.getRawQuery()));
     }
 
     static class Reply {

+ 8 - 2
mec-biz/src/main/java/com/ym/mec/biz/service/impl/receipt/gateway/ProviderBills.java

@@ -99,7 +99,7 @@ final class ProviderBills {
     }
 
     private byte[] huifuFile(LocalDate day) throws Exception {
-        URI download;
+        String url;
         try {
             Map<String, Object> params = new HashMap<>();
             params.put("bill_date", day.format(DateTimeFormatter.BASIC_ISO_DATE));
@@ -109,7 +109,7 @@ final class ProviderBills {
                 throw rejected(ticket, "error_code");
             if (!"true".equals(ticket.path("prod_mode").asText())) throw new BillHttp.Failure("非生产账务响应");
             if ("pending".equals(status)) throw new NotReady("汇付仍在处理请求");
-            download = BillHttp.download(text(ticket, "bill_download_url"), "jarvisfile.oss-cn-shanghai.aliyuncs.com");
+            url = text(ticket, "bill_download_url");
         } catch (NotReady pending) {
             throw pending;
         } catch (BaseAdaPayException failure) {
@@ -125,6 +125,12 @@ final class ProviderBills {
         } catch (Exception failure) {
             throw new BillHttp.Failure("汇付申请账单地址失败:" + BillHttp.reason(failure));
         }
+        URI download;
+        try {
+            download = BillHttp.download(url, "jarvisfile.oss-cn-shanghai.aliyuncs.com");
+        } catch (BillHttp.Failure invalid) {
+            throw new BillHttp.Failure("汇付已返回账单地址,下载地址校验失败:" + invalid.getMessage());
+        }
         try {
             return huifuBody(http.send(download, Collections.emptyMap(), null, BillHttp.FILE_LIMIT));
         } catch (Exception failure) {

+ 29 - 0
mec-biz/src/test/java/com/ym/mec/biz/service/impl/receipt/gateway/ReceiptBillGatewayTest.java

@@ -201,6 +201,8 @@ public class ReceiptBillGatewayTest {
             verify("SHA1withRSA", AdapayCore.apiBase + exchange.getRequestURI() + body,
                     exchange.getRequestHeaders().getFirst("Signature"));
             String data = "{\"status\":\"succeeded\",\"prod_mode\":true,\"bill_download_url\":\"https://jarvisfile.oss-cn-shanghai.aliyuncs.com/test\"}";
+            if ("unconfirmedHost".equals(mode)) data = data.replace("jarvisfile.oss-cn-shanghai.aliyuncs.com/test",
+                    "files.example.org/private-file?Signature=private-signature");
             if ("pending".equals(mode)) data = "{\"status\":\"pending\",\"prod_mode\":true}";
             if ("ticket401".equals(mode)) {
                 status = 401;
@@ -307,6 +309,33 @@ public class ReceiptBillGatewayTest {
         assertEquals(1, downloads);
     }
 
+    @Test public void huifuIdentifiesUnconfirmedDownloadDomainWithoutExposingSignedUrl() throws Exception {
+        mode = "unconfirmedHost";
+        ReceiptBillDto bill = fetch("101", "HUIFU", DAY);
+        assertEquals("ERROR", bill.getStatus());
+        assertTrue(bill.getMessage(), bill.getMessage().contains("下载地址校验失败:未登记的账单下载域名(files.example.org)"));
+        assertFalse(bill.getMessage().contains("private-"));
+        assertEquals(1, tickets);
+        assertEquals(0, downloads);
+    }
+
+    @Test public void downloadPreservesSignedPathAndQueryWithStandardPorts() throws Exception {
+        String host = "jarvisfile.oss-cn-shanghai.aliyuncs.com";
+        String resource = "/app%2Ffile.zip?Signature=a%2Bb%3D&Expires=123";
+        for (String origin : Arrays.asList("HTTP://" + host.toUpperCase(Locale.ROOT) + ":80", "https://" + host + ":443")) {
+            assertEquals("https://" + host + resource, BillHttp.download(origin + resource, host).toString());
+        }
+        for (String origin : Arrays.asList("https://" + host + ":444", "https://user@" + host,
+                "ftp://" + host, "https://" + host + "/#fragment", "https://" + host + ".example.org")) {
+            try {
+                BillHttp.download(origin + resource, host);
+                fail("地址应被拒绝");
+            } catch (BillHttp.Failure expected) {
+                assertFalse(expected.getMessage().contains("Signature"));
+            }
+        }
+    }
+
     @Test public void huifuUnsignedOrEmptyRejectionsStayAtTicketStage() throws Exception {
         for (String scenario : Arrays.asList("empty401", "plain401")) {
             mode = scenario;