Skip to content

Commit 965b3a1

Browse files
authoredJul 4, 2024··
fix: incorrect data URL parsing (#596)
Fixes #597 with correct regex. [Data URL Specification](https://www.rfc-editor.org/rfc/rfc2397#section-3)
1 parent 3d3bee3 commit 965b3a1

4 files changed

+23
-1
lines changed
 

‎src/handler/image.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ export async function resolveImageData(
174174

175175
try {
176176
decodedURI =
177-
/data:(?<imageType>[a-z/+]+)(;(charset=)?(?<encodingType>.*))?,(?<dataString>.*)/g.exec(
177+
/data:(?<imageType>[a-z/+]+)(;[^;=]+=[^;=]+)*?(;(?<encodingType>[^;,]+))?,(?<dataString>.*)/g.exec(
178178
src
179179
).groups as typeof decodedURI
180180
} catch (err) {

‎test/image.test.tsx

+22
Original file line numberDiff line numberDiff line change
@@ -638,4 +638,26 @@ describe('background-image: url()', () => {
638638

639639
expect(toImage(svg, 100)).toMatchImageSnapshot()
640640
})
641+
642+
it('should handle charset=utf-8 with comma in data', async () => {
643+
const svg = await satori(
644+
<img
645+
src={`data:image/svg+xml;charset=utf-8,<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" viewBox="0 0 100 100"><polygon fill="#ffffff" points="50,0 100,85 0,85" /></svg>`}
646+
/>,
647+
{ width: 100, height: 100, fonts }
648+
)
649+
650+
expect(toImage(svg, 100)).toMatchImageSnapshot()
651+
})
652+
653+
it('should handle charset=utf-8 with in base64', async () => {
654+
const svg = await satori(
655+
<img
656+
src={`data:image/svg+xml;charset=utf-8;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSI0MCIgaGVpZ2h0PSI0MCI+PGNpcmNsZSBjeD0iMjAiIGN5PSIyMCIgcj0iMTUiIGZpbGw9IiNlZTc2MjEiLz48L3N2Zz4=`}
657+
/>,
658+
{ width: 100, height: 100, fonts }
659+
)
660+
661+
expect(toImage(svg, 100)).toMatchImageSnapshot()
662+
})
641663
})

0 commit comments

Comments
 (0)
Please sign in to comment.