Skip to content

Commit 34af023

Browse files
committed
Ensure that ZipHeaderPeekIS doesn't miss bytes on partial header read
Closes gh-13032
1 parent b17a00d commit 34af023

File tree

2 files changed

+169
-7
lines changed

2 files changed

+169
-7
lines changed

spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/JarWriter.java

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -315,26 +315,34 @@ public void write(OutputStream outputStream) throws IOException {
315315
/**
316316
* {@link InputStream} that can peek ahead at zip header bytes.
317317
*/
318-
private static class ZipHeaderPeekInputStream extends FilterInputStream {
318+
static class ZipHeaderPeekInputStream extends FilterInputStream {
319319

320320
private static final byte[] ZIP_HEADER = new byte[] { 0x50, 0x4b, 0x03, 0x04 };
321321

322322
private final byte[] header;
323323

324+
private final int headerLength;
325+
326+
private int position;
327+
324328
private ByteArrayInputStream headerStream;
325329

326330
protected ZipHeaderPeekInputStream(InputStream in) throws IOException {
327331
super(in);
328332
this.header = new byte[4];
329-
int len = in.read(this.header);
330-
this.headerStream = new ByteArrayInputStream(this.header, 0, len);
333+
this.headerLength = in.read(this.header);
334+
this.headerStream = new ByteArrayInputStream(this.header, 0,
335+
this.headerLength);
331336
}
332337

333338
@Override
334339
public int read() throws IOException {
335340
int read = (this.headerStream != null ? this.headerStream.read() : -1);
336341
if (read != -1) {
337-
this.headerStream = null;
342+
this.position++;
343+
if (this.position >= this.headerLength) {
344+
this.headerStream = null;
345+
}
338346
return read;
339347
}
340348
return super.read();
@@ -349,11 +357,20 @@ public int read(byte[] b) throws IOException {
349357
public int read(byte[] b, int off, int len) throws IOException {
350358
int read = (this.headerStream != null ? this.headerStream.read(b, off, len)
351359
: -1);
352-
if (read != -1) {
360+
if (read > 0) {
361+
this.position += read;
362+
}
363+
else {
364+
read = 0;
365+
}
366+
if (read < len) {
367+
read += super.read(b, off + read, len - read);
368+
this.position += read;
369+
}
370+
if (this.position >= this.headerLength) {
353371
this.headerStream = null;
354-
return read;
355372
}
356-
return super.read(b, off, len);
373+
return read;
357374
}
358375

359376
public boolean hasZipHeader() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/*
2+
* Copyright 2012-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.loader.tools;
18+
19+
/**
20+
* Tests for {@link ZipHeaderPeekInputStream}.
21+
*
22+
* @author Andy Wilkinson
23+
*/
24+
import java.io.ByteArrayInputStream;
25+
import java.io.IOException;
26+
27+
import org.junit.Test;
28+
29+
import org.springframework.boot.loader.tools.JarWriter.ZipHeaderPeekInputStream;
30+
31+
import static org.assertj.core.api.Assertions.assertThat;
32+
33+
public class ZipHeaderPeekInputStreamTests {
34+
35+
@Test
36+
public void hasZipHeaderReturnsTrueWhenStreamStartsWithZipHeader()
37+
throws IOException {
38+
ZipHeaderPeekInputStream in = null;
39+
try {
40+
in = new ZipHeaderPeekInputStream(new ByteArrayInputStream(
41+
new byte[] { 0x50, 0x4b, 0x03, 0x04, 5, 6 }));
42+
assertThat(in.hasZipHeader()).isTrue();
43+
}
44+
finally {
45+
if (in != null) {
46+
in.close();
47+
}
48+
}
49+
}
50+
51+
@Test
52+
public void hasZipHeaderReturnsFalseWheStreamDoesNotStartWithZipHeader()
53+
throws IOException {
54+
ZipHeaderPeekInputStream in = null;
55+
try {
56+
in = new ZipHeaderPeekInputStream(
57+
new ByteArrayInputStream(new byte[] { 0, 1, 2, 3, 4, 5 }));
58+
assertThat(in.hasZipHeader()).isFalse();
59+
}
60+
finally {
61+
if (in != null) {
62+
in.close();
63+
}
64+
}
65+
}
66+
67+
@Test
68+
public void readIndividualBytes() throws IOException {
69+
ZipHeaderPeekInputStream in = null;
70+
try {
71+
in = new ZipHeaderPeekInputStream(
72+
new ByteArrayInputStream(new byte[] { 0, 1, 2, 3, 4, 5 }));
73+
assertThat(in.read()).isEqualTo(0);
74+
assertThat(in.read()).isEqualTo(1);
75+
assertThat(in.read()).isEqualTo(2);
76+
assertThat(in.read()).isEqualTo(3);
77+
assertThat(in.read()).isEqualTo(4);
78+
assertThat(in.read()).isEqualTo(5);
79+
}
80+
finally {
81+
if (in != null) {
82+
in.close();
83+
}
84+
}
85+
}
86+
87+
@Test
88+
public void readMultipleBytes() throws IOException {
89+
ZipHeaderPeekInputStream in = null;
90+
try {
91+
in = new ZipHeaderPeekInputStream(
92+
new ByteArrayInputStream(new byte[] { 0, 1, 2, 3, 4, 5 }));
93+
byte[] bytes = new byte[3];
94+
assertThat(in.read(bytes)).isEqualTo(3);
95+
assertThat(bytes).containsExactly(0, 1, 2);
96+
assertThat(in.read(bytes)).isEqualTo(3);
97+
assertThat(bytes).containsExactly(3, 4, 5);
98+
assertThat(in.read(bytes)).isEqualTo(-1);
99+
}
100+
finally {
101+
if (in != null) {
102+
in.close();
103+
}
104+
}
105+
}
106+
107+
@Test
108+
public void readingMoreThanEntireStreamReadsToEndOfStream() throws IOException {
109+
ZipHeaderPeekInputStream in = null;
110+
try {
111+
in = new ZipHeaderPeekInputStream(
112+
new ByteArrayInputStream(new byte[] { 0, 1, 2, 3, 4, 5 }));
113+
byte[] bytes = new byte[8];
114+
assertThat(in.read(bytes)).isEqualTo(6);
115+
assertThat(bytes).containsExactly(0, 1, 2, 3, 4, 5, 0, 0);
116+
assertThat(in.read(bytes)).isEqualTo(-1);
117+
}
118+
finally {
119+
if (in != null) {
120+
in.close();
121+
}
122+
}
123+
}
124+
125+
@Test
126+
public void readOfSomeOfTheHeaderThenMoreThanEntireStreamReadsToEndOfStream()
127+
throws IOException {
128+
ZipHeaderPeekInputStream in = null;
129+
try {
130+
in = new ZipHeaderPeekInputStream(
131+
new ByteArrayInputStream(new byte[] { 0, 1, 2, 3, 4, 5 }));
132+
byte[] bytes = new byte[8];
133+
assertThat(in.read(bytes, 0, 3)).isEqualTo(3);
134+
assertThat(bytes).containsExactly(0, 1, 2, 0, 0, 0, 0, 0);
135+
assertThat(in.read(bytes, 3, 5)).isEqualTo(3);
136+
assertThat(bytes).containsExactly(0, 1, 2, 3, 4, 5, 0, 0);
137+
}
138+
finally {
139+
if (in != null) {
140+
in.close();
141+
}
142+
}
143+
}
144+
145+
}

0 commit comments

Comments
 (0)