Skip to content

Commit acfabc1

Browse files
joke1196sonartech
authored andcommitted
SONARPY-2993: S6437: Fix False Positive when an F-string is used and contains a formatted expression (#716)
GitOrigin-RevId: 3fbfcbd700893f4c73e9242f58474d8f49802bd1
1 parent c664999 commit acfabc1

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

python-checks/src/main/java/org/sonar/python/checks/HardcodedCredentialsCallCheck.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,14 @@ private static void checkArgument(SubscriptionContext ctx, RegularArgument argum
8989
Optional.of(argExp)
9090
.filter(StringLiteral.class::isInstance)
9191
.map(StringLiteral.class::cast)
92+
.filter(Predicate.not(HardcodedCredentialsCallCheck::containsFormattedExpressions))
9293
.filter(HardcodedCredentialsCallCheck::isNotEmpty)
9394
.ifPresent(string -> ctx.addIssue(argument, MESSAGE));
9495
} else if (argExp.is(Tree.Kind.NAME)) {
9596
findAssignment((Name) argExp, 0)
9697
.filter(StringLiteral.class::isInstance)
9798
.map(StringLiteral.class::cast)
99+
.filter(Predicate.not(HardcodedCredentialsCallCheck::containsFormattedExpressions))
98100
.filter(HardcodedCredentialsCallCheck::isNotEmpty)
99101
.ifPresent(assignedValue -> ctx.addIssue(argument, MESSAGE).secondary(assignedValue, MESSAGE));
100102
}
@@ -104,7 +106,12 @@ private static boolean isNotEmpty(StringLiteral stringLiteral) {
104106
return Optional.of(stringLiteral)
105107
.map(StringLiteral::trimmedQuotesValue)
106108
.filter(Predicate.not(String::isEmpty))
107-
.isPresent();
109+
.isPresent();
110+
}
111+
112+
private static boolean containsFormattedExpressions(StringLiteral stringLiteral) {
113+
return stringLiteral.stringElements().stream()
114+
.anyMatch(element -> !element.formattedExpressions().isEmpty());
108115
}
109116

110117
private static Optional<Tree> findAssignment(Name name, int depth) {

python-checks/src/test/resources/checks/hardcodedCredentialsCall.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,17 @@ def ldap_methods_check(p):
112112
import pgdb
113113
import pg
114114

115-
def db_connect(pwd):
115+
def db_connect(pwd, PASS):
116116
mysql.connector.connect(host='localhost', user='sonarsource', password='')
117117
mysql.connector.connect(host='localhost', password='', user='sonarsource')
118118
mysql.connector.connect('localhost', 'sonarsource', '')
119119

120120
mysql.connector.connection.MySQLConnection(host='localhost', user='sonarsource', password='')
121121
pymysql.connect(host='localhost', user='sonarsource', password='')
122122
pymysql.connections.Connection(host='localhost', user='sonarsource', password='abc') # Noncompliant
123+
pymysql.connections.Connection(host='localhost', user='sonarsource', password=f'{PASS}') # Compliant
124+
pymysql.connections.Connection(host='localhost', user='sonarsource', password=f'') # Compliant
125+
pymysql.connections.Connection(host='localhost', user='sonarsource', password=f'pass') # Noncompliant
123126
psycopg2.connect(host='localhost', user='postgres', password='')
124127
pgdb.connect(host='localhost', user='postgres', password='')
125128

0 commit comments

Comments
 (0)