Skip to content

Commit b49599b

Browse files
SONARJAVA-4880 S5804: Support detection of User Enumeration for Spring (#4753)
1 parent 26ef2ab commit b49599b

4 files changed

Lines changed: 17 additions & 3 deletions

File tree

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"ruleKey": "S5804",
33
"hasTruePositives": false,
4-
"falseNegatives": 9,
4+
"falseNegatives": 11,
55
"falsePositives": 0
6-
}
6+
}

java-checks-test-sources/default/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,11 @@
206206
<type>jar</type>
207207
<scope>provided</scope>
208208
</dependency>
209+
<dependency>
210+
<groupId>org.springframework.security</groupId>
211+
<artifactId>spring-security-ldap</artifactId>
212+
<version>6.2.3</version>
213+
</dependency>
209214
<dependency>
210215
<groupId>org.springframework</groupId>
211216
<artifactId>spring-aop</artifactId>

java-checks-test-sources/default/src/main/java/checks/security/UserEnumerationCheck.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.springframework.security.core.userdetails.UserDetailsService;
1313
import org.springframework.security.core.userdetails.UsernameNotFoundException;
1414
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
15+
import org.springframework.security.ldap.authentication.LdapAuthenticationProvider;
1516

1617
public class UserEnumerationCheck {
1718

@@ -93,6 +94,13 @@ public void config() {
9394
throw new UsernameNotFoundException("userName not found"); // Noncompliant
9495
}
9596

97+
void ldap(LdapAuthenticationProvider ldapAuthenticationProvider) {
98+
ldapAuthenticationProvider.setHideUserNotFoundExceptions(false); // Noncompliant
99+
ldapAuthenticationProvider.setHideUserNotFoundExceptions(MY_CONSTANT); // Noncompliant
100+
boolean variableFalse = false;
101+
ldapAuthenticationProvider.setHideUserNotFoundExceptions(variableFalse); // Compliant, not a constant
102+
}
103+
96104
public void compliantConfig() {
97105
boolean b = false;
98106

java-checks/src/main/java/org/sonar/java/checks/security/UserEnumerationCheck.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public class UserEnumerationCheck extends IssuableSubscriptionVisitor {
4141

4242
private static final String MESSAGE = "Make sure allowing user enumeration is safe here.";
4343
private static final String ABSTRACT_USER_DETAILS_AUTHENTICATION_PROVIDER = "org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider";
44+
private static final String SPRING_SEC_LDAP_AUTHENTICATION_PROVIDER = "org.springframework.security.ldap.authentication.LdapAuthenticationProvider";
4445
private static final String USER_DETAILS_SERVICE = "org.springframework.security.core.userdetails.UserDetailsService";
4546
private static final String USERNAME_NOT_FOUND_EXCEPTION = "org.springframework.security.core.userdetails.UsernameNotFoundException";
4647
private static final String HIDE_USER_NOT_FOUND_EXCEPTIONS = "setHideUserNotFoundExceptions";
@@ -52,7 +53,7 @@ public class UserEnumerationCheck extends IssuableSubscriptionVisitor {
5253
private final Deque<MethodTree> stack = new ArrayDeque<>();
5354

5455
private static final MethodMatchers SET_HIDE_USER_MATCHER = MethodMatchers.create()
55-
.ofSubTypes(ABSTRACT_USER_DETAILS_AUTHENTICATION_PROVIDER)
56+
.ofSubTypes(ABSTRACT_USER_DETAILS_AUTHENTICATION_PROVIDER, SPRING_SEC_LDAP_AUTHENTICATION_PROVIDER)
5657
.names(HIDE_USER_NOT_FOUND_EXCEPTIONS)
5758
.addParametersMatcher(BOOLEAN)
5859
.build();

0 commit comments

Comments
 (0)