Skip to content

Commit 0d966b2

Browse files
maxholmanclaude
andcommitted
feat: protect URL path integrity from unsafe interpolated values
Path parameters containing `/`, `#`, or other reserved characters would corrupt the URL structure. A tagged template literal now applies encodeURIComponent to all interpolated path segments. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 73b3141 commit 0d966b2

1 file changed

Lines changed: 28 additions & 3 deletions

File tree

lib/process-document.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,30 @@ export async function processOpenApiDocument(
136136
moduleSpecifier: "@block65/rest-client",
137137
});
138138

139+
commandsFile.addFunction({
140+
name: "encodePath",
141+
docs: [
142+
{
143+
description: wordWrap(
144+
"Tagged template literal that applies encodeURIComponent to all interpolated values, protecting path integrity from characters like `/` and `#`.",
145+
),
146+
tags: [
147+
{
148+
tagName: "example",
149+
text: 'encodePath`/users/${userId}` // "/users/foo%2Fbar"',
150+
},
151+
],
152+
},
153+
],
154+
parameters: [
155+
{ name: "strings", type: "TemplateStringsArray" },
156+
{ name: "...values", type: "string[]" },
157+
],
158+
returnType: "string",
159+
statements:
160+
"return String.raw({ raw: strings }, ...values.map(encodeURIComponent));",
161+
});
162+
139163
commandsFile.addImportDeclaration({
140164
namedImports: ["Jsonifiable"],
141165
moduleSpecifier: "type-fest",
@@ -786,9 +810,10 @@ export async function processOpenApiDocument(
786810
?.addTypeArgument(queryType.getName());
787811
}
788812

789-
const pathname = `\`${path
790-
// .replaceAll(/\{(\w+)\}/g, camelcase)
791-
.replaceAll(/{/g, "${")}\``;
813+
const hasPathParams = path.includes("{");
814+
const pathname = hasPathParams
815+
? `encodePath\`${path.replaceAll(/{/g, "${")}\``
816+
: `"${path}"`;
792817

793818
const hasJsonBody = !!jsonBodyType;
794819

0 commit comments

Comments
 (0)