Auth & Security Questions with Answers

11 practice questions on Auth & Security (React & Web) with answers and step-by-step solutions. Try 10 samples below, then practise all 11 free in the AptiNest app.

Sample questions

Question 1Easy

What does CORS control in a browser?

  1. Whether a server can read the user disk
  2. How data is compressed on the network
  3. Whether a page may read responses from another origin
  4. How passwords are stored in a database

Answer: C. Whether a page may read responses from another origin

Explanation

CORS is enforced by browsers using headers such as Access-Control-Allow-Origin set by the server.

Step-by-step solution

  1. Same-origin policy blocks cross-origin reads.
  2. CORS headers relax it safely.

Why the other options are wrong

  • A. Browsers do not give servers disk access.
  • B. Compression is a separate header.
  • D. CORS is not about storage.

Question 2Easy

Which is a main advantage of token-based (JWT) authentication over server sessions for APIs?

  1. Tokens can never be stolen
  2. Tokens remove the need for HTTPS
  3. The server keeps no per-user session state
  4. Tokens never expire

Answer: C. The server keeps no per-user session state

Explanation

A signed token carries claims the server can verify, so no session lookup is needed.

Step-by-step solution

  1. Token is signed.
  2. Server verifies signature.
  3. No stored session needed.

Why the other options are wrong

  • A. Tokens can be stolen if leaked.
  • B. HTTPS is still needed.
  • D. Tokens normally carry an expiry.

Question 3Easy

Which statement correctly separates authentication from authorisation?

  1. They are two names for the same process
  2. Authorisation happens before authentication
  3. Authentication is only for admins
  4. Authentication proves identity; authorisation grants access

Answer: D. Authentication proves identity; authorisation grants access

Explanation

First verify identity, then check permissions.

Step-by-step solution

  1. Identity first.
  2. Permissions next.

Why the other options are wrong

  • A. They solve different problems.
  • B. You must know the user first.
  • C. Every user is authenticated.

Question 4Medium

Which technique prevents SQL injection most reliably?

  1. Parameterised queries (prepared statements)
  2. Escaping with a custom regex only
  3. Hiding the database port
  4. Using longer table names

Answer: A. Parameterised queries (prepared statements)

Explanation

Placeholders keep user input as data, never as part of the SQL command.

Step-by-step solution

  1. Query text and values are separate.
  2. Input cannot change the query structure.

Why the other options are wrong

  • B. Ad hoc escaping is easy to get wrong.
  • C. This does not stop injection through the app.
  • D. Names do not affect injection.

Question 5Medium

Where is it generally safer to keep a session or refresh token in a browser app to reduce XSS theft?

  1. In an HttpOnly, Secure cookie
  2. In localStorage
  3. In a global JavaScript variable
  4. In the URL query string

Answer: A. In an HttpOnly, Secure cookie

Explanation

HttpOnly cookies cannot be read by JavaScript, limiting what an XSS payload can steal.

Step-by-step solution

  1. XSS runs script in the page.
  2. HttpOnly hides the cookie from script.

Why the other options are wrong

  • B. Any injected script can read localStorage.
  • C. Scripts can read it too.
  • D. URLs leak through history and logs.

Question 6Medium

In CORS, what is the purpose of a preflight request?

  1. A request that logs the user in
  2. A GET request that caches the page
  3. A request sent by the server to the browser
  4. An OPTIONS check the browser sends before the real request

Answer: D. An OPTIONS check the browser sends before the real request

Explanation

For non-simple cross-origin requests the browser sends an OPTIONS preflight asking about method and headers.

Step-by-step solution

  1. Non-simple request detected.
  2. Browser sends OPTIONS.
  3. Real request follows if allowed.

Why the other options are wrong

  • A. Login is unrelated.
  • B. It is an OPTIONS check.
  • C. Browsers send it to the server.

Question 7Medium

Two URLs differ: https://app.example.com and https://api.example.com. Are they the same origin?

  1. No, the host differs, so they are different origins
  2. Yes, because the scheme and port match
  3. Yes, because the domain is example.com
  4. Only if both use cookies

Answer: A. No, the host differs, so they are different origins

Explanation

An origin is scheme + host + port; any difference makes the origins different.

Step-by-step solution

  1. Scheme: same.
  2. Host: app vs api, different.
  3. So not same origin.

Why the other options are wrong

  • B. The host is part of the origin.
  • C. Subdomains are different origins.
  • D. Cookies do not define the origin.

Question 8Medium

What does a salt do when hashing passwords?

  1. It makes equal passwords hash differently
  2. It encrypts the password so it can be decrypted later
  3. It makes the hash shorter
  4. It lets users reset passwords

Answer: A. It makes equal passwords hash differently

Explanation

A unique random salt per password stops rainbow tables and shows no matches for shared passwords.

Step-by-step solution

  1. Random salt per user.
  2. Hash(password + salt).
  3. Identical passwords differ.

Why the other options are wrong

  • B. Hashes are one-way.
  • C. Salt does not shorten hashes.
  • D. Resets are a separate flow.

Question 9Hard

A site stores a JWT in localStorage. Which attack makes this risky?

  1. SQL injection on the database
  2. Cross-site scripting (XSS), since scripts can read it
  3. Clickjacking in an iframe
  4. A DDoS flood of requests

Answer: B. Cross-site scripting (XSS), since scripts can read it

Explanation

Any script running on the page can read localStorage, so an XSS bug exposes the token.

Step-by-step solution

  1. XSS runs attacker script.
  2. Script reads localStorage.
  3. Token is exfiltrated.

Why the other options are wrong

  • A. SQL injection targets queries, not browser storage.
  • C. Clickjacking tricks clicks, it does not read storage.
  • D. DDoS affects availability.

Question 10Hard

Which attack does a SameSite cookie attribute primarily help against?

  1. SQL injection
  2. Brute-force password guessing
  3. Man-in-the-middle sniffing
  4. Cross-site request forgery (CSRF)

Answer: D. Cross-site request forgery (CSRF)

Explanation

SameSite limits when cookies are sent on cross-site requests, which blunts CSRF.

Step-by-step solution

  1. CSRF relies on cookies sent automatically.
  2. SameSite limits cross-site sending.

Why the other options are wrong

  • A. That needs parameterised queries.
  • B. Rate limiting helps there.
  • C. HTTPS and Secure cookies help there.

Practise all 11 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all React & Web topics · All topics