Skip to content

Commit 18ba20e

Browse files
committed
progress
1 parent ec645ef commit 18ba20e

File tree

1 file changed

+27
-0
lines changed
  • src/backend/utils/adt

1 file changed

+27
-0
lines changed

src/backend/utils/adt/xml.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,21 @@ static void SPI_sql_row_to_xmlelement(int rownum, StringInfo result,
142142
#define NAMESPACE_XSI "http://www.w3.org/2001/XMLSchema-instance"
143143
#define NAMESPACE_SQLXML "http://standards.iso.org/iso/9075/2003/sqlxml"
144144

145+
/* forbidden C0 control chars */
146+
#define FORBIDDEN_C0 \
147+
"\x01\x02\x03\x04\x05\x06\x07\x08\x0B\x0C\x0E\x0F\x10\x11" \
148+
"\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F"
149+
150+
static inline void
151+
check_forbidden_chars(char * str)
152+
{
153+
if (strpbrk(str,FORBIDDEN_C0) != NULL)
154+
ereport(ERROR,
155+
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
156+
errmsg("character out of range"),
157+
errdetail("XML does not support control characters.")));
158+
159+
}
145160

146161
#ifdef USE_LIBXML
147162

@@ -411,6 +426,8 @@ xmlcomment(PG_FUNCTION_ARGS)
411426
appendStringInfoText(&buf, arg);
412427
appendStringInfo(&buf, "-->");
413428

429+
check_forbidden_chars(buf.data);
430+
414431
PG_RETURN_XML_P(stringinfo_to_xmltype(&buf));
415432
#else
416433
NO_XML_SUPPORT();
@@ -718,6 +735,8 @@ xmlpi(char *target, text *arg, bool arg_is_null, bool *result_is_null)
718735
}
719736
appendStringInfoString(&buf, "?>");
720737

738+
check_forbidden_chars(buf.data);
739+
721740
result = stringinfo_to_xmltype(&buf);
722741
pfree(buf.data);
723742
return result;
@@ -741,6 +760,8 @@ xmlroot(xmltype *data, text *version, int standalone)
741760
len = VARSIZE(data) - VARHDRSZ;
742761
str = text_to_cstring((text *) data);
743762

763+
check_forbidden_chars(str);
764+
744765
parse_xml_decl((xmlChar *) str, &len, &orig_version, NULL, &orig_standalone);
745766

746767
if (version)
@@ -1184,6 +1205,9 @@ xml_parse(text *data, XmlOptionType xmloption_arg, bool preserve_whitespace,
11841205
encoding,
11851206
PG_UTF8);
11861207

1208+
/* check for illegal XML chars */
1209+
check_forbidden_chars((char *) utf8string);
1210+
11871211
/* Start up libxml and its parser (no-ops if already done) */
11881212
pg_xml_init();
11891213
xmlInitParser();
@@ -1804,6 +1828,9 @@ map_sql_value_to_xml_value(Datum value, Oid type, bool xml_escape_strings)
18041828
getTypeOutputInfo(type, &typeOut, &isvarlena);
18051829
str = OidOutputFunctionCall(typeOut, value);
18061830

1831+
/* check for illegal XML chars */
1832+
check_forbidden_chars(str);
1833+
18071834
/* ... exactly as-is for XML, and when escaping is not wanted */
18081835
if (type == XMLOID || !xml_escape_strings)
18091836
return str;

0 commit comments

Comments
 (0)