Skip to content

Commit 430d510

Browse files
committed
Merge pull request #29675 from mhalbritter
* gh-29675: Ignore invalid stream types when reading log update events Closes gh-29675
2 parents 43dc378 + 262db65 commit 430d510

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/LogUpdateEvent.java

+23-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,6 +22,8 @@
2222
import java.util.function.Consumer;
2323
import java.util.regex.Pattern;
2424

25+
import org.springframework.util.StreamUtils;
26+
2527
/**
2628
* An update event used to provide log updates.
2729
*
@@ -80,6 +82,16 @@ static void readAll(InputStream inputStream, Consumer<LogUpdateEvent> consumer)
8082
consumer.accept(event);
8183
}
8284
}
85+
catch (IllegalStateException ex) {
86+
// Parsing has failed, abort further parsing
87+
LogUpdateEvent abortedEvent = new LogUpdateEvent(StreamType.STD_ERR,
88+
ex.getMessage().getBytes(StandardCharsets.UTF_8));
89+
consumer.accept(abortedEvent);
90+
91+
// At this point, the inputStream is burned, consume it fully to prevent
92+
// further processing
93+
StreamUtils.drain(inputStream);
94+
}
8395
finally {
8496
inputStream.close();
8597
}
@@ -90,12 +102,21 @@ private static LogUpdateEvent read(InputStream inputStream) throws IOException {
90102
if (header == null) {
91103
return null;
92104
}
93-
StreamType streamType = StreamType.values()[header[0]];
105+
106+
// First byte denotes stream type. 0 = stdin, 1 = stdout, 2 = stderr
107+
byte streamTypeId = header[0];
108+
if (streamTypeId < 0 || streamTypeId >= StreamType.values().length) {
109+
throw new IllegalStateException("Stream type is out of bounds. Must be >= 0 and < "
110+
+ StreamType.values().length + ", but was " + streamTypeId + ". Will abort parsing.");
111+
}
112+
94113
long size = 0;
95114
for (int i = 0; i < 4; i++) {
96115
size = (size << 8) + (header[i + 4] & 0xff);
97116
}
98117
byte[] payload = read(inputStream, size);
118+
119+
StreamType streamType = StreamType.values()[streamTypeId];
99120
return new LogUpdateEvent(streamType, payload);
100121
}
101122

spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/test/java/org/springframework/boot/buildpack/platform/docker/LogUpdateEventTests.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -53,6 +53,14 @@ void readAllWhenAnsiStreamReturnsEvents() throws Exception {
5353
assertThat(events.get(2).toString()).isEqualTo(" OpenJDK JRE 11.0.5: Reusing cached layer");
5454
}
5555

56+
@Test
57+
void readSucceedsWhenStreamTypeIsInvalid() throws IOException {
58+
List<LogUpdateEvent> events = readAll("log-update-event-invalid-stream-type.stream");
59+
assertThat(events).hasSize(1);
60+
assertThat(events.get(0).toString())
61+
.isEqualTo("Stream type is out of bounds. Must be >= 0 and < 3, but was 3. Will abort parsing.");
62+
}
63+
5664
private List<LogUpdateEvent> readAll(String name) throws IOException {
5765
List<LogUpdateEvent> events = new ArrayList<>();
5866
try (InputStream inputStream = getClass().getResourceAsStream(name)) {

0 commit comments

Comments
 (0)