Skip to content
risk-governance security

AI Code Security Risks: What Engineering Teams Miss

The specific security patterns AI coding tools get wrong — dependency issues, auth bypasses, hardcoded secrets, and insecure defaults.

Pierre Sauvignon
Pierre Sauvignon March 16, 2026 14 min read
AI code security risks — what engineering teams miss

The biggest AI code security risks are hardcoded secrets, SQL injection via string concatenation, insecure dependency choices, authentication bypasses, overly permissive CORS, missing input validation, and insecure default configurations — all patterns that AI tools reproduce because they reflect the training data, which is full of insecure public code. These vulnerabilities are structurally predictable, meaning teams can detect and prevent them with targeted review practices. This article covers each pattern in detail: why the AI generates it, what the real-world impact looks like, and how to catch it before it ships.

AI coding tools do not write insecure code because they are malicious. They write insecure code because they are mirrors. They reflect the patterns they were trained on, and the internet is full of insecure code. Most teams run a linter, check for obvious issues, and move on — meanwhile, the AI-generated code that passed review carries vulnerabilities that a structured checklist would have caught. If you are managing AI-generated code in production, this is your detection checklist.

1. Hardcoded Secrets and API Keys

This is the most common and most preventable security risk in AI-generated code. It is also the one most teams think they have already solved.

Why AI generates it

AI coding tools learn from public repositories. Public repositories are full of example code with placeholder credentials, demo API keys, and hardcoded tokens. When a developer asks for code that connects to a database, calls an external API, or authenticates with a service, the tool reaches for patterns it has seen thousands of times. Those patterns include inline credentials.

The tool is not trying to expose your secrets. It is generating a working example. The problem is that “working example” code has a habit of becoming production code. A developer copies the generated function, replaces the placeholder with a real key to test it locally, and forgets to move it to an environment variable before committing.

Real-world impact

Hardcoded secrets in version control are permanent. Even if you delete the line in the next commit, the key persists in git history. Automated scanners sweep public repositories for exactly this pattern. Once a key is exposed, the window between exposure and exploitation is measured in minutes, not days.

The blast radius depends on the key. A database credential exposes your entire data layer. An API key for a payment provider enables unauthorized transactions. An internal service token can escalate into lateral movement across your infrastructure.

Detection method

Pre-commit hooks with secret scanning are the minimum. Tools like git-secrets, TruffleHog, or your CI provider’s built-in secret detection should run on every commit. But do not stop at commit time. Run periodic scans of your entire git history. AI-generated code increases the surface area for accidental secret inclusion, so increase your scanning frequency proportionally.

Establish a team policy: AI-generated code that includes any credential-like string — even an obvious placeholder — gets flagged in review. This trains developers to treat credential handling as a deliberate step, not an afterthought.

2. SQL Injection via String Concatenation

This vulnerability should have been solved twenty years ago. AI coding tools are bringing it back.

Why AI generates it

String concatenation for database queries is the single most common pattern in training data. It predates parameterized queries. It appears in tutorials, blog posts, Stack Overflow answers, and legacy codebases that have been public for decades. When a developer asks the AI for code that queries a database with user-provided input, the tool frequently produces something like "SELECT * FROM users WHERE id = " + userId — because that pattern outnumbers parameterized alternatives in the training corpus by an enormous margin.

The tool may produce parameterized queries some of the time. The inconsistency is the problem. You cannot rely on the tool to always get this right.

Real-world impact

SQL injection remains in the OWASP Top Ten for a reason. A single injectable endpoint can expose your entire database. Attackers can read, modify, or delete data. They can escalate privileges. In some configurations, they can execute operating system commands through the database server.

The impact is amplified when AI generates multiple endpoints in a session. If the developer does not catch the pattern in the first function, the same vulnerability propagates across every endpoint the tool produces in that context window.

Detection method

Static analysis tools that flag string concatenation in database queries are essential. Configure them to fail the build, not just warn. Add SQL injection-specific test cases to your CI/CD quality gates. If you use an ORM, enforce its use through linting rules and flag any raw query construction.

During code review, treat any AI-generated database interaction as high-risk. Check every query for parameterization, regardless of how simple it looks.

3. Overly Permissive CORS and Auth Configuration

AI coding tools optimize for making code work. Security configuration that prevents code from working gets removed.

Why AI generates it

When a developer is building an API and asks the AI to help with CORS configuration, the fastest path to “it works” is Access-Control-Allow-Origin: *. When authentication middleware causes a 401 during development, the fastest fix is to comment it out or set it to permissive mode. AI tools have learned this pattern from thousands of development tutorials and quick-start guides that prioritize functionality over security.

The generated code often includes comments like “configure for production” or “restrict in production” — but those comments are not enforcement mechanisms. They are wishes. And wishes do not survive the pressure to ship.

Real-world impact

Wildcard CORS allows any website to make authenticated requests to your API on behalf of your users. This enables cross-site request forgery at scale. Permissive authentication configurations create endpoints that anyone can access, bypassing your entire authorization model.

These vulnerabilities are particularly dangerous because they are invisible during normal testing. Everything works. The API responds correctly. The frontend renders as expected. The vulnerability only manifests when an attacker makes a request from an unauthorized origin or without valid credentials — and your API happily serves the response.

Detection method

Infrastructure-as-code scanning should flag wildcard CORS and disabled authentication middleware. Add automated tests that verify CORS headers are restrictive and that unauthenticated requests to protected endpoints return 401 or 403.

Maintain a security configuration checklist specifically for AI-generated API code. Review every CORS, authentication, and authorization configuration line-by-line, regardless of whether the AI included reassuring comments about security.

4. Outdated Dependencies with Known CVEs

AI tools recommend packages based on what appeared frequently in training data. Training data has a cutoff. The packages it recommends may have been compromised since.

Why AI generates it

The tool suggests package-name@2.3.1 because that version appeared in thousands of code samples during training. It does not check the npm advisory database, the CVE registry, or the package’s current release notes. It cannot. It generates code based on statistical patterns, not live vulnerability data.

This is not a flaw in the tool. It is a structural limitation. The tool does not have access to real-time security advisories. It recommends what it has seen, and what it has seen may be years out of date.

Real-world impact

A single dependency with a known vulnerability can compromise your entire application. Supply chain attacks through compromised packages are increasingly common. When the AI recommends an outdated version, it effectively pins your dependency to a snapshot in time that may include critical security flaws.

The risk multiplies across a team. If every developer is using AI tools that suggest the same outdated packages, the entire codebase converges on the same vulnerable dependency versions. This is the opposite of defense in depth.

Detection method

Automated dependency scanning in CI is non-negotiable. Tools like Dependabot, Snyk, or Socket should run on every pull request and block merges when critical CVEs are detected. Configure alerts for new advisories against your existing dependency tree.

Establish a policy that AI-generated dependency recommendations are treated as suggestions, not instructions. Every new dependency should be verified against the current advisory database before installation.

5. Missing Input Validation

This is the quiet one. No linter catches it. No scanner flags it. It just sits in your codebase, waiting for malformed input to arrive.

See how developers track their AI coding

Explore LobsterOne

Why AI generates it

AI tools generate code that handles the happy path. When a developer asks for an endpoint that accepts a user profile update, the tool produces a handler that reads the request body, updates the database, and returns a success response. It works perfectly with well-formed input.

What it does not do: validate that the email field contains an email. Check that the age field is a positive integer. Verify that the bio field does not contain 50 megabytes of text. Ensure that the user ID in the request matches the authenticated user. These validations do not appear in training data as often as the core logic, because they are the boring part. The AI skips the boring part.

Real-world impact

Missing input validation enables a range of attacks: buffer overflows from oversized inputs, type confusion from unexpected data types, business logic bypasses from unconstrained values, and denial-of-service from resource-exhaustive payloads.

The impact is context-dependent. An unvalidated search field might enable injection. An unvalidated file upload might enable remote code execution. An unvalidated numeric field in a financial calculation might enable fraud. The specific risk varies. The pattern is consistent: AI-generated code trusts input that should not be trusted.

Detection method

Schema validation at the API boundary is the strongest defense. Use request validation libraries that enforce schemas before your handler code executes. If input fails validation, it never reaches business logic.

Add negative test cases to your test suite. For every AI-generated endpoint, write tests with malformed input: wrong types, boundary values, oversized payloads, missing required fields. If the AI did not generate these tests (it usually does not), write them manually.

6. Insecure Deserialization

This risk is more subtle than the others. It requires understanding what happens when your application converts external data back into internal objects.

Why AI generates it

AI tools frequently generate code that deserializes user-supplied data — JSON, XML, YAML, or binary formats — without restricting what types can be instantiated. In languages like Java, Python, and PHP, unrestricted deserialization allows an attacker to supply crafted payloads that execute arbitrary code during the deserialization process.

The tool produces pickle.loads(data) or ObjectInputStream.readObject() because those are the common patterns in training data. The safe alternatives — using allowlists, switching to safer formats, or using restricted deserializers — appear less frequently.

Real-world impact

