Skip to content

Commit 1c70b85

Browse files
authored
Implement CSP (Content Security Policy) (#145)
* Implement CSP(Content Security Policy) * bump version
1 parent dbda78d commit 1c70b85

File tree

4 files changed

+33
-12
lines changed

4 files changed

+33
-12
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@
3232
"@storybook/react": "6.4.22",
3333
"@svgr/webpack": "^6.1.2",
3434
"@testing-library/react": "13.4.0",
35+
"@types/dompurify": "2.0.4",
3536
"@types/jest": "29.4.4",
3637
"@types/node": "18.11.9",
3738
"@types/react": "18.0.25",
3839
"@types/react-dom": "18.0.9",
3940
"@types/react-test-renderer": "^17.0.1",
4041
"@types/toposort": "^2.0.3",
42+
"@types/trusted-types": "^2.0.7",
4143
"@types/uuid": "^8.3.1",
4244
"@typescript-eslint/eslint-plugin": "5.56.0",
4345
"@typescript-eslint/parser": "5.56.0",
@@ -77,6 +79,7 @@
7779
"@storybook/core-server": "6.4.22",
7880
"@storybook/manager-webpack5": "6.4.22",
7981
"core-js": "^3.6.5",
82+
"dompurify": "^2.0.11",
8083
"eventemitter3": "^4.0.7",
8184
"react": "18.2.0",
8285
"react-dom": "18.2.0",

packages/react-dag-editor/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"repository": {
55
"url": "https://github.com/microsoft/react-dag-editor.git"
66
},
7-
"version": "0.4.2",
7+
"version": "0.4.3",
88
"dependencies": {
99
"@fluentui/merge-styles": "^8.2.0",
1010
"eventemitter3": "^4.0.7",

packages/react-dag-editor/src/lib/utils/wheel-delta.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* https://stackoverflow.com/questions/20110224/what-is-the-height-of-a-line-in-a-wheel-event-deltamode-dom-delta-line
55
*/
66

7+
import DOMPurify from "dompurify";
78
import { Debug } from "./debug";
89

910
/**
@@ -16,20 +17,20 @@ function getScrollLineHeight(): number {
1617
const iframe = document.createElement("iframe");
1718
iframe.src = "#";
1819
document.body.appendChild(iframe);
19-
const { contentWindow } = iframe;
20-
if (!contentWindow) {
20+
const { contentDocument } = iframe;
21+
if (!contentDocument) {
2122
throw new Error("Fail to create iframe");
2223
}
23-
const doc = contentWindow.document;
24-
if (!doc) {
25-
throw new Error("Fail to create iframe");
26-
}
27-
doc.open();
28-
doc.write(
29-
"<!DOCTYPE html><html><head></head><body><span>a</span></body></html>"
24+
25+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
26+
// @ts-ignore
27+
contentDocument.documentElement.innerHTML = DOMPurify.sanitize(
28+
"<span>a</span>",
29+
{
30+
RETURN_TRUSTED_TYPE: true,
31+
}
3032
);
31-
doc.close();
32-
const span = doc.body.firstElementChild as HTMLSpanElement;
33+
const span = contentDocument.body.firstElementChild as HTMLSpanElement;
3334
const height = span.offsetHeight;
3435
document.body.removeChild(iframe);
3536
return height;

yarn.lock

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4428,6 +4428,13 @@
44284428
dependencies:
44294429
"@types/node" "*"
44304430

4431+
4432+
version "2.0.4"
4433+
resolved "https://registry.yarnpkg.com/@types/dompurify/-/dompurify-2.0.4.tgz#25fce15f1f4b1bc0df0ad957040cf226416ac2d7"
4434+
integrity sha512-y6K7NyXTQvjr8hJNsAFAD8yshCsIJ0d+OYEFzULuIqWyWOKL2hRru1I+rorI5U0K4SLAROTNuSUFXPDTu278YA==
4435+
dependencies:
4436+
"@types/trusted-types" "*"
4437+
44314438
"@types/eslint-scope@^3.7.0":
44324439
version "3.7.2"
44334440
resolved "https://registry.npmjs.org/@types/eslint-scope/-/eslint-scope-3.7.2.tgz#11e96a868c67acf65bf6f11d10bb89ea71d5e473"
@@ -4800,6 +4807,11 @@
48004807
resolved "https://registry.yarnpkg.com/@types/tough-cookie/-/tough-cookie-4.0.2.tgz#6286b4c7228d58ab7866d19716f3696e03a09397"
48014808
integrity sha512-Q5vtl1W5ue16D+nIaW8JWebSSraJVlK+EthKn7e7UcD4KWsaSJ8BqGPXNaPghgtcn/fhvrN17Tv8ksUsQpiplw==
48024809

4810+
"@types/trusted-types@*", "@types/trusted-types@^2.0.7":
4811+
version "2.0.7"
4812+
resolved "https://registry.yarnpkg.com/@types/trusted-types/-/trusted-types-2.0.7.tgz#baccb07a970b91707df3a3e8ba6896c57ead2d11"
4813+
integrity sha512-ScaPdn1dQczgbl0QFTeTOmVHFULt394XJgOQNoyVhZ6r2vLnMLJfBPd53SB52T/3G36VI1/g2MZaX0cwDuXsfw==
4814+
48034815
"@types/uglify-js@*":
48044816
version "3.13.1"
48054817
resolved "https://registry.npmjs.org/@types/uglify-js/-/uglify-js-3.13.1.tgz#5e889e9e81e94245c75b6450600e1c5ea2878aea"
@@ -7829,6 +7841,11 @@ domhandler@^4.0.0, domhandler@^4.2.0, domhandler@^4.3.0:
78297841
dependencies:
78307842
domelementtype "^2.2.0"
78317843

7844+
dompurify@^2.0.11:
7845+
version "2.4.7"
7846+
resolved "https://registry.yarnpkg.com/dompurify/-/dompurify-2.4.7.tgz#277adeb40a2c84be2d42a8bcd45f582bfa4d0cfc"
7847+
integrity sha512-kxxKlPEDa6Nc5WJi+qRgPbOAbgTpSULL+vI3NUXsZMlkJxTqYI9wg5ZTay2sFrdZRWHPWNi+EdAhcJf81WtoMQ==
7848+
78327849
domutils@^2.5.2, domutils@^2.8.0:
78337850
version "2.8.0"
78347851
resolved "https://registry.npmjs.org/domutils/-/domutils-2.8.0.tgz#4437def5db6e2d1f5d6ee859bd95ca7d02048135"

0 commit comments

Comments
 (0)