Skip to content

Commit aa93165

Browse files
committed
Swift: Add heuristic sinks.
1 parent d91c5c0 commit aa93165

File tree

2 files changed

+42
-18
lines changed

2 files changed

+42
-18
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,30 @@ private class PrintfCleartextLoggingSink extends CleartextLoggingSink {
130130
}
131131
}
132132

133+
/**
134+
* Holds if `f` is a function that might be a logging function.
135+
*/
136+
private predicate logLikeHeuristic(Function f) {
137+
f.getName().regexpMatch("(?i).*log.*") or
138+
f.getDeclaringDecl().(NominalTypeDecl).getName().regexpMatch("(?i).*log.*")
139+
}
140+
141+
/**
142+
* A cleartext logging sink that is determined by imprecise methods.
143+
*/
144+
class HeuristicCleartextLoggingSink extends CleartextLoggingSink {
145+
HeuristicCleartextLoggingSink() {
146+
exists(CallExpr ce, Function f, Expr e |
147+
// by function name
148+
logLikeHeuristic(f) and
149+
ce.getStaticTarget() = f and
150+
ce.getAnArgument().getExpr() = e and
151+
e.getType().getUnderlyingType().getName() = ["String", "NSString"] and
152+
this.asExpr() = e
153+
)
154+
}
155+
}
156+
133157
private class LoggingSinks extends SinkModelCsv {
134158
override predicate row(string row) {
135159
row =

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

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -355,30 +355,30 @@ func test6(passwordString: String) {
355355
}
356356

357357
func test7(authKey: String, authKey2: Int, authKey3: Float) {
358-
log(message: authKey) // $ MISSING: hasCleartextLogging=
359-
log(message: String(authKey2)) // $ MISSING: hasCleartextLogging=
360-
logging(message: authKey) // $ MISSING: hasCleartextLogging=
361-
logfile(file: 0, message: authKey) // $ MISSING: hasCleartextLogging=
362-
logMessage(NSString(string: authKey)) // $ MISSING: hasCleartextLogging=
363-
logInfo(authKey) // $ MISSING: hasCleartextLogging=
364-
logError(errorMsg: authKey) // $ MISSING: hasCleartextLogging=
358+
log(message: authKey) // $ hasCleartextLogging=358
359+
log(message: String(authKey2)) // $ hasCleartextLogging=359
360+
logging(message: authKey) // $ hasCleartextLogging=360
361+
logfile(file: 0, message: authKey) // $ hasCleartextLogging=361
362+
logMessage(NSString(string: authKey)) // $ hasCleartextLogging=362
363+
logInfo(authKey) // $ hasCleartextLogging=363
364+
logError(errorMsg: authKey) // $ hasCleartextLogging=364
365365
harmless(authKey) // GOOD: not logging
366366
logarithm(authKey3) // GOOD: not logging
367-
doLogin(login: authKey) // GOOD: not logging
367+
doLogin(login: authKey) // $ SPURIOUS: hasCleartextLogging=367 (not logging)
368368

369369
let logger = LogFile()
370370
let msg = "authKey: " + authKey
371-
logger.log(msg) // $ MISSING: hasCleartextLogging=
372-
logger.trace(msg) // $ MISSING: hasCleartextLogging=
373-
logger.debug(msg) // $ MISSING: hasCleartextLogging=
374-
logger.info(NSString(string: msg)) // $ MISSING: hasCleartextLogging=
375-
logger.notice(msg) // $ MISSING: hasCleartextLogging=
376-
logger.warning(msg) // $ MISSING: hasCleartextLogging=
377-
logger.error(msg) // $ MISSING: hasCleartextLogging=
378-
logger.critical(msg) // $ MISSING: hasCleartextLogging=
379-
logger.fatal(msg) // $ MISSING: hasCleartextLogging=
371+
logger.log(msg) // $ hasCleartextLogging=370
372+
logger.trace(msg) // $ hasCleartextLogging=370
373+
logger.debug(msg) // $ hasCleartextLogging=370
374+
logger.info(NSString(string: msg)) // $ hasCleartextLogging=370
375+
logger.notice(msg) // $ hasCleartextLogging=370
376+
logger.warning(msg) // $ hasCleartextLogging=370
377+
logger.error(msg) // $ hasCleartextLogging=370
378+
logger.critical(msg) // $ hasCleartextLogging=370
379+
logger.fatal(msg) // $ hasCleartextLogging=370
380380

381381
let logic = Logic()
382382
logic.addInt(authKey2) // GOOD: not logging
383-
logic.addString(authKey) // GOOD: not logging
383+
logic.addString(authKey) // $ SPURIOUS: hasCleartextLogging=383 (not logging)
384384
}

0 commit comments

Comments
 (0)