Repository navigation
Unified: New query for hard-coded cryptographic values - #23301
Conversation
Create the byte-for-byte baseline copies from original Swift for the unified Swift port of swift/constant-salt, including query help, sample, and query tests. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copy the data flow configuration and query-specific models from ConstantSaltExtensions.qll and ConstantSaltQuery.qll into ConstantSalt.ql, preserving declarations and comments unchanged. API adaptations follow separately. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Rewrite ConstantSalt.ql using unified taint tracking, literal salt sources, an append/interpolation barrier, and declarative encryption-salt sink models for CryptoSwift and RNCryptor. All ten RNCryptor constant-salt cases are detected and all string-appending cases match original Swift. Annotate the byte-array literal and String.utf8 cases in test.swift as MISSING, and mark the String.map false positives in rncryptor.swift caused by receiver-insensitive Data.map(_:) matching as SPURIOUS. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Unified Swift infers Int for integer literals in array literals such as [0x2a, 0x3a, ...], even when assigned to an Array<UInt8> variable. Treat non-empty array literals of integer literals as constant salt sources alongside Array<UInt8>-typed literals. This detects all four constantSalt cases in test.swift across HKDF, PKCS5.PBKDF1, PKCS5.PBKDF2, and Scrypt. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Remove the duplicate declaration of randomArray in the same function scope and use a realistic 32-byte derived key length and 16-byte random salt in ConstantSalt.swift. Validate query-help generation and external reference link. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Use a switch statement so goodSalt is used by at most one key derivation per call, and drop the redundant legacy PBKDF1 cases. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Include a copy of the ConstantSalt.swift sample with inline expectations so all three BAD cases and three GOOD cases are tested directly. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Name the model file after its cryptography library theme rather than the specific ConstantSalt query. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Drop redundant trailing '// GOOD' and '// BAD ... [NOT DETECTED]' markers on lines that already carry SPURIOUS or MISSING inline expectations, while keeping standalone '// GOOD' comments and concise explanations. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…alue Move the query, query help, sample, and test files from CWE-760/ConstantSalt to CWE-798/HardcodedCryptographicValue ahead of generalizing the query to hard-coded cryptographic values. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Update metadata, query id (unified/swift/hard-coded-cryptographic-value), class/module names, and sink-anchored alert messages using CryptographicValueKind so additional cryptographic sink kinds (such as IVs) can be enabled incrementally. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Update overview, recommendation, example prose, and references to cover hard-coded cryptographic values in general, and update the Okta reference to its canonical URL. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…e're supporting right now.
Merge the CryptoSwift and RNCryptor stubs and test cases from Swift's CWE-1204 StaticInitializationVector tests into test.swift and rncryptor.swift, with all IV expectations initially marked MISSING ahead of adding IV sink models. Use random dummy salts/IVs in the opposite test functions so salt and IV cases are tested independently. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Add encryption-iv sink models for CryptoSwift ciphers/modes and RNCryptor initializers and encryptData methods in crypto.model.json. This detects 27 of the 28 call-site IV test cases across RNCryptor (8/8) and CryptoSwift (19/20, with only the [UInt8](str.utf8) case remaining MISSING), while isBarrierOut suppresses the duplicate alert inside GCM's delegating convenience initializer. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
… as salts or ivs).
…(matching Rust and most other languages).
|
QHelp previews: unified/ql/src/queries/security/CWE-798/HardcodedCryptographicValue.qhelpHard-coded cryptographic valueHard-coded salts and initialization vectors should not be used for cryptographic operations.
RecommendationUse cryptographically secure random number generation to produce fresh salts, initialization vectors, and key material, and never hard-code passwords or keys in source code. ExampleThe following example shows several key-derivation calls using salts. In the 'BAD' cases, the salt is hard-coded, making the derived outputs vulnerable to precomputation and dictionary attacks. In the 'GOOD' cases, the salt is randomly generated, protecting against precomputation. func encrypt(choice: Int) throws {
// ...
let badSalt: Array<UInt8> = [0x2a, 0x3a, 0x80, 0x05]
let goodSalt = (0..<16).map({ _ in UInt8.random(in: 0...UInt8.max) })
let randomArray = (0..<10).map({ _ in UInt8.random(in: 0...UInt8.max) })
switch choice {
// BAD: Using constant salts for hashing
case 0:
_ = try HKDF(password: randomArray, salt: badSalt, info: randomArray, keyLength: 32, variant: Variant.sha2)
case 1:
_ = try PKCS5.PBKDF2(password: randomArray, salt: badSalt, iterations: 120120, keyLength: 32)
case 2:
_ = try Scrypt(password: randomArray, salt: badSalt, dkLen: 64, N: 16384, r: 8, p: 1)
// GOOD: Using randomly generated salts for hashing
case 3:
_ = try HKDF(password: randomArray, salt: goodSalt, info: randomArray, keyLength: 32, variant: Variant.sha2)
case 4:
_ = try PKCS5.PBKDF2(password: randomArray, salt: goodSalt, iterations: 120120, keyLength: 32)
default:
_ = try Scrypt(password: randomArray, salt: goodSalt, dkLen: 64, N: 16384, r: 8, p: 1)
}
// ...
}References
|
There was a problem hiding this comment.
🟡 Changes recommended
Unresolved query and modeling issues produce false positives for dynamic or externally sourced values and valid HKDF salts, while incomplete OCB coverage misses hard-coded nonces.
7 open issues
Remove the unconditional fixed-salt HKDF sink · New Model OCB nonce initializer overloads · New Use applicable salt/IV tags and security severity · New Require constant elements for byte-array sources · New Treat formatted output with arguments as a taint barrier · New Handle append(contentsOf:) as a taint barrier · New Stop taint propagation from external-input sources · New
What changed in this PR
Adds a unified Swift query combining hard-coded salt and static initialization-vector detection, following the Rust query’s design.
Changes:
- Introduces path-based detection with barriers for string and array appends.
- Adds CryptoSwift and RNCryptor models for salts, initialization vectors, and nonces.
- Adds query help, Swift examples, and inline-expectation tests.
| File | Description |
|---|---|
unified/ql/test/query-tests/security/CWE-798/HardcodedCryptographicValue/test.swift |
Adds CryptoSwift fixtures, salt/IV cases, and help-example tests. |
unified/ql/test/query-tests/security/CWE-798/HardcodedCryptographicValue/rncryptor.swift |
Adds RNCryptor fixtures and salt/IV flow cases. |
unified/ql/test/query-tests/security/CWE-798/HardcodedCryptographicValue/HardcodedCryptographicValue.qlref |
Configures query execution with inline-expectation postprocessing. |
unified/ql/test/query-tests/security/CWE-798/HardcodedCryptographicValue/HardcodedCryptographicValue.expected |
Records generated alert and flow-path expectations. |
unified/ql/src/queries/security/CWE-798/HardcodedCryptographicValue.swift |
Demonstrates fixed versus randomly generated salts. |
unified/ql/src/queries/security/CWE-798/HardcodedCryptographicValue.ql |
Defines query metadata, sources, sinks, barriers, and path alerts. |
unified/ql/src/queries/security/CWE-798/HardcodedCryptographicValue.qhelp |
Documents risks, recommendations, examples, and references. |
unified/ql/lib/ext/crypto.model.json |
Defines cryptographic argument sink models. |
Review effort: Max (adaptive)
tausbn
left a comment
There was a problem hiding this comment.
Overall, this looks good to me. I think some of the remaining comments from Copilot (surrounding CWE numbers and random arrays with a type we treat as constant) are probably worth addressing, but this could equally be done as follow-up work.
|
Thanks for your review (and prompt re-approval). Though annoyingly there was a merge conflict, which I've now fixed as well... |
|
Thanks again. Lets get this merged! 🚀 The docs review is still outstanding, I'm happy to receive feedback after the merge and I will make a new pull request addressing any concerns raised. |

Adds new query
unified/swift/hard-coded-cryptographic-value. This is a union of two queries in our original Swift support:swift/constant-saltandswift/static-initialization-vector- in particular that's where the tests and model targets come from. However the new query is styled afterrust/hard-coded-cryptographic-value. I believe the rust approach of having one unified query is cleaner, easier to maintain and extend (there are other kinds of cryptographic constants we may add in future), and produces a cleaner user experience (since none of these are very noisy queries).Notes:
"123" + "abc") are the same as in original Swift. There's a tradeoff here, unless we make analysis more complex (which is something we could consider in future)..qhelpis new, if close to what we had in Rust. I think it deserves a quick review from the docs team.edit: forgot to say most of the work was generated by Copilot, Gemini 4 Carbon, high effort. Carbon was pretty good though a little bit slow, I think I'll try one of the smaller Gemini models at some point.