BitSphere
← Back to Insights

AI-Generated Code Can Work Perfectly and Still Be Insecure

AI-Generated Code Can Work Perfectly and Still Be Insecure

Just because something works on the surface, doesn’t mean it’s safe underneath.

I wanted to put together a quick proof of concept that needed to retrieve a user record from a database. Something that’s been done a million times and the perfect case for handing it to an AI agent.

I asked my agent to do that and also check the user’s password, then wrap it into a simple authentication method. My prompts resulted in the following line of code:

My prompts resulted in the following line of code:

string query = $"SELECT * FROM Users WHERE Email = '{username}' AND Password = '{password}'";

On the face of it, it does exactly what I asked for. It won’t return a user unless both the username and password match, a couple of quick tests confirmed that. If I had no knowledge of security principles, I could have easily left it there and pushed it all the way to production later on.

A couple of quick tests confirmed that.

The security problem

The problem is, this code has a pretty big security flaw (and any software engineer would probably shout about it if they saw it). SQL injection means an attacker could enter a certain value into the password field and end up authenticating successfully no matter what details they put in.

For example, putting:

a' OR 'a'='a

into the password box would turn the query into something like:

SELECT * FROM Users
WHERE Email = 'example@example.com' AND Password = '' OR 'a'='a'

This completely changes the logic. Instead of checking the real password, the database is now being asked a question that’s always true, which means it will always return a user. And now you’ve just let someone log in who shouldn’t be there.

In the interest of full transparency, I did try the same prompt a few more times and I did get back a more sensible solution, including suggestions for hashed passwords and using an auth library like bcrypt. But that’s sort of the point. The chance is always there that insecure code gets produced, and if nobody notices, it could end up in production.

The problem is that the AI agent didn’t challenge me in the same way a good developer would. It dutifully carried out my wishes without thinking around the problem or looking at the different angles.

This is where experience is still critical. If you're building an app with AI, BitSphere can provide the engineering oversight needed to make sure it isn't just functional, but secure and reliable. Contact us to discuss your project.