Insecure deserialization can lead to remote code execution. An attacker who can control the serialized data can execute arbitrary commands on your server. This is not theoretical. Deserialization vulnerabilities have been the root cause of major breaches across every industry — which is why insecure deserialization was included in the OWASP Top Ten and remains a priority in the OWASP Application Security Verification Standard.

The risk is highest when AI-generated code processes data from untrusted sources: API request bodies, message queue payloads, file uploads, or cached data that an attacker could tamper with.

Detection method

Static analysis rules should flag unsafe deserialization functions. Maintain a list of banned functions for your language and framework — pickle.loads, yaml.load without SafeLoader, unserialize in PHP — and enforce through linting.

In code review, ask a specific question for every AI-generated deserialization: “Can an attacker control this input?” If the answer is yes or maybe, the code needs a safe alternative.

7. Verbose Error Messages Leaking Internals

The final pattern is the one that makes all the others easier to exploit.

Why AI generates it

During development, detailed error messages are helpful. They show stack traces, database query details, file paths, and internal variable states. AI tools generate error handling that exposes this information because development-friendly error handling dominates training data.

The tool produces catch (error) { res.status(500).json({ error: error.message, stack: error.stack }) } because that pattern helps developers debug. It also helps attackers map your internal architecture, identify your technology stack, discover file paths, and locate potential injection points.

Real-world impact

Verbose error messages are a reconnaissance tool. They reveal database table names, file system structures, internal IP addresses, library versions, and sometimes partial query content. This information transforms a speculative attack into a targeted one.

An attacker who triggers an error on an AI-generated endpoint and receives a full stack trace now knows your framework, your ORM, your database driver, and potentially the structure of the query they need to inject into. Every subsequent attack is more precise because of the information your error handler volunteered.

Detection method

Enforce a global error handler that sanitizes responses in production. Error details should go to your logging system, not to the client. The client should receive a generic error message and a correlation ID for support purposes.

Add integration tests that verify error responses do not contain stack traces, file paths, or internal details. Run these tests against every environment, including staging, where verbose errors often persist because someone forgot to update the configuration.

The Structural Problem

These seven patterns share a root cause. AI coding tools are trained on code that was written to be read, understood, and demonstrated — not code that was written to be secure in production. Tutorials prioritize clarity. Stack Overflow answers prioritize conciseness. Open-source examples prioritize functionality. Security is the thing that gets added later, if it gets added at all.

This does not make AI coding tools dangerous. It makes them tools that require a specific kind of oversight. Your security processes need to account for the structural patterns that AI introduces. This means:

  • Pattern-specific static analysis. Configure your SAST tools to flag the seven patterns described above. Generic security scanning is not enough. You need rules that target the specific ways AI tools fail.
  • AI-aware code review. Reviewers should know that AI-generated code has predictable failure modes. Train your team to check for these patterns specifically, not just scan for general quality.
  • Quality gates in CI/CD. Automated checks should block code with these patterns from reaching production. A well-configured quality gate catches what review misses.
  • Dependency verification. Every AI-recommended package should be verified against current advisory databases before installation. This should be automated and enforced.

Building a Detection Practice

The teams that handle AI code security well do not rely on a single defense. They layer detection across the workflow:

  1. At generation time. Developers learn to recognize the seven patterns and reject them immediately. This is the cheapest detection point.
  2. At commit time. Pre-commit hooks catch secrets, banned functions, and unsafe patterns before they enter version control.
  3. At review time. Human reviewers apply AI-specific checklists. They know what to look for because the failure modes are predictable.
  4. At CI time. Automated scanning catches anything that slipped through. The build fails. The code does not merge.
  5. At runtime. Monitoring detects anomalous behavior that suggests a vulnerability is being exploited. This is the most expensive detection point, but it is your last line of defense.

The goal is not zero AI-generated vulnerabilities. The goal is a system where vulnerabilities are caught before they reach production, consistently, regardless of which developer generated the code or which AI tool they used.

The Takeaway

AI coding tools introduce security risks that are specific, predictable, and preventable. They are not random. They follow patterns that emerge directly from how the tools were trained. Hardcoded secrets, SQL injection, permissive CORS, outdated dependencies, missing validation, insecure deserialization, and verbose errors — these are not edge cases. They are the default output for many common prompts.

The teams that ship AI-generated code safely are not the teams that avoid AI tools. They are the teams that understand exactly where those tools fail and build detection into every stage of their pipeline. The security risks are real. The mitigations are straightforward. The only thing that is unacceptable is pretending the risks do not exist.

Pierre Sauvignon

Pierre Sauvignon

Founder

Founder of LobsterOne. Building tools that make AI-assisted development visible, measurable, and fun.

Related Articles