Skip to content

Commit 09bf38d

Browse files
committed
fix(common): 修复日期时间戳解析溢出
1 parent db880bb commit 09bf38d

3 files changed

Lines changed: 54 additions & 1 deletion

File tree

weixin-java-common/src/main/java/me/chanjar/weixin/common/util/json/WxDateTypeAdapter.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,12 @@ public Date read(JsonReader in) throws IOException {
3535
in.nextNull();
3636
return null;
3737
case NUMBER:
38-
return new Date(in.nextInt() * 1000);
38+
long seconds = in.nextLong();
39+
try {
40+
return new Date(Math.multiplyExact(seconds, 1000L));
41+
} catch (ArithmeticException e) {
42+
throw new JsonParseException("Timestamp seconds out of range: " + seconds, e);
43+
}
3944
default:
4045
throw new JsonParseException("Expected NUMBER but was " + peek);
4146
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package me.chanjar.weixin.common.util.json;
2+
3+
import com.google.gson.JsonParseException;
4+
import org.testng.annotations.Test;
5+
6+
import java.io.IOException;
7+
import java.util.Date;
8+
9+
import static org.assertj.core.api.Assertions.assertThat;
10+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
11+
12+
/**
13+
* Tests for {@link WxDateTypeAdapter}.
14+
*/
15+
public class WxDateTypeAdapterTest {
16+
private final WxDateTypeAdapter adapter = new WxDateTypeAdapter();
17+
18+
@Test
19+
public void testReadTimestampAfter2038() throws IOException {
20+
Date date = this.adapter.fromJson("4102444800");
21+
22+
assertThat(date).isEqualTo(new Date(4102444800000L));
23+
}
24+
25+
@Test
26+
public void testReadRejectsPositiveMillisecondOverflow() {
27+
long overflowingSeconds = Long.MAX_VALUE / 1000 + 1;
28+
29+
assertThatThrownBy(() -> this.adapter.fromJson(Long.toString(overflowingSeconds)))
30+
.isInstanceOf(JsonParseException.class)
31+
.hasMessageContaining("out of range");
32+
}
33+
34+
@Test
35+
public void testReadRejectsNegativeMillisecondOverflow() {
36+
long overflowingSeconds = Long.MIN_VALUE / 1000 - 1;
37+
38+
assertThatThrownBy(() -> this.adapter.fromJson(Long.toString(overflowingSeconds)))
39+
.isInstanceOf(JsonParseException.class)
40+
.hasMessageContaining("out of range");
41+
}
42+
43+
@Test
44+
public void testWriteUsesSeconds() throws IOException {
45+
assertThat(this.adapter.toJson(new Date(4102444800123L))).isEqualTo("4102444800");
46+
}
47+
}

weixin-java-common/src/test/resources/testng.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<class name="me.chanjar.weixin.common.error.WxErrorTest"/>
88
<class name="me.chanjar.weixin.common.bean.WxMenuTest"/>
99
<class name="me.chanjar.weixin.common.util.crypto.WxCryptUtilTest"/>
10+
<class name="me.chanjar.weixin.common.util.json.WxDateTypeAdapterTest"/>
1011
<class name="me.chanjar.weixin.common.api.WxMessageInMemoryDuplicateCheckerTest"/>
1112
<class name="me.chanjar.weixin.common.session.SessionTest"/>
1213
</classes>

0 commit comments

Comments
 (0)