-
Notifications
You must be signed in to change notification settings - Fork 725
SONARJAVA-6492: Implement S8912 - CredentialsProvider implementations should be annotated with @Unremovable #5707
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
Open
romainbrenguier
wants to merge
8
commits into
master
Choose a base branch
from
new-rule/SONARJAVA-6492-S8912
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+433
−4
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5aefe2c
SONARJAVA-6492: Implement rule S8912
romainbrenguier 2867a42
Fix CI failures
romainbrenguier 4c08246
Address review comment from romainbrenguier on java-checks/src/main/j…
romainbrenguier fa65927
Address review comment from NoemieBenard on java-checks/src/main/java…
romainbrenguier d8b3f64
Address review comment from NoemieBenard on sonar-java-plugin/src/mai…
romainbrenguier 0660813
Address review comment from NoemieBenard on java-checks-test-sources/…
romainbrenguier bb1b5f8
Merge remote-tracking branch 'origin/master' into new-rule/SONARJAVA-…
romainbrenguier 2c425c0
Fix CI: Updated S1128 autoscan baseline falseNegatives from 31 to 30 …
romainbrenguier File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| { | ||
| "ruleKey": "S1168", | ||
| "hasTruePositives": true, | ||
| "falseNegatives": 24, | ||
| "falseNegatives": 25, | ||
| "falsePositives": 0 | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| { | ||
| "ruleKey": "S6813", | ||
| "hasTruePositives": true, | ||
| "falseNegatives": 66, | ||
| "falseNegatives": 67, | ||
| "falsePositives": 0 | ||
| } |
6 changes: 6 additions & 0 deletions
6
its/autoscan/src/test/resources/autoscan/diffs/diff_S8912.json
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "ruleKey": "S8912", | ||
| "hasTruePositives": false, | ||
| "falseNegatives": 8, | ||
| "falsePositives": 0 | ||
| } |
134 changes: 134 additions & 0 deletions
134
...-test-sources/default/src/main/java/checks/CredentialsProviderUnremovableCheckSample.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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| package checks; | ||
|
|
||
| import io.quarkus.arc.Unremovable; | ||
| import io.quarkus.credentials.CredentialsProvider; | ||
| import jakarta.enterprise.context.ApplicationScoped; | ||
| import jakarta.enterprise.context.Dependent; | ||
| import jakarta.enterprise.context.RequestScoped; | ||
| import jakarta.enterprise.context.SessionScoped; | ||
| import jakarta.inject.Inject; | ||
| import jakarta.inject.Singleton; | ||
| import java.util.Map; | ||
|
|
||
| @ApplicationScoped | ||
| class MyCredentialsProvider implements CredentialsProvider { // Noncompliant {{Add the @Unremovable annotation to this CredentialsProvider implementation.}} | ||
| // ^^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| @Override | ||
| public Map<String, String> getCredentials(String credentialsProviderName) { | ||
| return Map.of(USER_PROPERTY_NAME, "user", PASSWORD_PROPERTY_NAME, "password"); | ||
| } | ||
| } | ||
|
|
||
| @ApplicationScoped | ||
| @Unremovable | ||
| class CompliantProvider implements CredentialsProvider { | ||
|
|
||
| @Override | ||
| public Map<String, String> getCredentials(String credentialsProviderName) { | ||
| return Map.of(USER_PROPERTY_NAME, "user", PASSWORD_PROPERTY_NAME, "password"); | ||
| } | ||
| } | ||
|
|
||
| @RequestScoped | ||
| class RequestScopedProvider implements CredentialsProvider { // Noncompliant | ||
| // ^^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| @Override | ||
| public Map<String, String> getCredentials(String credentialsProviderName) { | ||
| return Map.of(USER_PROPERTY_NAME, "user", PASSWORD_PROPERTY_NAME, "pass"); | ||
| } | ||
| } | ||
|
|
||
| @Singleton | ||
| class SingletonProvider implements CredentialsProvider { // Noncompliant | ||
| // ^^^^^^^^^^^^^^^^^ | ||
|
|
||
| @Override | ||
| public Map<String, String> getCredentials(String credentialsProviderName) { | ||
| return Map.of(USER_PROPERTY_NAME, "admin", PASSWORD_PROPERTY_NAME, "secret"); | ||
| } | ||
| } | ||
|
|
||
| @Dependent | ||
| class DependentProvider implements CredentialsProvider { // Noncompliant | ||
| // ^^^^^^^^^^^^^^^^^ | ||
|
|
||
| @Override | ||
| public Map<String, String> getCredentials(String credentialsProviderName) { | ||
| return Map.of(USER_PROPERTY_NAME, "user", PASSWORD_PROPERTY_NAME, "pwd"); | ||
| } | ||
| } | ||
|
|
||
| @SessionScoped | ||
| class SessionScopedProvider implements CredentialsProvider { // Noncompliant | ||
| // ^^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| @Override | ||
| public Map<String, String> getCredentials(String credentialsProviderName) { | ||
| return Map.of(USER_PROPERTY_NAME, "user", PASSWORD_PROPERTY_NAME, "pass"); | ||
| } | ||
| } | ||
|
|
||
| class NoScopeProvider implements CredentialsProvider { | ||
|
|
||
| @Override | ||
| public Map<String, String> getCredentials(String credentialsProviderName) { | ||
| return Map.of(USER_PROPERTY_NAME, "user", PASSWORD_PROPERTY_NAME, "password"); | ||
| } | ||
| } | ||
|
|
||
| @ApplicationScoped | ||
| class SomeOtherService { | ||
| public void doSomething() { | ||
| } | ||
| } | ||
|
|
||
| @Singleton | ||
| @Unremovable | ||
| class CompliantSingleton implements CredentialsProvider { | ||
|
|
||
| @Override | ||
| public Map<String, String> getCredentials(String credentialsProviderName) { | ||
| return Map.of(USER_PROPERTY_NAME, "admin", PASSWORD_PROPERTY_NAME, "secret"); | ||
| } | ||
| } | ||
|
|
||
| @ApplicationScoped | ||
| class ProviderWithDependencies implements CredentialsProvider { // Noncompliant | ||
| // ^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| @Inject | ||
| ConfigService configService; | ||
|
|
||
| @Override | ||
| public Map<String, String> getCredentials(String credentialsProviderName) { | ||
| String username = configService.getUsername(); | ||
| String password = configService.getPassword(); | ||
| return Map.of(USER_PROPERTY_NAME, username, PASSWORD_PROPERTY_NAME, password); | ||
| } | ||
| } | ||
|
|
||
| @ApplicationScoped | ||
| class MultiInterfaceProvider implements CredentialsProvider, AutoCloseable { // Noncompliant | ||
| // ^^^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| @Override | ||
| public Map<String, String> getCredentials(String credentialsProviderName) { | ||
| return Map.of(USER_PROPERTY_NAME, "user", PASSWORD_PROPERTY_NAME, "password"); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| } | ||
| } | ||
|
|
||
| class ConfigService { | ||
| String getUsername() { | ||
| return "configUser"; | ||
| } | ||
|
|
||
| String getPassword() { | ||
| return "configPass"; | ||
| } | ||
| } |
15 changes: 15 additions & 0 deletions
15
...ources/default/src/main/java/checks/CredentialsProviderUnremovableCheckSampleMinimal.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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package checks; | ||
|
|
||
| import io.quarkus.credentials.CredentialsProvider; | ||
| import jakarta.enterprise.context.ApplicationScoped; | ||
| import java.util.Map; | ||
|
|
||
| @ApplicationScoped | ||
| class MinimalProvider implements CredentialsProvider { // Noncompliant {{Add the @Unremovable annotation to this CredentialsProvider implementation.}} | ||
| // ^^^^^^^^^^^^^^^ | ||
|
|
||
| @Override | ||
| public Map<String, String> getCredentials(String credentialsProviderName) { | ||
| return null; | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
java-checks-test-sources/default/src/main/java/io/quarkus/arc/Unremovable.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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package io.quarkus.arc; | ||
|
|
||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| @Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD}) | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| public @interface Unremovable { | ||
| } |
10 changes: 10 additions & 0 deletions
10
...checks-test-sources/default/src/main/java/io/quarkus/credentials/CredentialsProvider.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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package io.quarkus.credentials; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| public interface CredentialsProvider { | ||
| String USER_PROPERTY_NAME = "user"; | ||
| String PASSWORD_PROPERTY_NAME = "password"; | ||
|
|
||
| Map<String, String> getCredentials(String credentialsProviderName); | ||
| } |
85 changes: 85 additions & 0 deletions
85
java-checks/src/main/java/org/sonar/java/checks/CredentialsProviderUnremovableCheck.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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| /* | ||
| * SonarQube Java | ||
| * Copyright (C) SonarSource Sàrl | ||
| * mailto:info AT sonarsource DOT com | ||
| * | ||
| * You can redistribute and/or modify this program under the terms of | ||
| * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
| * See the Sonar Source-Available License for more details. | ||
| * | ||
| * You should have received a copy of the Sonar Source-Available License | ||
| * along with this program; if not, see https://sonarsource.com/license/ssal/ | ||
| */ | ||
| package org.sonar.java.checks; | ||
|
|
||
| import java.util.List; | ||
| import org.sonar.check.Rule; | ||
| import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; | ||
| import org.sonar.plugins.java.api.semantic.SymbolMetadata; | ||
| import org.sonar.plugins.java.api.tree.ClassTree; | ||
| import org.sonar.plugins.java.api.tree.Tree; | ||
|
|
||
| @Rule(key = "S8912") | ||
| public class CredentialsProviderUnremovableCheck extends IssuableSubscriptionVisitor { | ||
| private static final String CREDENTIALS_PROVIDER_FQN = "io.quarkus.credentials.CredentialsProvider"; | ||
| private static final String UNREMOVABLE_FQN = "io.quarkus.arc.Unremovable"; | ||
|
|
||
| private static final List<String> CDI_SCOPE_ANNOTATIONS = List.of( | ||
| "jakarta.enterprise.context.ApplicationScoped", | ||
| "jakarta.enterprise.context.RequestScoped", | ||
| "jakarta.enterprise.context.SessionScoped", | ||
| "jakarta.enterprise.context.Dependent", | ||
| "jakarta.inject.Singleton", | ||
| "javax.enterprise.context.ApplicationScoped", | ||
| "javax.enterprise.context.RequestScoped", | ||
| "javax.enterprise.context.SessionScoped", | ||
| "javax.enterprise.context.Dependent", | ||
| "javax.inject.Singleton" | ||
| ); | ||
|
|
||
| @Override | ||
| public List<Tree.Kind> nodesToVisit() { | ||
| return List.of(Tree.Kind.CLASS); | ||
| } | ||
|
|
||
| @Override | ||
| public void visitNode(Tree tree) { | ||
| ClassTree classTree = (ClassTree) tree; | ||
|
|
||
| if (classTree.simpleName() == null) { | ||
| return; | ||
| } | ||
|
|
||
| if (!implementsCredentialsProvider(classTree)) { | ||
| return; | ||
| } | ||
|
|
||
| SymbolMetadata metadata = classTree.symbol().metadata(); | ||
|
|
||
| if (!hasCdiScopeAnnotation(metadata)) { | ||
| return; | ||
| } | ||
|
|
||
| if (metadata.isAnnotatedWith(UNREMOVABLE_FQN)) { | ||
| return; | ||
| } | ||
|
|
||
| reportIssue( | ||
| classTree.simpleName(), | ||
| "Add the @Unremovable annotation to this CredentialsProvider implementation." | ||
| ); | ||
| } | ||
|
|
||
| private static boolean implementsCredentialsProvider(ClassTree classTree) { | ||
| return classTree.symbol().type().isSubtypeOf(CREDENTIALS_PROVIDER_FQN); | ||
| } | ||
|
|
||
| private static boolean hasCdiScopeAnnotation(SymbolMetadata metadata) { | ||
| return CDI_SCOPE_ANNOTATIONS.stream() | ||
| .anyMatch(metadata::isAnnotatedWith); | ||
| } | ||
| } | ||
53 changes: 53 additions & 0 deletions
53
java-checks/src/test/java/org/sonar/java/checks/CredentialsProviderUnremovableCheckTest.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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| /* | ||
| * SonarQube Java | ||
| * Copyright (C) SonarSource Sàrl | ||
| * mailto:info AT sonarsource DOT com | ||
| * | ||
| * You can redistribute and/or modify this program under the terms of | ||
| * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
| * See the Sonar Source-Available License for more details. | ||
| * | ||
| * You should have received a copy of the Sonar Source-Available License | ||
| * along with this program; if not, see https://sonarsource.com/license/ssal/ | ||
| */ | ||
| package org.sonar.java.checks; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.sonar.java.checks.verifier.CheckVerifier; | ||
|
|
||
| import static org.sonar.java.checks.verifier.TestUtils.mainCodeSourcesPath; | ||
|
|
||
| class CredentialsProviderUnremovableCheckTest { | ||
| private static final String SAMPLE_FILE = mainCodeSourcesPath("checks/CredentialsProviderUnremovableCheckSample.java"); | ||
| private static final String MINIMAL_FILE = mainCodeSourcesPath("checks/CredentialsProviderUnremovableCheckSampleMinimal.java"); | ||
| private static final CredentialsProviderUnremovableCheck CHECK = new CredentialsProviderUnremovableCheck(); | ||
|
|
||
| @Test | ||
| void testMinimal() { | ||
| CheckVerifier.newVerifier() | ||
| .onFile(MINIMAL_FILE) | ||
| .withCheck(CHECK) | ||
| .verifyIssues(); | ||
| } | ||
|
|
||
| @Test | ||
| void test() { | ||
| CheckVerifier.newVerifier() | ||
| .onFile(SAMPLE_FILE) | ||
| .withCheck(CHECK) | ||
| .verifyIssues(); | ||
| } | ||
|
|
||
| @Test | ||
| void testWithoutSemantic() { | ||
| CheckVerifier.newVerifier() | ||
| .onFile(SAMPLE_FILE) | ||
| .withCheck(CHECK) | ||
| .withoutSemantic() | ||
| .verifyNoIssues(); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure it matters here, but this can be null