Repository navigation
Conversation
Copy the original Swift WeakPasswordHashing query, query help, examples, and query tests byte-for-byte into unified/ to establish an unchanged baseline for the unified Swift port. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…nd models Inline the data flow configuration and query-specific models unchanged from WeakPasswordHashingQuery.qll, WeakPasswordHashingExtensions.qll, and WeakSensitiveDataHashingExtensions.qll, marking their provenance before adapting to unified Swift APIs. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Adapt WeakPasswordHashing.ql to unified Swift using SensitiveData password sources, existing models-as-data sinks from legacy-swift.model.json, and an implementation barrier covering HMAC/PBKDF1/PBKDF2 declarations and extensions. Update the query ID, .qlref, and inline test expectations. All 48 original positive cases are detected with 0 testFailures. Because LegacyMaD currently ignores receiver types for method calls, overloaded sink names (hash, update, calculate) match multiple algorithm models per call site (152 #select rows total). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Correct the four CryptoSwift SHA3 sink models in legacy-swift.model.json that mistakenly used weak-password-hash-input-SHA2 instead of weak-password-hash-input-SHA3, and update WeakPasswordHashing.expected. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Add test cases covering the HMAC/PBKDF1/PBKDF2 implementation barrier (both struct and extension declarations, plus a control helper) in testCryptoSwift.swift, and a SHA256.self metatype password-hashing case in testCryptoKit.swift. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Update the libsodium bindings anchor from #bindings-programming-languages to #bindings-for-programming-languages in WeakPasswordHashing.qhelp. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
QHelp previews: unified/ql/src/queries/security/CWE-328/WeakPasswordHashing.qhelpUse of an inappropriate cryptographic hashing algorithm on passwordsHash functions that are not sufficiently computationally hard can leave data vulnerable. You should not use such functions for password hashing. A strong cryptographic hash function should be resistant to:
Password hashing algorithms should be slow and/or memory intensive to compute, to make brute force attacks more difficult. RecommendationFor password storage, you should use a sufficiently computationally hard cryptographic hash function, such as one of the following:
ExampleThe following examples show two versions of the same function. In both cases, a password is hashed using a cryptographic hashing algorithm. In the first case, the SHA-512 hashing algorithm is used. It is vulnerable to offline brute force attacks: let passwordData = Data(passwordString.utf8)
let passwordHash = Crypto.SHA512.hash(data: passwordData) // BAD: SHA-512 is not suitable for password hashing.
// ...
if Crypto.SHA512.hash(data: Data(passwordString.utf8)) == passwordHash {
// ...
}Here is the same function using Argon2, which is suitable for password hashing: import Argon2Swift
let salt = Salt.newSalt()
let result = try! Argon2Swift.hashPasswordString(password: passwordString, salt: salt) // GOOD: Argon2 is suitable for password hashing.
let passwordHash = result.encodedString()
// ...
if try! Argon2Swift.verifyHashString(password: passwordString, hash: passwordHash) {
// ...
}References
|
There was a problem hiding this comment.
🟡 Changes recommended
Receiver-unaware sink matching produces duplicate and unrelated alerts, while incomplete source and callable-invocation modeling misses weak password hashes.
5 open issues
What changed in this PR
Ports Swift’s weak-password hashing query to unified CodeQL analysis.
Changes:
- Adds the query, documentation, and secure/insecure examples.
- Adds CryptoKit, CryptoSwift, and Argon2 test fixtures with generated expectations.
- Corrects SHA3 algorithm labels in existing models.
| File | Description |
|---|---|
unified/ql/test/query-tests/security/CWE-328/WeakPasswordHashing.qlref |
Runs the query with inline expectations. |
unified/ql/test/query-tests/security/CWE-328/WeakPasswordHashing.expected |
Records generated results and paths. |
unified/ql/test/query-tests/security/CWE-328/testCryptoSwift.swift |
Tests CryptoSwift hashing and implementation barriers. |
unified/ql/test/query-tests/security/CWE-328/testCryptoKit.swift |
Tests CryptoKit hashing, updates, and metatypes. |
unified/ql/test/query-tests/security/CWE-328/testArgon2Swift.swift |
Covers suitable password hashing. |
unified/ql/src/queries/security/CWE-328/WeakPasswordHashingGood.swift |
Shows an Argon2 example. |
unified/ql/src/queries/security/CWE-328/WeakPasswordHashingBad.swift |
Shows inappropriate SHA-512 password hashing. |
unified/ql/src/queries/security/CWE-328/WeakPasswordHashing.ql |
Adds password-to-hashing taint analysis. |
unified/ql/src/queries/security/CWE-328/WeakPasswordHashing.qhelp |
Explains risks and recommended alternatives. |
unified/ql/lib/ext/legacy-swift.model.json |
Corrects SHA3 sink labels. |
Review effort: Max (adaptive)
| ["", "SHA3", true, "calculate(for:)", "", "", "Argument[for:]", "weak-password-hash-input-SHA2", "manual"], | ||
| ["", "SHA3", true, "process(block:currentHash:)", "", "", "Argument[block:]", "weak-password-hash-input-SHA2", "manual"], | ||
| ["", "SHA3", true, "update(withBytes:isLast?:)", "", "", "Argument[withBytes:]", "weak-password-hash-input-SHA2", "manual"], | ||
| ["", "SHA3", true, "callAsFunction(_:)", "", "", "Argument[0]", "weak-password-hash-input-SHA3", "manual"], |
| */ | ||
| module WeakPasswordHashingConfig implements DataFlow::ConfigSig { | ||
| predicate isSource(DataFlow::Node node) { | ||
| node.(SensitiveData).getClassification() = SensitiveDataClassification::password() |
| exists(Parameter param, ClassLikeDeclaration cls, string name | | ||
| node.asExpr() = param.getPattern() and | ||
| param.getParent().getParent+() = cls and |
| predicate isBarrierOut(DataFlow::Node node) { | ||
| // make sinks barriers so that we only report the closest instance | ||
| isSink(node) | ||
| } |


Ports the query
swift/weak-password-hashingto unified Swift asunified/swift/weak-password-hashing. Most of the implementation work was done by Copilot (Gemini 4 Neon High Effort). I did a bit of cleanup after, and it cleaned up my cleanup after that. Go figure.Note that all of the missing results in the test are for
unified/swift/weak-sensitive-data-hashing, another query which hasn’t been ported yet. It's next on my list after this one is merged. The tests for this query and that one are shared, since they’re very similar in requirements and structure.TODO: