Skip to content

Commit 3591394

Browse files
fix-it-felix-sentry[bot]claude
authored andcommitted
fix: Add path traversal protection for schema file loading
Add validation to prevent path traversal vulnerabilities when loading structured output schema files. The fix ensures that resolved file paths remain within the intended schemas directory by validating that the resolved path starts with the schemas directory path. This addresses a potential security issue where malicious input could potentially manipulate file paths through path traversal sequences. The fix adds checks in two locations: 1. In schemaPathFor() for schema files 2. In getMcpOutputSchema() for the common schema file Resolves: https://linear.app/getsentry/issue/VULN-1596 Resolves: https://linear.app/getsentry/issue/ENG-7560 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 92c9fe7 commit 3591394

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

src/core/structured-output-schema.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,17 @@ function readJsonObject(filePath: string, label: string): JsonObject {
7373
}
7474

7575
function schemaPathFor(ref: StructuredOutputSchemaRef): string {
76-
return path.join(getStructuredOutputSchemasDir(), ref.schema, `${ref.version}.schema.json`);
76+
const schemasDir = getStructuredOutputSchemasDir();
77+
const schemaPath = path.join(schemasDir, ref.schema, `${ref.version}.schema.json`);
78+
const resolvedPath = path.resolve(schemaPath);
79+
const resolvedSchemasDir = path.resolve(schemasDir);
80+
81+
// 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}`);
84+
}
85+
86+
return schemaPath;
7787
}
7888

7989
function collectAndRewriteCommonRefs(value: unknown, pendingDefs: Set<string>): unknown {
@@ -192,8 +202,19 @@ export function getMcpOutputSchema(ref: StructuredOutputSchemaRef): JsonObject {
192202
}
193203

194204
const rootSchema = readJsonObject(schemaPathFor(ref), `${ref.schema}@${ref.version}`);
205+
206+
// Validate common schema path to prevent path traversal
207+
const schemasDir = getStructuredOutputSchemasDir();
208+
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+
195216
const commonSchema = readJsonObject(
196-
path.join(getStructuredOutputSchemasDir(), '_defs', 'common.schema.json'),
217+
commonSchemaPath,
197218
'common structured output definitions',
198219
);
199220
const bundled = bundleSchema(rootSchema, commonSchema);

0 commit comments

Comments
 (0)