Builder15 min readBadge: Web Guardian

Web security basics: access checks, XSS, CORS and cookies

Most attacks on small apps use a few old tricks. Check every request on the server, keep what people type as text, keep data out of your commands, know what CORS really does, and set sign-in cookies safely.

Never trust the browser

Everything that arrives from a browser can be changed by the person using it: form fields, the URL, headers, cookies, even a price in a hidden input. DevTools or a one-line script can send anything.

So the server decides: who is this, are they allowed to do this, and does the data make sense? Checks in the browser are a convenience, never the lock.

Check access on every request

  • Broken access control is number one in the OWASP Top 10, as it was in 2021: apps that let people see or change things that aren't theirs.
  • Signing in answers who someone is. Each request must also ask whether they may do this: /api/orders/42 must check that order 42 belongs to the person asking.
  • Hiding a button isn't protection. Anyone can call the URL behind it.
  • Admin pages need a server-side check that the person is an admin, not a secret URL.
  • In Supabase, row level security runs this check inside the database for every query.

Cross-site scripting (XSS): text that becomes code

XSS happens when something a person typed goes into a page as HTML, and the browser runs it as code for whoever views it. That code acts as the victim: it can read the page, call your API and change their settings.

  • React shows {text} as plain text, so the default is safe.
  • The risky doors are innerHTML, outerHTML, document.write and React's dangerouslySetInnerHTML. Use textContent for text.
  • If you must show HTML that people wrote, such as rich comments, clean it with a sanitiser like DOMPurify first.
  • Links can carry code too. Only accept http: and https: links from people, never javascript:.

Injection: keep data out of commands

  • Never glue what people type into a SQL string. Use parameters, or a client such as supabase-js that sends values separately.
  • The same goes for shell commands and file paths: a name like ../../secrets can walk out of the folder you meant.
  • AI prompts are commands too. Text from a page or an email can carry instructions, so treat it as data and limit what your AI feature can do.

CORS is a browser rule, not a lock

  • The same-origin policy stops a page on one site from reading responses from another. CORS headers are how a server says which other sites may read it.
  • A CORS error comes from the browser: the request often reached the server, but the browser wouldn't hand your page the answer.
  • CORS doesn't protect your API. Scripts, curl and other servers ignore it, so your API still needs sign-in and access checks.
  • The usual fix for a third-party API is to call it from your own server route, which also keeps the key secret. Don't send Access-Control-Allow-Origin: * with private data.

Cookies and forged requests

  • Sign-in cookies should be HttpOnly (JavaScript can't read them), Secure (sent over HTTPS only) and SameSite=Lax or Strict.
  • Cross-site request forgery (CSRF) tricks someone's browser into sending a request to your site with their cookies. SameSite cookies stop most of it.
  • Change data with POST, PUT, PATCH or DELETE, never GET, so a link or an image can't change anything.
  • Next.js Server Actions turn away requests whose Origin doesn't match your site. Route handlers you write don't do that for you.

Security bugs to spot in AI-written code

  • innerHTML or dangerouslySetInnerHTML with anything a person typed.
  • A route that reads an id from the URL and returns the row without checking who owns it.
  • SQL built from template strings with user input.
  • Access-Control-Allow-Origin: * on routes that return private data.
  • Admin checks only in the page, not on the server.
  • Error messages that show visitors stack traces or database details.

Key terms

Broken access control
When an app lets people see or change things that aren't theirs, usually because the server never checked. Number one in the OWASP Top 10.
Cross-site scripting (XSS)
When text someone typed goes into a page as HTML and runs as code in other people's browsers.
Sanitiser (HTML)
A tool, such as DOMPurify, that strips anything that could run as code out of HTML before you show it.
SQL injection
When text someone typed is glued into a SQL query and changes what it does. Parameters prevent it.
Same-origin policy
The browser rule that stops a page on one site from reading responses from another site.
CORS
Cross-Origin Resource Sharing: headers a server sends to tell browsers which other sites may read its responses. It isn't a lock on your API.
Cross-site request forgery (CSRF)
A trick that makes someone's browser send a request to your site with their cookies. SameSite cookies and origin checks stop it.
Cookie
A small piece of data a site stores in your browser and sends back with requests, often to keep you signed in.
Row level security (RLS)
Database rules that decide which rows each user may read or change.
Prompt injection
Instructions hidden in content (an email, a web page) that try to hijack an AI.
See the full glossary

Try it

See XSS happen, then stop it

  1. Open the 'Text, not code' starter in the code playground. Add <b>hi</b> both ways. What's different?
  2. Add the <img src=x onerror=…> line from the task with innerHTML, and watch the page run code someone typed. Then add it with textContent.
  3. In your own app, search the code for innerHTML and dangerouslySetInnerHTML. Where does each value come from?
  4. Pick a route that takes an id. Signed in as one test user, ask for another test user's id. Does it say no?
  5. Ask your AI helper to review one route for access checks, injection and XSS, then check each thing it finds.

Ask an AI tutor about this guide

Copy a prompt into ChatGPT, Claude or Gemini to go deeper. Check what it tells you against the guide.

Study with an AI tutor: copy this whole guide with tutoring instructions, then paste it into your AI assistant.
  • Explain it like I'm 12
  • Quiz me (5 questions, one at a time)
  • Show me a real example for my situation
  • What do beginners get wrong?

Knowledge check

Five questions. Get 4 right to earn the Web Guardian badge. You can retake it as often as you like.

Question 1 of 5

The Delete button only shows for admins. Is the delete route safe?

Finished the Build safely guides? Take the Build safely track test for ten mixed questions.

My notes

Write what clicked, what confused you, and what you want to try. Saved in this browser only.

Learn it live

Digital Builders University runs small live workshops where you build something real with an instructor beside you, starting with Vibe Coding 101.

See workshops

Practise it

Last reviewed November 25, 2026. AI tools change fast; check each tool’s own site for current features, plans and prices.