How Secure is Code Generated by ChatGPT?
This paper evaluates the security of code generated by ChatGPT, finding that while the model is aware of potential vulnerabilities, it frequently produces source code that remains susceptible to specific attacks, even when prompted to improve security.
Original paper licensed under CC BY 4.0 (http://creativecommons.org/licenses/by/4.0/). This is an AI-generated explanation of the paper below. It is not written or endorsed by the authors. For technical accuracy, refer to the original paper. Read full disclaimer
Imagine you've hired a super-smart, incredibly fast apprentice coder named ChatGPT. This apprentice has read almost every book, tutorial, and code snippet ever written. You ask them to build you a house (a computer program), and they whip one up in seconds.
The big question this paper asks is: "Is this house safe to live in, or is it full of hidden traps?"
The researchers (the "home inspectors") decided to test this by asking ChatGPT to build 21 different types of digital structures, ranging from simple calculators to secure vaults. Here is what they found, explained simply:
1. The "Speed vs. Safety" Problem
When ChatGPT builds code on its own, it's like a chef who cooks a meal very quickly but forgets to wash their hands.
- The Result: Out of 21 programs, only 5 were built safely right from the start.
- The Analogy: Imagine asking ChatGPT to build a fence. It builds a beautiful fence, but it forgets to put a gate lock on it. Or it builds a door that opens from the outside without a handle. It looks like a fence, but a burglar can walk right through.
2. The "Honest but Clueless" Apprentice
Here is the most interesting part: ChatGPT knows it made a mistake, but it doesn't always fix it unless you specifically ask.
- The Scenario: The researchers asked, "Hey, if I throw a rock at your fence, will it break?"
- ChatGPT's Answer: "Oh! You're right, I didn't reinforce the wood. That's a bad idea. Here is how to fix it."
- The Catch: If you don't ask the specific question, ChatGPT just hands you the broken fence and says, "Here is your fence!" It assumes you are a nice person who won't try to break in. It doesn't naturally think like a hacker.
3. The "Magic Wand" Effect
The researchers found that if they acted like a skeptical security guard, they could force ChatGPT to build better code.
- The Process:
- Ask: "Build me a password generator." (ChatGPT builds a weak one).
- Probe: "Is this secure? What if a hacker tries to guess it?"
- Fix: "Please rebuild it so it's unbreakable."
- The Result: ChatGPT often produced a much stronger, secure version after being prompted. It's like telling the apprentice, "Hey, I need this to be a bank vault, not a cardboard box." Once they understand the stakes, they do a better job.
4. The "Language Confusion"
Sometimes, ChatGPT builds a safe house in one language (like Java) but a dangerous one in another (like C++), even when the instructions are the same.
- The Analogy: It's like asking an architect to build a bridge. If you ask for a "steel bridge," they build a strong one. If you ask for a "wooden bridge," they might use rotten wood because they saw a bad example in a book they read. The researchers couldn't always predict why it happened, which makes it tricky to rely on.
5. The "Ethical Paradox"
The paper points out a strange contradiction in ChatGPT's personality:
- Refusal: If you ask, "How do I build a bomb?" ChatGPT says, "No, I can't do that."
- Acceptance: If you ask, "How do I build a website that lets anyone delete your database?" ChatGPT says, "Sure, here is the code!" (even though that code is basically a bomb for your data).
- The Lesson: ChatGPT is great at following rules about what to build, but it's not always great at judging how dangerous the building is.
The Big Takeaway
ChatGPT is not ready to replace a professional security engineer.
Think of ChatGPT as a brilliant but naive intern.
- Don't let the intern run the whole project alone; you will get hacked.
- Do use the intern to get a head start, but you must act as the Senior Manager. You need to review their work, ask the hard questions ("Is this safe?"), and force them to fix the holes.
The paper concludes that while this AI is a fantastic teaching tool to help students learn how to code, we cannot trust it to write secure code on its own. It needs a human in the loop to double-check the locks and windows.
Drowning in papers in your field?
Get daily digests of the most novel papers matching your research keywords — with technical summaries, in your language.