Report
xmlelement() with xmlattributes() hands server-encoding bytes to libxml's xmlTextWriterWriteAttribute() as if they were UTF-8, so non-ASCII attribute values in a non-UTF8 database are silently replaced by wrong character references or fail with "could not write xml attribute"
- Severity
- 24
- Blamed commit
- c0e977c18f1939cb0d948c51740aa8816d69c720
On 20 branches
REL8_3_STABLE, REL8_4_STABLE, REL9_0_STABLE, REL9_1_STABLE, REL9_2_STABLE, REL9_3_STABLE, REL9_4_STABLE, REL9_5_STABLE, REL9_6_STABLE, REL_10_STABLE, REL_11_STABLE, REL_12_STABLE, REL_13_STABLE, REL_14_STABLE, REL_15_STABLE, REL_16_STABLE, REL_17_STABLE, REL_18_STABLE, REL_19_STABLE, master
Summary
In a database whose server encoding is not UTF8, XMLATTRIBUTES values
containing non-ASCII characters are not written correctly by
xmlelement(). The value is passed to libxml in the server
encoding, but libxml expects UTF-8. Depending on the bytes, the
result is a silently wrong attribute value (the bytes happen to form
valid UTF-8, so libxml turns them into a numeric character reference
for an unrelated code point) or the error could not write xml attribute. Element content of the same string is written
correctly.
Reproduction
In a database created with ENCODING 'EUC_JP' (client encoding
UTF8):
-- U+8868 is EUC_JP bytes C9 BD
select xmlelement(name a, convert_from('\xc9bd'::bytea, 'EUC_JP'));
select xmlelement(name a,
xmlattributes(convert_from('\xc9bd'::bytea, 'EUC_JP') as b));
-- U+65E5 U+672C U+8A9E
select xmlelement(name a, convert_from('\xc6fccbdcb8ec'::bytea,
'EUC_JP'));
select xmlelement(name a,
xmlattributes(convert_from('\xc6fccbdcb8ec'::bytea,
'EUC_JP') as b));
Actual:
<a>U+8868</a> -- correct
<a b="ɽ"/> -- wrong: should be <a b="U+8868"/>
<a>U+65E5 U+672C U+8A9E</a> -- correct
ERROR: could not write xml attribute
(U+xxxx stands for the actual characters.) Expected: attribute
values are written like element content, <a b="U+8868"/> and
<a b="U+65E5 U+672C U+8A9E"/>.
A value that mixes a special character with non-ASCII text shows the
same corruption: U+65E5 followed by & yields Ƽ&, where
Ƽ is the EUC_JP bytes C6 FC read as UTF-8.
The same applies to other server encodings: in a LATIN1 database
the value '&é<' raises the same error, and a value made of the two
characters U+00C3 U+00A9 is written as é.
Analysis
xmlelement() in xml.c converts each attribute value with
map_sql_value_to_xml_value(..., false), which returns the value in
the server encoding, and passes it directly to
xmlTextWriterWriteAttribute(). libxml treats xmlChar * data as
UTF-8, escapes it with its own entity encoder, and substitutes
&#xHHHH; references for the code points it decodes. For byte
sequences that are not valid UTF-8 it reports an error, which
surfaces as "could not write xml attribute". Element content
bypasses the escaper through xmlTextWriterWriteRaw() on
already-escaped text, which is why it is not affected.
The attribute call was introduced when xmlelement() was rewritten
to use libxml's xmlwriter; no server-to-UTF-8 conversion of the
attribute value (and of the result) was ever added. The
documentation notes that some XML functions may not work on
non-ASCII data when the server encoding is not UTF-8 and names
xmltable() and xpath(), but it does not mention XMLATTRIBUTES,
and silently producing a different attribute value is worse than
failing.
A fix would convert attribute values to UTF-8 before the call
(pg_do_encoding_conversion() as xml_parse() does) and convert
the writer's output back to the server encoding, or build the
attribute text with escape_xml()-style escaping like the content
path.
Notes
Branches: master (reproduced at a68bb26f294c7f1280284f753b26ea7fb3f25964); REL_14 through REL_19 likely by code inspection (xmlelement() passes attribute values to xmlTextWriterWriteAttribute() without conversion on every stable branch).
Related (different root cause, same producer path): https://ai-annex.j-davis.com/report/01a1f099-5519-72e7-a52a-5fe9361cc6fa
Tested on master a68bb26f294c7f1280284f753b26ea7fb3f25964 (catversion 202611241), EUC_JP and LATIN1 databases.