Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(wwebjs): refresh the auto-resolved WhatsApp Web pin daily

Yudhi Armyndharisc91f1ff4adb1
diff --git a/src/engine/wa-web-version.spec.ts b/src/engine/wa-web-version.spec.ts
index 0e8430717..dc8094947 100644
--- a/src/engine/wa-web-version.spec.ts
+++ b/src/engine/wa-web-version.spec.ts
@@ -126,6 +126,47 @@ describe('resolveCurrentWebVersion', () => {
const fetcher = jest.fn(() => Promise.resolve(json({ currentBeta: null, currentVersion: '2.3000.SOLO-alpha' })));
await expect(resolveCurrentWebVersion(fetcher as never)).resolves.toBe('2.3000.SOLO-alpha');
});
+
+ // The registry deletes a build's HTML about 60 days after release. A pin cached for the life of the
+ // process ended up pointing at a 404, and the page silently loaded the live build (#488 class).
+ describe('on a long-running process', () => {
+ const DAY = 86_400_000;
+ let now = FIXED_NOW;
+ beforeEach(() => {
+ now = FIXED_NOW;
+ jest.spyOn(Date, 'now').mockImplementation(() => now);
+ });
+ afterEach(() => jest.restoreAllMocks());
+
+ it('re-reads the registry once the pin is a day old', async () => {
+ const fetcher = jest
+ .fn()
+ .mockResolvedValueOnce(json({ currentVersion: '2.3000.OLD-alpha' }))
+ .mockResolvedValueOnce(json({ currentVersion: '2.3000.NEW-alpha' }));
+
+ await expect(resolveCurrentWebVersion(fetcher as never)).resolves.toBe('2.3000.OLD-alpha');
+ now += DAY - 1;
+ await expect(resolveCurrentWebVersion(fetcher as never)).resolves.toBe('2.3000.OLD-alpha');
+ expect(fetcher).toHaveBeenCalledTimes(1);
+ now += 1;
+ await expect(resolveCurrentWebVersion(fetcher as never)).resolves.toBe('2.3000.NEW-alpha');
+ expect(fetcher).toHaveBeenCalledTimes(2);
+ });
+
+ it('keeps the previous pin when a refresh fails, rather than dropping it', async () => {
+ const fetcher = jest
+ .fn()
+ .mockResolvedValueOnce(json({ currentVersion: '2.3000.OLD-alpha' }))
+ .mockRejectedValue(new Error('boom'));
+
+ await resolveCurrentWebVersion(fetcher as never);
+ now += DAY;
+ await expect(resolveCurrentWebVersion(fetcher as never)).resolves.toBe('2.3000.OLD-alpha');
+ // Inside the failure backoff too: no second fetch, and still the previous pin.
+ await expect(resolveCurrentWebVersion(fetcher as never)).resolves.toBe('2.3000.OLD-alpha');
+ expect(fetcher).toHaveBeenCalledTimes(2);
+ });
+ });
});
// Remote-HTML pins execute inside the authenticated web.whatsapp.com origin with no integrity
diff --git a/src/engine/wa-web-version.ts b/src/engine/wa-web-version.ts
index 4d2d8886d..6c95941df 100644
--- a/src/engine/wa-web-version.ts
+++ b/src/engine/wa-web-version.ts
@@ -30,7 +30,12 @@ const FAILURE_BACKOFF_MS = 60_000;
// published at least this long ago is far less likely to hang before reaching QR readiness on a
// fresh start (the #488 / #684 failure class). Exposed for tests.
export const WEB_VERSION_SETTLE_MS = 12 * 60 * 60 * 1000; // 12h
+// How long a resolved build is reused before the registry is read again. The registry deletes a
+// build's HTML about 60 days after release, so a pin held for the life of the process eventually 404s
+// and the page silently loads the live build instead.
+const CACHE_TTL_MS = 24 * 60 * 60 * 1000;
let cachedCurrentVersion: string | undefined;
+let cachedAt = 0;
let inFlight: Promise<string | null> | null = null;
let lastFailureAt = 0;
@@ -39,6 +44,7 @@ let warnedRemoteTrust = false;
/** Test-only: reset the resolved-version cache between cases. */
export function __resetWebVersionCache(): void {
cachedCurrentVersion = undefined;
+ cachedAt = 0;
inFlight = null;
lastFailureAt = 0;
warnedRemoteTrust = false;
@@ -79,7 +85,16 @@ function warnRemoteTrustOnce(pin: WebVersionPin): void {
* `lastFailureAt` backoff returns before the fetch, so at most one attempt (hence one warning) per
* FAILURE_BACKOFF_MS.
*/
-function warnResolveFailed(reason: string): void {
+function warnResolveFailed(reason: string, previous: string | null): void {
+ if (previous) {
+ logger.warn('Could not refresh the WhatsApp Web build from the wa-version registry; keeping the previous pin', {
+ action: 'web_version_refresh_failed',
+ reason,
+ webVersion: previous,
+ registry: WA_VERSION_REGISTRY_URL,
+ });
+ return;
+ }
logger.warn('Could not resolve a WhatsApp Web build from the wa-version registry — continuing WITHOUT a pin', {
action: 'web_version_resolve_failed',
reason,
@@ -128,17 +143,19 @@ export function pickSettledWebVersion(versions: unknown, now: number, currentVer
/**
* Fetch the current known-good WhatsApp Web build from the wa-version registry. A SUCCESSFUL resolve
- * is cached for the process lifetime; a failure resolves to null WITHOUT caching, so a later call
- * retries (a single transient outage must not permanently defeat the #488 fix). Concurrent callers
- * share one in-flight fetch. Prefers a build that has settled (see `pickSettledWebVersion`) over the
- * registry's possibly-minute-old `currentVersion`.
+ * is cached for CACHE_TTL_MS, then refreshed; a failure is NOT cached, so a later call retries (a
+ * single transient outage must not permanently defeat the #488 fix). A failed refresh keeps the
+ * previous build, and only a process that never resolved one gets null. Concurrent callers share one
+ * in-flight fetch. Prefers a build that has settled (see `pickSettledWebVersion`) over the registry's
+ * possibly-minute-old `currentVersion`.
*/
export async function resolveCurrentWebVersion(fetcher: typeof fetch = fetch): Promise<string | null> {
- if (typeof cachedCurrentVersion === 'string') return cachedCurrentVersion;
+ const previous = cachedCurrentVersion ?? null;
+ if (previous && Date.now() - cachedAt < CACHE_TTL_MS) return previous;
if (inFlight) return inFlight;
- // Within the backoff window after a recent failure, return null instantly without a network call so
- // a firewalled/offline host doesn't re-stall on every status poll / session start.
- if (lastFailureAt && Date.now() - lastFailureAt < FAILURE_BACKOFF_MS) return null;
+ // Within the backoff window after a recent failure, answer instantly without a network call so a
+ // firewalled/offline host doesn't re-stall on every status poll / session start.
+ if (lastFailureAt && Date.now() - lastFailureAt < FAILURE_BACKOFF_MS) return previous;
inFlight = (async (): Promise<string | null> => {
try {
const controller = new AbortController();
@@ -152,18 +169,19 @@ export async function resolveCurrentWebVersion(fetcher: typeof fetch = fetch): P
const picked = pickSettledWebVersion(json.versions, Date.now(), rawCurrent);
if (picked) {
cachedCurrentVersion = picked; // cache only on success
+ cachedAt = Date.now();
return picked;
}
lastFailureAt = Date.now(); // nothing usable — back off, then retry
- warnResolveFailed('the registry carried no usable build');
- return null;
+ warnResolveFailed('the registry carried no usable build', previous);
+ return previous;
} finally {
clearTimeout(timer);
}
} catch (error) {
lastFailureAt = Date.now(); // fetch failed — back off, then retry
- warnResolveFailed(error instanceof Error ? error.message : String(error));
- return null;
+ warnResolveFailed(error instanceof Error ? error.message : String(error), previous);
+ return previous;
} finally {
inFlight = null;
}