|
| 1 | +//codeql-extractor-options: -module-name Crypto |
| 2 | + |
| 3 | +enum Insecure { |
| 4 | + struct MD5 { |
| 5 | + static func hash<D>(data: D) -> [UInt8] { |
| 6 | + return [] |
| 7 | + } |
| 8 | + |
| 9 | + func update<D>(data: D) {} |
| 10 | + func update(bufferPointer: UnsafeRawBufferPointer) {} |
| 11 | + func finalize() -> [UInt8] { return [] } |
| 12 | + } |
| 13 | + struct SHA1 { |
| 14 | + static func hash<D>(data: D) -> [UInt8] { |
| 15 | + return [] |
| 16 | + } |
| 17 | + |
| 18 | + func update<D>(data: D) {} |
| 19 | + func update(bufferPointer: UnsafeRawBufferPointer) {} |
| 20 | + func finalize() -> [UInt8] { return [] } |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +func test1(passwd : UnsafeRawBufferPointer, encrypted_passwd : String, account_no : String, credit_card_no : String) { |
| 25 | + var hash = Crypto.Insecure.MD5.hash(data: passwd) // BAD |
| 26 | + hash = Crypto.Insecure.MD5.hash(data: encrypted_passwd) // GOOD (not sensitive) |
| 27 | + hash = Crypto.Insecure.MD5.hash(data: account_no) // BAD [NOT DETECTED] |
| 28 | + hash = Crypto.Insecure.MD5.hash(data: credit_card_no) // BAD |
| 29 | +} |
| 30 | + |
| 31 | +func test2(passwd : String, encrypted_passwd : String, account_no : String, credit_card_no : String) { |
| 32 | + var hash = Crypto.Insecure.SHA1.hash(data: passwd) // BAD |
| 33 | + hash = Crypto.Insecure.SHA1.hash(data: encrypted_passwd) // GOOD (not sensitive) |
| 34 | + hash = Crypto.Insecure.SHA1.hash(data: account_no) // BAD [NOT DETECTED] |
| 35 | + hash = Crypto.Insecure.SHA1.hash(data: credit_card_no) // BAD |
| 36 | +} |
| 37 | + |
| 38 | +func test3(passwd : String, encrypted_passwd : String, account_no : String, credit_card_no : String) { |
| 39 | + var hash = Crypto.Insecure.MD5() |
| 40 | + hash.update(data: passwd) // BAD |
| 41 | + hash.update(data: encrypted_passwd) // GOOD (not sensitive) |
| 42 | + hash.update(data: account_no) // BAD [NOT DETECTED] |
| 43 | + hash.update(data: credit_card_no) // BAD |
| 44 | +} |
| 45 | + |
| 46 | +func test4(passwd : String, encrypted_passwd : String, account_no : String, credit_card_no : String) { |
| 47 | + var hash = Crypto.Insecure.SHA1() |
| 48 | + hash.update(data: passwd) // BAD |
| 49 | + hash.update(data: encrypted_passwd) // GOOD (not sensitive) |
| 50 | + hash.update(data: account_no) // BAD [NOT DETECTED] |
| 51 | + hash.update(data: credit_card_no) // BAD |
| 52 | +} |
| 53 | + |
| 54 | +func test5(passwd : UnsafeRawBufferPointer, encrypted_passwd : UnsafeRawBufferPointer, account_no : UnsafeRawBufferPointer, credit_card_no : UnsafeRawBufferPointer) { |
| 55 | + var hash = Crypto.Insecure.MD5() |
| 56 | + hash.update(bufferPointer: passwd) // BAD |
| 57 | + hash.update(bufferPointer: encrypted_passwd) // GOOD (not sensitive) |
| 58 | + hash.update(bufferPointer: account_no) // BAD [NOT DETECTED] |
| 59 | + hash.update(bufferPointer: credit_card_no) // BAD |
| 60 | +} |
| 61 | + |
| 62 | +func test6(passwd : UnsafeRawBufferPointer, encrypted_passwd : UnsafeRawBufferPointer, account_no : UnsafeRawBufferPointer, credit_card_no : UnsafeRawBufferPointer) { |
| 63 | + var hash = Crypto.Insecure.SHA1() |
| 64 | + hash.update(bufferPointer: passwd) // BAD |
| 65 | + hash.update(bufferPointer: encrypted_passwd) // GOOD (not sensitive) |
| 66 | + hash.update(bufferPointer: account_no) // BAD [NOT DETECTED] |
| 67 | + hash.update(bufferPointer: credit_card_no) // BAD |
| 68 | +} |
0 commit comments