Skip to content
Interview prep · questions

Security interview questions

10 questions, from core to senior. Say your answer out loud first, then open the model answer and check it against the points a strong answer covers.

On the server, how do you prevent cross-site scripting with output encoding, and what does a Content Security Policy add?

  • Core
Show the model answer

XSS happens when untrusted data reaches a page and the browser reads it as code. The main defense is to encode output for the place where it goes. In HTML text, characters such as the angle brackets and the ampersand become entities. Inside an attribute, I also encode quotes and always quote the value. Inside a script, or in a URL, the rules are different again, so I avoid putting user data there, and I allow only http and https links. A template engine with auto-escaping does the HTML case for me, and every raw output is a place I review. Content Security Policy is a response header that tells the browser which scripts may run. A strict policy uses a new random nonce on each response, so only script tags that carry it run, and it does not allow unsafe-inline. Injected inline scripts and inline event handlers then do not run. I start in report-only mode to find what breaks.

A strong answer covers

  • Encodes output for its context: HTML text, attribute, script, URL
  • Uses a template engine with auto-escaping and reviews every raw output
  • Explains a nonce-based CSP: a new random value on every response
  • Avoids unsafe-inline, so injected inline scripts are blocked
  • Rolls out CSP in report-only mode first

Likely follow-up

Why is a CSP that allows a whole CDN domain for scripts weaker than it looks?

How do you protect a cookie-based API against CSRF on the server, and what does same-site mean for SameSite cookies?

  • Core
Show the model answer

CSRF works because the browser attaches cookies to a request that another site starts. SameSite limits that. With Strict, the cookie is never sent on a cross-site request. With Lax, it is sent only on top-level navigations with a safe method such as GET, so a GET must never change state. With None, it is always sent, and the cookie must also be Secure. Same-site is not the same as same-origin. A site is the scheme plus the registrable domain, so two subdomains of example.com are the same site. If one subdomain is compromised, SameSite does not stop it. So I add a second check on every request that changes state. I check the Origin header, or the Sec-Fetch-Site header, against my own origin and reject others. Or I use a CSRF token tied to the session. I set SameSite on every cookie explicitly, because browser defaults are not the same everywhere.

A strong answer covers

  • Explains Strict, Lax and None, and that None requires Secure
  • Says Lax still sends cookies on top-level GET, so GET must be safe
  • Says same-site means the registrable domain, so sibling subdomains count
  • Adds a server check: Origin or Sec-Fetch-Site, or a CSRF token

Likely follow-up

Your API accepts only JSON with a custom header. Is it safe from CSRF without a token?

What is wrong with this code, and how do you fix it?

  • Core
// name comes from the query string
const rows = await db.query(
  "SELECT * FROM users WHERE name = '" + name + "'"
);

// fixed
const rows = await db.query(
  "SELECT * FROM users WHERE name = $1",
  [name]
);
Show the model answer

The first query builds SQL by joining strings, so the input becomes part of the SQL code. That is SQL injection. If name is a quote followed by OR 1=1 and a comment, the query returns every user. Worse inputs can read other tables or change data. The fix is a parameterised query, like the second one. The SQL text and the values go to the database separately, so a value is always data and never code, whatever it contains. Escaping by hand is not a fix, because it is easy to miss a case. Parameters work only for values. A table name, a column name or the direction in ORDER BY cannot be a parameter, so for those I check the input against a fixed allowlist. ORMs and query builders use parameters, but their raw query functions bring the risk back. As a second layer, the database user of the app has only the rights it needs.

A strong answer covers

  • Says string building makes the input part of the SQL code
  • Fixes it with a parameterised query: SQL and values sent separately
  • Says manual escaping is not a reliable fix
  • Says identifiers such as column names need an allowlist
  • Knows raw query functions in an ORM can bring the risk back

Likely follow-up

Users can sort a table by any column. How do you build that query safely?

How do you store user passwords, and why is SHA-256 with a salt not good enough?

  • Core
Show the model answer

I never store the password itself, and I never encrypt it, because encryption can be reversed. I store a hash made with a password hashing function. My first choice is Argon2id, and bcrypt is still acceptable. SHA-256 is the wrong tool because it is designed to be fast. If the database leaks, an attacker with GPUs can try a huge number of guesses per second against each hash. Argon2id is slow on purpose and uses a lot of memory for each guess, which makes guessing in parallel on GPUs expensive. bcrypt is slow too, with a cost factor, and it uses only the first 72 bytes of the input. A salt is a random value, unique for each user and stored next to the hash. It means two users with the same password get different hashes, and precomputed tables do not work. I use the library's verify function to check a password. When I raise the cost settings, I rehash at the next successful login.

A strong answer covers

  • Stores a hash from a slow password hashing function, not encryption
  • Names Argon2id first, with bcrypt as an accepted choice
  • Explains why a fast hash such as SHA-256 is easy to guess at scale
  • Says a unique random salt per user defeats precomputed tables
  • Rehashes at login when the cost settings change

