Skip to content

Commit 9feec94

Browse files
authored
fix: text matching Object.prototype property names rendered as image (#768)
## Problem (#746) When text content passed to Satori **exactly matched** an `Object.prototype` property name (e.g. `"constructor"`, `"toString"`, `"valueOf"`), the text was silently not rendered. Instead, an `<image>` element with an invalid `href` was emitted. ### Root Cause Two locations in `src/text/index.ts` used bracket-notation lookup on a user-provided `graphemeImages` object: 1. `isImage(s)` (line 122): `graphemeImages[s]` — when `s` is `"constructor"`, this returns `Object.prototype.constructor` (a truthy function), so the text was incorrectly treated as an image. 2. Rendering path (line 558): `graphemeImages[text]` — same issue, returns the inherited property instead of `undefined`. ### Fix Use `Object.prototype.hasOwnProperty.call()` to check for own properties before accessing the value: ```ts // Before graphemeImages[s] // After Object.prototype.hasOwnProperty.call(graphemeImages, s) && graphemeImages[s] ``` ### Testing All 433 existing tests pass. The fix only affects edge cases where text exactly matches `Object.prototype` property names — normal usage is unaffected.
1 parent 91ade3b commit 9feec94

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

src/text/index.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,11 @@ export default async function* buildTextNodes(
119119
}
120120

121121
function isImage(s: string): boolean {
122-
return !!(graphemeImages && graphemeImages[s])
122+
return !!(
123+
graphemeImages &&
124+
Object.prototype.hasOwnProperty.call(graphemeImages, s) &&
125+
graphemeImages[s]
126+
)
123127
}
124128

125129
const { measureGrapheme, measureGraphemeArray, measureText } = genMeasurer(
@@ -551,7 +555,11 @@ export default async function* buildTextNodes(
551555
let path: string | null = null
552556
let isLastDisplayedBeforeEllipsis = false
553557

554-
const image = graphemeImages ? graphemeImages[text] : null
558+
const image =
559+
graphemeImages &&
560+
Object.prototype.hasOwnProperty.call(graphemeImages, text)
561+
? graphemeImages[text]
562+
: null
555563

556564
let topOffset = layout.y
557565
let leftOffset = layout.x

0 commit comments

Comments
 (0)