Skip to content

Commit 1f0c19f

Browse files
Copilothotlong
andcommitted
Add YAML security restriction and improve string literal handling
Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent 2831c2f commit 1f0c19f

2 files changed

Lines changed: 31 additions & 8 deletions

File tree

packages/metadata/src/serializers/typescript-serializer.ts

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,36 @@ export class TypeScriptSerializer implements MetadataSerializer {
5353
}
5454

5555
// Find the matching closing brace by counting braces
56+
// Handle string literals to avoid counting braces inside strings
5657
let braceCount = 0;
5758
let braceEnd = -1;
59+
let inString = false;
60+
let stringChar = '';
61+
5862
for (let i = braceStart; i < content.length; i++) {
59-
if (content[i] === '{') braceCount++;
60-
if (content[i] === '}') {
61-
braceCount--;
62-
if (braceCount === 0) {
63-
braceEnd = i;
64-
break;
63+
const char = content[i];
64+
const prevChar = i > 0 ? content[i - 1] : '';
65+
66+
// Track string literals (simple handling of " and ')
67+
if ((char === '"' || char === "'") && prevChar !== '\\') {
68+
if (!inString) {
69+
inString = true;
70+
stringChar = char;
71+
} else if (char === stringChar) {
72+
inString = false;
73+
stringChar = '';
74+
}
75+
}
76+
77+
// Count braces only when not inside strings
78+
if (!inString) {
79+
if (char === '{') braceCount++;
80+
if (char === '}') {
81+
braceCount--;
82+
if (braceCount === 0) {
83+
braceEnd = i;
84+
break;
85+
}
6586
}
6687
}
6788
}
@@ -85,7 +106,7 @@ export class TypeScriptSerializer implements MetadataSerializer {
85106
} catch (error) {
86107
throw new Error(
87108
`Failed to parse object literal as JSON: ${error instanceof Error ? error.message : String(error)}. ` +
88-
'Make sure the TypeScript/JavaScript object uses JSON-compatible syntax.'
109+
'Make sure the TypeScript/JavaScript object uses JSON-compatible syntax (no functions, comments, or trailing commas).'
89110
);
90111
}
91112
}

packages/metadata/src/serializers/yaml-serializer.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ export class YAMLSerializer implements MetadataSerializer {
2222
}
2323

2424
deserialize<T>(content: string, schema?: z.ZodSchema): T {
25-
const parsed = yaml.load(content);
25+
// Use JSON_SCHEMA to prevent arbitrary code execution
26+
// This restricts YAML to JSON-compatible types only
27+
const parsed = yaml.load(content, { schema: yaml.JSON_SCHEMA });
2628

2729
if (schema) {
2830
return schema.parse(parsed) as T;

0 commit comments

Comments
 (0)