Skip to content

Unified: New query for hard-coded cryptographic values - #23301

Open
geoffw0 wants to merge 21 commits into
github:mainfrom
geoffw0:unifiedconstant
Open

geoffw0 wants to merge 21 commits into
github:mainfrom
geoffw0:unifiedconstant

Conversation

@geoffw0

@geoffw0 geoffw0 commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Adds new query unified/swift/hard-coded-cryptographic-value. This is a union of two queries in our original Swift support: swift/constant-salt and swift/static-initialization-vector - in particular that's where the tests and model targets come from. However the new query is styled after rust/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:

  • some missing test results will be addressed by flow models in future.
  • the spurious results should be fixed when we have better models-as-data method qualifier matching.
  • the missing test results involving string appends ("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).
  • a few of the models I think are less robust than the original Swift versions, noted for future improvement.
  • the .qhelp is new, if close to what we had in Rust. I think it deserves a quick review from the docs team.
  • DCA run to follow.

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.

geoffw0 and others added 21 commits November 27, 2026 17:10
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>
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>
@geoffw0
geoffw0 requested a review from a team as a code owner November 30, 2026 14:54
@geoffw0 geoffw0 added no-change-note-required This PR does not need a change note ready-for-doc-review This PR requires and is ready for review from the GitHub docs team. Unified labels Nov 30, 2026
Copilot AI adaptive review requested due to automatic review settings November 30, 2026 14:54
@github-actions

Copy link
Copy Markdown
Contributor

QHelp previews:

unified/ql/src/queries/security/CWE-798/HardcodedCryptographicValue.qhelp

Hard-coded cryptographic value

Hard-coded salts and initialization vectors should not be used for cryptographic operations.

  • Attackers can easily recover hard-coded values if they have access to the source code or compiled binary.
  • Some hard-coded values are easily guessable.
  • Use of hard-coded values may leave cryptographic operations vulnerable to dictionary attacks, rainbow tables, and other forms of cryptanalysis.

Recommendation

Use cryptographically secure random number generation to produce fresh salts, initialization vectors, and key material, and never hard-code passwords or keys in source code.

Example

The 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

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 ready-for-doc-review This PR requires and is ready for review from the GitHub docs team. Unified

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant