Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Utilize DOMPurify hooks to preserve <use> elements in playground mode #156

Merged
merged 1 commit into from
Oct 6, 2020

Conversation

listopadiya
Copy link
Contributor

Closes #155.

<use> tags were filtered out after sanitization, but with DOMPurify hooks this problem can be solved (regular expression still filter out links to external resources in the interest of security).

Can be checked in playground with following SVG code:

<svg xmlns="http://www.w3.org/2000/svg" width="200" height="100" viewBox="0 0 200 100">
  <style type="text/css">
    use { fill: orange; }
  </style>
  <g class="special" style="fill: blue">
    <g id="face">
      <circle cx="50" cy="50" r="40" stroke="#000000" />
      <circle cx="30" cy="40" r="5" fill="#000000" />
      <circle cx="70" cy="40" r="5" fill="#000000" />
      <path
        fill="none"
        stroke="#000000"
        stroke-width="3"
        stroke-linecap="round"
        d="M30,60 C30,80 70,80 70,60" />
    </g>
  </g>
  <use xlink:href="#face" x="100" />
</svg>

Before PR
image

After PR
image

Copy link
Member

@HackbrettXXX HackbrettXXX left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR :)

@@ -1,6 +1,12 @@
const { jsPDF } = window.jspdf
const DOMPurify = window.DOMPurify

DOMPurify.addHook('afterSanitizeAttributes', node => {
if(node.hasAttribute('xlink:href') && !node.getAttribute('xlink:href').match(/^#/)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also check the href attribute without the xlink namespace.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I didn't know that it is deprecated in SVG 2. I have amended the commit and also added playground to prettier check.

package.json Outdated
@@ -25,7 +25,7 @@
"test-typescript": "karma start ./test/deployment/typescript/karma.conf.js",
"test:ci": "cross-env SHOW_DIFF=true npm run test",
"createreferences": "node test/common/reference-server.js",
"prettier": "prettier --write {src,tests,typings}/**/*.{ts,js}",
"prettier": "prettier --write {playground,src,tests,typings}/**/*.{ts,js}",
"lint": "eslint {src,tests,typings}/**/*.{ts,js}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for nitpicking, but we should also add it to the lint task then.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, I updated that 👌

Copy link
Member

@HackbrettXXX HackbrettXXX left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright. Thanks for the PR :)

@HackbrettXXX HackbrettXXX merged commit efd36ea into yWorks:master Oct 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

<use> tag doesn't work in playground
2 participants