Skip to content

[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
mainfrom
rust/cryptographic-key-material
Draft

felickz wants to merge 2 commits into
mainfrom
rust/cryptographic-key-material

Conversation

@felickz

@felickz felickz commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

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

  • The standard rust/hard-coded-cryptographic-value query knows the RustCrypto key sinks, but not the key and passphrase constructors of age, orion and ring, and a key that is wrapped with GenericArray::from or secrecy::Secret::new loses the flow. The first query adds those, and does not repeat a result that the standard query already reports.
  • The standard rust/cleartext-logging query recognizes sensitive data by variable names, so a key called key_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

  • Hard-coded key: a constant (a literal, a constant array or a repeated value array) that reaches the key or passphrase argument of a cryptographic API: the sinks of the standard query, age passphrase encryption and decryption, orion SecretKey::from_slice and Password::from_slice, and ring AEAD, 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 of copy_from_slice, fill and similar methods), is a barrier.
  • Logged key: a key from a key generation API (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

  • Tests use real crates (aes, aes-gcm, age, camellia, orion, ring, rand, getrandom, hex, log). The hard-coded key test has 18 alerts and the logging test has 10, each with safe counterparts. They pass with the pinned CLI (codeql test run), and both queries compile with --warnings=error.
  • On a random sample of 11 public Rust repositories (1,521 source files, plus the crates they depend on) the logging query reports 0 alerts. The hard-coded key query reports 3 alerts, all in the test code of one project: two fixed keys in unit tests, and a key of the wrong length that a test passes on purpose to check that it is rejected. So there is no finding in production code yet.
  • Cost: the queries' own predicates take about 2 to 3 seconds each on the largest database (about 19,000 Rust files, almost all of them dependency sources).

Known limitations

  • The logging query also reports a tool whose purpose is to generate a key and print it. The query help says to dismiss the alert in that case.
  • A buffer that is filled in place is treated as filled at every use, and what it is filled with is not checked. A constant key that is used before the fill, or that is filled with another constant, is not reported.
  • A helper that returns a randomly filled buffer is recognized only when the buffer flows locally to the return value, and nested helpers are not promoted to sources (see the query help).
  • The age, orion and ring call targets are matched by their canonical paths in CryptoKeyMaterial.qll. They could become credentials-key and credentials-password model rows later.

Notes

  • Metadata: @precision medium for both queries, tags security, experimental and the CWE tags. Both are selected by rust.qls.
  • The shared library is in rust/src/common/ because the Rust packs have no lib pack yet.
  • The pack version is not changed here. It changes when a release is cut.

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
Copilot AI balanced review requested due to automatic review settings November 22, 2026 03:02

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

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

Comment thread rust/src/common/CryptoKeyMaterial.qll Resolved
…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
Copilot AI balanced review requested due to automatic review settings November 22, 2026 03:50

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.

🔵 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

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants