Skip to content

Commit

Permalink
Check of unsafe HTML in SVG files (#3250)
Browse files Browse the repository at this point in the history
* Check of unsafe HTML in SVG files

Fixes #3249

* mention in index.html file

* feedbacked
  • Loading branch information
peterbe authored Mar 15, 2021
1 parent 51dcc55 commit 8556a9e
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 3 deletions.
17 changes: 14 additions & 3 deletions filecheck/checker.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,20 @@ async function checkFile(filePath, options) {
throw new Error(`${filePath} does not appear to be an SVG`);
}
const $ = cheerio.load(content);
if ($("script").length) {
throw new Error(`${filePath} contains a <script> tag`);
}
const disallowedTagNames = new Set(["script", "object", "iframe", "embed"]);
$("*").each((i, element) => {
const { tagName } = element;
if (disallowedTagNames.has(tagName)) {
throw new Error(`${filePath} contains a <${tagName}> tag`);
}
for (const key in element.attribs) {
if (/(\\x[a-f0-9]{2}|\b)on\w+/.test(key)) {
throw new Error(
`${filePath} <${tagName}> contains an unsafe attribute: '${key}'`
);
}
}
});
} else {
// Check that the file extension matches the file header.
const fileType = await FileType.fromFile(filePath);
Expand Down
25 changes: 25 additions & 0 deletions testing/tests/filecheck.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const fs = require("fs");
const path = require("path");

const { checkFile } = require("../../filecheck/checker");

const SAMPLES_DIRECTORY = path.join(__dirname, "samplefiles");

describe("checking files", () => {
it("should spot SVGs with scripts inside them", async () => {
const filePath = path.join(SAMPLES_DIRECTORY, "script.svg");
// Sanity check the test itself
console.assert(fs.existsSync(filePath), `${filePath} does not exist`);
await expect(checkFile(filePath)).rejects.toThrow(
"contains a <script> tag"
);
});
it("should spot SVGs with onLoad inside an element", async () => {
const filePath = path.join(SAMPLES_DIRECTORY, "onhandler.svg");
// Sanity check the test itself
console.assert(fs.existsSync(filePath), `${filePath} does not exist`);
await expect(checkFile(filePath)).rejects.toThrow(
"<path> contains an unsafe attribute: 'onload'"
);
});
});
3 changes: 3 additions & 0 deletions testing/tests/samplefiles/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!-- Must be mention in the adjacent index.html file -->
<img src="script.svg" />
<img src="onhandler.svg" />
3 changes: 3 additions & 0 deletions testing/tests/samplefiles/onhandler.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions testing/tests/samplefiles/script.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8556a9e

Please sign in to comment.