Skip to content

fix(whatsapp): write creds.json atomically - #63577

Merged
mcaxtr merged 3 commits into
openclaw:mainfrom
OwenYWT:codex/whatsapp-atomic-creds
Apr 16, 2026
Merged

mcaxtr merged 3 commits into
openclaw:mainfrom
OwenYWT:codex/whatsapp-atomic-creds

Conversation

@OwenYWT

@OwenYWT OwenYWT commented Apr 9, 2026

Copy link
Copy Markdown
Contributor

Summary

Stop WhatsApp web credential updates from rewriting creds.json in-place by switching the queued save path to an atomic temp-file rename.

Changes

  • add an explicit writeCredsJsonAtomically() helper for WhatsApp auth state
  • reuse the existing queued creds.update path, but swap the final write to temp-file + rename
  • extend the Baileys test mock to expose BufferJSON
  • add regression coverage for the atomic write helper and the updated save path behavior

Test Plan

  • pnpm --dir /Users/wentao/Desktop/openclaw exec vitest run extensions/whatsapp/src/session.test.ts
  • NODE_OPTIONS=--max-old-space-size=8192 pnpm --dir /Users/wentao/Desktop/openclaw exec tsc --noEmit --pretty false

@openclaw-barnacle openclaw-barnacle Bot added channel: whatsapp-web Channel integration: whatsapp-web size: S labels Apr 9, 2026
@greptile-apps

greptile-apps Bot commented Apr 9, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR replaces the in-place WhatsApp creds.json write with an atomic write helper (writeCredsJsonAtomically) that writes to a pid+timestamp-named temp file with 0o600 permissions and then does a POSIX rename into place, eliminating the corruption window that existed when the process was killed mid-write. The queue-based serialization and multi-account isolation are preserved, and the backup-before-write logic in safeSaveCreds is unaffected.

Confidence Score: 5/5

Safe to merge — the atomic write logic is correct and the only findings are minor style issues.

All findings are P2 (redundant chmod, duplicate log warnings on failure). The core fix — temp-file + rename atomicity, same-filesystem guarantee, queue preservation, and 0o600 permissions — is implemented correctly. Tests cover the unit path, the serialization invariant, and the multi-authDir case.

No files require special attention.

Vulnerabilities

  • The temp file is created with { mode: 0o600 } and a best-effort chmodSync is applied after rename, keeping credentials readable only by the owning user.
  • The temp file is named <pid>.<timestamp>.tmp inside the same authDir as the final file, ensuring the rename is same-filesystem and therefore atomic on POSIX.
  • No new attack surface introduced; the change narrows an existing integrity risk by removing the in-place write window.

Comments Outside Diff (1)

  1. extensions/whatsapp/src/session.ts, line 118-124 (link)

    P2 Redundant chmodSync after atomic write

    writeCredsJsonAtomically already does a best-effort chmodSync(credsPath, 0o600) after the rename. The second chmodSync here in safeSaveCreds is now a no-op on every save. It was meaningful when saveCreds was the bare Baileys write, but can be removed.

    Prompt To Fix With AI
    This is a comment left during a code review.
    Path: extensions/whatsapp/src/session.ts
    Line: 118-124
    
    Comment:
    **Redundant `chmodSync` after atomic write**
    
    `writeCredsJsonAtomically` already does a best-effort `chmodSync(credsPath, 0o600)` after the rename. The second `chmodSync` here in `safeSaveCreds` is now a no-op on every save. It was meaningful when `saveCreds` was the bare Baileys write, but can be removed.
    
    
    
    How can I resolve this? If you propose a fix, please make it concise.
Prompt To Fix All With AI
This is a comment left during a code review.
Path: extensions/whatsapp/src/session.ts
Line: 58-66

Comment:
**Duplicate log entries on failure**

When the write fails, `writeCredsJsonAtomically` logs `"failed atomic WhatsApp creds write"` then rethrows, and `safeSaveCreds` catches that same error and logs `"failed saving WhatsApp creds"`. Every creds-write failure produces two distinct warnings for one root cause, which can look like two separate failures in production logs.

Consider removing the `logger.warn` here and letting `safeSaveCreds` own the single error log, or suppress the outer one.

```suggestion
  } catch (err) {
    try {
      await fs.rm(tempPath, { force: true });
    } catch {
      // best-effort cleanup
    }
    throw err;
  }
```

How can I resolve this? If you propose a fix, please make it concise.

---

This is a comment left during a code review.
Path: extensions/whatsapp/src/session.ts
Line: 118-124

Comment:
**Redundant `chmodSync` after atomic write**

`writeCredsJsonAtomically` already does a best-effort `chmodSync(credsPath, 0o600)` after the rename. The second `chmodSync` here in `safeSaveCreds` is now a no-op on every save. It was meaningful when `saveCreds` was the bare Baileys write, but can be removed.

```suggestion
    await Promise.resolve(saveCreds());
```

How can I resolve this? If you propose a fix, please make it concise.

Reviews (1): Last reviewed commit: "fix(whatsapp): write creds.json atomical..." | Re-trigger Greptile

