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

[JENKINS-71139] Fail fast when serializing invalid XML 1.1 data #7875

Merged
merged 2 commits into from
Apr 26, 2023
Merged
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
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/util/XStream2.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ private static class StaxDriver extends StandardStaxDriver {

@Override
public HierarchicalStreamWriter createWriter(Writer out) {
return new PrettyPrintWriter(out, getNameCoder());
return new PrettyPrintWriter(out, PrettyPrintWriter.XML_1_1, getNameCoder());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Causes JENKINS-71182

}

@Override
Expand Down
50 changes: 50 additions & 0 deletions core/src/test/java/hudson/util/XStream2Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
Expand All @@ -38,10 +40,15 @@
import com.thoughtworks.xstream.XStreamException;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.mapper.CannotResolveClassException;
import hudson.Functions;
import hudson.model.Result;
import hudson.model.Run;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -590,4 +597,47 @@ public void testEmojiEscaped() throws Exception {
}
assertEquals("Fox 🦊", bar.s);
}

@Issue("JENKINS-71139")
@Test
public void nullsWithoutEncodingDeclaration() throws Exception {
Bar b = new Bar();
String text = "x\u0000y";
b.s = text;
StringWriter w = new StringWriter();
XStream2 xs = new XStream2();
try {
xs.toXML(b, w);
} catch (RuntimeException x) {
assertThat("cause is com.thoughtworks.xstream.io.StreamException: Invalid character 0x0 in XML stream", Functions.printThrowable(x), containsString("0x0"));
return; // not supported to read either
}
String xml = w.toString();
assertThat(xml, not(containsString("version=\"1.1\"")));
System.out.println(xml);
b = (Bar) xs.fromXML(xml);
assertEquals(text, b.s);
}

@Issue("JENKINS-71139")
@Test
public void nullsWithEncodingDeclaration() throws Exception {
Bar b = new Bar();
String text = "x\u0000y";
b.s = text;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
XStream2 xs = new XStream2();
try {
xs.toXMLUTF8(b, baos);
} catch (RuntimeException x) {
assertThat("cause is com.thoughtworks.xstream.io.StreamException: Invalid character 0x0 in XML stream", Functions.printThrowable(x), containsString("0x0"));
return; // not supported to read either
}
String xml = baos.toString(StandardCharsets.UTF_8);
System.out.println(xml);
assertThat(xml, containsString("version=\"1.1\""));
b = (Bar) xs.fromXML(new ByteArrayInputStream(baos.toByteArray()));
assertEquals(text, b.s);
}

}