diff --git a/pkgs/id/web/src/highlight.test.ts b/pkgs/id/web/src/highlight.test.ts index 96b33cc2db..3d05ecadda 100644 --- a/pkgs/id/web/src/highlight.test.ts +++ b/pkgs/id/web/src/highlight.test.ts @@ -414,6 +414,19 @@ describe("detectLanguage", () => { expect(detectLanguage("")).toBeUndefined(); }); + it("returns undefined for prototype-member extensions (#2787)", () => { + // Plain-object lookups on EXT_TO_LANG matched inherited + // Object.prototype members: these returned a function / object + // instead of a language string. + expect(detectLanguage("notes.constructor")).toBeUndefined(); + expect(detectLanguage("notes.__proto__")).toBeUndefined(); + }); + + it("returns undefined for prototype-member filenames (#2787)", () => { + expect(detectLanguage("constructor")).toBeUndefined(); + expect(detectLanguage("__proto__")).toBeUndefined(); + }); + it("is case-insensitive for extensions", () => { expect(detectLanguage("App.JS")).toBe("javascript"); expect(detectLanguage("style.CSS")).toBe("css"); diff --git a/pkgs/id/web/src/highlight.ts b/pkgs/id/web/src/highlight.ts index 8bce2d9b7a..2e41d2888c 100644 --- a/pkgs/id/web/src/highlight.ts +++ b/pkgs/id/web/src/highlight.ts @@ -145,7 +145,10 @@ export function detectLanguage(filename: string): string | undefined { // A known extension wins over a special-name stem: // "makefile.md" is Markdown documentation, not a Makefile. const ext = basename.includes(".") ? basename.split(".").pop()?.toLowerCase() : undefined; - if (ext && EXT_TO_LANG[ext]) { + // Own-key check (#2787): a plain lookup also matches inherited + // Object.prototype members, so "notes.constructor" / "notes.__proto__" + // returned the Object constructor / prototype object as a "language". + if (ext && Object.hasOwn(EXT_TO_LANG, ext)) { return EXT_TO_LANG[ext]; } @@ -156,11 +159,11 @@ export function detectLanguage(filename: string): string | undefined { ? basenameLower.slice(0, basenameLower.lastIndexOf(".")) : basenameLower; - if (FILENAME_TO_LANG[nameWithoutExt]) { + if (Object.hasOwn(FILENAME_TO_LANG, nameWithoutExt)) { return FILENAME_TO_LANG[nameWithoutExt]; } // Full basename (e.g., "Dockerfile" with no ext) - if (FILENAME_TO_LANG[basenameLower]) { + if (Object.hasOwn(FILENAME_TO_LANG, basenameLower)) { return FILENAME_TO_LANG[basenameLower]; } @@ -277,7 +280,10 @@ async function getHighlighter(): Promise { async function ensureLanguage(highlighter: ShikiHighlighter, lang: string): Promise { if (loadedLangs.has(lang)) return; - const importFn = LANG_IMPORTS[lang]; + // Own-key check (#2787): `lang` can be an authored code-block language + // attribute; a plain lookup returns Object.prototype members for + // "constructor"/"__proto__", which were then called as import functions. + const importFn = Object.hasOwn(LANG_IMPORTS, lang) ? LANG_IMPORTS[lang] : undefined; if (!importFn) { // Language not in our static registry — mark as loaded to avoid retries loadedLangs.add(lang);