Skip to content

Commit

Permalink
fix(jruby): SAX parser uses an entity resolver
Browse files Browse the repository at this point in the history
to avoid XXE injections.

This behavior now matches the CRuby implementation.
  • Loading branch information
flavorjones committed Sep 26, 2021
1 parent f943ee4 commit 4bd943c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions ext/java/nokogiri/XmlSaxParserContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ public class XmlSaxParserContext extends ParserContext
preParse(runtime, handlerRuby, handler);
parser.setContentHandler(handler);
parser.setErrorHandler(handler);
parser.setEntityResolver(new NokogiriEntityResolver(runtime, errorHandler, options));

try {
parser.setProperty("http://xml.org/sax/properties/lexical-handler", handler);
Expand Down
33 changes: 33 additions & 0 deletions test/xml/sax/test_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -426,5 +426,38 @@ def call_parse_io_with_encoding(encoding)

assert_predicate(handler.errors, :empty?)
end

it "does not resolve entities by default" do
xml = <<~EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE doc [
<!ENTITY local SYSTEM "file:///#{File.expand_path(__FILE__)}">
<!ENTITY custom "resolved>
]>
<doc><foo>&local;</foo><foo>&custom;</foo></doc>
EOF

doc = Doc.new
parser = Nokogiri::XML::SAX::Parser.new(doc)
parser.parse(xml)

assert_nil(doc.data)
end

it "does not resolve network external entities by default" do
xml = <<~EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE doc [
<!ENTITY remote SYSTEM "http://0.0.0.0:8080/evil.dtd">
]>
<doc><foo>&remote;</foo></doc>
EOF

doc = Doc.new
parser = Nokogiri::XML::SAX::Parser.new(doc)
parser.parse(xml)

assert_nil(doc.data)
end
end
end

0 comments on commit 4bd943c

Please # to comment.