Blog / Build with AI /

Security basics for AI-built web apps

Ask an AI tool for a login page and you’ll get a login page. It will look right, it will probably work, and it may quietly store passwords in plain text. Nobody told it not to.

That’s the thing to understand before you put anything AI-built on the internet. The model is trying to give you code that runs. It is not trying to give you code that survives a stranger poking at it. Security is the part you have to ask for, and the part you have to check.

You don’t need to be a security engineer. Most attacks on small apps use the same handful of old tricks, and a handful of habits closes most of the doors. Here’s the list I’d run through before sharing any tool with other people.

1. Keep secrets out of the code you can see

API keys, database passwords and tokens must never sit in front-end code, meaning anything that runs in the visitor’s browser. If it’s in the page, it’s public. Anyone can open the browser’s developer tools and read it.

AI tools do this constantly, because the quickest way to make a demo work is to paste the key straight into the script. I wrote about why that’s a problem in what an API key is and why you should never share it. The fix is to keep the key on a server (or in environment variables) and let your page talk to your own small server endpoint instead.

A quick test: search your project for the word key, secret and token. If a real value turns up in a file that gets sent to the browser, move it today. If it was ever committed to a public repository, treat it as leaked and generate a new one.

2. Never trust what users type

Every text box is an invitation. People will type things you didn’t plan for, some by accident and some on purpose.

Two classic problems:

  • Cross-site scripting (XSS). Someone types a snippet of script into a comment box, and your app displays it back to other visitors as live code instead of plain text.
  • SQL injection. Someone types database commands into a field, and your app pastes them straight into a query.

The defence is boring and reliable. Treat input as text, never as code. Escape anything you display. If you use a database, use parameterised queries (sometimes called prepared statements), where the query and the data travel separately.

You can ask for this directly:

“Rewrite this so all database queries are parameterised, and all user-supplied text is escaped before it is displayed. Explain each change.”

Then read the answer. If the code still builds queries by gluing strings together, it hasn’t done what you asked.

3. Handle logins like they matter

If your app has accounts, three rules cover a lot of ground:

  • Don’t write your own password system if you can avoid it. Use the login features built into your framework, or a trusted service. Rolling your own is where the plain-text passwords come from.
  • Passwords must be hashed, using a purpose-built method such as bcrypt or Argon2. Not stored as typed, and not “encrypted” in a way you can reverse.
  • Check permissions on the server. Hiding a button from non-admins isn’t security. If the admin page still loads when someone types its address, anyone can use it.

That last one catches a lot of AI-built apps. The interface looks locked down, but the underlying requests answer to anybody.

4. Use HTTPS everywhere

If your tool sends anything personal, even just an email address, it needs HTTPS. Most hosts give you a free certificate now, so there’s little excuse. Check that the plain http:// address redirects to the secure one. I covered what the padlock does and doesn’t promise in the earlier explainer.

5. Be careful with dependencies

AI-written code loves to pull in packages and libraries. Each one is someone else’s code running inside your app.

  • Install only what you actually need.
  • Check that the package name is spelled correctly and does what you think. Look-alike names are a real way to get malicious code into a project.
  • Don’t paste in a script from a random address just because the AI suggested it.
  • Keep things updated. Old versions are where known holes live.

If the AI suggests a package you’ve never heard of, search for it before installing. It may not exist, or it may not be what the model described. That’s a separate problem I’ll come back to when we look at invented functions.

6. Limit what can go wrong

Plan for the day something does break:

  • Give every key the least access it needs. A key that only reads data is safer than one that can delete it.
  • Cap your spending. If your app calls a paid API, set a monthly limit in the provider’s dashboard. A bug or an abuser shouldn’t be able to run up a bill overnight.
  • Rate-limit forms and logins, so one visitor can’t send ten thousand requests.
  • Keep backups of anything you’d miss.

7. Ask the AI to review its own work, then don’t trust the review

This is useful, with a catch. Paste the code back and ask:

“Act as a security reviewer. List the vulnerabilities in this code, from most to least serious, and show how to fix each one.”

You’ll often get a decent list. But the same model that wrote the weak code can also miss the same weakness on review, so treat the answer as a prompt for your own checking, not a certificate. For anything holding real user data or payments, get a human who knows security to look at it. Asking a developer to look over your code is cheaper than apologising to users later, and the five mistakes in that post are a good first pass.

What to do if your app is small and personal

Not everything needs all seven. A page that only runs in your own browser, stores nothing and talks to no server has very little to attack. A calculator is not a bank.

The risk grows with three things: other people’s data, money, and a server that stays online. The moment your tool touches any of them, the list above stops being optional.

Key takeaways

  • AI writes code that works, not code that’s safe. Security is on you to request and verify.
  • Keep secrets off the browser side, treat all input as untrusted, and check permissions on the server.
  • Use HTTPS, install fewer packages, and set spending caps on paid APIs.
  • A security review from the same AI is a starting point, not a verdict.

If you’ve built something with AI and it’s about to go public, which of the seven would you be least comfortable explaining to a user? Start there.