Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JCR-4935 skip invalid xml characters in session.exportDocumentView() #132

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,41 @@ public void testMultiValue() throws Exception {
}
}

/**
* Test case for
* <a href="https://issues.apache.org/jira/browse/JCR-4935">JCR-4935</a>:
* session.exportDocumentView() generates unparsable XML if a JCR Property contains invalid XML character
*/
public void testInvalidXmlCharacter() throws Exception {

Node root = superuser.getRootNode();

Node node = root.addNode("invalid-xml-character-test", "nt:unstructured");
node.setProperty("0x3", "\u0003");
node.setProperty("0xB", "\u000B");
node.setProperty("0xC", "\u000C");
node.setProperty("0x19", "\u0019");
node.setProperty("0xD800", "\uD800");
node.setProperty("0xFFFE", "\uFFFE");
node.setProperty("0xD800", "\uD800");

ByteArrayOutputStream buffer = new ByteArrayOutputStream();
superuser.exportDocumentView("/invalid-xml-character-test", buffer, true, true);
superuser.refresh(false);

superuser.importXML(
"/", new ByteArrayInputStream(buffer.toByteArray()),
ImportUUIDBehavior.IMPORT_UUID_COLLISION_THROW);

node = root.getNode("invalid-xml-character-test");
assertEquals("\\u0003", node.getProperty("0x3").getString());
assertEquals("\\u000b", node.getProperty("0xB").getString());
assertEquals("\\u000c", node.getProperty("0xC").getString());
assertEquals("\\u0019", node.getProperty("0x19").getString());
assertEquals("\\ud800", node.getProperty("0xD800").getString());
assertEquals("\\ufffe", node.getProperty("0xFFFE").getString());
assertEquals("\\ud800", node.getProperty("0xD800").getString());
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.io.Writer;
import java.nio.charset.StandardCharsets;

import org.apache.jackrabbit.util.Text;
import org.apache.jackrabbit.util.XMLChar;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
Expand Down Expand Up @@ -140,7 +142,11 @@ private void write(char[] ch, int start, int length, boolean attribute)
} else if (attribute && ch[i] == '\'') {
writer.write("&apos;");
} else {
writer.write(ch[i]);
if (XMLChar.isValid(ch[i])) {
writer.write(ch[i]);
} else {
writer.append(escapeIllegalXmlChar(ch[i]));
}
}
} catch (IOException e) {
throw new SAXException(
Expand All @@ -149,6 +155,22 @@ private void write(char[] ch, int start, int length, boolean attribute)
}
}

/**
* Escape invalid xml characters to Unicode code points,
* similar to FileVault .
*
* See https://jackrabbit.apache.org/filevault/docview.html#escaping
*/
private String escapeIllegalXmlChar(char c){
StringBuilder buf = new StringBuilder();
buf.append("\\u");
buf.append(Text.hexTable[(c >> 12) & 15]);
buf.append(Text.hexTable[(c >> 8) & 15]);
buf.append(Text.hexTable[(c >> 4) & 15]);
buf.append(Text.hexTable[c & 15]);
return buf.toString();
}

private void closeStartTagIfOpen() throws SAXException {
if (startTagIsOpen) {
try {
Expand Down Expand Up @@ -275,5 +297,4 @@ public void endElement(
public String toString() {
return writer.toString();
}

}