Git Product home page Git Product logo

Comments (2)

flavorjones avatar flavorjones commented on July 30, 2024 1

Hi! Thanks for asking this question.

This code snippet uses #scrub_fragment which does two things:

  • parse the fragment into a Nokogiri::HTML::DocumentFragment
  • sanitize that DocumentFragment

Let's separate these two operations to see what's going on ...

Loofah.fragment("<hello message").children
# => [#<Nokogiri::XML::Element:0x2bc name="hello" attributes=[#<Nokogiri::XML::Attr:0x2d0 name="message">]>]

Interesting: Nokogiri parses that fragment into a <hello></hello> element. Why is that? Nokogiri (actually, libxml2) treats this as a "markup error" and tries to fix it:

Loofah.fragment("<hello message").errors
# => 
# [#<Nokogiri::XML::SyntaxError: 1:27: ERROR: Tag hello invalid>,
#  #<Nokogiri::XML::SyntaxError: 1:27: ERROR: Couldn't find end of Start Tag hello>]

If your intention is to have this string interpreted as a "text node" that equals <hello message you should be aware that a bare < in an HTML text node is considered malformed, and you should use &lt; instead. You may want to consider HTML-escaping anything that's a text node before passing it to Loofah:

CGI.escapeHTML("<hello message")
# => "&lt;hello message"
Loofah.fragment(CGI.escapeHTML("<hello message"))
# => #(DocumentFragment:0x3d4 { name = "#document-fragment", children = [ #(Text "<hello message")] })
Loofah.fragment(CGI.escapeHTML("<hello message")).to_html
# => "&lt;hello message"

The <hello> element is being removed by the Strip scrubber. The documentation says:

+:strip+ removes unknown/unsafe tags

Is <hello></hello> a known and safe tag? Let's look at the code:

class Strip < Scrubber
def initialize
@direction = :bottom_up
end
def scrub(node)
return CONTINUE if html5lib_sanitize(node) == CONTINUE

which calls html5lib_sanitize:

def html5lib_sanitize(node)
case node.type
when Nokogiri::XML::Node::ELEMENT_NODE
if HTML5::Scrub.allowed_element? node.name

which calls allowed_element?:

def allowed_element?(element_name)
::Loofah::HTML5::SafeList::ALLOWED_ELEMENTS_WITH_LIBXML2.include?(element_name)
end

Which uses ALLOWED_ELEMENTS_WITH_LIBXML2 -- basically this allowlist which hello is not a member of:

ACCEPTABLE_ELEMENTS = Set.new([
"a",
"abbr",
"acronym",
"address",
"area",
"article",
"aside",
"audio",
"b",
"bdi",
"bdo",
"big",
"blockquote",
"br",
"button",
"canvas",
"caption",
"center",
"cite",
"code",
"col",
"colgroup",
"command",
"datalist",
"dd",
"del",
"details",
"dfn",
"dir",
"div",
"dl",
"dt",
"em",
"fieldset",
"figcaption",
"figure",
"font",
"footer",
"form",
"h1",
"h2",
"h3",
"h4",
"h5",
"h6",
"header",
"hr",
"i",
"img",
"input",
"ins",
"kbd",
"label",
"legend",
"li",
"main",
"map",
"mark",
"menu",
"meter",
"nav",
"ol",
"optgroup",
"option",
"output",
"p",
"pre",
"q",
"s",
"samp",
"section",
"select",
"small",
"span",
"strike",
"strong",
"sub",
"summary",
"sup",
"table",
"tbody",
"td",
"textarea",
"tfoot",
"th",
"thead",
"time",
"tr",
"tt",
"u",
"ul",
"var",
"video",
"wbr",
])

If we use something in the list instead, like audio, we see Loofah keeps it around:

Loofah.fragment("<audio message").scrub!(:strip).to_html
# => "<audio></audio>"

I hope that makes sense!

from loofah.

piyush-ally avatar piyush-ally commented on July 30, 2024

Thank you @flavorjones for an amazing explanation of underlying code.

from loofah.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.