From 45bd81cdbb661efdee2039d7888a284609859807 Mon Sep 17 00:00:00 2001 From: ngqile Date: Wed, 2 Dec 2026 12:44:38 +0800 Subject: [PATCH] fix(gateway): only label reads skip the user token, not message writes Any URL containing /message/ skipped the user token, saves included. Only the catalog reads that services and the sign-in page make stay open now, each an exact GET path, plus the enabled-language list. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs --- openspec/changes/add-zh-cn-messages/tasks.md | 2 +- .../solvway/gateway/filter/JWTAuthFilter.java | 12 ++- .../filter/JWTAuthFilterMessagePathTest.java | 89 +++++++++++++++++++ 3 files changed, 100 insertions(+), 3 deletions(-) create mode 100644 solvway-api-gateway/src/test/java/com/solvway/gateway/filter/JWTAuthFilterMessagePathTest.java diff --git a/openspec/changes/add-zh-cn-messages/tasks.md b/openspec/changes/add-zh-cn-messages/tasks.md index 7a6c599..3f87952 100644 --- a/openspec/changes/add-zh-cn-messages/tasks.md +++ b/openspec/changes/add-zh-cn-messages/tasks.md @@ -24,7 +24,7 @@ Verify commands: ## 3. Gateway -- [ ] 3.1 Narrow the token bypass from any `/message/` URL to the exact read paths in use (grep the callers first), plus `/locale/findAllEnabled`. Tests cover open reads and token-gated writes. Security code: read the whole diff. +- [x] 3.1 Narrow the token bypass from any `/message/` URL to the exact read paths in use (grep the callers first), plus `/locale/findAllEnabled`. Tests cover open reads and token-gated writes. Security code: read the whole diff. - Verify: `mvn -T 1C -B -q -pl solvway-api-gateway -am verify` ## 4. mobile-api diff --git a/solvway-api-gateway/src/main/java/com/solvway/gateway/filter/JWTAuthFilter.java b/solvway-api-gateway/src/main/java/com/solvway/gateway/filter/JWTAuthFilter.java index 24d8f9c..8d21843 100644 --- a/solvway-api-gateway/src/main/java/com/solvway/gateway/filter/JWTAuthFilter.java +++ b/solvway-api-gateway/src/main/java/com/solvway/gateway/filter/JWTAuthFilter.java @@ -59,7 +59,15 @@ public class JWTAuthFilter implements GlobalFilter, Ordered { // Password hint on public password screens: switched-on rule names and numbers only, no user data publicPath(HttpMethod.GET, "/passwordRule/adminActive"), // WhatsApp webhook forward from mobile-api: no user token; the service re-verifies Meta's signature itself - publicPath(HttpMethod.POST, "/whatsapp/webhook/ingest")); + publicPath(HttpMethod.POST, "/whatsapp/webhook/ingest"), + // Label catalog reads (services and the sign-in page have no user token). Saves, search and full listings need one. + publicPath(HttpMethod.GET, "/message/catalogVersion"), + publicPath(HttpMethod.GET, "/message/getAllMessages/[A-Z]+"), + publicPath(HttpMethod.GET, "/message/getMessageJsonByAreaAndLocale/[A-Z]+/[A-Za-z_]+"), + publicPath(HttpMethod.GET, "/message/getMessagesByCode/[^/]+"), + publicPath(HttpMethod.GET, "/message/getMessagesByCodeAndArea/[^/]+/[A-Z]+"), + publicPath(HttpMethod.GET, "/message/findByCodeAndLocale/[^/]+/[A-Za-z_]+"), + publicPath(HttpMethod.GET, "/locale/findAllEnabled")); private static Map.Entry publicPath(HttpMethod method, String regex) { return Map.entry(method, Pattern.compile(regex)); @@ -157,7 +165,7 @@ public class JWTAuthFilter implements GlobalFilter, Ordered { boolean bypassToken = (url.contains("/auth/") || url.contains("/auth/memberAutoLoginUpdate") || url.contains("/auth/fingerPrintLoginWithToken") - || url.contains("/actuator/") || url.contains("/message/") + || url.contains("/actuator/") || url.contains("/appInitialize/") || url.contains("/blockLoginPeriod/") || url.contains("/member/forgetPassword") // Co-Lite admin forgot password (public; answers the same whether or not details match) diff --git a/solvway-api-gateway/src/test/java/com/solvway/gateway/filter/JWTAuthFilterMessagePathTest.java b/solvway-api-gateway/src/test/java/com/solvway/gateway/filter/JWTAuthFilterMessagePathTest.java new file mode 100644 index 0000000..61395c5 --- /dev/null +++ b/solvway-api-gateway/src/test/java/com/solvway/gateway/filter/JWTAuthFilterMessagePathTest.java @@ -0,0 +1,89 @@ +package com.solvway.gateway.filter; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.solvway.gateway.model.AuthAppKey; +import com.solvway.gateway.model.AuthAppKeyMap; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.cloud.gateway.filter.GatewayFilterChain; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.mock.http.server.reactive.MockServerHttpRequest; +import org.springframework.mock.web.server.MockServerWebExchange; +import org.springframework.test.util.ReflectionTestUtils; +import reactor.core.publisher.Mono; + +import java.net.InetSocketAddress; +import java.util.concurrent.atomic.AtomicBoolean; + +import static org.assertj.core.api.Assertions.assertThat; + +/** Message and language reads that services and the sign-in page make stay open; saves, search and full listings need a user token. */ +class JWTAuthFilterMessagePathTest { + + private static final String KEY = "gateway-test-key"; + + private JWTAuthFilter filter; + + @BeforeEach + void setUp() throws Exception { + AuthAppKey key = new ObjectMapper().readValue( + "{\"key\":\"" + KEY + "\",\"type\":\"BO\",\"ipList\":[\"0.0.0.0/0\"],\"allowList\":[\"/\"]}", + AuthAppKey.class); + AuthAppKeyMap map = new AuthAppKeyMap(); + map.add(KEY, key); + filter = new JWTAuthFilter(); + ReflectionTestUtils.setField(filter, "authAppKeyMap", map); + } + + private MockServerWebExchange call(HttpMethod method, String path) { + return MockServerWebExchange.from(MockServerHttpRequest.method(method, path) + .header("api-key", KEY) + .remoteAddress(new InetSocketAddress("127.0.0.1", 40000))); + } + + private boolean passes(MockServerWebExchange exchange) { + AtomicBoolean called = new AtomicBoolean(false); + GatewayFilterChain chain = ex -> { + called.set(true); + return Mono.empty(); + }; + filter.filter(exchange, chain).block(); + return called.get(); + } + + @Test + void catalogReadsNeedNoUserToken() { + for (String path : new String[] { + "/message/catalogVersion", + "/message/getAllMessages/A", + "/message/getMessageJsonByAreaAndLocale/A/zh_CN", + "/message/getMessagesByCode/Leads", + "/message/getMessagesByCodeAndArea/Leads/A", + "/message/findByCodeAndLocale/Leads/zh_CN", + "/message/getMessageJsonForMobile/zh_CN", + "/locale/findAllEnabled" }) { + assertThat(passes(call(HttpMethod.GET, path))).as(path).isTrue(); + } + } + + @Test + void writesSearchAndFullListingsNeedAUserToken() { + for (String[] c : new String[][] { + {"POST", "/message/save"}, + {"POST", "/message/search"}, + {"GET", "/message/findAll"}, + {"GET", "/message/findById/7"}, + {"GET", "/message/findAllPageable"}, + {"POST", "/message/catalogVersion"}, + {"GET", "/message/catalogVersion/x"}, + {"POST", "/locale/save"}, + {"POST", "/locale/update"}, + {"GET", "/locale/findAll"}, + {"POST", "/locale/findAllEnabled"} }) { + MockServerWebExchange ex = call(HttpMethod.valueOf(c[0]), c[1]); + assertThat(passes(ex)).as(c[0] + " " + c[1]).isFalse(); + assertThat(ex.getResponse().getStatusCode()).as(c[0] + " " + c[1]).isEqualTo(HttpStatus.UNAUTHORIZED); + } + } +}