From 00c5ff948101bb5ca4638a374a20b533590c5b24 Mon Sep 17 00:00:00 2001 From: Tommaso Barbugli Date: Tue, 24 Nov 2026 17:32:02 +0100 Subject: [PATCH] fix(http): reject empty and dot-segment path parameters --- __tests__/integration/path-params.test.ts | 14 ++++++++++++++ src/ApiClient.ts | 11 +++++++---- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/__tests__/integration/path-params.test.ts b/__tests__/integration/path-params.test.ts index b559ffa..cd69d91 100644 --- a/__tests__/integration/path-params.test.ts +++ b/__tests__/integration/path-params.test.ts @@ -71,4 +71,18 @@ describe('path param serialization', () => { '/api/v2/moderation/config/chat%3Amessaging/versions/3', ); }); + + it.each(['', '.', '..'])( + 'rejects empty or dot-segment path parameter %j', + async (badValue) => { + const fetchSpy = vi.spyOn(globalThis, 'fetch'); + + await expect( + createApiClient().sendRequest('GET', '/api/v2/chat/messages/{id}', { + id: badValue, + }), + ).rejects.toThrow("Invalid path parameter 'id'"); + expect(fetchSpy).not.toHaveBeenCalled(); + }, + ); }); diff --git a/src/ApiClient.ts b/src/ApiClient.ts index 790cc21..8c83aed 100644 --- a/src/ApiClient.ts +++ b/src/ApiClient.ts @@ -28,10 +28,13 @@ export class ApiClient { const encodedParams = this.queryParamsStringify(queryParams); if (pathParams) { Object.keys(pathParams).forEach((paramName) => { - url = url.replace( - `{${paramName}}`, - encodeURIComponent(pathParams[paramName]), - ); + const rawValue = String(pathParams[paramName]); + if (rawValue === '' || rawValue === '.' || rawValue === '..') { + throw new Error( + `Invalid path parameter '${paramName}': must not be empty or a dot-segment`, + ); + } + url = url.replace(`{${paramName}}`, encodeURIComponent(rawValue)); }); }