Skip to content

Commit 8ba01e3

Browse files
committed
fix(java): fix disallowed.txt check in windows (#2128)
## What does this PR do? - add windows ci for java21 - Fix sha256 check error on windows: ![Image](https://github.com/user-attachments/assets/b082fc7a-c929-42fc-a910-707f604251f4) ## Related issues Closes #2100 ## Does this PR introduce any user-facing change? <!-- If any user-facing interface changes, please [open an issue](https://github.com/apache/fury/issues/new/choose) describing the need to do so and update the document if necessary. --> - [ ] Does this PR introduce any public API change? - [ ] Does this PR introduce any binary protocol compatibility change? ## Benchmark <!-- When the PR has an impact on performance (if you don't know whether the PR will have an impact on performance, you can submit the PR first, and if it will have impact on performance, the code reviewer will explain it), be sure to attach a benchmark data here. -->
1 parent ea4c1bf commit 8ba01e3

File tree

4 files changed

+86
-9
lines changed

4 files changed

+86
-9
lines changed

.github/workflows/ci.yml

+21
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,26 @@ jobs:
9494
run: ./ci/run_ci.sh install_pyfury
9595
- name: Run CI with Maven
9696
run: ./ci/run_ci.sh java${{ matrix.java-version }}
97+
98+
java21_windows:
99+
name: Windows Java 21 CI
100+
runs-on: windows-2022
101+
env:
102+
MY_VAR: "PATH"
103+
strategy:
104+
matrix:
105+
java-version: ["21"]
106+
steps:
107+
- uses: actions/checkout@v4
108+
- name: Set up JDK ${{ matrix.java-version }}
109+
uses: actions/setup-java@v4
110+
with:
111+
java-version: ${{ matrix.java-version }}
112+
distribution: "temurin"
113+
- name: Run CI with Maven
114+
shell: bash
115+
run: ./ci/run_ci.sh windows_java21
116+
97117
graalvm:
98118
name: GraalVM CI
99119
runs-on: ubuntu-latest
@@ -113,6 +133,7 @@ jobs:
113133
with:
114134
python-version: 3.8
115135
- name: Build native image and run
136+
shell: bash
116137
run: ./ci/run_ci.sh graalvm_test
117138

118139
scala:

ci/run_ci.sh

+16
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,19 @@ jdk17_plus_tests() {
162162
echo "Executing latest_jdk_tests succeeds"
163163
}
164164

165+
windows_java21_test() {
166+
java -version
167+
echo "Executing fury java tests"
168+
cd "$ROOT/java"
169+
set +e
170+
mvn -T10 --batch-mode --no-transfer-progress test -Dtest=!org.apache.fury.CrossLanguageTest install -pl '!fury-format,!fury-testsuite'
171+
testcode=$?
172+
if [[ $testcode -ne 0 ]]; then
173+
exit $testcode
174+
fi
175+
echo "Executing fury java tests succeeds"
176+
}
177+
165178
case $1 in
166179
java8)
167180
echo "Executing fury java tests"
@@ -192,6 +205,9 @@ case $1 in
192205
java21)
193206
jdk17_plus_tests
194207
;;
208+
windows_java21)
209+
windows_java21_test
210+
;;
195211
integration_tests)
196212
echo "Install jdk"
197213
install_jdks

java/fury-core/src/main/java/org/apache/fury/resolver/DisallowedList.java

+14-9
Original file line numberDiff line numberDiff line change
@@ -27,32 +27,37 @@
2727
import java.security.NoSuchAlgorithmException;
2828
import java.util.Arrays;
2929
import java.util.Set;
30+
import java.util.TreeSet;
3031
import java.util.stream.Collectors;
3132
import org.apache.fury.exception.InsecureException;
3233

