From c015abe682db654bc8e4cfb14c288bdd6bd863bb Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Fri, 20 Nov 2026 02:19:05 +0000 Subject: [PATCH] fix: encode endpoint and contentId as single URL path segments makeRequest concatenated the caller-supplied endpoint and contentId into the request URL, so '?', '#', '/', '\' or dot segments in those values could add query parameters or rewrite the request to another endpoint or content while still sending the service API key. Percent-encode each value as one path segment and refuse '.', '..' and empty segments. --- src/createClient.ts | 8 +++++--- src/utils/encodePathSegment.ts | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 src/utils/encodePathSegment.ts diff --git a/src/createClient.ts b/src/createClient.ts index 1003331..21163ce 100644 --- a/src/createClient.ts +++ b/src/createClient.ts @@ -29,6 +29,7 @@ import { MAX_RETRY_COUNT, MIN_TIMEOUT_MS, } from './utils/constants'; +import { encodePathSegment } from './utils/encodePathSegment'; import { isString } from './utils/isCheckValue'; import { parseQuery } from './utils/parseQuery'; import type { MicroCMSSchemaEntry, TypedMicroCMSClient } from './typedSchema'; @@ -65,9 +66,10 @@ const createClientImpl = ({ }: MakeRequest) => { const fetchClient = generateFetchClient(apiKey); const queryString = parseQuery(queries); - const url = `${baseUrl}/${endpoint}${contentId ? `/${contentId}` : ''}${ - queryString ? `?${queryString}` : '' - }`; + const path = [endpoint, ...(contentId ? [contentId] : [])] + .map(encodePathSegment) + .join('/'); + const url = `${baseUrl}/${path}${queryString ? `?${queryString}` : ''}`; const getMessageFromResponse = async (response: Response) => { // Enclose `response.json()` in a try since it may throw an error diff --git a/src/utils/encodePathSegment.ts b/src/utils/encodePathSegment.ts new file mode 100644 index 0000000..044656b --- /dev/null +++ b/src/utils/encodePathSegment.ts @@ -0,0 +1,14 @@ +/** + * Encode a value so that it occupies exactly one URL path segment. + * + * @param {string} segment + * @return {string} encoded segment + */ +export const encodePathSegment = (segment: string): string => { + const value = String(segment); + // `.` and `..` survive percent-encoding and are resolved as dot segments by the URL parser. + if (value === '' || value === '.' || value === '..') { + throw new Error(`invalid path segment: ${JSON.stringify(value)}`); + } + return encodeURIComponent(value); +};