Skip to content

Commit

Permalink
Fix detection of invalid spaces in prolog (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet authored Jun 23, 2023
1 parent 27d6127 commit d227a49
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ limitations under the License.
<version>5.9.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>2.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/org/codehaus/plexus/util/xml/pull/MXParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2528,9 +2528,11 @@ private void parsePI() throws XmlPullParserException, IOException {
if ((buf[piTargetStart] == 'x' || buf[piTargetStart] == 'X')
&& (buf[piTargetStart + 1] == 'm' || buf[piTargetStart + 1] == 'M')
&& (buf[piTargetStart + 2] == 'l' || buf[piTargetStart + 2] == 'L')) {
if (piTargetStart > 3) { // <?xml is allowed as first characters in input ...
if (piTargetStart > 2) { // <?xml is allowed as first characters in input ...
throw new XmlPullParserException(
"processing instruction can not have PITarget with reserved xml name",
eventType == 0
? "XMLDecl is only allowed as first characters in input"
: "processing instruction can not have PITarget with reserved xml name",
this,
null);
} else {
Expand Down
22 changes: 22 additions & 0 deletions src/test/java/org/codehaus/plexus/util/xml/pull/MXParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.xml.XmlStreamReader;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.*;

/**
Expand Down Expand Up @@ -1473,4 +1477,22 @@ public void testProcessingInstructionTokenizeAfterXMLDeclAndBeforeFirstTag() thr
assertEquals(XmlPullParser.END_TAG, parser.nextToken());
assertEquals(XmlPullParser.END_DOCUMENT, parser.nextToken());
}

@ParameterizedTest
@ValueSource(strings = {" ", "\n", "\r", "\r\n", " ", "\n "})
void testBlankAtBeginning(String ws) throws XmlPullParserException, IOException {
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><test>nnn</test>";

MXParser parser = new MXParser();
parser.setInput(new StringReader(ws + xml));
assertThat(
assertThrows(XmlPullParserException.class, parser::next).getMessage(),
containsString("XMLDecl is only allowed as first characters in input"));

parser.setInput(new StringReader(ws + xml));
assertEquals(XmlPullParser.IGNORABLE_WHITESPACE, parser.nextToken());
assertThat(
assertThrows(XmlPullParserException.class, parser::nextToken).getMessage(),
containsString("processing instruction can not have PITarget with reserved xml name"));
}
}

0 comments on commit d227a49

Please sign in to comment.