AI coding tools can generate application code quickly, but they can also introduce security problems that are easy to miss during development.
One important example is secret exposure.
A generated code change may accidentally contain an API key, database credential, access token, private key, connection string, or another sensitive value. The developer may not notice it because the value is buried inside a configuration file, test fixture, script, or generated code.
This becomes more important when AI coding agents can create changes across multiple files and prepare pull requests with less manual interaction.
GitHub Secret Scanning provides a way to detect credentials and other sensitive tokens that appear in repositories. The important engineering question is not only whether secrets are detected after code reaches a repository. Teams should also design their development workflow so that suspicious values are detected before a pull request is accepted.
A useful security workflow looks like this:
AI-generated code
|
v
Local validation
|
v
Secret scanning
|
v
Build and tests
|
v
Pull request
|
v
Security checks
|
v
Review
|
v
Merge
The goal is to make secret detection part of the normal development pipeline rather than treating it as a separate security activity.
Why AI-Generated Code Can Create Secret Risks
AI coding agents generally work with repository files, configuration, tests, and development instructions.
That means they may encounter sensitive values while performing legitimate tasks.
For example, a developer might ask an agent to configure an external API:
Add the payment service integration.
Use the existing configuration pattern
and add the required client initialization.
If the repository contains a credential in a configuration file or test fixture, the agent may copy or reproduce that value elsewhere.
Another risk occurs when a developer gives an AI tool sensitive information directly through a prompt or development environment.
There is also a simpler problem: generated examples may contain values that look like real credentials.
For example:
public class PaymentOptions
{
public string ApiKey { get; set; } =
"sk_live_example_value";
}
Even if the value was originally intended as a placeholder, committing credentials directly into source code creates a dangerous pattern that can later lead to real secret exposure.
What Counts as a Secret?
A secret is generally a value that can provide authentication, authorization, or access to a protected resource.
Common examples include:
Secret Type | Example |
|---|
API key | External service authentication key |
Access token | Token used to access an API |
Database credential | Username or password |
Cloud credential | Service account or access key |
Private key | Cryptographic private key |
Connection string | Database connection information |
OAuth credential | Client secret or access token |
Webhook secret | Value used to authenticate callbacks |
Not every sensitive-looking string is necessarily a secret.
For example:
Environment=Production
Region=India
ApplicationName=CustomerApi
does not provide access to a protected system.
Secret scanning therefore needs to distinguish actual credential patterns from normal configuration values.
Why Scan Before the Pull Request?
Scanning only after a pull request is created creates unnecessary exposure.
Consider this workflow:
Developer asks AI agent to modify application
|
v
Agent generates code
|
v
Secret accidentally included
|
v
Pull request created
|
v
Secret scanner detects it
The scanner has done its job, but the sensitive value has already entered the pull request workflow.
A better workflow is:
Developer asks AI agent to modify application
|
v
Agent generates code
|
v
Local secret scanning
|
v
Problem detected
|
v
Secret removed
|
v
Pull request created
This reduces the chance that developers or reviewers will accidentally copy or distribute the exposed value.
However, pre-PR scanning should complement repository-level and CI scanning rather than replace it. Multiple layers provide better coverage.
Enable Secret Scanning in the Repository
GitHub Secret Scanning can scan repository content for supported secret patterns.
The exact availability of secret scanning depends on the GitHub plan, repository configuration, and organization settings.
At the repository level, security features can be managed from the repository's security settings.
The important point is to enable the feature for repositories that handle application code, infrastructure configuration, automation, or other content that may contain credentials.
For organizations, security policies can also be standardized so that individual development teams do not have to configure every repository independently.
Add Secret Detection to the Development Workflow
Secret scanning should be treated as one stage of the development workflow.
A typical pull request workflow can include:
name: Pull Request Security
on:
pull_request:
jobs:
security:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Run tests
run: dotnet test --no-restore
- name: Run build
run: dotnet build --no-restore --configuration Release
The build and test stages above are only examples of a broader workflow. Secret scanning should be configured using the security tooling appropriate for the repository and GitHub configuration.
The important architectural idea is that security checks should execute automatically rather than depending on a developer remembering to run them.
Protect the AI Coding Workflow
When an AI agent is involved, secret protection should exist at multiple levels.
Repository level
Keep secrets out of source control whenever possible.
For example, instead of:
var connectionString =
"Server=db.example.com;User=admin;Password=secret";
use configuration:
var connectionString =
configuration.GetConnectionString("ApplicationDatabase");
Then provide the actual value through a protected configuration mechanism.
Development environment
Use environment variables, local secret stores, or other appropriate development mechanisms instead of committing credentials.
For an ASP.NET Core application, local development configuration can use the .NET user secrets mechanism:
dotnet user-secrets init
dotnet user-secrets set "ConnectionStrings:ApplicationDatabase" "your-local-value"
Application code can then read the configuration normally:
var connectionString =
builder.Configuration.GetConnectionString("ApplicationDatabase");
The source code does not need to contain the credential.
Pull request workflow
The pull request pipeline should include automated security checks.
This provides a second line of defense if a secret accidentally reaches the repository.
Production environment
Production credentials should come from a managed secret store or protected environment configuration rather than source code.
The exact service depends on the deployment platform, but the principle remains the same: source code should not be the storage location for production credentials.
Be Careful With Test Data
Developers often assume that test code is safe because it does not run in production.
That assumption can cause security problems.
For example:
[Fact]
public void PaymentConfiguration_ShouldWork()
{
var apiKey = "real-production-api-key";
// Test implementation
}
A test may eventually be copied, executed in CI, or inspected by another developer.
Use fake values that cannot authenticate against a real system:
[Fact]
public void PaymentConfiguration_ShouldWork()
{
var apiKey = "test-api-key";
// Test implementation
}
Better still, use dependency injection and test configuration so that secrets are never required in source code.
Handling a Secret Detection Alert
When a secret scanner reports a potential secret, do not simply delete the line and close the alert.
The correct response depends on whether the value is real.
If it is a false positive
Review the detected value and determine why it was identified.
If the value is clearly a non-sensitive test value, document the situation according to the repository's security process.
If it is a real credential
Treat the credential as compromised.
The general response is:
Identify the affected credential.
Revoke or rotate it.
Remove it from the source code.
Replace it with secure configuration.
Check whether the credential appeared elsewhere.
Review repository and CI history as appropriate.
Confirm that the replacement credential is protected.
Simply deleting the value from the latest version of the file is not necessarily enough.
A credential that has already been committed may remain in repository history, logs, artifacts, or other locations.
Avoid Embedding Secrets in AI Instructions
Developers should also consider what they provide to an AI coding agent.
For example, avoid prompts such as:
Use this production API key:
actual-production-secret-value
The better approach is to describe the configuration requirement:
Configure the payment client to read
Payment:ApiKey from application configuration.
Do not hard-code the value.
This gives the agent enough information to implement the integration without exposing the actual credential.
The same principle applies to:
The agent should know how the application obtains the secret, not necessarily the secret itself.
Common Mistakes
Relying only on pull request scanning
PR scanning is useful, but it should not be the only security layer.
Developers should also use secure local configuration and repository-level protection.
Assuming generated code is safe
AI-generated code should be reviewed like code written by a developer.
Generated code can contain incorrect configuration, insecure patterns, or sensitive values.
Using real credentials in tests
Test environments are still part of the software supply chain.
Use safe test credentials or mocks.
Deleting a leaked credential without rotating it
If a real credential was exposed, deleting it from the current file does not make the credential safe again.
Rotate or revoke it.
Putting secrets into source-controlled configuration
Files such as configuration templates can accidentally become credential storage.
Use placeholders and secure configuration mechanisms instead.
Ignoring development logs
An agent or build process may produce diagnostic output containing sensitive values.
Review logging and error handling to ensure credentials are not written to logs.
Best Practices
Keep production secrets outside source code.
Use managed secret storage or protected environment configuration for deployed applications.
Use local secret management for development instead of committing credentials.
Run secret detection before important changes are submitted.
Keep repository and pull request security checks enabled.
Treat AI-generated code as untrusted until it passes normal validation.
Never provide real production credentials to an AI agent when a configuration description is sufficient.
Use safe test values and mocks.
Rotate credentials immediately when a real secret is exposed.
Review security alerts instead of automatically dismissing them.
Keep security checks in CI so that local workflow differences do not become security gaps.
Periodically review repositories for old credentials and configuration mistakes.
Advantages
Adding secret scanning to an AI-assisted development workflow provides several benefits:
It reduces the chance of credentials being merged accidentally.
It creates an automated security control around generated code.
It provides an additional layer of protection beyond human review.
It helps identify secrets introduced through source files and configuration.
It encourages developers to separate application code from sensitive configuration.
It makes security part of the normal pull request workflow.
The biggest advantage is that secret detection becomes repeatable. Developers do not have to remember every possible credential pattern manually.
Disadvantages and Limitations
Secret scanning is not a complete application security solution.
A scanner may not recognize every possible secret, especially custom credentials or unusual formats. It can also produce false positives that require human review.
Scanning also does not prevent a developer or agent from exposing a secret through logs, external systems, prompts, screenshots, or other channels.
Another limitation is that detecting a secret does not automatically solve the incident. A real exposed credential may need immediate revocation and investigation.
For this reason, secret scanning should be one part of a broader secure development lifecycle.
Troubleshooting Secret Scanning Problems
A legitimate test value is detected
Check whether the value resembles a supported credential pattern. If it is genuinely non-sensitive, handle the alert according to your repository's security process.
A real secret was detected
Do not reuse it. Rotate or revoke the credential and remove the secret from the code and other affected locations.
The same secret keeps appearing
Search for other copies in configuration files, tests, scripts, documentation, generated files, and repository history where appropriate.
Developers keep adding secrets
Improve the workflow rather than relying only on reminders.
Use local secret management, repository checks, secure configuration, and automated pull request protection.
The AI agent keeps hard-coding configuration
Update the repository guidance to explicitly describe how configuration should be loaded and where secrets belong.
Summary
AI coding agents can accelerate software development, but generated code still needs the same security controls as manually written code.
Secret exposure is particularly important because a credential can be hidden inside a configuration file, test, script, or generated implementation and may not be noticed during normal code review.
GitHub Secret Scanning can help detect supported secrets, but the strongest approach is to combine scanning with secure configuration practices, automated pull request checks, CI validation, and careful handling of AI-generated code.
The development workflow should make the secure path the easiest path. Developers and AI agents should work with configuration references instead of real credentials, production secrets should remain outside source control, and exposed credentials should be rotated rather than simply deleted.
When secret detection becomes part of the normal development lifecycle, AI-generated code can move through the pull request process with a stronger security boundary and less dependence on manual inspection.
Join the conversation! Your thoughts help the community grow.