@pontalabs/baileys 1.0.4 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -116,20 +116,29 @@ function makeLibSignalRepository(auth, onWhatsAppFunc, logger) {
116
116
  }
117
117
  }
118
118
  }
119
- return auth.keys.transaction(async () => {
120
- let result;
121
- switch (type) {
122
- case 'pkmsg':
123
- result = await session.decryptPreKeyWhisperMessage(ciphertext);
124
- break;
125
- case 'msg':
126
- result = await session.decryptWhisperMessage(ciphertext);
127
- break;
128
- default:
129
- throw new Error(`Unknown message type: ${type}`);
119
+ try {
120
+ return await auth.keys.transaction(async () => {
121
+ let result;
122
+ switch (type) {
123
+ case 'pkmsg':
124
+ result = await session.decryptPreKeyWhisperMessage(ciphertext);
125
+ break;
126
+ case 'msg':
127
+ result = await session.decryptWhisperMessage(ciphertext);
128
+ break;
129
+ default:
130
+ throw new Error(`Unknown message type: ${type}`);
131
+ }
132
+ return result;
133
+ }, jid);
134
+ } catch (error) {
135
+ const errText = String(error?.message || error || '')
136
+ if (/bad mac|no session|invalid message|duplicate message|cannot decrypt/i.test(errText)) {
137
+ await storage.removeSession(addr.toString()).catch(() => null)
138
+ logger?.warn?.({ jid, type, errText }, 'reset stale session after decrypt failure')
130
139
  }
131
- return result;
132
- }, jid);
140
+ throw error
141
+ }
133
142
  },
134
143
  async encryptMessage({ jid, data }) {
135
144
  const addr = jidToSignalProtocolAddress(jid);
@@ -203,6 +212,9 @@ function signalStorage({ creds, keys }) {
203
212
  storeSession: async (id, session) => {
204
213
  await keys.set({ session: { [id]: session.serialize() } });
205
214
  },
215
+ removeSession: async (id) => {
216
+ await keys.set({ session: { [id]: null } });
217
+ },
206
218
  isTrustedIdentity: () => {
207
219
  return true;
208
220
  },
@@ -14,6 +14,16 @@ const crypto_2 = require("./crypto")
14
14
  const generics_1 = require("./generics")
15
15
  const mutex_1 = require("async-mutex")
16
16
 
17
+
18
+ // Cache bypass for signal-sensitive keys so runtime state doesn't keep stale
19
+ // session/pre-key material alive after decrypt failures or identity rotation.
20
+ const NO_CACHE_TYPES = new Set([
21
+ 'pre-key',
22
+ 'session',
23
+ 'signed-pre-key',
24
+ 'identity-key'
25
+ ])
26
+
17
27
  /**
18
28
  * Adds caching capability to a SignalKeyStore
19
29
  * @param store the store to add caching to
@@ -35,6 +45,11 @@ function makeCacheableSignalKeyStore(store, logger, _cache) {
35
45
  }
36
46
  return {
37
47
  async get(type, ids) {
48
+ if (NO_CACHE_TYPES.has(type)) {
49
+ logger?.trace({ type, items: ids.length }, 'bypassing cache for sensitive key type')
50
+ return await store.get(type, ids)
51
+ }
52
+
38
53
  return cacheMutex.runExclusive(async () => {
39
54
  const data = {}
40
55
  const idsToFetch = []
@@ -65,6 +80,9 @@ function makeCacheableSignalKeyStore(store, logger, _cache) {
65
80
  return cacheMutex.runExclusive(async () => {
66
81
  let keys = 0
67
82
  for (const type in data) {
83
+ if (NO_CACHE_TYPES.has(type)) {
84
+ continue
85
+ }
68
86
  for (const id in data[type]) {
69
87
  await cache.set(getUniqueId(type, id), data[type][id])
70
88
  keys += 1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pontalabs/baileys",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "description": "PontaLabs Baileys is a lightweight, modern, and customizable WhatsApp Web API library built on Baileys.",
5
5
  "keywords": [
6
6
  "facebook",