Skip to content

Commit

Permalink
fix: incorrect data URL parsing (#596)
Browse files Browse the repository at this point in the history
Fixes #597 with correct regex.

[Data URL
Specification](https://www.rfc-editor.org/rfc/rfc2397#section-3)
  • Loading branch information
kiwiyou authored Jul 4, 2024
1 parent 3d3bee3 commit 965b3a1
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/handler/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export async function resolveImageData(

try {
decodedURI =
/data:(?<imageType>[a-z/+]+)(;(charset=)?(?<encodingType>.*))?,(?<dataString>.*)/g.exec(
/data:(?<imageType>[a-z/+]+)(;[^;=]+=[^;=]+)*?(;(?<encodingType>[^;,]+))?,(?<dataString>.*)/g.exec(
src
).groups as typeof decodedURI
} catch (err) {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions test/image.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -638,4 +638,26 @@ describe('background-image: url()', () => {

expect(toImage(svg, 100)).toMatchImageSnapshot()
})

it('should handle charset=utf-8 with comma in data', async () => {
const svg = await satori(
<img
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>`}
/>,
{ width: 100, height: 100, fonts }
)

expect(toImage(svg, 100)).toMatchImageSnapshot()
})

it('should handle charset=utf-8 with in base64', async () => {
const svg = await satori(
<img
src={`data:image/svg+xml;charset=utf-8;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSI0MCIgaGVpZ2h0PSI0MCI+PGNpcmNsZSBjeD0iMjAiIGN5PSIyMCIgcj0iMTUiIGZpbGw9IiNlZTc2MjEiLz48L3N2Zz4=`}
/>,
{ width: 100, height: 100, fonts }
)

expect(toImage(svg, 100)).toMatchImageSnapshot()
})
})

0 comments on commit 965b3a1

Please sign in to comment.