AI Security

Generative AI Meets Cybersecurity: What Happens When AI Can Generate Code?

GitHub Copilot became generally available in June, and code-generating models are now part of everyday development. That has consequences for security on both sides: developers may ship vulnerable suggestions, and attackers can generate tooling faster. We look at the evidence and at what security teams should do now.

By EthicalHat Research · · 3 min read
Infographic: AI-generated code and security, by the numbers

In June, GitHub made Copilot, its AI pair programmer built on OpenAI's Codex model, generally available after a year-long preview. In February, DeepMind's AlphaCode showed a model reaching roughly median performance among human participants in programming competitions. Code-generating AI has moved from research to everyday tooling in about a year.

For security teams, that raises two questions. What happens to the security of our code when a model writes some of it? And what happens when attackers can generate code just as easily?

Question 1: is AI-generated code secure?

The most cited evidence comes from researchers at New York University. In Asleep at the Keyboard? Assessing the Security of GitHub Copilot's Code Contributions, first published in 2021 and presented at IEEE Security & Privacy this year, they asked Copilot to complete code in 89 scenarios designed around common weaknesses (from MITRE's CWE Top 25). Of the 1,689 programs it produced, about 40% contained vulnerabilities.

That is not surprising once you remember how these models learn: by imitating large amounts of public code, including insecure code. Typical problems include:

  • SQL injection from string-built queries,
  • path traversal from unsanitised file paths,
  • weak or outdated cryptography (MD5, ECB mode, hard-coded keys),
  • hard-coded credentials and secrets,
  • missing authorisation checks, and
  • use of vulnerable or deprecated library APIs.

A typical example of the pattern models reproduce, and the fix a reviewer should insist on:

python
# A common suggestion pattern: string formatting into SQL (injectable)
def get_user(conn, username):
    cur = conn.cursor()
    cur.execute(f"SELECT id, email FROM users WHERE username = '{username}'")
    return cur.fetchone()

# What reviewers should insist on: parameterised queries
def get_user(conn, username):
    cur = conn.cursor()
    cur.execute("SELECT id, email FROM users WHERE username = %s", (username,))
    return cur.fetchone()

The model is not malicious or careless. It predicts likely code. Likely is not the same as secure.

Question 2: what do attackers gain?

The same tools lower the effort needed to write attack tooling:

  • Faster scripting: scanners, credential-stuffing tools and exploit adaptations.
  • Porting and variation: rewriting existing malware in another language, or producing many functionally similar variants that evade signatures.
  • Lower skill barriers: less experienced actors can assemble working tools with help from a model.

Model providers apply usage policies and filters, but they are imperfect, and open models without such restrictions are appearing. We should assume attackers will use AI to write code faster, just as defenders will.

What security teams should do now

1. Set a policy for AI coding tools. Decide which tools are approved, whether proprietary code may be sent to them, and which settings to use (for example, filters that block suggestions matching public code).

2. Keep accountability with the developer. Whoever commits the code owns it, however it was written. Reviews apply equally.

3. Strengthen automated checks. SAST, dependency scanning and secret scanning in every pull request catch many of the patterns AI tends to reproduce. They matter more as code is produced faster.

4. Update secure coding training. Teach developers to recognise the vulnerability patterns AI suggests most often, and to treat suggestions like code from an unknown contributor.

5. Give models better examples. Assistants imitate the surrounding code. Secure helper functions, safe defaults and good patterns in your codebase lead to better suggestions.

6. Expect faster attacker iteration. Behaviour-based detection and rapid patching become more important as attackers produce new variants more cheaply.

The opportunity for defenders

Generative AI can also help security: explaining unfamiliar code during reviews, writing tests for security requirements, drafting detection rules and helping developers fix findings. Those uses are just beginning. The key is to adopt them with the same scepticism we apply to AI-generated application code.

The bottom line

AI code generation is here to stay, and it makes developers faster. It also reproduces insecure patterns at scale. Organisations that pair AI assistants with clear policies, strong automated security checks and developers trained to review suggestions critically will get the speed without inheriting the vulnerabilities.

Our secure code review and application security testing services help teams catch the vulnerabilities AI assistants introduce, and our security training covers secure use of coding assistants.

EthicalHat Research

The EthicalHat research team: penetration testers, SOC analysts and engineers writing about security, software engineering and AI.

Want an expert opinion on your own systems?

Email info@ethicalhat.com or call +1-844-838-4422. Our security engineers are happy to talk it through.