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

Merged
felickz merged 5 commits into
mainfrom
rust/cryptographic-key-material
Nov 23, 2026
Merged

felickz merged 5 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 19 alerts and the logging test has 12, each with safe counterparts (including independent calls of one helper, or of a helper that wraps it, that fill a key and a logged request id, and a ring HKDF key that is hard-coded or generated and logged). 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, or fills one of its parameters in place, is recognized only when the fill is of the returned buffer or of the parameter itself. A fill of a part of a parameter (for example &mut out[..16]) keeps the source in the helper, shared by every call, 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

The Test Queries step splits the tests of a language into four slices and
starts one codeql test run per slice. The slices are made by test directory,
so a pull request with two Rust test directories extracts them at the same
time on a fresh runner.

The Rust extractor installs the toolchain that it needs with rustup on
demand. Both slices download it into the same ~/.rustup, and one of them
fails at extraction with "could not rename 'downloaded' file", which fails
Validate test results. Running the job again is unlikely to help, because
every runner starts without the toolchain.

Run Rust as a single slice, so that its test directories are extracted one
after the other. The other languages keep four slices.

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 23, 2026 15:23

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

Mutable-output helpers can share a source across calls, causing a separately generated and logged request ID to be falsely reported as leaked key material.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Track random sources per mutable-output helper call

rust/​src/​security/​CWE-312/​CleartextLoggingOfKeyMaterial.ql:66

A helper such as fill_random(out: &mut [u8]) that calls getrandom::getrandom(out) retains a shared source here because it does not return the buffer. If separate calls fill a key and a printed request ID, the key-use and logging checks can both succeed from that source, incorrectly reporting the request ID as leaked key material. Give mutable-output helpers call-site-specific sources too, and add a regression test with independent key and request-ID calls.

Review effort: Balanced

…ach call

A helper that fills a mutable parameter with random bytes, such as
fn fill_random(out: &mut [u8]), kept one source at the fill in its body,
shared by every call. A key and a logged request id that were filled by
separate calls shared it, so the request id was reported as key material.

Use the buffer that each call passes as the source instead, as is done for
a helper that returns the bytes, and drop the shared source in the helper.
A call that passes on a parameter of its own function is not a source, like
a nested returning wrapper.

Add regressions for independent calls that fill a key and a logged request
id, for a key that is filled by the helper and logged, and for a helper that
wraps the filling helper.

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 23, 2026 17:25
@felickz

felickz commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the third pass. The finding in the review body was valid and is fixed in f2237e8 with a failing test first.

  • With fn fill_random(out: &mut [u8]) called once for a key and once for a request id that is logged, the request id was reported as key material (test_independent_fill_helper_results), because the source was the fill inside the helper, shared by every call.
  • A call of a helper that fills a parameter in place is now the source, as it already was for a helper that returns the bytes: the argument that the call passes. The key that is filled by the helper and logged is still reported (test_logged_fill_helper_key).
  • Two limits are documented in the query help and the pull request body: a fill of only a part of a parameter (for example &mut out[..16]) keeps its source in the helper, and a call that passes on a parameter of the function that contains it is not a source (nested wrappers are not promoted, which test_independent_nested_fill_helper_results pins).

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

Human assessment is needed because neither test suite exercises the new HKDF sink, leaving its constructor recognition and role in the logging query unverified.

No open issues 🎉

Review effort: Balanced

Both queries treat the key that is passed to ring::hkdf::Prk::new_less_safe
as a key sink, but no test used it. Add a hard-coded HKDF key and a safe
counterpart to the hard-coded key test, and a generated key that is used for
HKDF and logged to the logging test. No query change: the sink was already
recognized.

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 23, 2026 18:21
@felickz

felickz commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Thanks. The HKDF key sink was already recognized, but you were right that no test used it. b371bac adds test_ring_hkdf_bad (a hard-coded key for ring::hkdf::Prk::new_less_safe) and a safe counterpart to the hard-coded key test, and test_logged_hkdf_key (a generated key that is used for HKDF and logged) to the logging test. Both queries report the bad cases, so there is no query change.

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

Subtle source-suppression gaps involving conversions and aliased parameters can conflate independent calls and produce false key-leak reports, requiring human assessment.

No open issues 🎉

Review effort: Balanced

@felickz
felickz marked this pull request as ready for review November 23, 2026 22:29
@felickz
felickz merged commit 62a1992 into main Nov 23, 2026
31 checks passed
@felickz
felickz deleted the rust/cryptographic-key-material branch November 23, 2026 22:29
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