Skip to content

Unified: Add weak sensitive data hashing query - #23354

Draft
geoffw0 wants to merge 15 commits into
github:mainfrom
geoffw0:unifiedsensitivehash
Draft

geoffw0 wants to merge 15 commits into
github:mainfrom
geoffw0:unifiedsensitivehash

Conversation

@geoffw0

@geoffw0 geoffw0 commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Ports the swift/weak-sensitive-data-hashing query to unified Swift as unified/swift/weak-sensitive-data-hashing. Most of the implementation work was done by Copilot (Gemini 4 Neon High Effort), in the same context as unified/swift/weak-password-hashing was ported - it seemed to know exactly what needed to happen for this closely related query.

There are some spurious test results, I believe all of these are due to the current limitation with models-as-data method qualifier matching. No missing results.

TODO:

  • this PR is currently atop the commits of Unified: Add weak password hashing query #23339 , since the tests are common between the two queries. When that's merged I'll update this PR so we see only the new commits, and take it out of draft.
  • review a DCA run.
  • address any Copilot comments.

geoffw0 and others added 15 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>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
…ne unchanged

Copy the original Swift WeakSensitiveDataHashing query, query help,
examples, .qlref, and .expected byte-for-byte into unified/ to establish
an unchanged baseline for the unified Swift port (reusing the shared
CWE-328 .swift test files).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…tion and models

Inline the data flow configuration and query-specific models unchanged
from WeakSensitiveDataHashingQuery.qll and
WeakSensitiveDataHashingExtensions.qll, marking their provenance before
adapting to unified Swift APIs.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…aseline

Adapt WeakSensitiveDataHashing.ql to unified Swift using SensitiveData
sources (excluding passwords, which are handled by
unified/swift/weak-password-hashing) and existing weak-hash-input-*
models from legacy-swift.model.json. Update the query ID, .qlref, and
inline expectations in the shared CWE-328 tests.

All 37 expected positive cases (including all 6 metatype/flow cases in
testWithFlowAndMetatypes, even the previously missed generic
testReceiver3) are detected with 0 testFailures (172 #select rows
total). Because LegacyMaD currently ignores receiver types for method
calls, SHA256/SHA384/SHA512 hash/update and SHA2/SHA3 calculate calls
currently produce SPURIOUS alerts documented via inline expectations.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…g.qhelp

Update the renamed OWASP Transport Layer Protection Cheat Sheet URL and
label to Transport Layer Security Cheat Sheet in
WeakSensitiveDataHashing.qhelp, and remove the stray executable bit
inherited from the original Swift files.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@geoffw0 geoffw0 added no-change-note-required This PR does not need a change note Unified labels Dec 3, 2026
@github-actions

github-actions Bot commented Dec 3, 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

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

Use of a broken or weak cryptographic hashing algorithm on sensitive data

Using a broken or weak cryptographic hash function can leave data vulnerable, and should not be used in security related code.

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).
    As an example, both MD5 and SHA-1 are known to be vulnerable to collision attacks.

Since it's OK to use a weak cryptographic hash function in a non-security context, this query only alerts when these are used to hash sensitive data (such as usernames, identifying information and private data).

Recommendation

Ensure that you use a strong, modern cryptographic hash function, such as:

  • Argon2, scrypt, bcrypt, or PBKDF2 for passwords and other data with limited input space where a dictionary-like attack is feasible.
  • SHA-2, or SHA-3 in other cases.
    Note that special purpose algorithms, which are used to ensure that a message comes from a particular sender, exist for message authentication. These algorithms should be used when appropriate, as they address common vulnerabilities of simple hashing schemes in this context.

Example

The following examples show a function for fetching data from a URL and computing a hash of the data, perhaps to check the data has not been tampered with.

In the first case the MD5 hashing algorithm is used that is known to be vulnerable to collision attacks.

func getContentsAndHash(url: URL) -> (Data, String)? {
    guard let data = try? Data(contentsOf: url) else {
        return nil
    }

    let digest = Insecure.MD5.hash(data: data) // BAD: MD5 is not suitable for hashing sensitive data.
    let hash = digest.map { String(format: "%02hhx", $0) }.joined()

    return (data, hash)
}

Here is the same function using SHA-512, which is a strong cryptographic hashing function.

func getContentsAndHash(url: URL) -> (Data, String)? {
    guard let data = try? Data(contentsOf: url) else {
        return nil
    }

    let digest = SHA512.hash(data: data) // GOOD: SHA-512 is suitable for hashing sensitive data.
    let hash = digest.map { String(format: "%02hhx", $0) }.joined()

    return (data, hash)
}

References

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.

1 participant