Skip to content

Unified: Add weak password hashing query - #23339

Open
geoffw0 wants to merge 9 commits into
github:mainfrom
geoffw0:unifiedpasswordhash
Open

geoffw0 wants to merge 9 commits into
github:mainfrom
geoffw0:unifiedpasswordhash

Conversation

@geoffw0

@geoffw0 geoffw0 commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Ports the query swift/weak-password-hashing to unified Swift as unified/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:

geoffw0 and others added 6 commits December 2, 2026 19:00
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>
@geoffw0 geoffw0 added the no-change-note-required This PR does not need a change note label Dec 2, 2026
@geoffw0
geoffw0 requested a review from a team as a code owner December 2, 2026 19:44
@geoffw0 geoffw0 added the Unified label Dec 2, 2026
Copilot AI adaptive review requested due to automatic review settings December 2, 2026 19:44
@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

QHelp previews:

unified/ql/src/queries/security/CWE-328/WeakPasswordHashing.qhelp

Use of an inappropriate cryptographic hashing algorithm on passwords

Hash 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:

  • Pre-image attacks. If you know a hash value h(x), you should not be able to easily find the input x.
  • Collision attacks. If you know a hash value h(x), you should not be able to easily find a different input y with the same hash value h(x) = h(y).
  • Brute force. If you know a hash value h(x), you should not be able to find an input y that computes to that hash value using brute force attacks without significant computational effort.
    All of MD5, SHA-1, SHA-2 and SHA-3 are weak against offline brute forcing, since they are not sufficiently computationally hard. This includes SHA-224, SHA-256, SHA-384 and SHA-512, which are in the SHA-2 family.

Password hashing algorithms should be slow and/or memory intensive to compute, to make brute force attacks more difficult.

Recommendation

For password storage, you should use a sufficiently computationally hard cryptographic hash function, such as one of the following:

  • Argon2
  • scrypt
  • bcrypt
  • PBKDF2

Example

The 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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation no-change-note-required This PR does not need a change note Unified

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants