Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(plugins): default a non-numeric getChatHistory limit before clamping

Yudhi Armyndharis8dc9f38f655b
diff --git a/src/core/plugins/plugin-capability-context.ts b/src/core/plugins/plugin-capability-context.ts
index da441d7f4..cc3e81b78 100644
--- a/src/core/plugins/plugin-capability-context.ts
+++ b/src/core/plugins/plugin-capability-context.ts
@@ -268,8 +268,9 @@ export class PluginCapabilityContext {
this.resolveEngineRead(plugin, sessionId).getChatHistory(
chatId,
// Clamp to the REST non-deep ceiling (MessageService.MAX_CHAT_HISTORY_LIMIT = 100) so an
- // untrusted plugin can't request an unbounded history fetch.
- Math.min(Math.max(Math.trunc(limit ?? 50), 1), 100),
+ // untrusted plugin can't request an unbounded history fetch. A non-finite limit (a sandboxed
+ // caller's arg is unvalidated) takes the default: NaN would reach the engine as "no limit".
+ Math.min(Math.max(typeof limit === 'number' && Number.isFinite(limit) ? Math.trunc(limit) : 50, 1), 100),
includeMedia ?? false,
),
canonicalChatId: (sessionId, chatId) => {
diff --git a/src/core/plugins/plugin-capability.spec.ts b/src/core/plugins/plugin-capability.spec.ts
index 981b4225f..a845a160f 100644
--- a/src/core/plugins/plugin-capability.spec.ts
+++ b/src/core/plugins/plugin-capability.spec.ts
@@ -288,6 +288,18 @@ describe('PluginLoaderService capability facade — ctx.engine', () => {
expect(engine.getChatHistory).toHaveBeenNthCalledWith(2, '[email protected]', 1, false);
});
+ // A sandboxed caller's args cross structured clone unvalidated; NaN would reach the engine as "no limit".
+ it.each([['x'], [NaN], [{}], [Infinity]])(
+ 'engine.getChatHistory defaults a non-finite limit (%p) to 50',
+ async limit => {
+ const engine = { getChatHistory: jest.fn().mockResolvedValue([]) };
+ build(engine);
+ const ctx = contextFor(makePlugin(['*'], ['engine:read']));
+ await ctx.engine.getChatHistory('sess-1', '[email protected]', limit as number);
+ expect(engine.getChatHistory).toHaveBeenCalledWith('[email protected]', 50, false);
+ },
+ );
+
it('denies engine.getChatHistory without the engine:read permission', async () => {
const { sessionService } = build({ getChatHistory: jest.fn() });
const ctx = contextFor(makePlugin(['*'], ['messages:send']));