Fix XPath variable handling and invalid value contamination#342
Open
tompng wants to merge 1 commit into
Open
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adjusts REXML’s XPath evaluation to prevent nil/invalid values from “contaminating” results, primarily by making id() return an empty node-set and by coercing variable values + applying remaining predicates/steps after variable/group evaluation.
Changes:
- Make unimplemented XPath function
id()return[](empty node-set) instead ofnil. - Add variable coercion and ensure remaining predicates/steps are applied after variable and grouped expressions.
- Add/extend tests covering
id()and variable handling (including invalid predicate application on non-node-set variables).
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| test/xpath/test_base.rb | Adds regression tests for id() and variable coercion/predicate behavior. |
| lib/rexml/xpath_parser.rb | Implements variable coercion and applies remaining predicates after variable/group evaluation. |
| lib/rexml/functions.rb | Changes id() to return an empty node-set ([]). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+348
to
+357
| def coerce_variable(value) | ||
| case value | ||
| when Array | ||
| value.grep(REXML::Node).uniq | ||
| when Numeric, String, true, false | ||
| value | ||
| else | ||
| "" | ||
| end | ||
| end |
| assert_equal(["e"], XPath.match(doc, "//e[preceding-sibling::* = '1']").map(&:name)) | ||
| end | ||
|
|
||
| def test_unimplemented_id_shouldnot_contaminate_nil |
Fix nil and other invalid value contamination. Nil can be injected through variables and through id function. Unimplemented function `id()` is fixed to return `[]` instead of `nil`. Add variable coerce and predicates after variables.
3f20d0b to
f343f5b
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix nil and other invalid value contamination. Nil can be injected through variables and through id function.
Unimplemented function
id()is fixed to return[]instead ofnil.Add variable coerce and predicates after variables.
Minor behavior changes for an invalid XPath match:
It may raise TypeError (because
$xwasn't evaluated to a nodeset), though, it shoudn't return[42]Note
Although nodeset as a variable has been accepted before, the code added in pull request explicitly permits it.
Nokogiri only accepts string value as a variable, so there's an option to limit the value types here.
Accepting nodeset may cause a problem if the passed nodes doesn't belong to a single document root. (or just consider such case as unsupported)