Skip to content

Commit

Permalink
SONARJAVA-4884 S4507: Support detection of enabled Debug Features in …
Browse files Browse the repository at this point in the history
…Spring programmatically (#4739)
  • Loading branch information
ValentinAebi-sonar authored Mar 28, 2024
1 parent 5b514a4 commit 373182d
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1970,7 +1970,7 @@
{
"ruleKey": "S4507",
"hasTruePositives": true,
"falseNegatives": 0,
"falseNegatives": 2,
"falsePositives": 0
},
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"ruleKey": "S4507",
"hasTruePositives": true,
"falseNegatives": 0,
"falseNegatives": 2,
"falsePositives": 0
}
}
2 changes: 1 addition & 1 deletion java-checks-test-sources/default/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>5.0.5.RELEASE</version>
<version>5.4.0</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import android.webkit.WebView;
import android.webkit.WebViewFactoryProvider;
import java.lang.reflect.InvocationTargetException;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;

// Tests for printStackTrace.
// Tests for @EnableWebSecurity are in files/non-compiling/checks/security/DebugFeatureEnabledCheck.java
Expand Down Expand Up @@ -60,4 +62,19 @@ void fun() {
ex.printStackTrace();
}
}

void foo(WebSecurity web, boolean cond){
web.debug(true); // Noncompliant [sc=9;ec=14] {{Make sure this debug feature is deactivated before delivering the code in production.}}
web.debug(false);
web.debug(cond);
}

public WebSecurityCustomizer debugCustomizer() {
return (web) -> web.debug(true); // Noncompliant [sc=25;ec=30] {{Make sure this debug feature is deactivated before delivering the code in production.}}
}

public WebSecurityCustomizer nonDebugCustomizer() {
return (web) -> web.debug(false);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public class DebugFeatureEnabledCheck extends IssuableSubscriptionVisitor {
.ofSubTypes("android.webkit.WebView", "android.webkit.WebViewFactoryProvider$Statics")
.names("setWebContentsDebuggingEnabled").addParametersMatcher("boolean").build();

private static final MethodMatchers DEBUG_MATCHER = MethodMatchers.create()
.ofSubTypes("org.springframework.security.config.annotation.web.builders.WebSecurity")
.names("debug").addParametersMatcher("boolean").build();

private final Deque<Symbol.TypeSymbol> enclosingClass = new LinkedList<>();

@Override
Expand Down Expand Up @@ -82,7 +86,7 @@ public void leaveNode(Tree tree) {
}

private void checkMethodInvocation(MethodInvocationTree mit) {
if (isPrintStackTraceIllegalUsage(mit) || isSetWebContentsDebuggingEnabled(mit)) {
if (isPrintStackTraceIllegalUsage(mit) || isSetWebContentsDebuggingEnabled(mit) || isDebugWithTrueArgument(mit)) {
reportIssue(ExpressionUtils.methodName(mit), MESSAGE);
}
}
Expand All @@ -96,6 +100,14 @@ private static boolean isSetWebContentsDebuggingEnabled(MethodInvocationTree mit
Boolean.TRUE.equals(ExpressionUtils.resolveAsConstant(mit.arguments().get(0)));
}

private static boolean isDebugWithTrueArgument(MethodInvocationTree mit){
if (!DEBUG_MATCHER.matches(mit.methodSymbol())){
return false;
}
var cstArg = mit.arguments().get(0).asConstant();
return cstArg.isPresent() && cstArg.get().equals(true);
}

private void checkAnnotation(AnnotationTree annotation) {
if (annotation.symbolType().is("org.springframework.security.config.annotation.web.configuration.EnableWebSecurity")) {
annotation.arguments().stream()
Expand Down

0 comments on commit 373182d

Please sign in to comment.