fix(daemon): normalise whitespace in checkTokenDrift to prevent false-positive warning (#39108)

This commit is contained in:
ademczuk
2026-03-07 20:10:54 +01:00
committed by GitHub
parent 74ecdec9ba
commit 70be8ce15c
2 changed files with 26 additions and 2 deletions

View File

@@ -118,6 +118,24 @@ describe("checkTokenDrift", () => {
expect(result).toBeNull();
});
it("returns null when tokens match but service token has trailing newline", () => {
const result = checkTokenDrift({ serviceToken: "same-token\n", configToken: "same-token" });
expect(result).toBeNull();
});
it("returns null when tokens match but have surrounding whitespace", () => {
const result = checkTokenDrift({ serviceToken: " same-token ", configToken: "same-token" });
expect(result).toBeNull();
});
it("returns null when both tokens have different whitespace padding", () => {
const result = checkTokenDrift({
serviceToken: "same-token\r\n",
configToken: " same-token ",
});
expect(result).toBeNull();
});
it("detects drift when config has token but service has different token", () => {
const result = checkTokenDrift({ serviceToken: "old-token", configToken: "new-token" });
expect(result).not.toBeNull();

View File

@@ -362,13 +362,19 @@ export function checkTokenDrift(params: {
}): ServiceConfigIssue | null {
const { serviceToken, configToken } = params;
// Normalise both tokens before comparing: service-file parsers (systemd,
// launchd) can return values with trailing newlines or whitespace that
// cause a false-positive mismatch against the config value.
const normService = serviceToken?.trim() || undefined;
const normConfig = configToken?.trim() || undefined;
// No drift if both are undefined/empty
if (!serviceToken && !configToken) {
if (!normService && !normConfig) {
return null;
}
// Drift: config has token, service has different or no token
if (configToken && serviceToken !== configToken) {
if (normConfig && normService !== normConfig) {
return {
code: SERVICE_AUDIT_CODES.gatewayTokenDrift,
message: