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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
This commit is contained in:
ngqileandClaude Opus 5.5 committed 2026-12-02 12:44:38 +08:00
1 parent ebfee582c2
commit 45bd81cdbb
3 files changed
+100 -3

No files matched your search

+1 -1
View File
@@ -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
@@ -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<HttpMethod, Pattern> 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)
@@ -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);
}
}
}