Skip to content

Commit 2a69b03

Browse files
committed
Swift: Extend cleartext logging tests (stubs).
1 parent 8be6aed commit 2a69b03

File tree

1 file changed

+103
-49
lines changed

1 file changed

+103
-49
lines changed

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

Lines changed: 103 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ class NSObject { }
44

55
func NSLog(_ format: String, _ args: CVarArg...) {}
66
func NSLogv(_ format: String, _ args: CVaListPointer) {}
7+
78
func getVaList(_ args: [CVarArg]) -> CVaListPointer { return CVaListPointer(_fromUnsafeMutablePointer: UnsafeMutablePointer(bitPattern: 0)!) }
89

910
struct OSLogType : RawRepresentable {
@@ -92,34 +93,88 @@ extension String : CVarArg {
9293
public var _cVarArgEncoding: [Int] { get { return [] } }
9394
}
9495

96+
struct NSExceptionName {
97+
init(_ rawValue: String) {}
98+
}
99+
100+
class NSException : NSObject
101+
{
102+
init(name aName: NSExceptionName, reason aReason: String?, userInfo aUserInfo: [AnyHashable : Any]? = nil) {}
103+
class func raise(_ name: NSExceptionName, format: String, arguments argList: CVaListPointer) {}
104+
func raise() {}
105+
}
106+
107+
class NSString : NSObject {
108+
convenience init(string aString: String) { self.init() }
109+
}
110+
95111
// from ObjC API; slightly simplified.
96112
func os_log(_ message: StaticString,
97113
dso: UnsafeRawPointer? = nil,
98114
log: OSLog = .default,
99115
type: OSLogType = .default,
100116
_ args: CVarArg...) { }
101117

118+
// imported from C
119+
typealias FILE = Int32 // this is a simplification
120+
typealias wchar_t = Int32
121+
typealias locale_t = OpaquePointer
122+
func dprintf(_ fd: Int, _ format: UnsafePointer<Int8>, _ args: CVarArg...) -> Int32 { return 0 }
123+
func vprintf(_ format: UnsafePointer<CChar>, _ arg: CVaListPointer) -> Int32 { return 0 }
124+
func vfprintf(_ file: UnsafeMutablePointer<FILE>?, _ format: UnsafePointer<CChar>?, _ arg: CVaListPointer) -> Int32 { return 0 }
125+
func vasprintf_l(_ ret: UnsafeMutablePointer<UnsafeMutablePointer<CChar>?>?, _ loc: locale_t?, _ format: UnsafePointer<CChar>?, _ ap: CVaListPointer) -> Int32 { return 0 }
126+
127+
// custom
128+
func log(message: String) {}
129+
func logging(message: String) {}
130+
func logfile(file: Int, message: String) {}
131+
func logMessage(_ msg: NSString) {}
132+
func logInfo(_ infoMsg: String) {}
133+
func logError(errorMsg str: String) {}
134+
func harmless(_ str: String) {} // safe
135+
func logarithm(_ val: Float) {} // safe
136+
func doLogin(login: String) {} // safe
137+
138+
// custom
139+
class LogFile {
140+
func log(_ str: String) {}
141+
func trace(_ message: String?) {}
142+
func debug(_ message: String) {}
143+
func info(_ info: NSString) {}
144+
func notice(_ notice: String) {}
145+
func warning(_ warningMessage: String) {}
146+
func error(_ msg: String) {}
147+
func critical(_ criticalMsg: String) {}
148+
func fatal(_ str: String) {}
149+
}
150+
151+
// custom
152+
class Logic {
153+
func addInt(_ val: Int) {} // safe
154+
func addString(_ str: String) {} // safe
155+
}
156+
102157
// --- tests ---
103158

104159
func test1(password: String, passwordHash : String, passphrase: String, pass_phrase: String) {
105-
print(password) // $ hasCleartextLogging=105
106-
print(password, separator: "") // $ $ hasCleartextLogging=106
107-
print("", separator: password) // $ hasCleartextLogging=107
108-
print(password, separator: "", terminator: "") // $ hasCleartextLogging=108
109-
print("", separator: password, terminator: "") // $ hasCleartextLogging=109
110-
print("", separator: "", terminator: password) // $ hasCleartextLogging=110
160+
print(password) // $ hasCleartextLogging=160
161+
print(password, separator: "") // $ $ hasCleartextLogging=161
162+
print("", separator: password) // $ hasCleartextLogging=162
163+
print(password, separator: "", terminator: "") // $ hasCleartextLogging=163
164+
print("", separator: password, terminator: "") // $ hasCleartextLogging=164
165+
print("", separator: "", terminator: password) // $ hasCleartextLogging=165
111166
print(passwordHash) // safe
112167

113-
debugPrint(password) // $ hasCleartextLogging=113
168+
debugPrint(password) // $ hasCleartextLogging=168
114169

115-
dump(password) // $ hasCleartextLogging=115
170+
dump(password) // $ hasCleartextLogging=170
116171

117-
NSLog(password) // $ hasCleartextLogging=117
118-
NSLog("%@", password) // $ hasCleartextLogging=118
119-
NSLog("%@ %@", "", password) // $ hasCleartextLogging=119
120-
NSLog("\(password)") // $ hasCleartextLogging=120
121-
NSLogv("%@", getVaList([password])) // $ hasCleartextLogging=121
122-
NSLogv("%@ %@", getVaList(["", password])) // $ hasCleartextLogging=122
172+
NSLog(password) // $ hasCleartextLogging=172
173+
NSLog("%@", password) // $ hasCleartextLogging=173
174+
NSLog("%@ %@", "", password) // $ hasCleartextLogging=174
175+
NSLog("\(password)") // $ hasCleartextLogging=175
176+
NSLogv("%@", getVaList([password])) // $ hasCleartextLogging=176
177+
NSLogv("%@ %@", getVaList(["", password])) // $ hasCleartextLogging=177
123178
NSLog(passwordHash) // safe
124179
NSLogv("%@", getVaList([passwordHash])) // safe
125180

@@ -129,39 +184,38 @@ func test1(password: String, passwordHash : String, passphrase: String, pass_phr
129184
log.log("\(password)") // safe
130185
log.log("\(password, privacy: .auto)") // safe
131186
log.log("\(password, privacy: .private)") // safe
132-
log.log("\(password, privacy: .public)") // $ MISSING: hasCleartextLogging=132
187+
log.log("\(password, privacy: .public)") // $ MISSING: hasCleartextLogging=187
133188
log.log("\(passwordHash, privacy: .public)") // safe
134189
log.log("\(password, privacy: .sensitive)") // safe
135-
log.log("\(bankAccount)") // $ MISSING: hasCleartextLogging=135
136-
log.log("\(bankAccount, privacy: .auto)") // $ MISSING: hasCleartextLogging=136
190+
log.log("\(bankAccount)") // $ MISSING: hasCleartextLogging=190
191+
log.log("\(bankAccount, privacy: .auto)") // $ MISSING: hasCleartextLogging=191
137192
log.log("\(bankAccount, privacy: .private)") // safe
138-
log.log("\(bankAccount, privacy: .public)") // $ MISSING: hasCleartextLogging=138
193+
log.log("\(bankAccount, privacy: .public)") // $ MISSING: hasCleartextLogging=193
139194
log.log("\(bankAccount, privacy: .sensitive)") // safe
140-
log.log(level: .default, "\(password, privacy: .public)") // $ MISSING: hasCleartextLogging=140
141-
log.trace("\(password, privacy: .public)") // $ MISSING: hasCleartextLogging=141
195+
log.log(level: .default, "\(password, privacy: .public)") // $ MISSING: hasCleartextLogging=195
196+
log.trace("\(password, privacy: .public)") // $ MISSING: hasCleartextLogging=196
142197
log.trace("\(passwordHash, privacy: .public)") // safe
143-
log.debug("\(password, privacy: .public)") // $ MISSING: hasCleartextLogging=143
198+
log.debug("\(password, privacy: .public)") // $ MISSING: hasCleartextLogging=198
144199
log.debug("\(passwordHash, privacy: .public)") // safe
145-
log.info("\(password, privacy: .public)") // $ MISSING: hasCleartextLogging=145
200+
log.info("\(password, privacy: .public)") // $ MISSING: hasCleartextLogging=200
146201
log.info("\(passwordHash, privacy: .public)") // safe
147-
log.notice("\(password, privacy: .public)") // $ MISSING: hasCleartextLogging=147
202+
log.notice("\(password, privacy: .public)") // $ MISSING: hasCleartextLogging=202
148203
log.notice("\(passwordHash, privacy: .public)") // safe
149-
log.warning("\(password, privacy: .public)") // $ MISSING: hasCleartextLogging=149
204+
log.warning("\(password, privacy: .public)") // $ MISSING: hasCleartextLogging=204
150205
log.warning("\(passwordHash, privacy: .public)") // safe
151-
log.error("\(password, privacy: .public)") // $ MISSING: hasCleartextLogging=151
206+
log.error("\(password, privacy: .public)") // $ MISSING: hasCleartextLogging=206
152207
log.error("\(passwordHash, privacy: .public)") // safe
153-
log.critical("\(password, privacy: .public)") // $ MISSING: hasCleartextLogging=153
208+
log.critical("\(password, privacy: .public)") // $ MISSING: hasCleartextLogging=208
154209
log.critical("\(passwordHash, privacy: .public)") // safe
155-
log.fault("\(password, privacy: .public)") // $ MISSING: hasCleartextLogging=155
210+
log.fault("\(password, privacy: .public)") // $ MISSING: hasCleartextLogging=210
156211
log.fault("\(passwordHash, privacy: .public)") // safe
157212

158-
NSLog(passphrase) // $ hasCleartextLogging=158
159-
NSLog(pass_phrase) // $ hasCleartextLogging=159
213+
NSLog(passphrase) // $ hasCleartextLogging=213
214+
NSLog(pass_phrase) // $ hasCleartextLogging=214
160215

161216
os_log("%@", log: .default, type: .default, "") // safe
162-
os_log("%@", log: .default, type: .default, password) // $ hasCleartextLogging=162
163-
os_log("%@ %@ %@", log: .default, type: .default, "", "", password) // $ hasCleartextLogging=163
164-
217+
os_log("%@", log: .default, type: .default, password) // $ hasCleartextLogging=217
218+
os_log("%@ %@ %@", log: .default, type: .default, "", "", password) // $ hasCleartextLogging=218
165219
}
166220

167221
class MyClass {
@@ -175,16 +229,16 @@ func doSomething(password: String) { }
175229
func test3(x: String) {
176230
// alternative evidence of sensitivity...
177231

178-
NSLog(x) // $ MISSING: hasCleartextLogging=179
232+
NSLog(x) // $ MISSING: hasCleartextLogging=233
179233
doSomething(password: x);
180-
NSLog(x) // $ hasCleartextLogging=179
234+
NSLog(x) // $ hasCleartextLogging=233
181235

182236
let y = getPassword();
183-
NSLog(y) // $ hasCleartextLogging=182
237+
NSLog(y) // $ hasCleartextLogging=236
184238

185239
let z = MyClass()
186240
NSLog(z.harmless) // safe
187-
NSLog(z.password) // $ hasCleartextLogging=187
241+
NSLog(z.password) // $ hasCleartextLogging=241
188242
}
189243

190244
struct MyOuter {
@@ -199,7 +253,7 @@ struct MyOuter {
199253
func test3(mo : MyOuter) {
200254
// struct members...
201255

202-
NSLog(mo.password.value) // $ hasCleartextLogging=202
256+
NSLog(mo.password.value) // $ hasCleartextLogging=256
203257
NSLog(mo.harmless.value) // safe
204258
}
205259

@@ -223,39 +277,39 @@ func test4(harmless: String, password: String) {
223277
print(myString1) // safe
224278

225279
print(password, to: &myString2)
226-
print(myString2) // $ hasCleartextLogging=225
280+
print(myString2) // $ hasCleartextLogging=279
227281

228282
print("log: " + password, to: &myString3)
229-
print(myString3) // $ hasCleartextLogging=228
283+
print(myString3) // $ hasCleartextLogging=282
230284

231285
debugPrint(harmless, to: &myString4)
232286
debugPrint(myString4) // safe
233287

234288
debugPrint(password, to: &myString5)
235-
debugPrint(myString5) // $ hasCleartextLogging=234
289+
debugPrint(myString5) // $ hasCleartextLogging=288
236290

237291
dump(harmless, to: &myString6)
238292
dump(myString6) // safe
239293

240294
dump(password, to: &myString7)
241-
dump(myString7) // $ hasCleartextLogging=240
295+
dump(myString7) // $ hasCleartextLogging=294
242296

243297
myString8.write(harmless)
244298
print(myString8)
245299

246300
myString9.write(password)
247-
print(myString9) // $ hasCleartextLogging=246
301+
print(myString9) // $ hasCleartextLogging=300
248302

249303
myString10.write(harmless)
250304
myString10.write(password)
251305
myString10.write(harmless)
252-
print(myString10) // $ hasCleartextLogging=250
306+
print(myString10) // $ hasCleartextLogging=304
253307

254308
harmless.write(to: &myString11)
255309
print(myString11)
256310

257311
password.write(to: &myString12)
258-
print(myString12) // $ hasCleartextLogging=257
312+
print(myString12) // $ hasCleartextLogging=311
259313

260314
print(password, to: &myString13) // $ safe - only printed to another string
261315
debugPrint(password, to: &myString13) // $ safe - only printed to another string
@@ -270,14 +324,14 @@ func test5(password: String, caseNum: Int) {
270324

271325
switch caseNum {
272326
case 0:
273-
assert(false, password) // $ MISSING: hasCleartextLogging=273
327+
assert(false, password) // $ MISSING: hasCleartextLogging=327
274328
case 1:
275-
assertionFailure(password) // $ MISSING: hasCleartextLogging=275
329+
assertionFailure(password) // $ MISSING: hasCleartextLogging=329
276330
case 2:
277-
precondition(false, password) // $ MISSING: hasCleartextLogging=277
331+
precondition(false, password) // $ MISSING: hasCleartextLogging=331
278332
case 3:
279-
preconditionFailure(password) // $ MISSING: hasCleartextLogging=279
333+
preconditionFailure(password) // $ MISSING: hasCleartextLogging=333
280334
default:
281-
fatalError(password) // $ MISSING: hasCleartextLogging=281
335+
fatalError(password) // $ MISSING: hasCleartextLogging=335
282336
}
283337
}

0 commit comments

Comments
 (0)