Skip to content

Commit

Permalink
Merge pull request #2296 from pepijnve/patch-1
Browse files Browse the repository at this point in the history
Resolve potential NPE in SchemaResourceResolver
  • Loading branch information
flavorjones committed Aug 19, 2021
2 parents 3e10984 + 88911fe commit 10bc4e7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
2 changes: 1 addition & 1 deletion ext/java/nokogiri/XmlSchema.java
Expand Up @@ -276,7 +276,7 @@ private class SchemaResourceResolver implements LSResourceResolver
String systemId,
String baseURI)
{
if (noNet && (systemId.startsWith("http://") || systemId.startsWith("ftp://"))) {
if (noNet && systemId != null && (systemId.startsWith("http://") || systemId.startsWith("ftp://"))) {
if (systemId.startsWith(XMLConstants.W3C_XML_SCHEMA_NS_URI)) {
return null; // use default resolver
}
Expand Down
26 changes: 12 additions & 14 deletions test/html4/test_node.rb
Expand Up @@ -187,21 +187,19 @@ def test_to_html_does_not_contain_entities
nokogiri.at("p").to_html.gsub(/ /, '')
end
end
end

def test_GH_1042
file = File.join(ASSETS_DIR, 'GH_1042.html');
html = Nokogiri::HTML(File.read(file))
table = html.xpath("//table")[1]
trs = table.xpath("tr").drop(1)

# the jruby inplementation of drop uses dup() on the IRubyObject (which
# is NOT the same dup() method on the ruby Object) which produces a
# shallow clone. a shallow of valid XMLNode triggers several
# NullPointerException on inspect() since loads of invariants
# are not set. the fix for GH1042 ensures a proper working clone.
assert_nothing_raised do
trs.inspect
def test_GH_1042
file = File.join(ASSETS_DIR, 'GH_1042.html');
html = Nokogiri::HTML(File.read(file))
table = html.xpath("//table")[1]
trs = table.xpath("tr").drop(1)

# the jruby inplementation of drop uses dup() on the IRubyObject (which
# is NOT the same dup() method on the ruby Object) which produces a
# shallow clone. a shallow of valid XMLNode triggers several
# NullPointerException on inspect() since loads of invariants
# are not set. the fix for GH1042 ensures a proper working clone.
trs.inspect # assert_nothing_raised
end
end
end
Expand Down
15 changes: 15 additions & 0 deletions test/xml/test_schema.rb
Expand Up @@ -214,6 +214,21 @@ def test_xsd_with_dtd
end
end

def test_xsd_import_with_no_systemid
# https://github.com/sparklemotion/nokogiri/pull/2296
xsd = <<~EOF
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.w3.org/1998/Math/MathML"
targetNamespace="http://www.w3.org/1998/Math/MathML"
>
<xs:import/>
</xs:schema>
EOF
Nokogiri::XML::Schema(xsd) # assert_nothing_raised
end

describe "CVE-2020-26247" do
# https://github.com/sparklemotion/nokogiri/security/advisories/GHSA-vr8q-g5c7-m54m
let(:schema) do
Expand Down

0 comments on commit 10bc4e7

Please sign in to comment.