Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(search): round a plugin provider's fractional numeric fields

Yudhi Armyndharise5a8565aa3d2
diff --git a/src/modules/search/providers/plugin-search-provider.spec.ts b/src/modules/search/providers/plugin-search-provider.spec.ts
index cc6861b09..045a12de3 100644
--- a/src/modules/search/providers/plugin-search-provider.spec.ts
+++ b/src/modules/search/providers/plugin-search-provider.spec.ts
@@ -25,7 +25,7 @@ describe('PluginSearchProvider', () => {
const transport = fakeTransport({ dispatchSearch });
const p = new PluginSearchProvider('p', 'P', transport, 7000);
- await expect(p.search({ q: 'hi' })).resolves.toBe(results);
+ await expect(p.search({ q: 'hi' })).resolves.toEqual(results);
expect(dispatchSearch).toHaveBeenCalledWith({ query: { q: 'hi' }, timeoutMs: 7000 });
});
@@ -127,6 +127,26 @@ describe('PluginSearchProvider', () => {
expect(res.total).toBe(2);
});
+ it.each([
+ ['unscoped', undefined],
+ ['scoped', ['s1']],
+ ])('rounds a fractional tookMs, total and hit timestamp to integers (%s)', async (_label, sessionIds) => {
+ // The Go and Java SDKs decode these into integer fields; one fractional value failed the whole call.
+ const results: SearchResults = {
+ hits: [mkHit({ timestamp: 1700000000.75 })],
+ total: 3.9,
+ tookMs: 12.37,
+ provider: 'plugin:p',
+ };
+ const dispatchSearch = jest.fn().mockResolvedValue({ ok: true, results });
+ const p = new PluginSearchProvider('p', 'P', fakeTransport({ dispatchSearch }), 1000);
+
+ const res = await p.search({ q: 'hi', sessionIds });
+ expect(res.tookMs).toBe(12);
+ expect(res.total).toBe(3);
+ expect(res.hits[0].timestamp).toBe(1700000000);
+ });
+
describe('result shape validation (untrusted wire payload)', () => {
const providerReturning = (results: unknown): PluginSearchProvider =>
new PluginSearchProvider(
@@ -179,7 +199,7 @@ describe('PluginSearchProvider', () => {
provider: 'plugin:p',
cursor: 'next-page',
};
- await expect(providerReturning(results).search({ q: 'x' })).resolves.toBe(results);
+ await expect(providerReturning(results).search({ q: 'x' })).resolves.toEqual(results);
});
});
});
diff --git a/src/modules/search/providers/plugin-search-provider.ts b/src/modules/search/providers/plugin-search-provider.ts
index f8352166a..d236e62a1 100644
--- a/src/modules/search/providers/plugin-search-provider.ts
+++ b/src/modules/search/providers/plugin-search-provider.ts
@@ -87,24 +87,33 @@ export class PluginSearchProvider implements SearchProvider {
// upstream) instead of fabricating a 200 — or crashing the re-filter below into a 500.
const invalid = validatePluginSearchResults(reply.results);
if (invalid) throw new BadGatewayException(`Search provider ${this.id} returned invalid results: ${invalid}`);
+ // The Go and Java SDKs decode tookMs, total and a hit's timestamp into integer fields, as the
+ // built-in provider only ever returns whole numbers there, so one fractional value from a plugin
+ // would fail the whole call client-side. The validator accepts any finite number: round here.
+ const results: SearchResults = {
+ ...reply.results,
+ total: Math.trunc(reply.results.total),
+ tookMs: Math.round(reply.results.tookMs),
+ hits: reply.results.hits.map(h => ({ ...h, timestamp: Math.trunc(h.timestamp) })),
+ };
// Defense-in-depth: the plugin is expected to honor sessionIds, but re-filter host-side so a plugin
// bug or leak can never surface a hit outside the caller's allowed session scope — mirroring the
// SQL-enforced scoping the built-in provider gets for free. The guard mirrors the built-in
// provider's applyFilters condition (`sessionIds && sessionIds.length`) so the two providers never
// diverge for the same query (an empty array is a no-op on both paths). When no filtering is
- // needed, return the results untouched. Preserve the plugin's total when no hits were out of scope
- // (the normal, well-behaved case) so pagination ("Load More" = hits.length < total) still works;
- // fall back to the filtered page count only when a leak was actually stripped (the plugin's claimed
- // total is then also suspect). tookMs/provider are passthrough metadata unrelated to scope.
- if (!query.sessionIds || !query.sessionIds.length) return reply.results;
+ // needed, return the normalized results as they are. Preserve the plugin's total when no hits were
+ // out of scope (the normal, well-behaved case) so pagination ("Load More" = hits.length < total)
+ // still works; fall back to the filtered page count only when a leak was actually stripped (the
+ // plugin's claimed total is then also suspect). tookMs/provider are metadata unrelated to scope.
+ if (!query.sessionIds || !query.sessionIds.length) return results;
const allowed = new Set(query.sessionIds);
- const scoped = reply.results.hits.filter(h => allowed.has(h.sessionId));
- const leaked = reply.results.hits.length - scoped.length;
+ const scoped = results.hits.filter(h => allowed.has(h.sessionId));
+ const leaked = results.hits.length - scoped.length;
return {
hits: scoped,
- total: leaked > 0 ? scoped.length : reply.results.total,
- tookMs: reply.results.tookMs,
- provider: reply.results.provider,
+ total: leaked > 0 ? scoped.length : results.total,
+ tookMs: results.tookMs,
+ provider: results.provider,
};
}