Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(search): bound dateFrom and dateTo

Yudhi Armyndharis61b43c9aa477
diff --git a/openapi.json b/openapi.json
index bb2eac722..1be82a0bd 100644
--- a/openapi.json
+++ b/openapi.json
@@ -17925,6 +17925,8 @@
"in": "query",
"description": "Epoch-ms lower bound (inclusive)",
"schema": {
+ "minimum": -9007199254740991,
+ "maximum": 9007199254740991,
"type": "number"
}
},
@@ -17934,6 +17936,8 @@
"in": "query",
"description": "Epoch-ms upper bound (inclusive)",
"schema": {
+ "minimum": -9007199254740991,
+ "maximum": 9007199254740991,
"type": "number"
}
},
diff --git a/src/modules/search/dto/search-query.dto.spec.ts b/src/modules/search/dto/search-query.dto.spec.ts
index e5b548dd4..cbfb103a5 100644
--- a/src/modules/search/dto/search-query.dto.spec.ts
+++ b/src/modules/search/dto/search-query.dto.spec.ts
@@ -58,6 +58,26 @@ describe('SearchQueryDto', () => {
expect(validateSync(fromQuery({ q: 'hello', offset: '100000' }))).toHaveLength(0);
});
+ // The provider binds ceil(dateFrom / 1000) into a bigint column. A bound near 1e22 overflows it on
+ // PostgreSQL (a 500 instead of a 400); the message list refuses the same values for since/until.
+ describe.each(['dateFrom', 'dateTo'] as const)('%s', field => {
+ const validate = (value: string): boolean => validateSync(fromQuery({ q: 'hello', [field]: value })).length === 0;
+
+ it.each(['9300000000000000000000', '-9300000000000000000000', String(2 ** 53), String(-(2 ** 53))])(
+ 'rejects %s, which is beyond the safe integer range',
+ value => {
+ expect(validate(value)).toBe(false);
+ },
+ );
+
+ it.each([String(Number.MAX_SAFE_INTEGER), String(-Number.MAX_SAFE_INTEGER), '0', '-1', '1700000000000.5'])(
+ 'accepts %s',
+ value => {
+ expect(validate(value)).toBe(true);
+ },
+ );
+ });
+
it('rejects an invalid direction (@IsEnum(MessageDirection))', () => {
const dto = fromQuery({ q: 'hello', direction: 'sideways' });
expect(validateSync(dto).some(e => e.property === 'direction')).toBe(true);
@@ -85,4 +105,16 @@ describe('SearchQueryDto', () => {
expect(published('limit')).toMatchObject({ type: 'integer', minimum: 1 });
expect(published('offset')).toMatchObject({ type: 'integer', minimum: 0, maximum: SEARCH_OFFSET_MAX });
});
+
+ it('publishes dateFrom and dateTo with the safe integer bounds they are validated against', () => {
+ const published = (key: string): unknown =>
+ Reflect.getMetadata(DECORATORS.API_MODEL_PROPERTIES, SearchQueryDto.prototype, key);
+ for (const key of ['dateFrom', 'dateTo']) {
+ expect(published(key)).toMatchObject({
+ type: Number,
+ minimum: -Number.MAX_SAFE_INTEGER,
+ maximum: Number.MAX_SAFE_INTEGER,
+ });
+ }
+ });
});
diff --git a/src/modules/search/dto/search-query.dto.ts b/src/modules/search/dto/search-query.dto.ts
index 44375042b..b4ab7b682 100644
--- a/src/modules/search/dto/search-query.dto.ts
+++ b/src/modules/search/dto/search-query.dto.ts
@@ -10,7 +10,8 @@ import { SEARCH_OFFSET_MAX, SEARCH_QUERY_MAX_LENGTH, SEARCH_TERMS_MAX } from '..
* Request shape for GET /search. Numeric fields are coerced from query-string strings via
* `@Type(() => Number)` and then validated, so `?limit=abc` (Number('abc') → NaN) surfaces as a 400
* from the global ValidationPipe, never reaching the provider as a NaN SQL param. The epoch-ms
- * bounds `dateFrom`/`dateTo` use `@IsNumber()`; `limit`/`offset` use `@IsInt()`, because they are
+ * bounds `dateFrom`/`dateTo` use `@IsNumber()` within the safe integer range, since the provider binds them
+ * as seconds into a bigint column that a larger value overflows; `limit`/`offset` use `@IsInt()`, because they are
* bound straight into `LIMIT ?`/`OFFSET ?` where better-sqlite3 rejects a fractional value with
* SQLITE_MISMATCH: a fraction has to be a 400 here rather than a driver-level 500. `sessionIds` and
* `chatIds` are intentionally absent: both scopes are injected by SearchService from the caller's
@@ -59,18 +60,32 @@ export class SearchQueryDto {
// it. @IsString() accepts any string; the provider matches it against stored messages.type values.
type?: MessageType;
- @ApiPropertyOptional({ description: 'Epoch-ms lower bound (inclusive)', type: Number })
+ @ApiPropertyOptional({
+ description: 'Epoch-ms lower bound (inclusive)',
+ type: Number,
+ minimum: -Number.MAX_SAFE_INTEGER,
+ maximum: Number.MAX_SAFE_INTEGER,
+ })
@ToStrictNumber()
@IsOptional()
@Type(() => Number)
@IsNumber()
+ @Min(-Number.MAX_SAFE_INTEGER)
+ @Max(Number.MAX_SAFE_INTEGER)
dateFrom?: number;
- @ApiPropertyOptional({ description: 'Epoch-ms upper bound (inclusive)', type: Number })
+ @ApiPropertyOptional({
+ description: 'Epoch-ms upper bound (inclusive)',
+ type: Number,
+ minimum: -Number.MAX_SAFE_INTEGER,
+ maximum: Number.MAX_SAFE_INTEGER,
+ })
@ToStrictNumber()
@IsOptional()
@Type(() => Number)
@IsNumber()
+ @Min(-Number.MAX_SAFE_INTEGER)
+ @Max(Number.MAX_SAFE_INTEGER)
dateTo?: number;
@ApiPropertyOptional({ description: 'Max hits to return', type: 'integer', minimum: 1 })