Repository navigation
[New Queries - githubsecuritylab/rust/hardcoded-cryptographic-key, githubsecuritylab/rust/cleartext-logging-of-key-material] - detect hard-coded and logged cryptographic keys in Rust - #259
Draft
felickz wants to merge 2 commits into
Conversation
Add githubsecuritylab/rust/hardcoded-cryptographic-key (CWE-321, CWE-798), which extends the standard rust/hard-coded-cryptographic-value query with the key and passphrase arguments of the age, orion and ring crates, and githubsecuritylab/rust/cleartext-logging-of-key-material (CWE-312, CWE-532), which reports a generated key that is written to a log or to the console. The two queries share common/CryptoKeyMaterial.qll. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 4124a708-0b20-41df-a2e2-c945fbf86752
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
The module-path regex misses Orion’s chacha20 key constructor, causing detection gaps, and the documentation overstates the safety guarantee of the order- and content-insensitive fill check.
2 open issues
What changed in this PR
Adds two experimental Rust security queries that complement the standard CodeQL queries by detecting hard-coded cryptographic keys and generated key material written to logs or the console.
Changes:
- Adds shared models for cryptographic APIs, random-buffer fills, key conversions, and hex encoding.
- Implements both path queries with filtering for duplicate reports and non-sensitive properties.
- Adds query help, examples, and real-crate regression fixtures with expected results.
| File | Description |
|---|---|
| rust/test/security/CWE-321/HardcodedCryptographicKey/test_ring.rs | Tests hard-coded and runtime ring keys. |
| rust/test/security/CWE-321/HardcodedCryptographicKey/test_rand.rs | Tests random-fill barriers and unrelated buffers. |
| rust/test/security/CWE-321/HardcodedCryptographicKey/test_orion.rs | Tests Orion key and password constructors. |
| rust/test/security/CWE-321/HardcodedCryptographicKey/test_filled.rs | Tests in-place buffer-fill handling. |
| rust/test/security/CWE-321/HardcodedCryptographicKey/test_camellia.rs | Tests wrapped Camellia keys. |
| rust/test/security/CWE-321/HardcodedCryptographicKey/test_age.rs | Tests direct and indirect age passphrases. |
| rust/test/security/CWE-321/HardcodedCryptographicKey/test_aes.rs | Tests AES conversions and duplicate suppression. |
| rust/test/security/CWE-321/HardcodedCryptographicKey/options.yml | Configures hard-coded-key fixture dependencies. |
| rust/test/security/CWE-321/HardcodedCryptographicKey/HardcodedCryptographicKey.qlref | Connects fixtures to the query. |
| rust/test/security/CWE-321/HardcodedCryptographicKey/HardcodedCryptographicKey.expected | Records expected alerts and flow paths. |
| rust/test/security/CWE-321/HardcodedCryptographicKey/Cargo.lock | Locks hard-coded-key test dependencies. |
| rust/test/security/CWE-312/CleartextLoggingOfKeyMaterial/test_ring.rs | Tests logging, helper returns, and safe outputs. |
| rust/test/security/CWE-312/CleartextLoggingOfKeyMaterial/test_rand.rs | Tests logged random keys and non-key buffers. |
| rust/test/security/CWE-312/CleartextLoggingOfKeyMaterial/test_aes.rs | Tests log macros and hex-encoded output. |
| rust/test/security/CWE-312/CleartextLoggingOfKeyMaterial/test_aes_gcm.rs | Tests generated keys and safe nonce/ciphertext output. |
| rust/test/security/CWE-312/CleartextLoggingOfKeyMaterial/options.yml | Configures logging fixture dependencies. |
| rust/test/security/CWE-312/CleartextLoggingOfKeyMaterial/CleartextLoggingOfKeyMaterial.qlref | Connects fixtures to the query. |
| rust/test/security/CWE-312/CleartextLoggingOfKeyMaterial/CleartextLoggingOfKeyMaterial.expected | Records expected logging alerts and paths. |
| rust/test/security/CWE-312/CleartextLoggingOfKeyMaterial/Cargo.lock | Locks logging test dependencies. |
| rust/src/security/CWE-321/HardcodedCryptographicKeyGood.rs | Illustrates a runtime-supplied key. |
| rust/src/security/CWE-321/HardcodedCryptographicKeyBad.rs | Illustrates a hard-coded AES key. |
| rust/src/security/CWE-321/HardcodedCryptographicKey.ql | Detects constant keys with barriers and deduplication. |
| rust/src/security/CWE-321/HardcodedCryptographicKey.qhelp | Explains detection and remediation. |
| rust/src/security/CWE-312/CleartextLoggingOfKeyMaterialGood.rs | Illustrates logging only key length. |
| rust/src/security/CWE-312/CleartextLoggingOfKeyMaterialBad.rs | Illustrates unsafe key logging. |
| rust/src/security/CWE-312/CleartextLoggingOfKeyMaterial.ql | Tracks generated key material to logging sinks. |
| rust/src/security/CWE-312/CleartextLoggingOfKeyMaterial.qhelp | Documents logging risks and detection limitations. |
| rust/src/common/CryptoKeyMaterial.qll | Shares key sinks, random-fill recognition, and conversions. |
Review effort: Balanced
…ill barrier The orion sink patterns used a module path class without digits, so orion::hazardous::stream::chacha20::SecretKey::from_slice and similar constructors were not recognized. Allow digits and add a regression. State in the query help that the in-place fill check ignores the order of the statements and the content that is written. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 4124a708-0b20-41df-a2e2-c945fbf86752
Contributor
There was a problem hiding this comment.
🔵 Needs a closer look
The pinned codeql/rust-all implementation was unavailable for inspection, leaving source, barrier, sink, and standard-query deduplication behavior dependent on human validation against the library and pinned-CLI results.
No open issues 🎉
2 resolved since last review
Review effort: Balanced
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Summary
Adds two experimental Rust queries about cryptographic key material, and the library they share:
githubsecuritylab/rust/hardcoded-cryptographic-key(CWE-321, CWE-798)githubsecuritylab/rust/cleartext-logging-of-key-material(CWE-312, CWE-532)rust/src/common/CryptoKeyMaterial.qll(key sinks, random buffers and key conversions)Why
rust/hard-coded-cryptographic-valuequery knows the RustCrypto key sinks, but not the key and passphrase constructors ofage,orionandring, and a key that is wrapped withGenericArray::fromorsecrecy::Secret::newloses the flow. The first query adds those, and does not repeat a result that the standard query already reports.rust/cleartext-loggingquery recognizes sensitive data by variable names, so a key calledkey_bytes, or a buffer that a random number generator filled, is missed. The second query reports generated key material that reaches a log or the console, also after hex encoding.What is reported
agepassphrase encryption and decryption,orionSecretKey::from_sliceandPassword::from_slice, andringAEAD, HMAC and HKDF keys. A buffer that is overwritten with random bytes, or filled in place (passed to a call as&mut buffer, or the target ofcopy_from_slice,filland similar methods), is a barrier.generate_key), or a buffer that a random number generator filled (ring,rand,getrandom) and that is also used as a key, that reaches a log macro or the console. A property of the key such as its length is not reported.Evidence
codeql test run), and both queries compile with--warnings=error.Known limitations
age,orionandringcall targets are matched by their canonical paths inCryptoKeyMaterial.qll. They could becomecredentials-keyandcredentials-passwordmodel rows later.Notes
@precision mediumfor both queries, tagssecurity,experimentaland the CWE tags. Both are selected byrust.qls.rust/src/common/because the Rust packs have nolibpack yet.