SONARJAVA-6783: Implement rule S9345 Classes with throwing constructors should be protected against Finalizer attacks - #5981
Conversation
This comment has been minimized.
This comment has been minimized.
|
❌ Ruling needs updating. A fix PR has been created: #5982 Please review and merge it into your branch. |
Ruling Diff SummaryDetected changes in 5 rule files: 0 issues removed, 149 issues added. S9345 (
|
|
❌ Ruling needs updating. A fix PR has been created: #5983 Please review and merge it into your branch. |
…rs should be protected against Finalizer attacks Detect non-final, non-abstract classes whose non-private constructors can throw exceptions (via throws clause or throw statements in the body), making them vulnerable to Finalizer attacks through malicious subclasses. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
🤖 Generated with GitHub Actions
…houtSemantic test - Make inner classes static in FinalizerAttackCheckSample to fix "non-static variable this cannot be referenced from a static context" compilation error caused by FactoryService's static factory method - Add S9345 placeholder to Sonar way quality profile - Add withoutSemantic test since the check only uses syntactic analysis Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
d5edb3f to
ea06a0f
Compare
|
❌ Ruling needs updating. A fix PR has been created: #5983 Please review and merge it into your branch. |
…ass as secondary The main issue location is now on the throwing constructor (primary) with the class declaration as a secondary location, instead of the other way around. Each vulnerable constructor gets its own issue. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…date rulings - Add null check for classTree.simpleName() to prevent NPE on anonymous classes - Skip sealed classes since they cannot be subclassed by attackers - Add sealed class test case - Add ruling results for eclipse-jetty-similar-to-main Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
❌ Ruling needs updating. A fix PR has been created: #5983 Please review and merge it into your branch. |
|
❌ Ruling needs updating. A fix PR has been created: #6000 Please review and merge it into your branch. |
nathsou
left a comment
There was a problem hiding this comment.
Inline findings for the requested changes.
- Remove abstract class exemption (abstract classes can be subclassed) - Refine sealed class logic: only skip sealed classes whose permitted subclasses are all final/sealed (flag those with non-sealed permits) - Recognize final finalize() method as a mitigation (skip these classes) - Detect throwing instance initializers in classes without explicit constructors - Exclude local classes (cannot be subclassed from other files) - Fix CWE mapping: remove incorrect CWE-586 (Explicit Call to Finalize), keep CERT OBJ11-J mapping - Add comprehensive test cases for all new behaviors Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
❌ Ruling needs updating. A fix PR has been created: #6007 Please review and merge it into your branch. |
… S9345 Extract checkMembers, reportVulnerableConstructor, and reportThrowingInitializers helper methods from visitNode to reduce cognitive complexity below the threshold. Add test cases for: non-final finalize(), throws in lambdas/anonymous classes, multiple throwing initializers, local class in constructor, and field initializers. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Non-private constructors are now reported even if they don't throw themselves, when the class has instance initializer blocks that throw. Instance initializers run as part of every constructor, so a throwing initializer makes all non-private constructors vulnerable to finalizer attacks. Also adds test cases for: abstract classes with throwing initializers, deep sealed hierarchies, final finalize() with initializers, static initializers (compliant), and constructors combined with throwing initializers. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
🤖 Generated with GitHub Actions
|
❌ Ruling needs updating. A fix PR has been created: #6010 Please review and merge it into your branch. |
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
… test coverage for S9345 Extract sealed class resolution logic into separate methods to eliminate duplicate branch code (S1871) and reduce cognitive complexity (S3776). Add additional test cases for coverage. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…-rule-s9345-sonarjava-6783' into romain/new-rule-s9345-sonarjava-6783
- Add test cases for abstract classes, sealed class hierarchies, finalize signature edge cases, and field initializer edge cases - Merge ruling fix PR #6010 (eclipse-jetty-similar-to-main) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Extract findClassInChildren helper to reduce cognitive complexity of findClassInTree. Add non-compiling test for sealed class resolution with unresolvable types and additional test cases for better coverage. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…branch Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
nathsou
left a comment
There was a problem hiding this comment.
Single-pass re-review of the follow-up commits.
- Walk sealed permitted descendants recursively to detect non-sealed types deep in the hierarchy - Treat unresolved permitted types conservatively as unsafe instead of silently suppressing findings - Detect field initializers calling methods/constructors that declare checked exceptions via throws clause - Require final finalize() to have an empty body before considering the class protected from finalizer attacks Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
❌ Ruling needs updating. A fix PR has been created: #6031 Please review and merge it into your branch. |
…9345 Add explicit constructors with throws declarations to semantic sample classes whose field initializers call methods declaring checked exceptions, fixing the Windows compilation error. Update commons-beanutils ruling results to account for additional findings from the improved field-initializer analysis. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Code Review ✅ Approved 3 resolved / 3 findingsImplements rule S9345 to detect classes with throwing constructors vulnerable to Finalizer attacks, addressing issues with sealed classes, throwing initializers, and extraneous ruling results. ✅ 3 resolved✅ Edge Case: Sealed classes flagged as noncompliant (false positive)
✅ Edge Case: Throwing field/instance initializers not detected
✅ Quality: Unrelated ruling results (S2160, S6212) bundled into new-rule PR
Implementation Status ✅ 1 of 1 objectives covered✅ SONARJAVA-6783 - 1 of 1 objectives coveredThis PR implements rule S9345 to protect classes with throwing constructors against Finalizer attacks. ✅ 1 covered here
OptionsAuto-apply is off → Gitar will not commit updates to this branch. Comment with these commands to change the behavior for this request:
Was this helpful? React with 👍 / 👎 | Gitar |
|




Summary
throwsclause or containsthrowstatementsfinalclasses, classes with onlyprivatethrowing constructors (factory pattern), enums, records, sealed classes (where all permitted subclasses are final/sealed), classes withfinal finalize()method, and local classesReview feedback addressed (commit 09126f5)
All 6 findings from @nathsou have been addressed:
protected final void finalize() {}recognized as safeTest plan
CheckVerifiercovering noncompliant and compliant patterns