Comment on lines +58 to +66
} catch (err) {
try {
await fs.rm(tempPath, { force: true });
} catch {
// best-effort cleanup
}
logger.warn({ error: String(err) }, "failed atomic WhatsApp creds write");
throw err;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Duplicate log entries on failure

When the write fails, writeCredsJsonAtomically logs "failed atomic WhatsApp creds write" then rethrows, and safeSaveCreds catches that same error and logs "failed saving WhatsApp creds". Every creds-write failure produces two distinct warnings for one root cause, which can look like two separate failures in production logs.

Consider removing the logger.warn here and letting safeSaveCreds own the single error log, or suppress the outer one.

Suggested change
} catch (err) {
try {
await fs.rm(tempPath, { force: true });
} catch {
// best-effort cleanup
}
logger.warn({ error: String(err) }, "failed atomic WhatsApp creds write");
throw err;
}
} catch (err) {
try {
await fs.rm(tempPath, { force: true });
} catch {
// best-effort cleanup
}
throw err;
}
Prompt To Fix With AI
This is a comment left during a code review.
Path: extensions/whatsapp/src/session.ts
Line: 58-66

Comment:
**Duplicate log entries on failure**

When the write fails, `writeCredsJsonAtomically` logs `"failed atomic WhatsApp creds write"` then rethrows, and `safeSaveCreds` catches that same error and logs `"failed saving WhatsApp creds"`. Every creds-write failure produces two distinct warnings for one root cause, which can look like two separate failures in production logs.

Consider removing the `logger.warn` here and letting `safeSaveCreds` own the single error log, or suppress the outer one.

```suggestion
  } catch (err) {
    try {
      await fs.rm(tempPath, { force: true });
    } catch {
      // best-effort cleanup
    }
    throw err;
  }
```

How can I resolve this? If you propose a fix, please make it concise.

@mcaxtr mcaxtr self-assigned this Apr 16, 2026
@mcaxtr
mcaxtr force-pushed the codex/whatsapp-atomic-creds branch from f4e708d to 70ef5b3 Compare April 16, 2026 05:35
@mcaxtr
mcaxtr merged commit b878d50 into openclaw:main Apr 16, 2026
43 checks passed
@mcaxtr

mcaxtr commented Apr 16, 2026

Copy link
Copy Markdown
Member

Merged via squash.

Thanks @OwenYWT!

xudaiyanzi pushed a commit to xudaiyanzi/openclaw that referenced this pull request Apr 17, 2026
Merged via squash.

Prepared head SHA: 70ef5b3
Co-authored-by: OwenYWT <[email protected]>
Co-authored-by: mcaxtr <[email protected]>
Reviewed-by: @mcaxtr
kvnkho pushed a commit to kvnkho/openclaw that referenced this pull request Apr 17, 2026
Merged via squash.

Prepared head SHA: 70ef5b3
Co-authored-by: OwenYWT <[email protected]>
Co-authored-by: mcaxtr <[email protected]>
Reviewed-by: @mcaxtr
Mquarmoc pushed a commit to Mquarmoc/openclaw that referenced this pull request Apr 20, 2026
Merged via squash.

Prepared head SHA: 70ef5b3
Co-authored-by: OwenYWT <[email protected]>
Co-authored-by: mcaxtr <[email protected]>
Reviewed-by: @mcaxtr
lovewanwan pushed a commit to lovewanwan/openclaw that referenced this pull request Apr 28, 2026
Merged via squash.

Prepared head SHA: 70ef5b3
Co-authored-by: OwenYWT <[email protected]>
Co-authored-by: mcaxtr <[email protected]>
Reviewed-by: @mcaxtr
ogt-redknie pushed a commit to ogt-redknie/OPENX that referenced this pull request May 2, 2026
Merged via squash.

Prepared head SHA: 70ef5b3
Co-authored-by: OwenYWT <[email protected]>
Co-authored-by: mcaxtr <[email protected]>
Reviewed-by: @mcaxtr
globalcaos pushed a commit to globalcaos/tinkerclaw that referenced this pull request May 13, 2026
Merged via squash.

Prepared head SHA: 70ef5b3
Co-authored-by: OwenYWT <[email protected]>
Co-authored-by: mcaxtr <[email protected]>
Reviewed-by: @mcaxtr
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request May 24, 2026
Merged via squash.

Prepared head SHA: 70ef5b3
Co-authored-by: OwenYWT <[email protected]>
Co-authored-by: mcaxtr <[email protected]>
Reviewed-by: @mcaxtr
jameslcowan pushed a commit to jameslcowan/openclaw that referenced this pull request Jun 2, 2026
Merged via squash.

Prepared head SHA: 70ef5b3
Co-authored-by: OwenYWT <[email protected]>
Co-authored-by: mcaxtr <[email protected]>
Reviewed-by: @mcaxtr
vixco pushed a commit to vixco/openclaw that referenced this pull request Aug 16, 2026
Merged via squash.

Prepared head SHA: 70ef5b3
Co-authored-by: OwenYWT <[email protected]>
Co-authored-by: mcaxtr <[email protected]>
Reviewed-by: @mcaxtr
Heetisk pushed a commit to Heetisk/openclaw that referenced this pull request Sep 28, 2026
Merged via squash.

Prepared head SHA: 70ef5b3
Co-authored-by: OwenYWT <[email protected]>
Co-authored-by: mcaxtr <[email protected]>
Reviewed-by: @mcaxtr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

channel: whatsapp-web Channel integration: whatsapp-web size: M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants