chore: copy kept Tastefully modules and portals into Co-Lite
Backend modules from tastefully 8bd93d15: core-libs, core-session, api-gateway, mobile-api, services-auth, services-system, services-counter, services-member, services-log, services-email. solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal from tastefully 8bd93d15. Copied as tracked files only. Literal secrets replaced with environment placeholders: - services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY, MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default) - services-system: JUMIO_TOKEN, JUMIO_SECRET (optional) - services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed - core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default removed, ipgeolocation key read from IPGEOLOCATION_API_KEY - services-system EkycAppConfig: commented Jumio credentials removed Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
This commit is contained in:
1 parent
e396568cc3
commit
5ee51d1913
2918 files changed
+302373
-3
No files matched your search
@@ -0,0 +1,136 @@
|
||||
package com.solvway.gateway.filter;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.Locale;
|
||||
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
/**
|
||||
* F47: what the gateway may write to its request log. Every method here runs on every request, on a Netty event-loop
|
||||
* thread, before authentication, so each must be linear in the input and must never throw.
|
||||
*/
|
||||
class LogRedactorTest {
|
||||
|
||||
private static final String SECRET = "S3cret#Pw";
|
||||
private static final String JWT = "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ1c2VyIn0.c2lnbmF0dXJl";
|
||||
|
||||
private static String body(String body, MediaType type) {
|
||||
return LogRedactor.body(body.getBytes(java.nio.charset.StandardCharsets.UTF_8), type);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("JSON: values of any type under a sensitive key are masked, at any depth; other fields kept")
|
||||
void jsonAnyValueTypeMasked() throws Exception {
|
||||
String logged = body("{\"username\":\"u1\",\"password\":\"" + SECRET + "\",\"otpToken\":123456,"
|
||||
+ "\"pwdHistory\":[\"" + SECRET + "\"],\"profile\":{\"clientSecret\":{\"v\":\"" + SECRET + "\"},\"name\":\"Ali\"},"
|
||||
+ "\"items\":[{\"refreshToken\":\"" + JWT + "\"}],\"newPassword\":null}", MediaType.APPLICATION_JSON);
|
||||
|
||||
assertThat(logged).doesNotContain(SECRET).doesNotContain(JWT).doesNotContain("123456");
|
||||
assertThat(new ObjectMapper().readTree(logged).path("username").asText()).isEqualTo("u1");
|
||||
assertThat(new ObjectMapper().readTree(logged).path("profile").path("name").asText()).isEqualTo("Ali");
|
||||
assertThat(new ObjectMapper().readTree(logged).path("password").asText()).isEqualTo("***");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("JSON: a unicode-escaped key is still recognised")
|
||||
void jsonEscapedKeyMasked() {
|
||||
assertThat(body("{\"\\u0070assword\":\"" + SECRET + "\"}", MediaType.APPLICATION_JSON)).doesNotContain(SECRET);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("JSON body without a content type is still parsed and masked")
|
||||
void jsonWithoutContentType() {
|
||||
assertThat(body("{\"password\":\"" + SECRET + "\"}", null)).doesNotContain(SECRET).contains("***");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("unparseable JSON is not logged, only its size")
|
||||
void brokenJsonNotLogged() {
|
||||
assertThat(body("{\"password\":\"" + SECRET, MediaType.APPLICATION_JSON))
|
||||
.doesNotContain(SECRET).contains("not logged");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("form: sensitive fields masked, including URL-encoded names; others kept")
|
||||
void formMasked() {
|
||||
String logged = body("username=u1&password=" + SECRET + "&pass%77ord=" + SECRET + "&user%5Bpwd%5D=" + SECRET
|
||||
+ "&remember=1", MediaType.APPLICATION_FORM_URLENCODED);
|
||||
|
||||
assertThat(logged).doesNotContain(SECRET).contains("username=u1").contains("remember=1").contains("password=***");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("multipart and other content types are not logged, only their size")
|
||||
void otherBodiesNotLogged() {
|
||||
assertThat(body("--b\r\nContent-Disposition: form-data; name=\"password\"\r\n\r\n" + SECRET + "\r\n--b--",
|
||||
MediaType.MULTIPART_FORM_DATA)).doesNotContain(SECRET).contains("not logged");
|
||||
assertThat(body("<req><password>" + SECRET + "</password></req>", MediaType.APPLICATION_XML))
|
||||
.doesNotContain(SECRET).contains("not logged");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("bodies over the parse limit are not logged; logged bodies are capped")
|
||||
void sizeLimits() {
|
||||
String big = "{\"note\":\"" + "x".repeat(LogRedactor.MAX_PARSE_BYTES) + "\"}";
|
||||
assertThat(body(big, MediaType.APPLICATION_JSON)).contains("not logged").hasSizeLessThan(200);
|
||||
|
||||
String medium = "{\"note\":\"" + "y".repeat(LogRedactor.MAX_LOGGED_CHARS * 2) + "\"}";
|
||||
assertThat(body(medium, MediaType.APPLICATION_JSON)).hasSizeLessThan(LogRedactor.MAX_LOGGED_CHARS + 100)
|
||||
.contains("truncated");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("adversarial bodies (long secret value, keyword floods) finish fast and never throw")
|
||||
void adversarialBodiesAreLinear() {
|
||||
String longToken = "{\"token\":\"" + "A".repeat(60_000) + "\"}";
|
||||
String keywordFloodJson = "{\"" + "password".repeat(7_000) + "\":1}";
|
||||
String keywordFloodForm = "password".repeat(7_000);
|
||||
assertTimeoutPreemptively(Duration.ofSeconds(2), () -> {
|
||||
assertThat(body(longToken, MediaType.APPLICATION_JSON)).doesNotContain("AAAAAAAAAA");
|
||||
body(keywordFloodJson, MediaType.APPLICATION_JSON);
|
||||
body(keywordFloodForm, MediaType.APPLICATION_FORM_URLENCODED);
|
||||
body(keywordFloodForm, null);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("headers: credential values masked case-insensitively, also under a Turkish default locale")
|
||||
void headersMasked() {
|
||||
Locale before = Locale.getDefault();
|
||||
try {
|
||||
Locale.setDefault(Locale.forLanguageTag("tr-TR"));
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.add("AUTHORIZATION", "Bearer " + JWT);
|
||||
headers.add("api-key", "KEY-" + SECRET);
|
||||
headers.add("Proxy-Authorization", "Basic " + SECRET);
|
||||
headers.add("Cookie", "SESSION=" + SECRET);
|
||||
headers.add("X-XSRF-TOKEN", SECRET);
|
||||
headers.add("Accept", "application/json");
|
||||
|
||||
String logged = LogRedactor.headers(headers);
|
||||
|
||||
assertThat(logged).doesNotContain(SECRET).doesNotContain(JWT).contains("Accept:[application/json]")
|
||||
.contains("AUTHORIZATION:[***]").contains("api-key:[***]");
|
||||
} finally {
|
||||
Locale.setDefault(before);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("path: JWT-shaped segments are masked, other segments kept")
|
||||
void pathJwtMasked() {
|
||||
assertThat(LogRedactor.path("/auth/findUserByToken/M/" + JWT)).isEqualTo("/auth/findUserByToken/M/***");
|
||||
assertThat(LogRedactor.path("/auth/logoutToken/" + JWT)).isEqualTo("/auth/logoutToken/***");
|
||||
assertThat(LogRedactor.path("/locationHierarchy/findParentByIdAndType/28/C"))
|
||||
.isEqualTo("/locationHierarchy/findParentByIdAndType/28/C");
|
||||
assertTimeoutPreemptively(Duration.ofSeconds(1), () -> LogRedactor.path("/" + "eyJ.".repeat(20_000)));
|
||||
}
|
||||
}
|
||||
+162
@@ -0,0 +1,162 @@
|
||||
package com.solvway.gateway.filter;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.core.io.buffer.DataBufferUtils;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
|
||||
import org.springframework.mock.web.server.MockServerWebExchange;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
|
||||
import ch.qos.logback.classic.Logger;
|
||||
import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||
import ch.qos.logback.core.read.ListAppender;
|
||||
|
||||
/**
|
||||
* The gateway logs every request (method, path, body, headers) and ships the log to Elasticsearch. Credentials must
|
||||
* not reach that log: the customer login body (/auth/webCustomerAuthenticate is not on the skip list), password
|
||||
* change/registration bodies, and the Authorization / api-key headers sent on every call. The downstream request
|
||||
* must still receive the original, unmasked body.
|
||||
*/
|
||||
class RequestBodyLoggerFilterTest {
|
||||
|
||||
private static final String PASSWORD = "S3cret#Pw";
|
||||
private static final String JWT = "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ1c2VyIn0.c2lnbmF0dXJl";
|
||||
private static final String API_KEY = "VNuqAx5GTestKey0123456789";
|
||||
|
||||
private final RequestBodyLoggerFilter filter = new RequestBodyLoggerFilter();
|
||||
private ListAppender<ILoggingEvent> appender;
|
||||
private Logger logger;
|
||||
|
||||
@BeforeEach
|
||||
void captureLog() {
|
||||
logger = (Logger) LoggerFactory.getLogger(RequestBodyLoggerFilter.class);
|
||||
appender = new ListAppender<>();
|
||||
appender.start();
|
||||
logger.addAppender(appender);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void detach() {
|
||||
logger.detachAppender(appender);
|
||||
}
|
||||
|
||||
private String loggedLine() {
|
||||
List<ILoggingEvent> events = appender.list;
|
||||
assertThat(events).hasSize(1);
|
||||
return events.get(0).getFormattedMessage();
|
||||
}
|
||||
|
||||
/** Runs the filter; returns the body the next filter in the chain receives. */
|
||||
private String run(MockServerHttpRequest request) {
|
||||
AtomicReference<String> downstream = new AtomicReference<>();
|
||||
ServerWebExchange exchange = MockServerWebExchange.from(request);
|
||||
filter.filter(exchange, ex -> DataBufferUtils.join(ex.getRequest().getBody())
|
||||
.map(buffer -> {
|
||||
byte[] bytes = new byte[buffer.readableByteCount()];
|
||||
buffer.read(bytes);
|
||||
DataBufferUtils.release(buffer);
|
||||
return new String(bytes, StandardCharsets.UTF_8);
|
||||
})
|
||||
.defaultIfEmpty("")
|
||||
.doOnNext(downstream::set)
|
||||
.then()).block();
|
||||
return downstream.get();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("customer login: the password is masked in the log and passed unchanged downstream")
|
||||
void customerLoginPasswordMasked() {
|
||||
String body = "{\"username\":\"cust@example.com\",\"password\":\"" + PASSWORD + "\"}";
|
||||
|
||||
String downstream = run(MockServerHttpRequest.post("/auth/webCustomerAuthenticate")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.header("api-key", API_KEY)
|
||||
.body(body));
|
||||
|
||||
String line = loggedLine();
|
||||
assertThat(line).doesNotContain(PASSWORD).doesNotContain(API_KEY)
|
||||
.contains("/auth/webCustomerAuthenticate")
|
||||
.contains("\"username\":\"cust@example.com\"")
|
||||
.contains("\"password\":\"***\"");
|
||||
assertThat(downstream).isEqualTo(body);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("password change and token fields are masked, other fields kept")
|
||||
void sensitiveFieldsMasked() {
|
||||
String body = "{\"id\":7,\"oldPassword\":\"old" + PASSWORD + "\",\"newPassword\":\"new" + PASSWORD
|
||||
+ "\",\"confirmPassword\":\"new" + PASSWORD + "\",\"refreshToken\":\"" + JWT + "\",\"clientSecret\":\"x"
|
||||
+ PASSWORD + "\",\"name\":\"Ali\"}";
|
||||
|
||||
String downstream = run(MockServerHttpRequest.post("/user/changePassword")
|
||||
.contentType(MediaType.APPLICATION_JSON).body(body));
|
||||
|
||||
String line = loggedLine();
|
||||
assertThat(line).doesNotContain(PASSWORD).doesNotContain(JWT)
|
||||
.contains("\"id\":7").contains("\"name\":\"Ali\"")
|
||||
.contains("\"oldPassword\":\"***\"").contains("\"refreshToken\":\"***\"");
|
||||
assertThat(downstream).isEqualTo(body);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("form-encoded password is masked")
|
||||
void formPasswordMasked() {
|
||||
run(MockServerHttpRequest.post("/auth/stockistAuthenticate")
|
||||
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
|
||||
.body("username=u1&password=" + PASSWORD + "&remember=1"));
|
||||
|
||||
assertThat(loggedLine()).doesNotContain(PASSWORD).contains("password=***").contains("remember=1");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("GET: Authorization and api-key header values are masked")
|
||||
void credentialHeadersMasked() {
|
||||
run(MockServerHttpRequest.get("/auth/findUserByToken/M/x")
|
||||
.header("Authorization", "Bearer " + JWT)
|
||||
.header("api-key", API_KEY)
|
||||
.header("Accept", "application/json")
|
||||
.build());
|
||||
|
||||
String line = loggedLine();
|
||||
assertThat(line).doesNotContain(JWT).doesNotContain(API_KEY)
|
||||
.contains("Accept:[application/json]")
|
||||
.containsIgnoringCase("authorization:[***]")
|
||||
.containsIgnoringCase("api-key:[***]");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("a JWT in the path is masked in the log")
|
||||
void jwtInPathMasked() {
|
||||
run(MockServerHttpRequest.get("/auth/findUserByToken/M/" + JWT).build());
|
||||
|
||||
assertThat(loggedLine()).doesNotContain(JWT).contains("/auth/findUserByToken/M/***");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("skip-listed paths are still not logged")
|
||||
void skipListUnchanged() {
|
||||
run(MockServerHttpRequest.post("/auth/authenticate").contentType(MediaType.APPLICATION_JSON)
|
||||
.body("{\"password\":\"" + PASSWORD + "\"}"));
|
||||
|
||||
assertThat(appender.list).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("an empty POST body is logged as (empty) and still forwarded")
|
||||
void emptyBody() {
|
||||
String downstream = run(MockServerHttpRequest.post("/user/ping").header("api-key", API_KEY).build());
|
||||
|
||||
assertThat(loggedLine()).contains("(empty)").doesNotContain(API_KEY);
|
||||
assertThat(downstream).isEmpty();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user