fix(auth): redact passwords on normalized login paths
- Normalize login paths before applying request-body redaction. - Cover trailing-slash paths and assert the password never reaches logs.
This commit is contained in:
1 parent
de41571b5c
commit
72e127888c
2 files changed
+50
-2
No files matched your search
@@ -9,8 +9,10 @@ import { Observable, tap } from "rxjs";
|
|||||||
import { TraceLogger } from "../common/trace-logger";
|
import { TraceLogger } from "../common/trace-logger";
|
||||||
|
|
||||||
export function redactAuthRequestBody(path: string, body: unknown): unknown {
|
export function redactAuthRequestBody(path: string, body: unknown): unknown {
|
||||||
|
const normalizedPath = path.toLowerCase().replace(/\/+$/, "");
|
||||||
if (
|
if (
|
||||||
!/^(?:\/api\/v1)?\/auth\/login$/.test(path) ||
|
(normalizedPath !== "/auth/login" &&
|
||||||
|
normalizedPath !== "/api/v1/auth/login") ||
|
||||||
!body ||
|
!body ||
|
||||||
typeof body !== "object" ||
|
typeof body !== "object" ||
|
||||||
Array.isArray(body)
|
Array.isArray(body)
|
||||||
|
|||||||
@@ -1,4 +1,10 @@
|
|||||||
import { redactAuthRequestBody } from "../src/interceptors/logging.interceptor";
|
import { CallHandler, ExecutionContext } from "@nestjs/common";
|
||||||
|
import { of } from "rxjs";
|
||||||
|
import { TraceLogger } from "../src/common/trace-logger";
|
||||||
|
import {
|
||||||
|
LoggingInterceptor,
|
||||||
|
redactAuthRequestBody,
|
||||||
|
} from "../src/interceptors/logging.interceptor";
|
||||||
|
|
||||||
describe("redactAuthRequestBody", () => {
|
describe("redactAuthRequestBody", () => {
|
||||||
it("redacts login passwords on local and API-prefixed route paths", () => {
|
it("redacts login passwords on local and API-prefixed route paths", () => {
|
||||||
@@ -11,6 +17,46 @@ describe("redactAuthRequestBody", () => {
|
|||||||
username: "operator",
|
username: "operator",
|
||||||
password: "[REDACTED]",
|
password: "[REDACTED]",
|
||||||
});
|
});
|
||||||
|
expect(redactAuthRequestBody("/api/v1/auth/login/", body)).toEqual({
|
||||||
|
username: "operator",
|
||||||
|
password: "[REDACTED]",
|
||||||
|
});
|
||||||
|
expect(redactAuthRequestBody("/API/V1/AUTH/LOGIN/", body)).toEqual({
|
||||||
|
username: "operator",
|
||||||
|
password: "[REDACTED]",
|
||||||
|
});
|
||||||
expect(redactAuthRequestBody("/api/v1/scenarios", body)).toBe(body);
|
expect(redactAuthRequestBody("/api/v1/scenarios", body)).toBe(body);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("never logs a password received on the accepted trailing-slash login path", () => {
|
||||||
|
const loggedMessages: unknown[][] = [];
|
||||||
|
const debug = jest
|
||||||
|
.spyOn(TraceLogger.prototype, "debug")
|
||||||
|
.mockImplementation((...args: unknown[]) => {
|
||||||
|
loggedMessages.push(args);
|
||||||
|
});
|
||||||
|
const context = {
|
||||||
|
switchToHttp: () => ({
|
||||||
|
getRequest: () => ({
|
||||||
|
method: "POST",
|
||||||
|
url: "/api/v1/auth/login/",
|
||||||
|
path: "/api/v1/auth/login/",
|
||||||
|
body: { username: "operator", password: "synthetic-login-secret" },
|
||||||
|
}),
|
||||||
|
getResponse: () => ({ statusCode: 200 }),
|
||||||
|
}),
|
||||||
|
} as unknown as ExecutionContext;
|
||||||
|
const handler = {
|
||||||
|
handle: () => of({ user: { username: "operator" } }),
|
||||||
|
} as CallHandler;
|
||||||
|
|
||||||
|
try {
|
||||||
|
new LoggingInterceptor().intercept(context, handler).subscribe();
|
||||||
|
const output = JSON.stringify(loggedMessages);
|
||||||
|
expect(output).toContain("[REDACTED]");
|
||||||
|
expect(output).not.toContain("synthetic-login-secret");
|
||||||
|
} finally {
|
||||||
|
debug.mockRestore();
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
Reference in new issue
Block a user