Thread
Commits
-
Fix handling of namespace nodes in xpath() (xml)
- 41876c8d7783 14 (unreleased) landed
- 1d8c72b2eff6 15 (unreleased) landed
- 1740766012e6 16 (unreleased) landed
- 6c08cbb7a7b4 17 (unreleased) landed
- 4c777d6dd9c9 18 (unreleased) landed
- 8bf257aebac1 19 (unreleased) landed
-
xml2: Fix crash with namespace nodes in xpath_nodeset()
- f3f901a53a17 14 (unreleased) landed
- bc5775149f59 15 (unreleased) landed
- 18955d412c82 16 (unreleased) landed
- 4a49ab289085 17 (unreleased) landed
- 91b57eadeb0f 18 (unreleased) landed
- 9d33a5a804db 19 (unreleased) landed
-
[PATCH] contrib/xml2: backend crash in xpath_nodeset() on the namespace axis
Andrey Chernyy <andrey.cherny@tantorlabs.com> — 2026-06-11T00:14:36Z
Hi, In the xml2 extension, xpath_nodeset() crashes the backend on an XPath that selects namespace-axis nodes. xpath_nodeset() is executable by PUBLIC by default, so any role that can run SQL can crash the server with one query: CREATE EXTENSION xml2; SELECT xpath_nodeset('<root xmlns:foo="http://example.com/foo"><child/></root>', '//namespace::*'); Cause: pgxmlNodeSetToText() (contrib/xml2/xpath.c:197) calls xmlNodeDump(buf, nodeset->nodeTab[i]->doc, nodeset->nodeTab[i], 1, 0); with no node-type check. Namespace-axis results are XML_NAMESPACE_DECL nodes (xmlNs structs cast to xmlNodePtr), so reading the node's ->doc field runs past the smaller xmlNs allocation, and the bogus value is then dereferenced as the document by xmlNodeDump(). xpath_list() and xpath_table() already avoid this via xmlXPathCastNodeToString(); only the xmlNodeDump() path is exposed. Reproduced on master; the same unguarded xmlNodeDump() call in pgxmlNodeSetToText() is present on every supported back-branch (REL_18 through REL_14). Patch attached: render XML_NAMESPACE_DECL nodes with xmlXPathCastNodeToString() like xpath_table() does. The repro then returns the namespace text, ordinary node-set output is unchanged, and the xml2 regression test passes. -- Andrey Chernyy -
Re: [PATCH] contrib/xml2: backend crash in xpath_nodeset() on the namespace axis
Michael Paquier <michael@paquier.xyz> — 2026-06-11T01:48:30Z
On Thu, Jun 11, 2026 at 03:14:36AM +0300, Andrey Chernyy wrote: > Reproduced on master; the same unguarded xmlNodeDump() call in > pgxmlNodeSetToText() is present on every supported back-branch (REL_18 > through REL_14). > > Patch attached: render XML_NAMESPACE_DECL nodes with > xmlXPathCastNodeToString() like xpath_table() does. The repro then > returns the namespace text, ordinary node-set output is unchanged, and > the xml2 regression test passes. Thanks for the report, that looks about right to fix the way you are doing in xml2. Some tests and we should be good. Hmm. We have a second caller of xmlNodeDump() in the core backend code in adt/xml.c, leading to a confusing error if I try to use a namespace: =# select xpath('//namespace::*', '<root xmlns:foo="http://example.com"/>'::xml); ERROR: 53200: could not copy node CONTEXT: SQL function "xpath" statement 1 LOCATION: xml_ereport, xml.c:2082 It looks to me that we should fallback to xmlXPathCastNodeToString() when dealing with a NAMESPACE_DECL. The attached patch leads me to the following result, that looks much better: =# select xpath('//namespace::*', '<root xmlns:foo="http://example.com"/>'::xml); xpath ----------------------------------------------------------- {http://www.w3.org/XML/1998/namespace,http://example.com} (1 row) What do you think? That also deserves a backpatch to me, even if it is less worse than the xml2 crash you have reported. I have grouped this fix with your patch in the attached. Both still need some tests. Each fix deserves its own commit, that's just a quick FYI version. -- Michael -
Re: [PATCH] contrib/xml2: backend crash in xpath_nodeset() on the namespace axis
Andrey Chernyy <andrey.cherny@tantorlabs.com> — 2026-06-11T22:59:06Z
On Thu, 11 Jun 2026 10:48:30 +0900 Michael Paquier <michael@paquier.xyz> wrote: > What do you think? That also deserves a backpatch to me, even if it > is less worse than the xml2 crash you have reported. > > I have grouped this fix with your patch in the attached. Both still > need some tests. Each fix deserves its own commit, that's just a > quick FYI version. Thanks Michael, and thanks for adding the tests and back-patching! The grouped patch looks good to me - applying the same XML_NAMESPACE_DECL fallback to xmlXPathCastNodeToString() in core xml.c makes sense too. I had a regression test in progress, but glad it's already covered. -- Andrey Chernyy
-
Re: [PATCH] contrib/xml2: backend crash in xpath_nodeset() on the namespace axis
Michael Paquier <michael@paquier.xyz> — 2026-06-12T01:27:23Z
On Fri, Jun 12, 2026 at 01:59:06AM +0300, Andrey Chernyy wrote: > Thanks Michael, and thanks for adding the tests and back-patching! The > grouped patch looks good to me - applying the same XML_NAMESPACE_DECL > fallback to xmlXPathCastNodeToString() in core xml.c makes sense too. I > had a regression test in progress, but glad it's already covered. Both issues are now fixed and backpatched, as of 8bf257aebac1 and 9d33a5a804db. -- Michael