Skip to content

Commit

Permalink
fix(jruby): Schema#validate should not accumulate errors in @errors
Browse files Browse the repository at this point in the history
Fixes #1282
  • Loading branch information
flavorjones committed Nov 22, 2021
1 parent 0bbfa30 commit 4631a8a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ end
### Fixed

* [JRuby] Fix NPE in Schema parsing when an imported resource doesn't have a `systemId`. [[#2296](https://github.com/sparklemotion/nokogiri/issues/2296)] (Thanks, [@pepijnve](https://github.com/pepijnve)!)
* [JRuby] Fix `Schema#validate` to only return the most recent Document's errors. Previously, if multiple documents were validated, this method returned the accumulated errors of all previous documents. [#1282]
* [JRuby] Fix `Schema#validate` to not clobber the `@errors` instance variable. [#1282]


## 1.12.3 / 2021-08-10
Expand Down
2 changes: 1 addition & 1 deletion ext/java/nokogiri/XmlSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public class XmlSchema extends RubyObject
IRubyObject
validate_document_or_file(ThreadContext context, XmlDocument xmlDocument)
{
RubyArray errors = (RubyArray) this.getInstanceVariable("@errors");
RubyArray errors = (RubyArray)context.runtime.newEmptyArray();
ErrorHandler errorHandler = new SchemaErrorHandler(context.runtime, errors);
setErrorHandler(errorHandler);

Expand Down
41 changes: 41 additions & 0 deletions test/xml/test_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,47 @@ def test_xsd_import_with_no_systemid
Nokogiri::XML::Schema(xsd) # assert_nothing_raised
end

describe "Schema#validate" do
let(:xsd) do
<<~EOF
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/contactExample">
<xs:element name="Contacts"></xs:element>
</xs:schema>
EOF
end

let(:good_xml) { %Q(<Contacts xmlns="http://www.example.org/contactExample"><Contact></Contact></Contacts>) }
let(:bad_xml) { %Q(<Contacts xmlns="http://www.example.org/wrongNs"><Contact></Contact></Contacts>) }

it "does not clobber @errors" do
schema = Nokogiri::XML::Schema.new(xsd)
bad_doc = Nokogiri::XML(bad_xml)

# assert on setup
assert_empty(schema.errors)
refute_empty(schema.validate(bad_doc))

# this is the bit under test
assert_empty(schema.errors)
end

it "returns only the most recent document's errors" do
# https://github.com/sparklemotion/nokogiri/issues/1282
schema = Nokogiri::XML::Schema.new(xsd)
good_doc = Nokogiri::XML(good_xml)
bad_doc = Nokogiri::XML(bad_xml)

# assert on setup
assert_empty(schema.validate(good_doc))
refute_empty(schema.validate(bad_doc))

# this is the bit under test
assert_empty(schema.validate(good_doc))
end
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 4631a8a

Please sign in to comment.