+Output written to the console or to a log is commonly retained in shared log +storage, and is readable by operators, log aggregation services, and anyone else +with access to those logs. If a cryptographic key is written to such output, an +attacker who can read the logs can recover the key and use it to decrypt or forge +the data that it protects. +
+
+This query finds generated key material that is written to a log or to standard
+output or standard error, possibly after hex encoding. Results from key-generation
+APIs such as KeyInit::generate_key are already known to be keys, even
+when returned without being passed to a constructor. Generic randomly filled
+buffers, including results of helpers returning such buffers, must also flow to a
+cryptographic key API. The standard
+rust/cleartext-logging query relies on variable names to recognize
+sensitive data, and does not recognize names such as key_bytes.
+
+Do not write cryptographic keys to logs or to the console, even in encoded form. If +you need to identify a key in diagnostic output, log a non-secret identifier for +the key instead. +
++In the following example, a freshly generated AES-256-GCM key is hex-encoded and +printed: +
++In the following corrected example, only the key length is printed: +
++For generic random buffers, both key-use and logging flows must be available from +the same source in the global data-flow graph. Data-flow state and operation order +can prevent some combinations from being correlated. The query does not infer this +relationship from variable names or use of the same helper. +
+
+Helpers are recognized only when the filled buffer flows locally to a return
+value, including explicit return expressions. Each call to such a
+helper is a separate source unless its result is itself locally returned by
+another wrapper. Those shared inner-call sources are suppressed to avoid
+correlating independent outer calls. Nested returning wrappers are not promoted
+to sources, so even logging and key use of the same outer-call result can be
+missed. Logging inside a helper that returns the filled buffer is also not covered
+by this call-site model.
+
+A cryptographic key or passphrase that is embedded in source code can be recovered +by anyone with access to the source code or to the compiled binary. An attacker who +recovers the key can decrypt data that it protects, or forge ciphertexts and +authentication tags that the application will accept as genuine. +
+
+This query finds constant values (literals, constant arrays, and repeated-value
+arrays) that flow to the key or passphrase argument of a cryptographic API. It
+complements the standard rust/hard-coded-cryptographic-value query by
+recognizing additional APIs from the age, orion and
+ring crates, and keys that are wrapped with
+GenericArray::from or secrecy::Secret::new before use.
+Results that the standard query already reports are not repeated.
+
+Do not embed keys or passphrases in source code. Generate keys with a +cryptographically secure random number generator, or load them at runtime from a +protected secret store such as a key management service, and rotate any key that +has been committed to source control. +
++In the following example, an AES key is a fixed array of zero bytes: +
++In the following corrected example, the key is supplied at runtime from a protected +secret store: +
+