Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

support .jar file to be scanned in cli #259

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

maxandersen
Copy link

as mentioned in #257 it would be nice to be able to scan jars.

this pr enables it so you can do things like:

jar -jar forbiddenapis.jar -d /Users/manderse/.m2/repository/io/quarkus/quarkus-core/3.18.1/quarkus-core-3.18.1.jar --allowmissingclasses --bundledsignatures jdk-system-out

or in more compact form with jbang (if you done jbang app install forbiddenapis.jar

forbiddenapis -d jbang info jar io.quarkus:quarkus-core:3.18.1 --allowmissingclasses --bundledsignatures jdk-system-out

wdyt? I find this superuseful as i can check any maven artifact or even lib on disk without changing their build.

@uschindler
Copy link
Member

uschindler commented Feb 2, 2025

There is no need to extract the JAR to a temporary directory. The Checker API can directly consume the InputStream when you pass them one by one. It must just handle a JAR file like a directory. Of course the classfile pattern needs to be applied, too.

So the JAR file must be serially be passed to:

public void addClassToCheck(final InputStream in, String name) throws IOException {
final ClassReader reader;
try (final InputStream in_ = in) {
reader = AsmUtils.readAndPatchClass(in_);
} catch (IllegalArgumentException iae) {
throw new IllegalArgumentException(String.format(Locale.ENGLISH,
"The class file format of '%s' is too recent to be parsed by ASM.", name));
}
final ClassMetadata metadata = new ClassMetadata(reader, false, true);
classesToCheck.put(metadata.getBinaryClassName(), metadata);
}

I can take your PR as a base, but the implementation is not ideal, so I won't merge this as is. To make this work more modifications are needed.

@maxandersen
Copy link
Author

@uschindler I was wondering what could work but as i couldn't get any of my ides to import all the deps via ivy/build.xml I didn't get that far :)

if you can take it from here and make it work right that would be cool - if not let me know and I'll try do it.

@uschindler
Copy link
Member

No problem, will fix that. I have some ideas. Basically I would use a ZIPInputStream instead, so the ZIP file is read sequentially. I just have to figure out how to apply the glob-based filtering there, I think its somewhere in plexus-utils to apply it to strings.

@dweiss
Copy link
Contributor

dweiss commented Feb 2, 2025

If you stick to NIO then you can use Path as an abstraction for file system files (.class files) and .class files inside ZIP archives (by opening a zip FileSystem [1]).

There is usually some awkwardness in closing zip file systems but they work very well and are robust in my experience. For any downstream code, a Path (and streams) to a file inside a zip archive appears just as a normal filesystem path.

[1] https://docs.oracle.com/en/java/javase/22/docs/api/jdk.zipfs/module-summary.html

@dweiss
Copy link
Contributor

dweiss commented Feb 2, 2025

As for globbing, I've also used glob PathMatcher instances on those zip filesystems and they tend to work quite fine. Again: there are some oddities here and there but they do work, generally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants