Skip to content

Commit 998d59b

Browse files
committed
Ignore system timezone when applying outputTimestamp to entries
Update `JarWriter` so that entry times are set with the default TimeZone offset removed. The Javadoc for `ZipEntry.setTime` states: The file entry is "encoded in standard `MS-DOS date and time format`. The default TimeZone is used to convert the epoch time to the MS-DOS data and time. Removing the offset from our UTC time before calling `entry.setTime()` ensures that we get consistent bytes in the zip file when the output stream reapplies the offset during write. Fixes gh-34424
1 parent 29a16a6 commit 998d59b

File tree

6 files changed

+149
-7
lines changed

6 files changed

+149
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2012-2023 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+
* https://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+
import java.nio.file.attribute.FileTime;
20+
import java.util.TimeZone;
21+
import java.util.zip.ZipEntry;
22+
23+
/**
24+
* Utility class that can be used change a UTC time based on the
25+
* {@link java.util.TimeZone#getDefault() default TimeZone}. This is required because
26+
* {@link ZipEntry#setTime(long)} expects times in the default timezone and not UTC.
27+
*
28+
* @author Phillip Webb
29+
*/
30+
class DefaultTimeZoneOffset {
31+
32+
static final DefaultTimeZoneOffset INSTANCE = new DefaultTimeZoneOffset(TimeZone.getDefault());
33+
34+
private final TimeZone defaultTimeZone;
35+
36+
DefaultTimeZoneOffset(TimeZone defaultTimeZone) {
37+
this.defaultTimeZone = defaultTimeZone;
38+
}
39+
40+
/**
41+
* Remove the default offset from the given time.
42+
* @param time the time to remove the default offset from
43+
* @return the time with the default offset removed
44+
*/
45+
FileTime removeFrom(FileTime time) {
46+
return FileTime.fromMillis(removeFrom(time.toMillis()));
47+
}
48+
49+
/**
50+
* Remove the default offset from the given time.
51+
* @param time the time to remove the default offset from
52+
* @return the time with the default offset removed
53+
*/
54+
long removeFrom(long time) {
55+
return time - this.defaultTimeZone.getOffset(time);
56+
}
57+
58+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2023 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.
@@ -89,7 +89,7 @@ public JarWriter(File file, LaunchScript launchScript, FileTime lastModifiedTime
8989
protected void writeToArchive(ZipEntry entry, EntryWriter entryWriter) throws IOException {
9090
JarArchiveEntry jarEntry = asJarArchiveEntry(entry);
9191
if (this.lastModifiedTime != null) {
92-
jarEntry.setLastModifiedTime(this.lastModifiedTime);
92+
jarEntry.setTime(DefaultTimeZoneOffset.INSTANCE.removeFrom(this.lastModifiedTime).toMillis());
9393
}
9494
this.jarOutputStream.putArchiveEntry(jarEntry);
9595
if (entryWriter != null) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2012-2023 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+
* https://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+
import java.time.OffsetDateTime;
20+
import java.time.ZoneOffset;
21+
import java.util.Calendar;
22+
import java.util.TimeZone;
23+
24+
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
25+
import org.junit.jupiter.api.Test;
26+
27+
import static org.assertj.core.api.Assertions.assertThat;
28+
29+
/**
30+
* Tests for {@link DefaultTimeZoneOffset}
31+
*
32+
* @author Phillip Webb
33+
*/
34+
class DefaultTimeZoneOffsetTests {
35+
36+
// gh-34424
37+
38+
@Test
39+
void removeFromWithLongInDifferentTimeZonesReturnsSameValue() {
40+
long time = OffsetDateTime.of(2000, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC).toInstant().toEpochMilli();
41+
TimeZone timeZone1 = TimeZone.getTimeZone("GMT");
42+
TimeZone timeZone2 = TimeZone.getTimeZone("GMT+8");
43+
TimeZone timeZone3 = TimeZone.getTimeZone("GMT-8");
44+
long result1 = new DefaultTimeZoneOffset(timeZone1).removeFrom(time);
45+
long result2 = new DefaultTimeZoneOffset(timeZone2).removeFrom(time);
46+
long result3 = new DefaultTimeZoneOffset(timeZone3).removeFrom(time);
47+
long dosTime1 = toDosTime(Calendar.getInstance(timeZone1), result1);
48+
long dosTime2 = toDosTime(Calendar.getInstance(timeZone2), result2);
49+
long dosTime3 = toDosTime(Calendar.getInstance(timeZone3), result3);
50+
assertThat(dosTime1).isEqualTo(dosTime2).isEqualTo(dosTime3);
51+
}
52+
53+
@Test
54+
void removeFromWithFileTimeReturnsFileTime() {
55+
long time = OffsetDateTime.of(2000, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC).toInstant().toEpochMilli();
56+
long result = new DefaultTimeZoneOffset(TimeZone.getTimeZone("GMT+8")).removeFrom(time);
57+
assertThat(result).isNotEqualTo(time).isEqualTo(946656000000L);
58+
}
59+
60+
/**
61+
* Identical functionality to package-private
62+
* org.apache.commons.compress.archivers.zip.ZipUtil.toDosTime(Calendar, long, byte[],
63+
* int) method used by {@link ZipArchiveOutputStream} to convert times.
64+
* @param calendar the source calendar
65+
* @param time the time to convert
66+
* @return the DOS time
67+
*/
68+
private long toDosTime(Calendar calendar, long time) {
69+
calendar.setTimeInMillis(time);
70+
final int year = calendar.get(Calendar.YEAR);
71+
final int month = calendar.get(Calendar.MONTH) + 1;
72+
return ((year - 1980) << 25) | (month << 21) | (calendar.get(Calendar.DAY_OF_MONTH) << 16)
73+
| (calendar.get(Calendar.HOUR_OF_DAY) << 11) | (calendar.get(Calendar.MINUTE) << 5)
74+
| (calendar.get(Calendar.SECOND) >> 1);
75+
}
76+
77+
}

spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/RepackagerTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,9 @@ void allEntriesUseProvidedTimestamp() throws IOException {
200200
Repackager repackager = createRepackager(this.testJarFile.getFile(), true);
201201
long timestamp = OffsetDateTime.of(2000, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC).toInstant().toEpochMilli();
202202
repackager.repackage(this.destination, NO_LIBRARIES, null, FileTime.fromMillis(timestamp));
203+
long offsetTimestamp = DefaultTimeZoneOffset.INSTANCE.removeFrom(timestamp);
203204
for (ZipArchiveEntry entry : getAllPackagedEntries()) {
204-
assertThat(entry.getTime()).isEqualTo(timestamp);
205+
assertThat(entry.getTime()).isEqualTo(offsetTimestamp);
205206
}
206207
}
207208

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/java/org/springframework/boot/maven/JarIntegrationTests.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Arrays;
2222
import java.util.List;
2323
import java.util.Map;
24+
import java.util.TimeZone;
2425
import java.util.concurrent.atomic.AtomicReference;
2526
import java.util.jar.JarFile;
2627
import java.util.stream.Collectors;
@@ -401,10 +402,12 @@ private String buildJarWithOutputTimestamp(MavenBuild mavenBuild) {
401402
mavenBuild.project("jar-output-timestamp").execute((project) -> {
402403
File repackaged = new File(project, "target/jar-output-timestamp-0.0.1.BUILD-SNAPSHOT.jar");
403404
assertThat(repackaged).isFile();
404-
assertThat(repackaged.lastModified()).isEqualTo(1584352800000L);
405+
long expectedModified = 1584352800000L;
406+
long offsetExpectedModified = expectedModified - TimeZone.getDefault().getOffset(expectedModified);
407+
assertThat(repackaged.lastModified()).isEqualTo(expectedModified);
405408
try (JarFile jar = new JarFile(repackaged)) {
406409
List<String> unreproducibleEntries = jar.stream()
407-
.filter((entry) -> entry.getLastModifiedTime().toMillis() != 1584352800000L)
410+
.filter((entry) -> entry.getLastModifiedTime().toMillis() != offsetExpectedModified)
408411
.map((entry) -> entry.getName() + ": " + entry.getLastModifiedTime())
409412
.collect(Collectors.toList());
410413
assertThat(unreproducibleEntries).isEmpty();

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/java/org/springframework/boot/maven/WarIntegrationTests.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.Arrays;
2323
import java.util.List;
2424
import java.util.Map;
25+
import java.util.TimeZone;
2526
import java.util.concurrent.atomic.AtomicReference;
2627
import java.util.jar.JarFile;
2728
import java.util.stream.Collectors;
@@ -96,10 +97,12 @@ private String buildWarWithOutputTimestamp(MavenBuild mavenBuild) {
9697
mavenBuild.project("war-output-timestamp").execute((project) -> {
9798
File repackaged = new File(project, "target/war-output-timestamp-0.0.1.BUILD-SNAPSHOT.war");
9899
assertThat(repackaged).isFile();
99-
assertThat(repackaged.lastModified()).isEqualTo(1584352800000L);
100+
long expectedModified = 1584352800000L;
101+
assertThat(repackaged.lastModified()).isEqualTo(expectedModified);
102+
long offsetExpectedModified = expectedModified - TimeZone.getDefault().getOffset(expectedModified);
100103
try (JarFile jar = new JarFile(repackaged)) {
101104
List<String> unreproducibleEntries = jar.stream()
102-
.filter((entry) -> entry.getLastModifiedTime().toMillis() != 1584352800000L)
105+
.filter((entry) -> entry.getLastModifiedTime().toMillis() != offsetExpectedModified)
103106
.map((entry) -> entry.getName() + ": " + entry.getLastModifiedTime())
104107
.collect(Collectors.toList());
105108
assertThat(unreproducibleEntries).isEmpty();

0 commit comments

Comments
 (0)