Likely follow-up

What is a pepper, and where would you keep it?

How do you manage secrets such as database passwords and API keys in a backend service?

  • Core
Show the model answer

Secrets never go in the code, the repository, a container image or a log. The service reads them at run time, from environment variables that the platform injects or from a secrets manager. Each environment has its own secrets, so a leak in staging does not open production. Only the services and people that need a secret can read it, and reads are logged. Where the platform allows it, I prefer short-lived credentials that the service gets from its identity, so there is no long-lived key to steal. I plan rotation from the start. The service can accept two keys for a while, so I add the new one, switch over, and then remove the old one with no downtime. I run a secret scanner in CI and before commits. If a secret leaks, the first step is to revoke and rotate it. Removing it from the git history is not enough, because someone may already have a copy.

A strong answer covers

  • Keeps secrets out of code, images and logs, and reads them at run time
  • Uses separate secrets for each environment
  • Plans rotation with two valid keys for a while, so there is no downtime
  • Prefers short-lived credentials from the service's identity
  • Revokes and rotates first after a leak

Likely follow-up

How do you find out whether a secret has already been written to your logs?

What does CORS protect, what does it not protect, and what are the common server mistakes?

  • Core
Show the model answer

CORS does not add protection. It relaxes the same-origin policy, which stops a script on one site from reading responses from another origin. That protects users: a page on another site cannot read your API's answers using the user's cookies. CORS does not protect the API itself. A script, curl or another server ignores CORS completely, so it is not authentication and not access control. It also does not stop CSRF, because a simple cross-site request is still sent; the browser only hides the response. The common mistakes are on the server. One is copying any Origin header into Access-Control-Allow-Origin together with Allow-Credentials true, which lets every site read user data. Another is matching origins with a loose pattern, so a domain like example.com.attacker.net passes. A third is allowing the origin null, which sandboxed pages can send. I keep an exact allowlist, and I send Vary: Origin when the answer depends on it.

A strong answer covers

  • Says CORS relaxes the same-origin policy and does not add protection
  • Says it protects users in the browser, not the API from other clients
  • Says it is not authentication and does not stop CSRF
  • Names the mistakes: reflecting any origin with credentials, loose matching, allowing null
  • Uses an exact allowlist and Vary: Origin

Likely follow-up

Is Access-Control-Allow-Origin with a star dangerous for a public API with no cookies?

What does least privilege mean for services and database users, and how do you apply it?

  • Senior
Show the model answer

Least privilege means each person, service and process has only the rights it needs for its job, and nothing more. The goal is to make a breach small. If an attacker takes over one service, they get only what that service could do. For the database, the app does not connect as the owner or a superuser. Migrations run with a separate user that can change the schema. The app's user can read and write only its own tables, and it cannot drop them. A reporting job gets a read-only user. For services, each one has its own identity and its own credentials, never a shared key. Its cloud role allows only the actions and the resources it uses, not a wildcard. In the network, a service can reach only the services it calls. The container runs as a non-root user. I review rights regularly, because they grow over time and are rarely removed.

A strong answer covers

  • Defines it: only the rights a job needs, so a breach stays small
  • Separates the migration user from the app user, with no superuser for the app
  • Gives each service its own identity and narrow, non-wildcard permissions
  • Limits the network and runs containers as non-root
  • Reviews rights regularly, because they only grow

Likely follow-up

A developer needs to read production data to debug an incident. How do you give access safely?

What should a backend log for security, and what must it never log?

  • Core
Show the model answer

I log the events I need to detect and investigate an attack. That means logins that succeed and fail, password and email changes, permission denials, admin actions, and validation failures at a high rate. Each entry has the time, a request ID, the user ID, the source IP and the result. I never log passwords, session IDs, access or refresh tokens, API keys, full card numbers, or the full Authorization header. Personal data such as emails, addresses or health data I keep out, or I mask it, because logs are copied to many places and kept for a long time, and privacy laws apply to them too. Secrets often leak by accident: a token in a query string ends up in proxy logs, or a whole request body is logged on an error. So I use structured logs, with a redaction step that removes known sensitive fields. Access to logs is limited, and old logs are deleted on a schedule.

A strong answer covers

  • Logs security events: logins, permission denials, admin actions
  • Includes who, when, from where, a request ID and the result
  • Never logs passwords, tokens, session IDs, API keys or card numbers
  • Keeps personal data out of logs or masks it
  • Uses structured logs with redaction, limited access and a retention period

Likely follow-up

You find access tokens in a week of logs. What do you do?

Your service fetches a URL that the user gives, for example to show a link preview. What is SSRF, and how do you defend against it?

  • Senior

The model answer to this senior question opens with a year in the app. What a year includes

A JWT arrives with this header. What are the common mistakes when servers verify and use JWTs?

  • Senior
{ "alg": "none", "typ": "JWT" }

The model answer to this senior question opens with a year in the app. What a year includes