3334
/** A class to record which classes are not allowed for serialization. */
3435
class DisallowedList {
3536
private static final String DISALLOWED_LIST_TXT_PATH = "fury/disallowed.txt";
36-
// when disallowed.txt changed, update this hash by result of `sha256sum disallowed.txt`
37+
// When the disallowed.txt file is modified, update this hash using the following steps:
38+
// 1. Run the DisallowedListTest#testCalculateSHA256 test method
39+
// 2. Copy the output hash from the test result
40+
// 3. Replace the value of SHA256_HASH below with the new hash
41+
// 4. Rerun all tests to ensure everything is working correctly with the new hash
3742
private static final String SHA256_HASH =
38-
"30dc5228f52b02f61aff35a94d29ccd903abbf490d8231810c5e1c0321c56557";
43+
"53ecb405085d795d45ce033cd4f1055ae06247a5dbaa617ecd20e4aac4303f60";
3944
private static final Set<String> DEFAULT_DISALLOWED_LIST_SET;
4045

4146
static {
4247
try (InputStream is =
4348
DisallowedList.class.getClassLoader().getResourceAsStream(DISALLOWED_LIST_TXT_PATH)) {
4449
if (is != null) {
4550
byte[] fileBytes = readAllBytes(is);
46-
String calculatedHash = calculateSHA256(fileBytes);
47-
if (!SHA256_HASH.equals(calculatedHash)) {
48-
// add a check to avoid some malicious overwrite disallowed.txt
49-
throw new SecurityException("Disallowed list has been tampered");
50-
}
5151
DEFAULT_DISALLOWED_LIST_SET =
5252
Arrays.stream(
5353
new String(fileBytes, StandardCharsets.UTF_8).split(System.lineSeparator()))
5454
.filter(line -> !line.isEmpty() && !line.startsWith("#"))
5555
.collect(Collectors.toSet());
56+
String calculatedHash = calculateSHA256(new TreeSet<>(DEFAULT_DISALLOWED_LIST_SET));
57+
if (!SHA256_HASH.equals(calculatedHash)) {
58+
// add a check to avoid some malicious overwrite disallowed.txt
59+
throw new SecurityException("Disallowed list has been tampered");
60+
}
5661
} else {
5762
throw new IllegalStateException(
5863
String.format("Read disallowed list %s failed", DISALLOWED_LIST_TXT_PATH));
@@ -74,10 +79,10 @@ private static byte[] readAllBytes(InputStream inputStream) throws IOException {
7479
return buffer.toByteArray();
7580
}
7681

77-
private static String calculateSHA256(byte[] input) {
82+
private static String calculateSHA256(TreeSet<String> set) {
7883
try {
7984
MessageDigest digest = MessageDigest.getInstance("SHA-256");
80-
byte[] hashBytes = digest.digest(input);
85+
byte[] hashBytes = digest.digest(String.join(",", set).getBytes(StandardCharsets.UTF_8));
8186
StringBuilder hexString = new StringBuilder();
8287
for (byte b : hashBytes) {
8388
String hex = Integer.toHexString(0xff & b);

java/fury-core/src/test/java/org/apache/fury/resolver/DisallowedListTest.java

+35
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,15 @@
1919

2020
package org.apache.fury.resolver;
2121

22+
import java.io.BufferedReader;
23+
import java.io.InputStream;
24+
import java.io.InputStreamReader;
25+
import java.nio.charset.StandardCharsets;
2226
import java.rmi.server.UnicastRemoteObject;
27+
import java.security.MessageDigest;
28+
import java.util.Set;
29+
import java.util.TreeSet;
30+
import java.util.stream.Collectors;
2331
import org.apache.fury.Fury;
2432
import org.apache.fury.FuryTestBase;
2533
import org.apache.fury.config.Language;
@@ -30,6 +38,33 @@
3038

3139
public class DisallowedListTest extends FuryTestBase {
3240

41+
@Test
42+
public void testCalculateSHA256() throws Exception {
43+
try (InputStream is =
44+
DisallowedList.class.getClassLoader().getResourceAsStream("fury/disallowed.txt")) {
45+
assert is != null;
46+
Set<String> set =
47+
new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))
48+
.lines()
49+
.filter(line -> !line.isEmpty() && !line.startsWith("#"))
50+
.collect(Collectors.toSet());
51+
MessageDigest digest = MessageDigest.getInstance("SHA-256");
52+
byte[] hashBytes =
53+
digest.digest(String.join(",", new TreeSet<>(set)).getBytes(StandardCharsets.UTF_8));
54+
StringBuilder hexString = new StringBuilder();
55+
for (byte b : hashBytes) {
56+
String hex = Integer.toHexString(0xff & b);
57+
if (hex.length() == 1) {
58+
hexString.append('0');
59+
}
60+
hexString.append(hex);
61+
}
62+
System.out.println("SHA256 HASH for disallowed.txt is " + hexString);
63+
Assert.assertEquals(
64+
hexString.toString(), "53ecb405085d795d45ce033cd4f1055ae06247a5dbaa617ecd20e4aac4303f60");
65+
}
66+
}
67+
3368
@Test
3469
public void testCheckHitDisallowedList() {
3570
// Hit the disallowed list.

0 commit comments

Comments
 (0)