From c30db1926176ea597139e3036a4f7b407ab8a1a3 Mon Sep 17 00:00:00 2001 From: Siraj saheb Date: Wed, 18 Nov 2026 00:24:07 +0530 Subject: [PATCH 1/3] reject characters not allowed in XML in xpath_literal --- cssselect/xpath.py | 8 ++++++++ tests/test_cssselect.py | 5 +++++ tests/test_hypothesis.py | 27 +++++++++++++++------------ 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/cssselect/xpath.py b/cssselect/xpath.py index fcdf952..ec3ad2a 100644 --- a/cssselect/xpath.py +++ b/cssselect/xpath.py @@ -124,6 +124,12 @@ def join(self, combiner: str, other: XPathExpr) -> Self: # Test that the string is not empty and does not contain whitespace is_non_whitespace = re.compile(r"[^ \t\r\n\f]+").fullmatch +# Characters not allowed in XML 1.0, which therefore cannot be part of an XPath +# expression either. +has_non_xml_char = re.compile( + "[\x00-\x08\x0b\x0c\x0e-\x1f\ud800-\udfff\ufffe\uffff]" +).search + def _or_conditions(conditions: list[str]) -> str: """Join conditions with "or".""" @@ -284,6 +290,8 @@ def xpath_pseudo_element( @staticmethod def xpath_literal(s: str) -> str: s = str(s) + if has_non_xml_char(s): + raise ExpressionError(f"{s!r} contains a character not allowed in XML") if "'" not in s: s = f"'{s}'" elif '"' not in s: diff --git a/tests/test_cssselect.py b/tests/test_cssselect.py index fe9f218..ba12e07 100644 --- a/tests/test_cssselect.py +++ b/tests/test_cssselect.py @@ -1216,6 +1216,11 @@ def test_input_preprocessing(self) -> None: assert css_to_xpath(':contains("x\udfffy")') == ( "descendant-or-self::*[contains(., 'x�y')]" ) + # Other characters not allowed in XML cannot be put in an XPath string. + with pytest.raises(ExpressionError): + css_to_xpath('*[aval="x\x01y"]') + with pytest.raises(ExpressionError): + css_to_xpath(':contains("x\uffffy")') # A raw NUL in an identifier becomes a valid U+FFFD name character. assert str(next(tokenize("foo\x00bar"))) == "" diff --git a/tests/test_hypothesis.py b/tests/test_hypothesis.py index dc380aa..3637b2c 100644 --- a/tests/test_hypothesis.py +++ b/tests/test_hypothesis.py @@ -4,12 +4,17 @@ import typing from typing import TYPE_CHECKING -import pytest from hypothesis import given from hypothesis import strategies as st from lxml import etree -from cssselect import GenericTranslator, HTMLTranslator, SelectorError, parse +from cssselect import ( + ExpressionError, + GenericTranslator, + HTMLTranslator, + SelectorError, + parse, +) from cssselect.parser import ( Token, TokenStream, @@ -191,10 +196,7 @@ def test_css_like(css: str) -> None: xpath = translator.css_to_xpath(css) except SelectorError: continue - # Escapes like "\b" can produce characters not allowed in XML, a known - # problem tested by test_escape_translates_non_xml(). - if not any(c in NON_XML_CHARS for c in xpath): - etree.XPath(xpath) + etree.XPath(xpath) try: selectors = parse(css) except SelectorError: @@ -205,7 +207,13 @@ def test_css_like(css: str) -> None: def check_escape_translates(context: str, char: str) -> None: css = context.format(f"\\{ord(char):x} ") for translator in (GenericTranslator(), HTMLTranslator()): - etree.XPath(translator.css_to_xpath(css)) + try: + xpath = translator.css_to_xpath(css) + except ExpressionError: + # Characters not allowed in XML cannot be put in an XPath string. + assert char in NON_XML_CHARS + continue + etree.XPath(xpath) @given( @@ -216,11 +224,6 @@ def test_escape_translates(context: str, char: str) -> None: check_escape_translates(context, char) -@pytest.mark.xfail( - raises=ValueError, - strict=True, - reason="Characters not allowed in XML end up in the XPath expression", -) @given(st.sampled_from(ESCAPE_CONTEXTS), st.sampled_from(NON_XML_CHARS)) def test_escape_translates_non_xml(context: str, char: str) -> None: check_escape_translates(context, char) From 76a0d29e83b29cad84477bfb55208e972dd1826e Mon Sep 17 00:00:00 2001 From: Siraj saheb Date: Thu, 19 Nov 2026 14:03:44 +0530 Subject: [PATCH 2/3] move non-XML character assertions into their own test --- tests/test_cssselect.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/test_cssselect.py b/tests/test_cssselect.py index ba12e07..f84d07d 100644 --- a/tests/test_cssselect.py +++ b/tests/test_cssselect.py @@ -1216,13 +1216,16 @@ def test_input_preprocessing(self) -> None: assert css_to_xpath(':contains("x\udfffy")') == ( "descendant-or-self::*[contains(., 'x�y')]" ) - # Other characters not allowed in XML cannot be put in an XPath string. + # A raw NUL in an identifier becomes a valid U+FFFD name character. + assert str(next(tokenize("foo\x00bar"))) == "" + + def test_non_xml_characters(self) -> None: + # Characters not allowed in XML cannot be put in an XPath string. + css_to_xpath = GenericTranslator().css_to_xpath with pytest.raises(ExpressionError): css_to_xpath('*[aval="x\x01y"]') with pytest.raises(ExpressionError): css_to_xpath(':contains("x\uffffy")') - # A raw NUL in an identifier becomes a valid U+FFFD name character. - assert str(next(tokenize("foo\x00bar"))) == "" def test_xpath_pseudo_elements(self) -> None: class CustomTranslator(GenericTranslator): From 2b3d10f5ff3c13c761448cac707d474dd2d82ff0 Mon Sep 17 00:00:00 2001 From: Siraj saheb Date: Thu, 19 Nov 2026 20:05:25 +0530 Subject: [PATCH 3/3] make _has_non_xml_char private --- cssselect/xpath.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cssselect/xpath.py b/cssselect/xpath.py index ec3ad2a..383bd2a 100644 --- a/cssselect/xpath.py +++ b/cssselect/xpath.py @@ -126,7 +126,7 @@ def join(self, combiner: str, other: XPathExpr) -> Self: # Characters not allowed in XML 1.0, which therefore cannot be part of an XPath # expression either. -has_non_xml_char = re.compile( +_has_non_xml_char = re.compile( "[\x00-\x08\x0b\x0c\x0e-\x1f\ud800-\udfff\ufffe\uffff]" ).search @@ -290,7 +290,7 @@ def xpath_pseudo_element( @staticmethod def xpath_literal(s: str) -> str: s = str(s) - if has_non_xml_char(s): + if _has_non_xml_char(s): raise ExpressionError(f"{s!r} contains a character not allowed in XML") if "'" not in s: s = f"'{s}'"