Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Whitelist builtin Java exceptions, rather than display any #1122

Open
Xyene opened this issue Mar 14, 2023 · 6 comments
Open

Whitelist builtin Java exceptions, rather than display any #1122

Xyene opened this issue Mar 14, 2023 · 6 comments

Comments

@Xyene
Copy link
Member

Xyene commented Mar 14, 2023

No description provided.

@Xyene
Copy link
Member Author

Xyene commented Jan 1, 2024

Overkill solution:

For Java 8, use ZipFile to list the contents of /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/rt.jar and filter anything ending with Exception|Error. Only a handful of builtin throwables don't follow this pattern (ThreadDeath is the only one I could quickly find).

For Java 9+ which moved classes to the JRT "filesystem", use something like the code below:

Main.java
import java.util.*;
import java.nio.file.*;
import java.net.*;

public class Main {
    private static void listFiles(Path dir) throws Exception {
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
            for (Path entry : stream) {
                if (Files.isDirectory(entry)) {
                    listFiles(entry);
                } else {
                    String demangledName = demangleClassName(entry);
                    if (demangledName.endsWith("Exception") || demangledName.endsWith("Error")) {
                        System.out.println(demangledName);
                    }
                }
            }
        }
    }

    private static String demangleClassName(Path filePath) {
        String fileName = filePath.toString();
        String withoutExtension = fileName.substring(0, fileName.lastIndexOf('.'));
        String withoutLeadingPackages = withoutExtension.replaceFirst("^/packages/.*?/.*?/", "");
        String demangledName = withoutLeadingPackages.replace('$', '.').replace('/', '.');
        return demangledName;
    }

    public static void main(String[] args) throws java.lang.Exception {
        FileSystem fs = FileSystems.getFileSystem(URI.create("jrt:/"));
        Path rootPath = fs.getPath("/");
        listFiles(rootPath);
    }
}

Maybe this should just live as a cronjob...? Not sure.

@kiritofeng
Copy link
Member

Should we try dynamically loading the classes and checking if they extend java.lang.Exception? 😛

@Xyene
Copy link
Member Author

Xyene commented Jan 1, 2024

If we're doing that then we should definitely do it as a cronjob, otherwise doing it on startup would take too long.

@kiritofeng
Copy link
Member

I'm in favour of doing it as a cronjob via Github Actions since this list will only change when Java updates, which is relatively infrequent (and not really worth the overhead on startup).

@Xyene
Copy link
Member Author

Xyene commented Jan 1, 2024

If by cronjob then we ought to do only the Java 9+ thing, since exceptions are unlikely to be removed, but may be added.

@Xyene
Copy link
Member Author

Xyene commented Jan 1, 2024

And I guess to make this more likely, we should only whitelist java.* exceptions.

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

2 participants