Skip to content

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

Closed
byungsker wants to merge 1 commit intoopenclaw:mainfrom
byungsker:fix/token-drift-false-positive-whitespace-26624
Closed

fix(daemon): normalise whitespace in checkTokenDrift to prevent false-positive token-mismatch warning#26656
byungsker wants to merge 1 commit intoopenclaw:mainfrom
byungsker:fix/token-drift-false-positive-whitespace-26624

Conversation

@byungsker
Copy link
Copy Markdown

@byungsker byungsker commented Feb 25, 2026

Problem

openclaw gateway restart shows:

⚠️ Config token differs from service token. The daemon will use the old token after restart.

…even when the two token values are identical.

Root Cause

checkTokenDrift compares serviceToken !== configToken as raw strings. Service-file parsers on Linux (systemd EnvironmentFile/Environment=) and macOS (launchd plist) can return environment-variable values with trailing newlines or spaces. The config token goes through trimToUndefined() inside resolveGatewayCredentialsFromConfig, but the service token extracted in lifecycle-core.ts at line 282 was passed without .trim():

// before — no trim on the service-file value
const serviceToken = command?.environment?.OPENCLAW_GATEWAY_TOKEN;

So "abc123\n" !== "abc123" → false-positive drift detected.

Fix

Two changes:

service-audit.ts: trim both sides inside checkTokenDrift itself, making the function robust regardless of how callers supply the raw values.

lifecycle-core.ts: add .trim() at the extraction site for defence-in-depth (mirrors the existing trim already present in auditGatewayServiceConfig at line 215).

Tests

Two new test cases in service-audit.test.ts:

  • "same-token\n" vs "same-token"null (no drift) ✅
  • " same-token " vs "same-token"null (no drift) ✅

All 24 daemon-cli and service-audit tests pass.

Fixes #26624

Greptile Summary

Fixed false-positive token-mismatch warning in openclaw gateway restart by normalizing whitespace in token comparison. Service-file parsers (systemd EnvironmentFile, launchd plist) can inject trailing newlines or spaces into environment variable values, causing identical tokens to appear different when compared as raw strings.

  • Added .trim() to service token extraction in lifecycle-core.ts:282 for defense-in-depth
  • Enhanced checkTokenDrift in service-audit.ts to normalize both tokens before comparison using .trim() || undefined pattern
  • Added test coverage for trailing newlines and surrounding whitespace scenarios

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk
  • The fix is surgical and well-tested: adds .trim() calls to normalize whitespace in token comparison, includes two new test cases covering the specific edge cases (trailing newlines and surrounding spaces), and follows the existing pattern already used elsewhere in the codebase (auditGatewayToken at line 215). The change is defensive, backward-compatible, and directly addresses the reported false-positive warning without introducing new behavior.
  • No files require special attention

Last reviewed commit: da16723

…-positive token-mismatch warning

Service-file parsers (systemd EnvironmentFile, launchd plist) can return
environment-variable values with trailing newlines or spaces, causing
checkTokenDrift to report 'Config token differs from service token' even
though the two tokens are logically identical.

Two-part fix:
- service-audit.ts: trim both serviceToken and configToken before comparing
  inside checkTokenDrift so the function is robust regardless of how callers
  obtain the raw values
- lifecycle-core.ts: also add .trim() at the call site for defence-in-depth
  (mirrors the existing trim already applied in auditGatewayServiceConfig)

Two new test cases cover the whitespace edge cases.

Fixes openclaw#26624
@openclaw-barnacle openclaw-barnacle bot added gateway Gateway runtime cli CLI command changes size: XS trusted-contributor labels Feb 25, 2026
@openclaw-barnacle
Copy link
Copy Markdown

This pull request has been automatically marked as stale due to inactivity.
Please add updates or it will be closed.

@openclaw-barnacle openclaw-barnacle bot added the stale Marked as stale due to inactivity label Mar 3, 2026
@byungsker

This comment was marked as spam.

@byungsker

This comment was marked as spam.

@openclaw-barnacle openclaw-barnacle bot removed the stale Marked as stale due to inactivity label Mar 4, 2026
@openclaw-barnacle
Copy link
Copy Markdown

Closing this PR because the author has more than 10 active PRs in this repo. Please reduce the active PR queue and reopen or resubmit once it is back under the limit. You can close your own PRs to get back under the limit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cli CLI command changes gateway Gateway runtime r: too-many-prs size: XS

Projects

None yet

Development

Successfully merging this pull request may close these issues.

False positive warning: 'Config token differs from service token' when tokens match

2 participants