Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(wwebjs): refuse a backport tree an interrupted run left half done

Yudhi Armyndharisebfa9512a939
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6b28ab450..564adcfbf 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -127,7 +127,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Bound `GET /api/search` to 1000 characters and 32 words, punctuation counted as a separator and each symbol or emoji as a word, and run a repeated word once, so one request can no longer hold the event loop on SQLite for seconds.
- Print the startup banner's API Docs URL only when Swagger is served, so a production boot no longer points at a `404`.
- Decode numeric and common named character references in a Baileys link preview's title and description, so `’` and `é` no longer reach the recipient as written.
-- Log an error at whatsapp-web.js start when the message-id backport is only partly applied, naming the files it did not reach.
+- Log an error at whatsapp-web.js start when the message-id backport is only partly applied, naming the files it did not reach, and have the patcher refuse such a tree, whichever file the interrupted install stopped at, instead of reporting version skew and leaving `.rej` files.
- Handle an engine disconnect again when the session row read fails, so the session reconnects instead of staying registered with no reconnect armed.
- Show the error a sandboxed plugin's worker threw before it died in the plugin's status and the log, instead of only its exit code.
diff --git a/scripts/patch-wwebjs-201832.js b/scripts/patch-wwebjs-201832.js
index 0ca843a5e..b3fc76934 100644
--- a/scripts/patch-wwebjs-201832.js
+++ b/scripts/patch-wwebjs-201832.js
@@ -81,9 +81,10 @@ function siteLanded(wwjsDir, rel) {
/**
* The REQUIRED_SITES a backport this patcher began did not reach, by relative path. The tree is judged
- * ours by the id read in the Message constructor: without it the backport never started (the caller's
- * own check says so) or upstream fixed the tree its own way. A file that cannot be read is not counted,
- * since an install that cannot be inspected is not evidence of a broken one.
+ * ours when at least one site carries the text its marker looks for: with none the backport never
+ * started, or upstream fixed the tree its own way. A run killed part-way leaves any prefix of the twelve
+ * files, so no single file can stand for the rest. A file that cannot be read is not counted, since an
+ * install that cannot be inspected is not evidence of a broken one.
*/
function missingSites(wwjsDir = DEFAULT_WWJS) {
const landed = rel => {
@@ -93,7 +94,7 @@ function missingSites(wwjsDir = DEFAULT_WWJS) {
return undefined;
}
};
- if (landed('src/structures/Message.js') !== true) return [];
+ if (!REQUIRED_SITES.some(([rel]) => landed(rel) === true)) return [];
return REQUIRED_SITES.filter(([rel]) => landed(rel) === false).map(([rel]) => rel);
}
@@ -232,7 +233,8 @@ function applyBackport(wwjsDir = DEFAULT_WWJS, patchFile = DEFAULT_PATCH) {
const msgSrc = fs.readFileSync(msgJs, 'utf8');
if (/_normalizeId\(|\.\$1/.test(msgSrc)) {
// Message.js normalizing is necessary but NOT sufficient to stand down. `patch` writes as it goes and
- // Message.js is patched early, so a run that died part-way through lands here too — and skipping would
+ // Message.js is the second to last file it writes, so a run that died after it lands here too (and one
+ // that died before it is caught below) — and skipping would
// latch that half-patched tree in permanently, because every later run takes this same branch and never
// reaches the assertions below. An upstream release that fixes this lands every site at once; a crashed
// run does not. Proving the tree is whole is what tells the two apart.
@@ -253,6 +255,20 @@ function applyBackport(wwjsDir = DEFAULT_WWJS, patchFile = DEFAULT_PATCH) {
return { skipped: true, reason: 'installed whatsapp-web.js already normalizes message ids' };
}
+ // A run that died before Message.js (the 11th of 12 files) left the earlier files patched and this one
+ // not. Applying the diff again would reject every hunk that already landed, litter the tree with .rej
+ // files and report version skew for what is an interrupted install.
+ const landedBefore = missingSites(wwjsDir);
+ if (landedBefore.length) {
+ throw partialTree(
+ new Error(
+ 'whatsapp-web.js is PARTIALLY patched — an earlier run was interrupted and ' +
+ `${landedBefore.join(', ')} did not land. The tree cannot repair itself. Reinstall the dependency ` +
+ '(`rm -rf node_modules/whatsapp-web.js && npm ci`) and re-run.',
+ ),
+ );
+ }
+
// Apply the real upstream diff via `patch` (no git-discovery interference).
// Flags, each load-bearing:
// --no-backup-if-mismatch GNU patch defaults to writing `<file>.~1~` backups for every file
diff --git a/src/engine/adapters/wwebjs-backport-201832.spec.ts b/src/engine/adapters/wwebjs-backport-201832.spec.ts
index a61698e54..9b598ccd6 100644
--- a/src/engine/adapters/wwebjs-backport-201832.spec.ts
+++ b/src/engine/adapters/wwebjs-backport-201832.spec.ts
@@ -135,10 +135,37 @@ describe('patch-wwebjs-201832 (build-time backport of upstream #201832)', () =>
expect(() => applyBackport(dir)).toThrow(/PARTIALLY patched[\s\S]*Injected\/Utils\.js/);
});
+ // `patch` writes in diff order and Message.js is the 11th of 12 files, so a run that was killed usually
+ // stopped before it. Applying the diff again would reject the ten hunks that landed, leave their .rej
+ // files in the tree and call an interrupted install version skew.
+ it('refuses a tree whose run stopped before Message.js, without touching it', () => {
+ const dir = copyWwjs();
+ const messageJs = path.join(dir, 'src', 'structures', 'Message.js');
+ const utilsJs = path.join(dir, 'src', 'util', 'Injected', 'Utils.js');
+ const pristine = [messageJs, utilsJs].map(file => fs.readFileSync(file, 'utf8'));
+ applyBackport(dir);
+ fs.writeFileSync(messageJs, pristine[0]);
+ fs.writeFileSync(utilsJs, pristine[1]);
+
+ expect(() => applyBackport(dir)).toThrow(/PARTIALLY patched[\s\S]*Message\.js[\s\S]*reinstall/i);
+
+ expect(fs.readFileSync(messageJs, 'utf8')).toBe(pristine[0]);
+ const litter: string[] = [];
+ const walk = (current: string): void => {
+ for (const entry of fs.readdirSync(current, { withFileTypes: true })) {
+ const full = path.join(current, entry.name);
+ if (entry.isDirectory()) walk(full);
+ else if (/\.(rej|orig)$/.test(entry.name)) litter.push(path.relative(dir, full));
+ }
+ };
+ walk(path.join(dir, 'src'));
+ expect(litter).toEqual([]);
+ });
+
it('refuses to stand down on a half-patched tree', () => {
const dir = copyWwjs();
- // Exactly what a run that died mid-apply leaves: `patch` writes as it goes and Message.js is patched
- // early, so the load-bearing constructor normalizes while its siblings never did. Standing down here
+ // Exactly what a run that died mid-apply leaves: `patch` writes as it goes and Message.js is the
+ // second to last file, so the load-bearing constructor normalizes while its siblings never did. Standing down here
// latches the broken tree in for good — every later run reads Message.js and takes the same branch —
// and ships a whatsapp-web.js whose ids work in some paths and not others.
const msgJs = path.join(dir, 'src', 'structures', 'Message.js');
diff --git a/src/engine/adapters/wwebjs-backport-check.spec.ts b/src/engine/adapters/wwebjs-backport-check.spec.ts
index 04212b948..ffa10db9d 100644
--- a/src/engine/adapters/wwebjs-backport-check.spec.ts
+++ b/src/engine/adapters/wwebjs-backport-check.spec.ts
@@ -90,12 +90,24 @@ describe('isBackportMissing', () => {
it('answers a tree with no backport at all with the missing-backport message, not a list of sites', () => {
const dir = wholeInstall();
- fs.writeFileSync(path.join(dir, 'src', 'structures', 'Message.js'), 'this.id = data.id;');
+ for (const [rel] of REQUIRED_SITES) fs.writeFileSync(path.join(dir, rel), 'this.id = data.id;');
expect(partialBackportSites(dir)).toEqual([]);
expect(backportProblem(dir)).toBe(BACKPORT_MISSING_MESSAGE);
});
+ // Message.js is the 11th of 12 files the patcher writes, so a run that was killed usually stopped
+ // before it: ten sites landed, Message.js did not, and the tree is not a pristine one.
+ it('names a tree whose run stopped before Message.js, not the missing-backport message', () => {
+ const dir = wholeInstall();
+ fs.writeFileSync(path.join(dir, 'src', 'structures', 'Message.js'), 'this.id = data.id;');
+ fs.writeFileSync(path.join(dir, 'src', 'util', 'Injected', 'Utils.js'), '_serialized: msg.id._serialized');
+
+ expect(isBackportMissing(dir)).toBe(true);
+ expect(partialBackportSites(dir)).toEqual(['src/structures/Message.js', 'src/util/Injected/Utils.js']);
+ expect(backportProblem(dir)).toContain('src/structures/Message.js, src/util/Injected/Utils.js');
+ });
+
it('leaves a tree fixed upstream in its own way alone', () => {
const dir = install('this.id = data.id._serialized || data.id.$1;');
diff --git a/src/engine/adapters/wwebjs-backport-check.ts b/src/engine/adapters/wwebjs-backport-check.ts
index 9aee7079a..b5d597b78 100644
--- a/src/engine/adapters/wwebjs-backport-check.ts
+++ b/src/engine/adapters/wwebjs-backport-check.ts
@@ -78,9 +78,13 @@ export function partialBackportSites(wwjsDir?: string): string[] {
}
}
-/** What the startup guard should say about the installed backport, or undefined when it needs no word. */
+/**
+ * What the startup guard should say about the installed backport, or undefined when it needs no word.
+ * A partly applied tree is named first: an interrupted run usually stops before Message.js, which then
+ * reads as unpatched, and the patcher refuses a partly applied tree rather than finish it.
+ */
export function backportProblem(wwjsDir?: string): string | undefined {
- if (isBackportMissing(wwjsDir)) return BACKPORT_MISSING_MESSAGE;
const sites = partialBackportSites(wwjsDir);
- return sites.length ? partialBackportMessage(sites) : undefined;
+ if (sites.length) return partialBackportMessage(sites);
+ return isBackportMissing(wwjsDir) ? BACKPORT_MISSING_MESSAGE : undefined;
}