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

How can I sanitize SVGs using this library? #198

Open
moraj-turing opened this issue Mar 28, 2024 · 2 comments
Open

How can I sanitize SVGs using this library? #198

moraj-turing opened this issue Mar 28, 2024 · 2 comments

Comments

@moraj-turing
Copy link

Is your feature request related to a problem? Please describe.
No, but would be nice to have a single sanitizing library/package

Describe the solution you'd like
SVG files, unlike other image formats are based on XML so they can store malicious tags eg:

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" baseProfile="full" >
   <polygon id="triangle" points="0,0 0,50 50,0" fill="#009900" stroke="#004400"/>
   <script type="text/javascript">
      alert(document.domain);
   </script>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 500 500">
    <script>//<![CDATA[
        alert(document.domain)
    //]]>
    </script>
</svg>

Describe alternatives you've considered
N/A

@kornelski
Copy link
Contributor

Use svg-hush. It's designed for this: https://lib.rs/svg-hush

@sebadob
Copy link

sebadob commented Jan 14, 2025

You could do it with for instance

ammonia::Builder::default()
    .add_tags(&["path", "svg"])
    .add_tag_attributes("path", &["d", "stroke-linecap", "stroke-linejoin"])
    .add_tag_attributes(
        "svg",
        &[
            "fill",
            "height",
            "opacity",
            "stroke",
            "stroke-width",
            "viewBox",
            "width",
        ],
    )
    .clean("my whatever input string")
    .to_string()

But I don't recommend doing that, because you need to add everything manually. svg-hush works really good out of the box. It only makes sense if you need to sanitize a bigger HTML input and you want to keep svgs inside it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants