Skip to content

Commit 77a73cc

Browse files
cursoragentcameroncooke
authored andcommitted
fix: Remove redundant path traversal check for hardcoded common schema path
1 parent 3591394 commit 77a73cc

1 file changed

Lines changed: 8 additions & 14 deletions

File tree

src/core/structured-output-schema.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,13 @@ function schemaPathFor(ref: StructuredOutputSchemaRef): string {
7979
const resolvedSchemasDir = path.resolve(schemasDir);
8080

8181
// Prevent path traversal attacks by ensuring the resolved path is within the schemas directory
82-
if (!resolvedPath.startsWith(resolvedSchemasDir + path.sep) && resolvedPath !== resolvedSchemasDir) {
83-
throw new Error(`Invalid schema path: attempted path traversal detected for ${ref.schema}@${ref.version}`);
82+
if (
83+
!resolvedPath.startsWith(resolvedSchemasDir + path.sep) &&
84+
resolvedPath !== resolvedSchemasDir
85+
) {
86+
throw new Error(
87+
`Invalid schema path: attempted path traversal detected for ${ref.schema}@${ref.version}`,
88+
);
8489
}
8590

8691
return schemaPath;
@@ -203,20 +208,9 @@ export function getMcpOutputSchema(ref: StructuredOutputSchemaRef): JsonObject {
203208

204209
const rootSchema = readJsonObject(schemaPathFor(ref), `${ref.schema}@${ref.version}`);
205210

206-
// Validate common schema path to prevent path traversal
207211
const schemasDir = getStructuredOutputSchemasDir();
208212
const commonSchemaPath = path.join(schemasDir, '_defs', 'common.schema.json');
209-
const resolvedCommonPath = path.resolve(commonSchemaPath);
210-
const resolvedSchemasDir = path.resolve(schemasDir);
211-
212-
if (!resolvedCommonPath.startsWith(resolvedSchemasDir + path.sep) && resolvedCommonPath !== resolvedSchemasDir) {
213-
throw new Error('Invalid common schema path: attempted path traversal detected');
214-
}
215-
216-
const commonSchema = readJsonObject(
217-
commonSchemaPath,
218-
'common structured output definitions',
219-
);
213+
const commonSchema = readJsonObject(commonSchemaPath, 'common structured output definitions');
220214
const bundled = bundleSchema(rootSchema, commonSchema);
221215
bundledSchemaCache.set(cacheKey, bundled);
222216
return cloneJson(bundled);

0 commit comments

Comments
 (0)