Use class literals instead of getClass() for resource loading (CodeQL java/unsafe-get-resource)#224
Open
vharseko wants to merge 2 commits into
Conversation
… java/unsafe-get-resource) The idiom getClass().getResource() / getResourceAsStream() resolves resources relative to the runtime class, so a subclass in another package (or one loaded by a different class loader) could resolve a different resource than the author intended. CodeQL flags this as java/unsafe-get-resource. Replace getClass()/this.getClass() with an explicit class literal (<EnclosingClass>.class) at all 27 flagged sites across 18 files in commons/util, commons/audit, commons/selfservice/example, cassandra-embedded and commons/doc-maven-plugin. The declaring class is always the one whose package the resource lives in, so resolution is unchanged in every case here: most paths are absolute (package-independent), and the only subclassed type (XmlTransformer) receives absolute XSL paths from same-package subclasses. This resolves all 27 java/unsafe-get-resource alerts. Behaviour is otherwise unchanged. Verified: commons/util/util, commons/audit/core, commons/audit/servlet, commons/selfservice/example, cassandra-embedded and commons/doc-maven-plugin compile.
maximthomas
approved these changes
Jul 8, 2026
…afe-get-resource # Conflicts: # commons/audit/core/src/main/java/org/forgerock/audit/events/EventTopicsMetaDataBuilder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Resolves all 27 CodeQL
java/unsafe-get-resourcealerts by replacing thegetClass().getResource(...)/getClass().getResourceAsStream(...)idiom withan explicit class literal (
<EnclosingClass>.class.getResource(...)).getClass()returns the runtime class, so if a class is extended by a subclassin another package — or loaded by a different class loader — the resource path is
resolved from a different location than the author intended. Pinning resolution to
the class where the code is written (a class literal) is the CodeQL-recommended
fix.
Why this is behaviour-preserving here
/foo), which resolve from theclass-path root regardless of package, so the runtime class never mattered.
XmlTransformer, loads XSL via aconstructor argument; all of its subclasses live in the same package and
pass absolute paths (
/xslt/...), soXmlTransformer.classresolvesidentically.
Scope — 27 sites across 18 files
commons/util/utilXMLHandlercommons/audit/coreEventTopicsMetaDataBuildercommons/audit/servletAuditHttpApplicationcommons/selfservice/exampleExampleSelfServiceApplicationcassandra-embeddedEmbeddedServercommons/doc-maven-pluginAbstractDocbkxMojo,AsciidocToPdfMojo,AsciidocPreProcessMojo,XmlTransformer,SyntaxHighlighterCopier,BootstrapCopier,Redirect,Robots,Htaccess,IndexHtml,WebhelpPost,Html,BootstrapTesting
All six affected modules compile.