Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions src/core/structured-output-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,22 @@ function readJsonObject(filePath: string, label: string): JsonObject {
}

function schemaPathFor(ref: StructuredOutputSchemaRef): string {
return path.join(getStructuredOutputSchemasDir(), ref.schema, `${ref.version}.schema.json`);
const schemasDir = getStructuredOutputSchemasDir();
const schemaPath = path.join(schemasDir, ref.schema, `${ref.version}.schema.json`);
const resolvedPath = path.resolve(schemaPath);
const resolvedSchemasDir = path.resolve(schemasDir);

// Prevent path traversal attacks by ensuring the resolved path is within the schemas directory
if (
!resolvedPath.startsWith(resolvedSchemasDir + path.sep) &&
resolvedPath !== resolvedSchemasDir
) {
throw new Error(
`Invalid schema path: attempted path traversal detected for ${ref.schema}@${ref.version}`,
);
}

return schemaPath;
}

function collectAndRewriteCommonRefs(value: unknown, pendingDefs: Set<string>): unknown {
Expand Down Expand Up @@ -192,10 +207,10 @@ export function getMcpOutputSchema(ref: StructuredOutputSchemaRef): JsonObject {
}

const rootSchema = readJsonObject(schemaPathFor(ref), `${ref.schema}@${ref.version}`);
const commonSchema = readJsonObject(
path.join(getStructuredOutputSchemasDir(), '_defs', 'common.schema.json'),
'common structured output definitions',
);

const schemasDir = getStructuredOutputSchemasDir();
const commonSchemaPath = path.join(schemasDir, '_defs', 'common.schema.json');
const commonSchema = readJsonObject(commonSchemaPath, 'common structured output definitions');
const bundled = bundleSchema(rootSchema, commonSchema);
bundledSchemaCache.set(cacheKey, bundled);
return cloneJson(bundled);
Expand Down
Loading