Skip to content

Commit 377368f

Browse files
Fix FN on S2068 repored in USER-1192
1 parent 77226e4 commit 377368f

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

sonar-xml-plugin/src/main/java/org/sonar/plugins/xml/checks/security/HardcodedCredentialsCheck.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ public class HardcodedCredentialsCheck extends SimpleXPathBasedCheck {
4747
private static final XPathExpression WEB_CONFIG_CREDENTIALS_PATH = XPathBuilder
4848
.forExpression("/configuration/system.web/authentication[@mode=\"Forms\"]/forms/credentials[@passwordFormat=\"Clear\"]/user/@password[string-length(.) > 0]").build();
4949

50+
private static final XPathExpression WEB_CONFIG_APP_SETTINGS_ADD_PATH =
51+
XPathBuilder.forExpression("//appSettings/add").build();
52+
5053
private static final Pattern VALID_CREDENTIAL_VALUES = Pattern.compile("[\\{$#]\\{");
5154
private static final Pattern VALID_WEB_CONFIG_CREDENTIAL_VALUES = Pattern.compile("^__.*__$");
5255

@@ -80,6 +83,9 @@ public void scanFile(XmlFile file) {
8083
evaluateAsList(WEB_CONFIG_CREDENTIALS_PATH, file.getDocument()).stream()
8184
.filter(passwordAttrNode -> !isValidWebConfigCredential(passwordAttrNode.getNodeValue()))
8285
.forEach(this::reportIssue);
86+
evaluateAsList(WEB_CONFIG_APP_SETTINGS_ADD_PATH, file.getDocument()).stream()
87+
.filter(HardcodedCredentialsCheck::isAddWithPassword)
88+
.forEach(this::reportIssue);
8389
} else {
8490
checkElements(file.getDocument());
8591
checkSpecialCases(file);
@@ -152,6 +158,16 @@ private static boolean isValidWebConfigCredential(String candidate) {
152158
return isValidCredential(candidate) || VALID_WEB_CONFIG_CREDENTIAL_VALUES.matcher(candidate).matches();
153159
}
154160

161+
/** Detects nodes with key="password" and "value" attributes. */
162+
private static boolean isAddWithPassword(Node node) {
163+
NamedNodeMap attributes = node.getAttributes();
164+
Optional<String> keyValueLowerCase =
165+
Optional.ofNullable(attributes.getNamedItem("key"))
166+
.map(Node::getNodeValue)
167+
.map(String::toLowerCase);
168+
return keyValueLowerCase.equals(Optional.of("password")) && attributes.getNamedItem("value") != null;
169+
}
170+
155171
private void checkSpecialCases(XmlFile file) {
156172
specialCases.forEach(specialCase -> specialCase.accept(file));
157173
}

sonar-xml-plugin/src/test/java/org/sonar/plugins/xml/checks/security/HardcodedCredentialsCheckTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,9 @@ void web_application() {
7575
SonarXmlCheckVerifier.verifyIssues(Paths.get("web-application", "web.config").toString(), CHECK);
7676
SonarXmlCheckVerifier.verifyIssues(Paths.get("web-application", "Machine.config").toString(), CHECK);
7777
}
78+
79+
@Test
80+
void web_application_app_settings() {
81+
SonarXmlCheckVerifier.verifyIssues(Paths.get("app-settings", "web.config").toString(), CHECK);
82+
}
7883
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<location>
4+
<system.webServer>
5+
<aspNetCore processPath="dotnet" arguments=".\Hidden.dll" stdoutLogEnabled="false" stdoutLogFile="\\?\%home%\LogFiles\stdout" hostingModel="inprocess" />
6+
</system.webServer>
7+
</location>
8+
<appSettings>
9+
<add key="Password" value="thisisbad" /> <!-- Noncompliant -->
10+
<add key="color" value="blue" />
11+
<add key="password" value="thisisbadtoo" /> <!-- Noncompliant -->
12+
<add key="password" />
13+
<add something="else" />
14+
</appSettings>
15+
</configuration>

0 commit comments

Comments
 (0)