Skip to content

Commit

Permalink
[JENKINS-61452] Tolerate corrupt Base64 in `PlainTextConsoleOutputStr…
Browse files Browse the repository at this point in the history
…eam` (#8378)
  • Loading branch information
jglick committed Aug 19, 2023
1 parent 7ad6a02 commit 7f3fa04
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.lang.StringEscapeUtils;

/**
* Filters out console notes.
Expand All @@ -36,6 +40,8 @@
*/
public class PlainTextConsoleOutputStream extends LineTransformationOutputStream.Delegating {

private static final Logger LOGGER = Logger.getLogger(PlainTextConsoleOutputStream.class.getName());

/**
*
*/
Expand Down Expand Up @@ -64,7 +70,11 @@ protected void eol(byte[] in, int sz) throws IOException {
int rest = sz - next;
ByteArrayInputStream b = new ByteArrayInputStream(in, next, rest);

ConsoleNote.skip(new DataInputStream(b));
try {
ConsoleNote.skip(new DataInputStream(b));
} catch (IOException x) {
LOGGER.log(Level.FINE, "Failed to skip annotation from \"" + StringEscapeUtils.escapeJava(new String(in, next, rest, Charset.defaultCharset())) + "\"", x);
}

int bytesUsed = rest - b.available(); // bytes consumed by annotations
written += bytesUsed;
Expand Down
26 changes: 25 additions & 1 deletion test/src/test/java/hudson/console/AnnotatedLargeTextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.matchesRegex;
import static org.junit.Assert.assertEquals;

import hudson.MarkupText;
Expand All @@ -52,7 +53,7 @@ public class AnnotatedLargeTextTest {
public static JenkinsRule r = new JenkinsRule();

@Rule
public LoggerRule logging = new LoggerRule().record(ConsoleAnnotationOutputStream.class, Level.FINE).capture(100);
public LoggerRule logging = new LoggerRule().record(ConsoleAnnotationOutputStream.class, Level.FINE).record(PlainTextConsoleOutputStream.class, Level.FINE).capture(100);

@Test
public void smokes() throws Exception {
Expand Down Expand Up @@ -138,6 +139,29 @@ public void badMac() throws Exception {
+ "AAA\\u001B[0myour home.\\n\"")); // TODO assert that this is IOException: MAC mismatch
}

@Issue("JENKINS-61452")
@Test
public void corruptedNote() throws Exception {
ByteBuffer buf = new ByteBuffer();
PrintStream ps = new PrintStream(buf, true, StandardCharsets.UTF_8);
ps.print("Some text.\n");
ps.print("Go back to " + TestNote.encodeTo("/root", "your home") + ".\n");
ps.print("More text.\n");
String original = buf.toString();
String corrupted = original.replace("+", "\u0000");
buf = new ByteBuffer();
buf.write(corrupted.getBytes());
AnnotatedLargeText<Void> text = new AnnotatedLargeText<>(buf, StandardCharsets.UTF_8, true, null);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
text.writeLogTo(0, baos);
assertThat(baos.toString(StandardCharsets.UTF_8), matchesRegex("Some text[.]\nGo back to .*your home[.]\nMore text[.]\n"));
assertThat(logging.getMessages(), hasItem(matchesRegex("Failed to skip annotation from .+")));
StringWriter w = new StringWriter();
text.writeHtmlTo(0, w);
assertThat(w.toString(), matchesRegex("Some text[.]\nGo back to .*your home[.]\nMore text[.]\n"));
assertThat(logging.getMessages(), hasItem(matchesRegex("Failed to resurrect annotation from .+")));
}

/** Simplified version of {@link HyperlinkNote}. */
static class TestNote extends ConsoleNote<Void> {
private final String url;
Expand Down

0 comments on commit 7f3fa04

Please sign in to comment.