papegaaij commented on code in PR #1876:
URL: https://github.com/apache/wicket/pull/1876#discussion_r4527798123
##########
wicket-core/src/main/java/org/apache/wicket/markup/parser/XmlPullParser.java:
##########
@@ -639,79 +679,113 @@ public String toString()
* @param tag
* @param tagText
* The text between tags
- * @return false in case of an error
+ * @return false if the tag text does not start with a tag name
* @throws ParseException
*/
private boolean parseTagText(final XmlTag tag, final String tagText)
throws ParseException
{
- // Get the length of the tagtext
- final int tagTextLength = tagText.length();
-
- // If we match tagname pattern
final TagNameParser tagnameParser = new TagNameParser(tagText);
- if (tagnameParser.matcher().lookingAt())
+ if (tagnameParser.matcher().lookingAt() == false)
{
- // Extract the tag from the pattern matcher
- tag.name = tagnameParser.getName();
- tag.namespace = tagnameParser.getNamespace();
-
- // Are we at the end? Then there are no attributes, so
we just
- // return the tag
- int pos = tagnameParser.matcher().end(0);
- if (pos == tagTextLength)
- {
- return true;
- }
-
- // Extract attributes
- final VariableAssignmentParser attributeParser = new
VariableAssignmentParser(tagText);
- while (attributeParser.matcher().find(pos))
- {
- // Get key and value using attribute pattern
- String value = attributeParser.getValue();
-
- // In case like <html xmlns:wicket> will the
value be null
- if (value == null)
- {
- value = "";
- }
+ return false;
+ }
- // Set new position to end of attribute
- pos = attributeParser.matcher().end(0);
+ tag.name = tagnameParser.getName();
+ tag.namespace = tagnameParser.getNamespace();
- // Chop off double quotes or single quotes
- if (value.startsWith("\"") ||
value.startsWith("\'"))
- {
- value = value.substring(1,
value.length() - 1);
- }
+ parseAttributes(tag, tagText, tagnameParser.matcher().end(0));
+ return true;
+ }
- // Trim trailing whitespace
- value = value.trim();
+ /**
+ * Reads the attributes the way the attribute states of the HTML
tokenizer do, see the class
+ * documentation.
+ *
+ * @param tag
+ * the tag to add the attributes to
+ * @param tagText
+ * The text between tags
+ * @param pos
+ * the index in the tag text right after the tag name
+ * @throws ParseException
+ */
+ private void parseAttributes(final XmlTag tag, final String tagText,
int pos)
+ throws ParseException
+ {
+ final int length = tagText.length();
+ while (true)
+ {
+ while ((pos < length) &&
+ (isWhitespace(tagText.charAt(pos)) ||
(tagText.charAt(pos) == '/')))
+ {
+ pos++;
+ }
+ if (pos == length)
+ {
+ return;
+ }
- // Unescape
- value =
Strings.unescapeMarkup(value).toString();
+ // The first character of a name may be '='
+ final int nameStart = pos++;
+ while ((pos < length) &&
(isWhitespace(tagText.charAt(pos)) == false) &&
+ (tagText.charAt(pos) != '/') &&
(tagText.charAt(pos) != '='))
+ {
+ pos++;
+ }
+ final String key = tagText.substring(nameStart, pos);
- // Get key
- final String key = attributeParser.getKey();
+ while ((pos < length) &&
isWhitespace(tagText.charAt(pos)))
+ {
+ pos++;
+ }
- // Put the attribute in the attributes hash
- if (null != tag.getAttributes().put(key, value))
+ String value = "";
+ if ((pos < length) && (tagText.charAt(pos) == '='))
+ {
+ pos++;
+ while ((pos < length) &&
isWhitespace(tagText.charAt(pos)))
{
- throw new ParseException("Same
attribute found twice: " + key +
- getLineAndColumnText(),
input.getPosition());
+ pos++;
}
- // The input has to match exactly (no left over
junk after
- // attributes)
- if (pos == tagTextLength)
+ if (pos < length)
{
- return true;
+ final char quote = tagText.charAt(pos);
+ if ((quote == '"') || (quote == '\''))
+ {
+ final int closingQuote =
tagText.indexOf(quote, pos + 1);
+ if (closingQuote == -1)
+ {
+ throw new
ParseException(
+
"Opening/closing quote not found for value of " + key +
+
getLineAndColumnText(),
+
input.getPosition());
+ }
+ value = tagText.substring(pos +
1, closingQuote);
+ pos = closingQuote + 1;
+ }
+ else
+ {
+ final int valueStart = pos;
+ while ((pos < length) &&
(isWhitespace(tagText.charAt(pos)) == false))
+ {
+ pos++;
+ }
+ value =
tagText.substring(valueStart, pos);
+ }
}
}
- return true;
+ if (null != tag.getAttributes().put(key,
Strings.unescapeMarkup(value).toString()))
Review Comment:
Keeping whitespace inside quotes is right, but it has a consequence an
upgrading application will notice that the description doesn't name: Wicket's
own attributes aren't trimmed any more either. `<span wicket:id="label ">` used
to render and now fails at render time with `Unable to find component with id
'label '`, and `<wicket:message key="foo ">` looks up `foo `. That should never
have worked, but templates that relied on it exist, and they only fail when the
page is opened.
Could the behaviour changes from the description go into
`migration-to-12.md` as a section, with this case spelled out? For example:
"Whitespace inside quotes is now part of an attribute value, including Wicket's
own attributes. `wicket:id="label "` refers to a component with the id `label
`, so a page whose markup has stray spaces in `wicket:id`, `wicket:message` or
`wicket:for` fails with 'Unable to find component with id'. Remove the spaces
from the markup."
##########
wicket-core-tests/src/test/java/org/apache/wicket/markup/parser/XmlPullParserTest.java:
##########
@@ -486,4 +493,126 @@ final void downlevelRevealedConditionalComments() throws
Exception
type = parser.next();
assertEquals(HttpTagType.CONDITIONAL_COMMENT_ENDIF, type);
}
-}
\ No newline at end of file
+
+ @Test
+ void closeTagWithoutNameIsRejected()
+ {
+ XmlPullParser parser = new XmlPullParser();
+
+ assertThrows(ParseException.class, () -> {
+ parser.parse("</>");
+ parser.next();
+ });
+ }
+
+ @Test
Review Comment:
The fuzz test guards against crashes but not against hangs: if a later
change leaves one of the parser's loops without progress for some input, the
build hangs instead of failing. A generous timeout turns that into a quick,
ordinary failure (it needs `import org.junit.jupiter.api.Timeout;`):
```suggestion
@Test
@Timeout(30)
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]