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

Allow img tag with src as data:image/png;base64 #65

Open
dkaushik411 opened this issue Jan 25, 2024 · 2 comments
Open

Allow img tag with src as data:image/png;base64 #65

dkaushik411 opened this issue Jan 25, 2024 · 2 comments

Comments

@dkaushik411
Copy link

I am converting a canvas to image to export in PDF from html. But HtmlSanitizeEx.Scrubber.Meta.allow_tag_with_uri_attributes(
"img",
["src"],
["http", "https", "mailto", "/", "data:image/png;base64"]
)

remove the src attribute from img tag.

@rrrene
Copy link
Owner

rrrene commented Jan 27, 2024

That is a bug 😞

Here is a workaround until I fix it:

defmodule MyScrubber do
  require HtmlSanitizeEx.Scrubber.Meta
  alias HtmlSanitizeEx.Scrubber.Meta

  Meta.remove_cdata_sections_before_scrub()
  Meta.strip_comments()
  
  Meta.allow_tag_with_these_attributes "img", []
  def scrub_attribute("img", {"src", "data:" <> _ = uri}) do
    {"src", uri}
  end
  Meta.allow_tag_with_uri_attributes(
      "img",
      ["src"],
      ["http", "https", "mailto", "data"]
    )

  Meta.strip_everything_not_covered()
end

~s|<img src="data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==" onload="malware()" />|
|> HtmlSanitizeEx.Scrubber.scrub(MyScrubber)

You have to add both statements:

  Meta.allow_tag_with_these_attributes "img", []
  def scrub_attribute("img", {"src", "data:" <> _ = uri}) do
    {"src", uri}
  end

before (!) the call to allow_tag_with_uri_attributes.

Sorry for the inconvenience. 🙏

@dkaushik411
Copy link
Author

Thanks @rrrene the workaround did work. Please notify once it is fixed and available in project.

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

2 participants