Skip to content

Commit eafa781

Browse files
committed
fix(integrations): strip url control characters only where the id was sent raw
1 parent c3299ab commit eafa781

12 files changed

Lines changed: 30 additions & 17 deletions

‎apps/sim/tools/pinecone/search_text.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ export const searchTextTool: ToolConfig<PineconeSearchTextParams, PineconeRespon
136136
},
137137
method: 'POST',
138138
url: (params) =>
139-
`${params.indexHost}/records/namespaces/${safeUrlPathSegment(params.namespace, 'namespace', { allowSeparators: true, preserveWhitespace: true })}/search`,
139+
`${params.indexHost}/records/namespaces/${safeUrlPathSegment(params.namespace, 'namespace', { allowSeparators: true, preserveWhitespace: true, stripTabsAndNewlines: true })}/search`,
140140
headers: (params) => ({
141141
'Api-Key': params.apiKey,
142142
'Content-Type': 'application/json',

‎apps/sim/tools/pinecone/upsert_text.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export const upsertTextTool: ToolConfig<PineconeUpsertTextParams, PineconeRespon
9292
},
9393
method: 'POST',
9494
url: (params) =>
95-
`${params.indexHost}/records/namespaces/${safeUrlPathSegment(params.namespace, 'namespace', { allowSeparators: true, preserveWhitespace: true })}/upsert`,
95+
`${params.indexHost}/records/namespaces/${safeUrlPathSegment(params.namespace, 'namespace', { allowSeparators: true, preserveWhitespace: true, stripTabsAndNewlines: true })}/upsert`,
9696
headers: (params) => ({
9797
'Api-Key': params.apiKey,
9898
'Content-Type': 'application/x-ndjson',

‎apps/sim/tools/qdrant/fetch_points.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export const fetchPointsTool: ToolConfig<QdrantFetchParams, QdrantResponse> = {
5959
request: {
6060
method: 'POST',
6161
url: (params) =>
62-
`${params.url.replace(/\/$/, '')}/collections/${safeUrlPathSegment(params.collection, 'collection', { preserveWhitespace: true })}/points`,
62+
`${params.url.replace(/\/$/, '')}/collections/${safeUrlPathSegment(params.collection, 'collection', { preserveWhitespace: true, stripTabsAndNewlines: true })}/points`,
6363
headers: (params) => ({
6464
'Content-Type': 'application/json',
6565
...(params.apiKey ? { 'api-key': params.apiKey } : {}),

‎apps/sim/tools/qdrant/upsert_points.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export const upsertPointsTool: ToolConfig<QdrantUpsertParams, QdrantResponse> =
4444
request: {
4545
method: 'PUT',
4646
url: (params) =>
47-
`${params.url.replace(/\/$/, '')}/collections/${safeUrlPathSegment(params.collection, 'collection', { preserveWhitespace: true })}/points`,
47+
`${params.url.replace(/\/$/, '')}/collections/${safeUrlPathSegment(params.collection, 'collection', { preserveWhitespace: true, stripTabsAndNewlines: true })}/points`,
4848
headers: (params) => ({
4949
'Content-Type': 'application/json',
5050
...(params.apiKey ? { 'api-key': params.apiKey } : {}),

‎apps/sim/tools/url-path.test.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,12 @@ describe('safeUrlPathSegment', () => {
107107
expect(safeUrlPathSegment(' doc-1 ', 'id', { preserveWhitespace: true })).toBe('%20doc-1%20')
108108
expect(() => safeUrlPathSegment('..', 'id', { preserveWhitespace: true })).toThrow(/id/)
109109
expect(() => safeUrlPathSegment(' ', 'id', { preserveWhitespace: true })).toThrow(/id/)
110-
expect(safeUrlPathSegment('coll\n', 'id', { preserveWhitespace: true })).toBe('coll')
111-
expect(safeUrlPathSegment('\tco\r\nll', 'id', { preserveWhitespace: true })).toBe('coll')
112-
expect(safeUrlPathSegment('a b', 'id', { preserveWhitespace: true })).toBe('a%20b')
113-
expect(() => safeUrlPathSegment('.\n.', 'id', { preserveWhitespace: true })).toThrow(
114-
/traversal/
115-
)
110+
const asRawUrl = { preserveWhitespace: true, stripTabsAndNewlines: true }
111+
expect(safeUrlPathSegment('coll\n', 'id', asRawUrl)).toBe('coll')
112+
expect(safeUrlPathSegment('\tco\r\nll', 'id', asRawUrl)).toBe('coll')
113+
expect(safeUrlPathSegment('a b', 'id', asRawUrl)).toBe('a%20b')
114+
expect(() => safeUrlPathSegment('.\n.', 'id', asRawUrl)).toThrow(/traversal/)
115+
expect(safeUrlPathSegment('a\nb', 'id', { preserveWhitespace: true })).toBe('a%0Ab')
116116
})
117117

118118
it.concurrent('every vector either throws or keeps the exact path shape', () => {

‎apps/sim/tools/url-path.ts‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,23 +168,30 @@ function encodeSegment(segment: string, paramName: string): string {
168168
* @param options.allowSeparators - Percent-encode `/` and `\` instead of
169169
* rejecting them, for opaque ids (such as base64) that may legitimately
170170
* contain them. Empty values and dot segments are still rejected.
171-
* @param options.preserveWhitespace - Keep spaces instead of trimming, for ids
172-
* where surrounding whitespace is significant (an exact-match document id).
173-
* Tabs and newlines are still removed, as the URL parser removes them from a
174-
* raw URL. Blank values and dot segments are still rejected.
171+
* @param options.preserveWhitespace - Encode the value as given instead of
172+
* trimming it, for ids where surrounding whitespace is significant (an
173+
* exact-match document id). Blank values and dot segments are still rejected.
174+
* @param options.stripTabsAndNewlines - Remove tabs, carriage returns and
175+
* newlines before encoding, as the WHATWG URL parser does to a raw URL. For
176+
* call sites that once interpolated the id unencoded, so an id ending in a
177+
* newline keeps addressing the same resource.
175178
* @returns The percent-encoded segment (trimmed unless `preserveWhitespace`), safe to interpolate.
176179
* @throws If the value is not a string or a usable number, is empty, is a dot
177180
* segment, contains a path separator (unless allowed), or cannot be encoded.
178181
*/
179182
export function safeUrlPathSegment(
180183
value: string | number | bigint,
181184
paramName: string,
182-
options?: { allowSeparators?: boolean; preserveWhitespace?: boolean }
185+
options?: {
186+
allowSeparators?: boolean
187+
preserveWhitespace?: boolean
188+
stripTabsAndNewlines?: boolean
189+
}
183190
): string {
184191
const raw = toGuardedString(value, paramName)
185192
const trimmed = raw.trim()
186-
// The WHATWG URL parser strips tabs and newlines anywhere in a URL, so an id sent raw lost them.
187-
const segment = options?.preserveWhitespace ? raw.replace(/[\t\r\n]/g, '') : trimmed
193+
const kept = options?.preserveWhitespace ? raw : trimmed
194+
const segment = options?.stripTabsAndNewlines ? kept.replace(/[\t\r\n]/g, '') : kept
188195

189196
if (!trimmed) {
190197
throw new Error(`${paramName} is required`)

‎apps/sim/tools/zep/add_messages.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ export const zepAddMessagesTool: ToolConfig<ZepAddMessagesParams, ZepResponse> =
105105
`https://api.getzep.com/api/v2/threads/${safeUrlPathSegment(params.threadId, 'threadId', {
106106
allowSeparators: true,
107107
preserveWhitespace: true,
108+
stripTabsAndNewlines: true,
108109
})}/messages`,
109110
method: 'POST',
110111
headers: (params) => ({

‎apps/sim/tools/zep/delete_thread.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export const zepDeleteThreadTool: ToolConfig<any, ZepResponse> = {
3030
`https://api.getzep.com/api/v2/threads/${safeUrlPathSegment(params.threadId, 'threadId', {
3131
allowSeparators: true,
3232
preserveWhitespace: true,
33+
stripTabsAndNewlines: true,
3334
})}`,
3435
method: 'DELETE',
3536
headers: (params) => ({

‎apps/sim/tools/zep/get_context.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export const zepGetContextTool: ToolConfig<any, ZepResponse> = {
5151
{
5252
allowSeparators: true,
5353
preserveWhitespace: true,
54+
stripTabsAndNewlines: true,
5455
}
5556
)}/context?${queryParams.toString()}`
5657
},

‎apps/sim/tools/zep/get_messages.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export const zepGetMessagesTool: ToolConfig<any, ZepResponse> = {
5858
{
5959
allowSeparators: true,
6060
preserveWhitespace: true,
61+
stripTabsAndNewlines: true,
6162
}
6263
)}/messages${queryString ? `?${queryString}` : ''}`
6364
},

0 commit comments

Comments
 (0)