mirror of
https://github.com/moltbot/moltbot.git
synced 2026-03-08 06:54:24 +00:00
fix(daemon): normalise whitespace in checkTokenDrift to prevent false-positive warning (#39108)
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user