Skip to content

Commit acdaf8a

Browse files
fix(markdown-core): use Object.hasOwn instead of in operator in parseFrontmatterBlock (#99129)
* fix(markdown-core): use Object.hasOwn instead of in operator in parseFrontmatterBlock * test(markdown-core): add regression test for prototype-named null frontmatter keys * style(markdown-core): fix unbound-method lint in regression test --------- Co-authored-by: Vincent Koc <[email protected]>
1 parent b68e2e7 commit acdaf8a

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

packages/markdown-core/src/frontmatter.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,28 @@ metadata:
103103
expect(parseFrontmatterBlock(content)).toStrictEqual({});
104104
});
105105

106+
it("preserves prototype-named keys when YAML value is null", () => {
107+
const content = `---
108+
title: Hello
109+
toString: null
110+
constructor: null
111+
valueOf: null
112+
hasOwnProperty: null
113+
---
114+
Body text`;
115+
const result = parseFrontmatterBlock(content);
116+
expect(Object.hasOwn(result, "toString")).toBe(true);
117+
expect(result["toString"]).toBe("null");
118+
expect(Object.hasOwn(result, "constructor")).toBe(true);
119+
expect(result["constructor"]).toBe("null");
120+
expect(Object.hasOwn(result, "valueOf")).toBe(true);
121+
expect(result["valueOf"]).toBe("null");
122+
expect(Object.hasOwn(result, "hasOwnProperty")).toBe(true);
123+
expect(result["hasOwnProperty"]).toBe("null");
124+
// normal key unaffected
125+
expect(result.title).toBe("Hello");
126+
});
127+
106128
it("parses frontmatter after a leading UTF-8 BOM", () => {
107129
const content = "\uFEFF---\nname: windows-skill\ndescription: Written by PowerShell\n---\n";
108130
const result = parseFrontmatterBlock(content);

packages/markdown-core/src/frontmatter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ export function parseFrontmatterBlock(content: string): ParsedFrontmatter {
222222
}
223223

224224
for (const [key, lineEntry] of Object.entries(lineParsed)) {
225-
if (!(key in merged)) {
225+
if (!Object.hasOwn(merged, key)) {
226226
merged[key] = lineEntry.value;
227227
}
228228
}

0 commit comments

Comments
 (0)