BrassCoders discovered hard-coded secrets in two of the fifteen AI-generated Python scripts they examined, exposing a concrete risk for developers who copy-paste code straight from large language model outputs. The findings show that a single misplaced key or password can turn a helpful snippet into a credential leak across version control and production environments.

What the test revealed

The first script, token_check.py, was produced from a prompt that asked for a function to sign session tokens and include a “usable example.” To make the code runnable, the model inserted a literal HMAC signing key directly in the source file.

  • Problem: The secret key resides in the code base.
  • Risk: Anyone with read access to the repository can see the key, and any deployment that pulls the file inherits the secret.
  • Consequence: An attacker who obtains the key can forge valid session tokens, bypassing authentication checks.

The second script, email_sender.py, answered a request for a function that sends email via SMTP. The model again supplied a literal password so the example would work out of the box.

  • Problem: The password appears as a plain-text string in the function call.
  • Risk: Rotating the password requires a code change and a new deployment, and the credential spreads to every environment that uses the file.
  • Consequence: The password can be harvested from source control, logs, or compiled packages, giving an adversary unauthorised access to the mail server.

Why AI spits out secrets

Large language models generate text by completing the prompt. When a user asks for a “usable example,” the model interprets that as “code that runs without additional setup.” It therefore fills in missing values—API keys, passwords, tokens—with plausible placeholders. The model has no awareness of secret-management best practices unless the prompt explicitly mentions them.

A recent Veracode analysis of AI-generated code found that 45 % of the snippets contain at least one vulnerability listed in the OWASP Top 10, with credential exposure representing a sizable share. The statistic underscores that the problem is not isolated to a few outliers; it is a systemic by-product of how these models are trained and prompted.

Mitigation steps developers can take now

The simplest defense is to keep any secret out of the code file itself. Environment variables are the most common, language-agnostic method:

# token_check.py – secure version
import os
import hmac
import hashlib

SECRET_KEY = os.environ["HMAC_SECRET_KEY"]

def sign_token(data: bytes) -> str:
    return hmac.new(SECRET_KEY.encode(), data, hashlib.sha256).hexdigest()
# email_sender.py – secure version
import os
import smtplib

smtp_password = os.environ["SMTP_PASSWORD"]
server = smtplib.SMTP("smtp.example.com", 587)
server.starttls()
server.login("noreply@example.com", smtp_password)

Using os.environ pulls the value from the runtime environment, keeping it out of version control and allowing rotation without touching source files. The same pattern works with configuration files that are excluded from commits, secret-management services, or container-orchestrated secrets.

Additional safeguards

  • Code reviews that flag literal strings matching common secret patterns (e.g., long alphanumeric sequences).
  • Static analysis tools tuned to detect hard-coded credentials in newly added files.
  • Prompt engineering: explicitly ask the model to “use environment variables for all secrets” or “omit real credentials.”
  • Post-generation linting: run a quick script that searches for suspicious literals before copying code into a project.

Counter-point: Does this mean AI code is unsafe?

The presence of hard-coded secrets does not imply that AI-generated code is universally insecure. In many cases, the model produces clean, well-structured logic that can accelerate development. The risk emerges when developers treat the output as production-ready without a security audit. Treat AI as a drafting assistant, not a substitute for established security practices.

What to watch next

  • Tooling updates: AI platforms are beginning to incorporate safety filters that replace secrets with placeholders. Monitoring those changes can reduce exposure.
  • Policy shifts: Organizations may formalise guidelines for AI-assisted coding, mandating secret-management checks as part of the CI pipeline.
  • Community patterns: As developers share more “secure prompts,” best-practice templates could become the default output for common tasks like token signing or email delivery.

ਮੁੱਖ ਨੁਕਤਾ: AI ਸਕਿੰਟਾਂ ਵਿੱਚ ਕੰਮ ਕਰਨ ਯੋਗ ਕੋਡ ਤਿਆਰ ਕਰ ਸਕਦਾ ਹੈ, ਪਰ ਜਦੋਂ ਤੱਕ ਡਿਵੈਲਪਰਜ਼ ਸੀਕਰੇਟ-ਮੈਨੇਜਮੈਂਟ ਅਨੁਸ਼ਾਸਨ ਨੂੰ ਲਾਗੂ ਨਹੀਂ ਕਰਦੇ, ਇਸ ਸਹੂਲਤ ਦੀ ਇੱਕ ਲੁਕੀ ਹੋਈ ਕੀਮਤ ਹੈ—ਐਕਸਪੋਜ਼ਡ ਕ੍ਰੈਡੈਂਸ਼ੀਅਲਜ਼ ਜੋ ਪੂਰੇ ਸਿਸਟਮ ਨੂੰ ਖਤਰੇ ਵਿੱਚ ਪਾ ਸਕਦੇ ਹਨ। ਹਰ ਸਨੀਪੇਟ ਨੂੰ ਇੱਕ ਡਰਾਫਟ ਵਜੋਂ ਮੰਨੋ, ਕਿਸੇ ਵੀ ਅਸਲ ਸੀਕਰੇਟਸ ਨੂੰ ਹਟਾ ਦਿਓ, ਅਤੇ ਕਮਿਟ ਕਰਨ ਤੋਂ ਪਹਿਲਾਂ ਉਹਨਾਂ ਨੂੰ ਐਨਵਾਇਰਨਮੈਂਟ ਵੇਰੀਏਬਲਜ਼ ਜਾਂ ਕਿਸੇ ਸਮਰਪਿਤ ਵੌਲਟ ਰਾਹੀਂ ਇੰਜੈਕਟ ਕਰੋ।