Skip to content

Commit

Permalink
Convert only first SVG element from input
Browse files Browse the repository at this point in the history
If the input to be converted contains multiple SVG elements, only the first SVG element should be converted. Currently, and undesirably, all SVG elements were being inserted into the page within Puppeteer, resulting in them all being stacked prior to the screenshot being taken.

Another symptom of this was that all recent safety measures implemented for remote code injection vulnerabilities can be easily circumvented by inserting a simple empty SVG element at the start of the input as it was the only one being sanitized.
  • Loading branch information
neocotic committed Jun 7, 2022
1 parent faef02a commit 2bbc498
Show file tree
Hide file tree
Showing 10 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/convert-svg-core/src/Converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ class Converter {
input = Buffer.isBuffer(input) ? input.toString('utf8') : input;

const { provider } = this;
const svg = cheerio.default.html(this[_sanitize](cheerio.load(input, null, false)('svg'), options));
const svg = cheerio.default.html(this[_sanitize](cheerio.load(input, null, false)('svg:first'), options));

if (!svg) {
throw new Error('SVG element not found in input. Check the SVG input');
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions packages/convert-svg-test-helper/src/tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@
"file": "issue-81.svg",
"message": "should strip onload attribute"
},
{
"name": "when SVG has multiple SVG elements",
"file": "issue-86.svg",
"options": { "width": 1000, "height": 1000 },
"message": "should only read first SVG element"
},
{
"name": "when setting both baseFile and baseUrl options",
"file": "external-file.svg",
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file modified packages/convert-svg-to-jpeg/test/fixtures/expected/37.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified packages/convert-svg-to-jpeg/test/fixtures/expected/38.jpeg
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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.

0 comments on commit 2bbc498

Please sign in to comment.