Skip to content

Chapter 103

Security when AI writes code with you

Secrets, permissions, code you have not read, and how to review what the model writes

Talqeen wrote this chapter and no editor has reviewed it yet. Search engines are not shown this page until it is reviewed.

When a language model writes part of your code, you remain responsible for it. The model does not know what sensitive data your app handles, or who may see what, unless you tell it. And it writes plausible code quickly, so it is easy to accept without reading. This chapter is about the habits that protect your project: where secrets live, what permissions you give the tool, and how to review what it writes.

Secrets

Keys and passwords belong in environment variables on the server, and .env files never enter the repository. Deny Claude Code from reading them in settings.json: what it does not read cannot appear in a conversation or a file it writes. In web apps, watch the variables that reach the browser: everything sent to the browser is visible to everyone.

A common mistake

const apiKey = "sk-live-1234"; // move it later

The right way

const apiKey = process.env.PAYMENT_API_KEY; // server only, set in the hosting settings

Permissions

Give the tool only what the task needs. Reading files and running tests is enough for most tasks. Commands such as mass deletion, deploying, or changing the production database should ask you every time. And if you add an MCP server, know what it can do: your project's server in Talqeen deletes nothing, and its write tools need a token with write scope.

Review what is written

Generated code needs the same review as a new colleague's code, and more. Look especially for these recurring mistakes:

Review checklist

  • Every user input is validated on the server, not only in the browser
  • Queries use parameters, not strings joined by hand (SQL injection)
  • Every page and API route checks who is asking and what they may do
  • No password, token or personal data appears in the logs
  • New packages the model added actually exist and are known

Prompt injection

If your app sends text a user wrote to a language model, that text may hold instructions trying to change the model's behaviour. Treat user text as data, not as instructions: put it inside clear boundaries, and never give the model permissions the user themselves should not have.

Ask for a security review

Only the prompt in the box is sent to that site, nothing from your projects.

Test yourself

1. Where should a secret API key live?
2. The model suggests a package you have never heard of. What do you do?