AI Agents and Coding Assistants
Security Mistakes AI Coding Tools Commonly Introduce
The weaknesses to look for in generated code: injection, missing permission checks, exposed secrets, weak defaults and more.
Key takeaways
- A program that passes its normal test can still let an attacker read, change or destroy data.
- Keep user input separate from commands, and check permission on the server for every sensitive action.
- Search for hard-coded secrets, disabled protections, overly broad access and detailed error messages.
- Combine automated scanners, written requirements and human review, since no single check finds everything.
AI coding tools can produce code that works and is also unsafe. The common problems are familiar ones from long before AI: user input treated as commands, missing permission checks, secrets written into files, weak or outdated protections, careless error handling and risky third-party packages. The difference is how quickly such code can appear, and how finished it looks.
A model learns from large amounts of existing code, which includes insecure examples and old habits. A request that says nothing about security often gets the shortest path to a working demonstration. You can lower the risk considerably if you know what to look for.
Input treated as instructions
Injection happens when text from outside gets interpreted as part of a command. The classic case is a database query built by joining strings.
# unsafe: the name becomes part of the command
query = "SELECT * FROM users WHERE name = '" + name + "'"
# safer: the value travels separately from the command
cursor.execute("SELECT * FROM users WHERE name = ?", (name,)) In the first version, a name containing a quote mark and extra query text can change what the database does. The second uses a parameter, a placeholder that the database fills with data only. The placeholder symbol varies between libraries, but the principle holds.
The same pattern appears elsewhere. Text placed into a web page without escaping can run as script in another visitor's browser, known as cross-site scripting. A file name from a user, joined onto a folder path, may reach files outside that folder. Input handed to a system shell may run extra commands. Ask of every outside value: could this be read as an instruction?
Missing permission checks
Generated applications often confirm that someone is signed in and stop there. If a page shows invoice number 1042 to any signed-in user who asks for it, changing the number reveals other people's invoices.
The fix is a check, on the server, that this particular user may perform this particular action on this particular record. It must exist for reading, editing, deleting and exporting alike. Hiding a link or disabling a button in the browser does not count, because requests can be sent without using your page.
Watch for administrator functions that anyone can call if they know the address, and for database services reached directly from the browser without access rules switched on. Test with two ordinary accounts and with no account at all.
Secrets in the open
A request to connect to a service may produce code with a key typed straight into it, often as a placeholder that a hurried person replaces with the real value. From there it can travel into a shared repository, a web page or a log file.
Keep secrets in protected settings on the server. Make sure local settings files are excluded from version control. If a real secret has been committed or published, replace it, since removing the line leaves it in the history. Take care also with what you paste into a chat with an assistant, and follow your organization's rules about it.
Weak protections and risky defaults
To make an error disappear, a tool may switch off the thing that raised it. These changes deserve a hard look.
| What you see | Why it is a problem |
|---|---|
| Certificate checking turned off | Connections can be intercepted |
| Any website allowed to call your service with credentials | Other sites may act as your users |
| Passwords stored as plain text or with a fast general hash | A stolen database exposes accounts |
| Homemade encryption or token schemes | Subtle flaws are easy to introduce |
| Predictable random numbers used for reset links | Links may be guessed |
| Debug mode left on in the live system | Internal details become visible |
| Wide-open file or database permissions | One fault reaches everything |
For passwords, use a well-reviewed library designed for password storage, which applies a deliberately slow algorithm with a unique random value (a salt) for each password. Better still for many projects is a trusted sign-in service, configured carefully.
Errors, logs and uploads
Detailed error messages help during development and help attackers afterward. A live system should show a plain message to the user and record the details where only your team can read them.
Logs themselves can become a leak if code records passwords, tokens or personal details. Generated debugging lines often print entire requests, so remove or trim them.
File uploads need limits on size and type, a new file name chosen by your system and storage where uploaded content cannot be run as a program. Code that fetches a web address supplied by a user needs restrictions too, or it may be tricked into contacting internal systems. Sign-in forms and costly operations need rate limits that slow repeated attempts.
Risks from the packages and the tool
A suggested library may be outdated, abandoned or carry known flaws. It may not exist at all, which an attacker can exploit by registering the name; see invented package names.
The assistant itself is another consideration when it can run commands or edit files. Instructions hidden in a web page, a document or a code comment can try to steer it, a problem called prompt injection. Limit what it can reach, review commands before they run and keep live credentials out of its environment, following least-access practice.
A worked example of what a short request leaves out
Say you ask for "a page where members can download their statements." A plausible first draft shows a sign-in screen, lists the member's files and serves whichever file name appears in the web address. In a demonstration it looks complete.
Now read it as someone with bad intentions. What happens if the file name is changed to another member's statement? What if it contains characters that climb to a parent folder? Can the link be used after signing out? Is each download recorded, and is there any limit on how many can be requested?
None of these questions appears in the original sentence, so there is no reason to expect the draft to answer them. A better request supplies the missing rules.
Members may download only their own statements.
Look files up by a record identifier, never by a path from the user.
Check ownership on the server for every download.
Signed-out requests are refused.
Log who downloaded what, without logging file contents.
Limit repeated requests from one account.
Write tests in which one member tries to fetch another's file. Then run those tests yourself, including the refusals. A test suite that only shows permitted downloads succeeding says nothing about the attack.
Layers of defense
No one measure catches everything, so stack several.
Put your security rules in the project instructions the assistant reads, for example that queries must use parameters and new endpoints must check ownership. This improves the first draft but does not guarantee it. Then verify, following a human review checklist.
Questions to ask about any change
- Where does outside data enter, and where does it end up?
- Who can call this, and what stops someone else?
- What secrets does it use, and where are they kept?
- What happens when it fails or is called thousands of times?
- Did any protection get weaker or disappear?
Test only systems you own or have written permission to test. For software that handles money, health records, identity documents or many people's data, arrange review by a qualified security professional and learn which legal duties apply.
Keep checking after release
Code that was acceptable last year may not be today. Flaws are found in libraries, services change their defaults and new features open new paths into old ones. Turn on dependency alerts where your tools offer them, apply security updates promptly and review who still has access to accounts and keys.
Be prepared for the day something goes wrong. Know how to take a feature offline, how to replace each secret, where the logs are and whom to call. Keep backups apart from the system they protect, and practice restoring one. A short written plan is far easier to follow than memory during a stressful hour.
Common questions
Can I just ask the assistant to make the code secure?
Asking helps it consider the issue and sometimes surfaces real faults. A general request cannot supply facts it lacks, such as who is allowed to see what, and its assurance is not evidence. Give specific rules and check them.
Is generated code less secure than human code?
It depends on the task, the tool, the instructions and the review. People write all of these mistakes too. Judge the code in front of you, whoever or whatever wrote it.
What should I do if I find a flaw in a live app?
Limit the damage first: disable the affected feature, replace exposed keys and keep the logs. Then fix, test and consider whether users or authorities must be told. Notification rules vary by place and type of data, so get advice promptly.