-
-
Notifications
You must be signed in to change notification settings - Fork 189
[bugfix] fn:xml-to-json: enforce F&O 3.1 §17.4.2 structural validation (+10 XQTS HEAD) #6350
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
Open
joewiz
wants to merge
3
commits into
eXist-db:develop
Choose a base branch
from
joewiz:bugfix/fn-xml-to-json-over-permissive-validation
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,7 +36,9 @@ | |
| import java.io.StringWriter; | ||
| import java.io.Writer; | ||
| import java.math.BigDecimal; | ||
| import java.util.ArrayDeque; | ||
| import java.util.ArrayList; | ||
| import java.util.Deque; | ||
|
|
||
| import static org.exist.xquery.FunctionDSL.*; | ||
|
|
||
|
|
@@ -106,6 +108,8 @@ private void nodeValueToJson(final NodeValue nodeValue, final Writer writer) thr | |
| final Integer stackSeparator = 0; | ||
| //use ArrayList<Object> to store String type keys and non-string type separators | ||
| final ArrayList<Object> mapkeyArrayList = new ArrayList<>(); | ||
| //track parent element local names so we can validate child structure (F&O 3.1 §17.4.2 / §17.5.4) | ||
| final Deque<String> elementStack = new ArrayDeque<>(); | ||
| boolean elementKeyIsEscaped = false; | ||
| boolean elementValueIsEscaped = false; | ||
| XMLStreamReader reader = null; | ||
|
|
@@ -127,6 +131,7 @@ private void nodeValueToJson(final NodeValue nodeValue, final Writer writer) thr | |
| "Invalid XML representation of JSON. Element '" + reader.getLocalName() | ||
| + "' is not in the required namespace '" + Namespaces.XPATH_FUNCTIONS_NS + "'."); | ||
| } | ||
| validateStartElement(reader, elementStack); | ||
| final String elementAttributeEscapedValue = reader.getAttributeValue(null, "escaped"); | ||
| elementValueIsEscaped = "true".equals(elementAttributeEscapedValue); | ||
| final String elementAttributeEscapedKeyValue = reader.getAttributeValue(null, "escaped-key"); | ||
|
|
@@ -162,10 +167,15 @@ private void nodeValueToJson(final NodeValue nodeValue, final Writer writer) thr | |
| break; | ||
| case XMLStreamReader.CHARACTERS: | ||
| case XMLStreamReader.CDATA: | ||
| tempStringBuilder.append(reader.getText()); | ||
| final String charText = reader.getText(); | ||
| validateTextInContext(charText, elementStack.peek()); | ||
| tempStringBuilder.append(charText); | ||
| break; | ||
| case XMLStreamReader.END_ELEMENT: | ||
| final String tempString = tempStringBuilder.toString(); | ||
| if (!elementStack.isEmpty()) { | ||
| elementStack.pop(); | ||
| } | ||
| switch (reader.getLocalName()) { | ||
| case "array": | ||
| jsonGenerator.writeEndArray(); | ||
|
|
@@ -252,4 +262,122 @@ private String unescapeEscapedJsonString(final String escapedJsonString) throws | |
| unescapedJsonString = unescapedJsonStringBuilder.toString(); | ||
| return unescapedJsonString; | ||
| } | ||
|
|
||
| /** | ||
| * Validate the current START_ELEMENT against the F&O 3.1 §17.4.2 / §17.5.4 structural rules | ||
| * and, on success, push the element's local name onto the parent-tracking stack. | ||
| */ | ||
| private void validateStartElement(final XMLStreamReader reader, final Deque<String> elementStack) throws XPathException { | ||
| final String localName = reader.getLocalName(); | ||
| if (!isJsonElementName(localName)) { | ||
| throw new XPathException(this, ErrorCodes.FOJS0006, | ||
| "Invalid XML representation of JSON. Element '" + localName | ||
| + "' is not one of [map, array, null, boolean, number, string]."); | ||
| } | ||
| final String parentLocalName = elementStack.peek(); | ||
| if (parentLocalName != null && isLeafElementName(parentLocalName)) { | ||
| throw new XPathException(this, ErrorCodes.FOJS0006, | ||
| "Invalid XML representation of JSON. Element '" + parentLocalName | ||
| + "' must not have element children."); | ||
| } | ||
| validateAttributes(reader, localName); | ||
| elementStack.push(localName); | ||
| } | ||
|
|
||
| /** | ||
| * Reject non-whitespace text node children of {@code map} and {@code array} per F&O 3.1 §17.4.2. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s.a. |
||
| */ | ||
| private void validateTextInContext(final String text, final String parentLocalName) throws XPathException { | ||
| if (parentLocalName == null) { | ||
| return; | ||
| } | ||
| if (!"map".equals(parentLocalName) && !"array".equals(parentLocalName)) { | ||
| return; | ||
| } | ||
| if (!isXmlWhitespace(text)) { | ||
| throw new XPathException(this, ErrorCodes.FOJS0006, | ||
| "Invalid XML representation of JSON. Element '" + parentLocalName | ||
| + "' must not have non-whitespace text content."); | ||
| } | ||
| } | ||
|
|
||
| private static boolean isJsonElementName(final String name) { | ||
| return switch (name) { | ||
| case "map", "array", "string", "number", "boolean", "null" -> true; | ||
| default -> false; | ||
| }; | ||
| } | ||
|
|
||
| private static boolean isLeafElementName(final String name) { | ||
| return switch (name) { | ||
| case "string", "number", "boolean", "null" -> true; | ||
| default -> false; | ||
| }; | ||
| } | ||
|
|
||
| private static boolean isXmlWhitespace(final String text) { | ||
| for (int i = 0; i < text.length(); i++) { | ||
| final char c = text.charAt(i); | ||
| if (c != ' ' && c != '\t' && c != '\n' && c != '\r') { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Validate that the attributes on the current element conform to F&O 3.1 §17.4.2 (the schema for JSON). | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s.a. |
||
| * <p> | ||
| * Per the schema (Appendix C.2), the only allowed no-namespace attributes are: | ||
| * <ul> | ||
| * <li>{@code key} and {@code escaped-key} on any of the six elements (when child of map; allowed at top-level too)</li> | ||
| * <li>{@code escaped} on {@code string} only</li> | ||
| * </ul> | ||
| * Attributes in the {@code http://www.w3.org/2005/xpath-functions} namespace are disallowed | ||
| * ({@code anyAttribute namespace="##other"}); attributes in any other namespace are ignored. | ||
| * The {@code escaped} and {@code escaped-key} attributes must hold a valid {@code xs:boolean} value. | ||
| */ | ||
| private void validateAttributes(final XMLStreamReader reader, final String localName) throws XPathException { | ||
| for (int i = 0; i < reader.getAttributeCount(); i++) { | ||
| final String attrNs = reader.getAttributeNamespace(i); | ||
| final String attrName = reader.getAttributeLocalName(i); | ||
| if (Namespaces.XPATH_FUNCTIONS_NS.equals(attrNs)) { | ||
| throw new XPathException(this, ErrorCodes.FOJS0006, | ||
| "Invalid XML representation of JSON. Attribute '" + attrName | ||
| + "' must not be in the namespace '" + Namespaces.XPATH_FUNCTIONS_NS + "'."); | ||
| } | ||
| if (attrNs != null && !attrNs.isEmpty()) { | ||
| continue; | ||
| } | ||
| switch (attrName) { | ||
| case "key", "escaped-key" -> { | ||
| if ("escaped-key".equals(attrName) && !isValidXsBoolean(reader.getAttributeValue(i))) { | ||
| throw new XPathException(this, ErrorCodes.FOJS0006, | ||
| "Invalid XML representation of JSON. Attribute 'escaped-key' must have a valid xs:boolean value, but got '" | ||
| + reader.getAttributeValue(i) + "'."); | ||
| } | ||
| } | ||
| case "escaped" -> { | ||
| // Per W3C bug 29917 / qt3tests xml-to-json-065, 'escaped' is tolerated on | ||
| // non-string elements as a no-op; only the lexical value is enforced. | ||
| if (!isValidXsBoolean(reader.getAttributeValue(i))) { | ||
| throw new XPathException(this, ErrorCodes.FOJS0006, | ||
| "Invalid XML representation of JSON. Attribute 'escaped' must have a valid xs:boolean value, but got '" | ||
| + reader.getAttributeValue(i) + "'."); | ||
| } | ||
| } | ||
| default -> throw new XPathException(this, ErrorCodes.FOJS0006, | ||
| "Invalid XML representation of JSON. Attribute '" + attrName | ||
| + "' is not allowed on element '" + localName + "'."); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static boolean isValidXsBoolean(final String value) { | ||
| if (value == null) { | ||
| return false; | ||
| } | ||
| final String trimmed = value.trim(); | ||
| return "true".equals(trimmed) || "false".equals(trimmed) || "1".equals(trimmed) || "0".equals(trimmed); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Double encoded ampersand