Skip to content

Commit 48972e8

Browse files
committed
fix (core): Introduce script to test for any new use of evil java.net.URL
1 parent 932dbc6 commit 48972e8

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

java/dev/enola/common/io/resource/ClasspathResource.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
/** {@link ReadableResource} on Java Classpath; e.g. <tt>classpath:/hello.txt</tt>. */
3030
public class ClasspathResource extends UrlResource {
3131

32+
// TODO Replace with ClassLoaderResource which uses ClassLoader.getResourceAsStream(String name)
33+
3234
// TODO Security: This *MUST* have a mandatory "allowed packages" sort of argument!
3335

3436
public static class Provider implements ResourceProvider {

java/dev/enola/common/io/resource/UrlResource.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
*/
4545
public class UrlResource extends BaseResource implements ReadableResource {
4646

47+
// TODO Remove, once there is a ClassLoaderResource
48+
4749
// TODO java.net.http <https://openjdk.org/groups/net/httpclient/intro.html> alternative!
4850

4951
public enum Scheme {

tools/evilurl/test.bash

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env bash
2+
# SPDX-License-Identifier: Apache-2.0
3+
#
4+
# Copyright 2023-2025 The Enola <https://enola.dev> Authors
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# https://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
18+
# set -euo pipefail
19+
20+
# This script detects any usages of java.net.URL in **/*.java.
21+
# See class dev.enola.common.io.iri.URL for background about why.
22+
#
23+
# It technically misses "implicit" usages of java.net.URL, such as:
24+
# Resources.getResource("test.txt").toURI(); but that's OK, for now.
25+
# TODO Write a better JVM level test; e.g. with ErrorProne or SpotBugs.
26+
27+
set -euo pipefail
28+
29+
allow_list=("java/dev/enola/common/io/resource/UrlResource.java"
30+
"java/dev/enola/common/io/resource/ClasspathResource.java")
31+
32+
found_files=$(find . -name "*.java" -print0 | xargs -0 grep -lE "(^|[^a-zA-Z0-9_.])java\.net\.URL($|[^a-zA-Z0-9_#}])" | while IFS= read -r file; do
33+
file_name=$(basename "$file")
34+
allow_path="${file//.\//}"
35+
36+
allowed=false
37+
for allowed_file in "${allow_list[@]}"; do
38+
if [[ "$file_name" == "$allowed_file" ]] || [[ "$allow_path" == "$allowed_file" ]]; then
39+
allowed=true
40+
break
41+
fi
42+
done
43+
44+
if "$allowed"; then
45+
continue
46+
fi
47+
48+
echo "$file"
49+
done)
50+
51+
if [[ -n "$found_files" ]]; then
52+
echo "Found use of forbidden bad java.net.URL in the following files, please fix:"
53+
echo "$found_files"
54+
exit 1
55+
else
56+
exit 0
57+
fi

0 commit comments

Comments
 (0)