defmodule StripJs do @moduledoc ~s""" StripJs is an Elixir module for stripping executable JavaScript from blocks of HTML and CSS. It handles: * `` and `` tags * Event handler attributes such as `onclick="..."` * `javascript:...` URLs in HTML and CSS * CSS `expression(...)` directives * HTML entity attacks (like `<script>`) ## Installation Add `strip_js` to your application's `mix.exs`: def application do [applications: [:strip_js]] end def deps do [{:strip_js, "~> #{StripJs.Mixfile.project[:version]}"}] end """ <> ~S""" ## Usage `clean_html/2` removes all JS vectors from an HTML string: iex> html = "" iex> StripJs.clean_html(html) "" `clean_css/2` removes all JS vectors from a CSS string: iex> css = "body { background-image: url('javascript:alert()'); }" iex> StripJs.clean_css(css) "body { background-image: url('removed_by_strip_js:alert()'); }" StripJs relies on the [Floki](https://github.com/philss/floki) HTML parser library, which is built using [Mochiweb](https://github.com/mochi/mochiweb). StripJs provides a `clean_html_tree/1` function to strip JS from `Floki.parse/1`- and `:mochiweb_html.parse/1`- style HTML parse trees. ## Bugs and Limitations The brokenness of invalid HTML may be amplified by `clean_html/2`. In uncommon cases, innocent CSS which very closely resembles JS-injection techniques may be mangled by `clean_css/2`. StripJs may not block 100% of executable JavaScript, though it gets quite close. If you believe there are JS injection methods not covered by this library, please submit an issue with a test case! ## Authorship and License Copyright 2017, Appcues, Inc. Project homepage: [StripJs](https://github.com/appcues/strip_js) StripJs is released under the [MIT License](https://opensource.org/licenses/MIT). """ require Logger @type opts :: Keyword.t # reserved for future use @type html_tag :: String.t @type html_attr :: {String.t, String.t} @type html_node :: String.t | {html_tag, [html_attr], [html_node]} @type html_tree :: html_node | [html_node] @doc ~S""" Removes JS vectors from the given HTML string. All non-tag text and tag attribute values will be HTML-escaped, except for the contents of `