Skip to content

Commit c89be6a

Browse files
committed
Swift: Refine the heuristic (mostly narrower).
1 parent 5bbc61e commit c89be6a

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

swift/ql/lib/codeql/swift/security/CleartextLoggingExtensions.qll

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,11 @@ private class PrintfCleartextLoggingSink extends CleartextLoggingSink {
134134
}
135135

136136
/**
137-
* Holds if `f` is a function that might be a logging function.
137+
* Holds if `label` looks like the name of a logging function.
138138
*/
139-
private predicate logLikeHeuristic(Function f) {
140-
f.getName().regexpMatch("(?i).*log(?!in).*") or
141-
f.getDeclaringDecl().(NominalTypeDecl).getName().regexpMatch("(?i).*log(?!in).*")
139+
bindingset[label]
140+
private predicate logLikeHeuristic(string label) {
141+
label.regexpMatch("(l|.*L)og([A-Z0-9].*)?") // e.g. "logMessage", "debugLog"
142142
}
143143

144144
/**
@@ -147,7 +147,10 @@ private predicate logLikeHeuristic(Function f) {
147147
class HeuristicCleartextLoggingSink extends CleartextLoggingSink {
148148
HeuristicCleartextLoggingSink() {
149149
exists(CallExpr ce, Function f, Expr e |
150-
logLikeHeuristic(f) and
150+
(
151+
logLikeHeuristic(f.getShortName()) or
152+
logLikeHeuristic(f.getDeclaringDecl().(NominalTypeDecl).getName())
153+
) and
151154
ce.getStaticTarget() = f and
152155
ce.getAnArgument().getExpr() = e and
153156
e.getType().getUnderlyingType().getName() = ["String", "NSString"] and

swift/ql/test/query-tests/Security/CWE-312/cleartextLoggingTest.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -364,10 +364,10 @@ func test6(passwordString: String) {
364364
func test7(authKey: String, authKey2: Int, authKey3: Float, password: String, secret: String) {
365365
log(message: authKey) // $ hasCleartextLogging=365
366366
log(message: String(authKey2)) // $ hasCleartextLogging=366
367-
logging(message: authKey) // $ hasCleartextLogging=367
368-
logfile(file: 0, message: authKey) // $ hasCleartextLogging=368
367+
logging(message: authKey) // $ MISSING: hasCleartextLogging=367
368+
logfile(file: 0, message: authKey) // $ MISSING: hasCleartextLogging=368
369369
logMessage(NSString(string: authKey)) // $ hasCleartextLogging=369
370-
logInfo(authKey) // $ MISSING: hasCleartextLogging=370
370+
logInfo(authKey) // $ hasCleartextLogging=370
371371
logError(errorMsg: authKey) // $ hasCleartextLogging=371
372372
harmless(authKey) // GOOD: not logging
373373
_ = logarithm(authKey3) // GOOD: not logging
@@ -387,10 +387,10 @@ func test7(authKey: String, authKey2: Int, authKey3: Float, password: String, se
387387

388388
let logic = Logic()
389389
logic.addInt(authKey2) // GOOD: not logging
390-
logic.addString(authKey) // $ SPURIOUS: hasCleartextLogging=390 (not logging)
390+
logic.addString(authKey) // GOOD: not logging
391391

392392
let rlogger = MyRemoteLogger()
393-
rlogger.setPassword(password: password) // $ SPURIOUS: hasCleartextLogging=393 (not logging)
394-
rlogger.login(password: password) // $ SPURIOUS: hasCleartextLogging=394 (not logging)
395-
rlogger.logout(secret: secret) // $ SPURIOUS: hasCleartextLogging=395 (not logging)
393+
rlogger.setPassword(password: password) // GOOD: not logging
394+
rlogger.login(password: password) // GOOD: not logging
395+
rlogger.logout(secret: secret) // GOOD: not logging
396396
}

0 commit comments

Comments
 (0)