Andrzej Bialecki created SOLR-18696:
---------------------------------------
Summary: Managed schema persistence silently changes or corrupts
string values that XML cannot store
Key: SOLR-18696
URL: https://issues.apache.org/jira/browse/SOLR-18696
Project: Solr
Issue Type: Bug
Affects Versions: 10.1
Reporter: Andrzej Bialecki
Assignee: Andrzej Bialecki
The Schema API accepts JSON, and JSON strings can contain characters that XML
1.0 cannot store. Solr does not check for them. When the managed schema is
saved, {{SchemaXmlWriter}} escapes strings with {{XML.escapeCharData}} and
{{{}XML.escapeAttributeValue{}}}. Those escapers were written for the
{{wt=xml}} response writer: they keep the output well-formed instead of
preserving the value. So on the next reload, the value has changed, or the
schema no longer loads.
This affects every string that reaches the managed schema through JSON: field
{{default}} values, extra field type arguments, analyzer and similarity
parameters, and the new {{<meta>}} entries (SOLR-18678, SOLR-18679). A schema
file edited by hand is not affected, because the XML parser already rejects
these characters when it loads the file.
What happens to each kind of character after a save and reload:
||Character||Written as||Read back as||
|U+0000–U+001F, except tab, newline and carriage return|the literal text
{{#1;}} (no {{{}&{}}})|the text {{{}#1;{}}}: the value changed|
|Carriage return in element text|a raw CR|a newline: XML parsers turn CR and
CRLF into LF|
|Tab, newline or carriage return in an attribute value|the raw character|a
space: XML attribute-value normalization|
|U+FFFE, U+FFFF|the raw character|nothing: the saved schema is invalid XML and
fails to load|
|An unpaired surrogate|{{{}?{}}}, from the UTF-8 encoder|{{{}?{}}}: the value
changed|
The U+FFFE / U+FFFF case is the most serious: a single Schema API call leaves a
managed schema that the collection cannot reload.
Example: {{add-field}} with {{"default": "a\uFFFFb"}} succeeds, and the core
then fails to load on the next reload. With {{{}"default": "line1\nline2"{}}},
the field's default comes back as {{{}"line1 line2"{}}}.
h2. Proposed fix
Fix it once, where the managed schema is written, so that every value is
covered whatever path it took into the schema. Leave {{XML.escapeCharData}} and
{{XML.escapeAttributeValue}} unchanged, because the {{wt=xml}} response writer
depends on their current behavior.
# *Reject characters XML cannot store.* When {{SchemaXmlWriter}} writes a
string, either an attribute value or element text, it fails with a 400 error if
the string contains a character outside the XML 1.0 range ({{{}#x9 | #xA | #xD
| [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]{}}}) or an unpaired
surrogate. The error names the element and attribute or entry, and the code
point, for example: {{{}field 'title', attribute 'default': character U+FFFF
cannot be stored in the schema{}}}.
# *Preserve whitespace that XML would otherwise change.* In element text,
write a carriage return as {{ }}. In attribute values, write tab, newline and
carriage return as {{ }}, {{ }} and {{ }}. XML parsers keep characters given as
character references, so these values come back unchanged.
This rejects a change before anything is written:
* Every way of saving a managed schema serializes it into memory first (for
local files, since SOLR-18679): {{ManagedIndexSchema.persistManagedSchema}} for
local files and ZooKeeper, and {{SchemaManager}} for non-additive changes in
SolrCloud. The error therefore reaches the Schema API caller, and the stored
schema stays as it was.
* The Schema Designer saves through the same code, so it gets the same check.
Rejecting at save time, rather than validating each Schema API command, is
simpler: the writer is the one place every value passes through, and it already
knows the element and attribute names for the error message.
h2. Tests
* For each character class in the table above: a Schema API change (field
{{{}default{}}}, field type argument, metadata entry) is rejected with a 400
that names the value, and the stored schema is unchanged.
* Tab, newline and carriage return in a field {{default}} and in metadata
survive a save and reload unchanged.
* Existing schemas, and schemas whose values contain no such characters, are
written byte-for-byte as before.
h2. Not covered
* The {{wt=xml}} response writer keeps its current escaping.
* Values in schema files edited by hand are already checked by the XML parser.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]