fix(gateway): never log request bodies
The gateway logged every POST and PUT body with only credentials masked, so lead, member, user and forgot-password personal data reached the log. It now logs method, masked path and masked headers only, and no longer reads or rebuilds the body. The body-masking code and its tests go. 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
f81202f46c
commit
0562689c6e
5 files changed
+56
-285
No files matched your search
@@ -9,12 +9,9 @@ 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
|
||||
* F47: the header and path copy the gateway writes 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 {
|
||||
@@ -22,85 +19,6 @@ 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() {
|
||||
|
||||
+44
-20
@@ -22,10 +22,9 @@ 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.
|
||||
* The gateway logs every request (method, path, headers) and ships the log to Elasticsearch. No request body may
|
||||
* reach that log (personal data and credentials alike), credential headers and JWTs in the path are masked, and the
|
||||
* downstream request must still receive the original body (spec gateway-request-logging).
|
||||
*/
|
||||
class RequestBodyLoggerFilterTest {
|
||||
|
||||
@@ -74,8 +73,8 @@ class RequestBodyLoggerFilterTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("customer login: the password is masked in the log and passed unchanged downstream")
|
||||
void customerLoginPasswordMasked() {
|
||||
@DisplayName("customer login: no body content in the log, body passed unchanged downstream")
|
||||
void customerLoginBodyNotLogged() {
|
||||
String body = "{\"username\":\"cust@example.com\",\"password\":\"" + PASSWORD + "\"}";
|
||||
|
||||
String downstream = run(MockServerHttpRequest.post("/auth/webCustomerAuthenticate")
|
||||
@@ -85,15 +84,14 @@ class RequestBodyLoggerFilterTest {
|
||||
|
||||
String line = loggedLine();
|
||||
assertThat(line).doesNotContain(PASSWORD).doesNotContain(API_KEY)
|
||||
.contains("/auth/webCustomerAuthenticate")
|
||||
.contains("\"username\":\"cust@example.com\"")
|
||||
.contains("\"password\":\"***\"");
|
||||
.contains("POST").contains("/auth/webCustomerAuthenticate")
|
||||
.doesNotContain("cust@example.com").doesNotContain("username").doesNotContain("password");
|
||||
assertThat(downstream).isEqualTo(body);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("password change and token fields are masked, other fields kept")
|
||||
void sensitiveFieldsMasked() {
|
||||
@DisplayName("password change: no body field name or value in the log")
|
||||
void passwordChangeBodyNotLogged() {
|
||||
String body = "{\"id\":7,\"oldPassword\":\"old" + PASSWORD + "\",\"newPassword\":\"new" + PASSWORD
|
||||
+ "\",\"confirmPassword\":\"new" + PASSWORD + "\",\"refreshToken\":\"" + JWT + "\",\"clientSecret\":\"x"
|
||||
+ PASSWORD + "\",\"name\":\"Ali\"}";
|
||||
@@ -103,19 +101,22 @@ class RequestBodyLoggerFilterTest {
|
||||
|
||||
String line = loggedLine();
|
||||
assertThat(line).doesNotContain(PASSWORD).doesNotContain(JWT)
|
||||
.contains("\"id\":7").contains("\"name\":\"Ali\"")
|
||||
.contains("\"oldPassword\":\"***\"").contains("\"refreshToken\":\"***\"");
|
||||
.doesNotContain("Ali").doesNotContain("oldPassword").doesNotContain("refreshToken")
|
||||
.contains("/user/changePassword");
|
||||
assertThat(downstream).isEqualTo(body);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("form-encoded password is masked")
|
||||
void formPasswordMasked() {
|
||||
run(MockServerHttpRequest.post("/auth/stockistAuthenticate")
|
||||
@DisplayName("form body: no field name or value in the log, body passed unchanged downstream")
|
||||
void formBodyNotLogged() {
|
||||
String body = "username=u1&password=" + PASSWORD + "&remember=1";
|
||||
String downstream = run(MockServerHttpRequest.post("/auth/stockistAuthenticate")
|
||||
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
|
||||
.body("username=u1&password=" + PASSWORD + "&remember=1"));
|
||||
.body(body));
|
||||
|
||||
assertThat(loggedLine()).doesNotContain(PASSWORD).contains("password=***").contains("remember=1");
|
||||
assertThat(loggedLine()).doesNotContain(PASSWORD).doesNotContain("username").doesNotContain("password=")
|
||||
.doesNotContain("remember").contains("/auth/stockistAuthenticate");
|
||||
assertThat(downstream).isEqualTo(body);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -152,11 +153,34 @@ class RequestBodyLoggerFilterTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("an empty POST body is logged as (empty) and still forwarded")
|
||||
@DisplayName("an empty POST is logged 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(loggedLine()).contains("POST").contains("/user/ping").doesNotContain(API_KEY);
|
||||
assertThat(downstream).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("lead update: name, mobile, email and notes stay out of the log, body passed unchanged")
|
||||
void leadUpdateBodyNotLogged() {
|
||||
String body = "{\"leadUuid\":\"3312900963345457152\",\"name\":\"Aisyah Rahman\",\"mobile\":\"+60123456701\","
|
||||
+ "\"email\":\"aisyah@example.test\",\"notes\":\"Prefers weekend calls\"}";
|
||||
|
||||
String downstream = run(MockServerHttpRequest.put("/counter/lead/update")
|
||||
.contentType(MediaType.APPLICATION_JSON).body(body));
|
||||
|
||||
assertThat(loggedLine()).contains("PUT").contains("/counter/lead/update")
|
||||
.doesNotContain("Aisyah").doesNotContain("60123456701").doesNotContain("aisyah@example.test")
|
||||
.doesNotContain("weekend").doesNotContain("3312900963345457152");
|
||||
assertThat(downstream).isEqualTo(body);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("the query string is not logged")
|
||||
void queryStringNotLogged() {
|
||||
run(MockServerHttpRequest.get("/counter/lead/list?q=aisyah&page=0").build());
|
||||
|
||||
assertThat(loggedLine()).contains("/counter/lead/list").doesNotContain("aisyah");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user