Cross-Origin JWT Security: Understanding CORS and Cookies Quiz

Explore the essentials of Cross-Origin Resource Sharing (CORS), JWT tokens, and secure cookie practices in token-based authentication. This quiz deepens understanding of how cross-origin security measures interact with JSON Web Tokens and cookie handling to protect web applications.

  1. CORS Preflight and JWT Authentication

    When a browser sends a CORS preflight OPTIONS request before accessing a protected API that uses JWT for authentication, what should the server do to ensure the client application can proceed?

    1. Respond with appropriate CORS headers and no authentication checks
    2. Block the request unless a valid JWT is present
    3. Return the requested resource without checking the HTTP method
    4. Inject the JWT into the request headers for the client

    Explanation: During a CORS preflight (OPTIONS) request, browsers expect only CORS headers to determine if the actual request is allowed. The correct action is to respond with the required CORS headers and avoid authentication checks, as no sensitive data is transferred at this stage. Blocking the preflight based on JWT or injecting tokens on the server side would prevent legitimate cross-origin traffic. Returning the resource outright without checking the method would break HTTP protocol rules.

  2. Secure Cookies with JWT

    What is the most important attribute to set on a cookie that stores a JWT to prevent cross-site scripting (XSS) attacks from accessing its value?

    1. HttpOnly
    2. SameSite
    3. Expires
    4. Path

    Explanation: Setting the HttpOnly attribute on a JWT cookie ensures it cannot be accessed via JavaScript, protecting it from theft through XSS attacks. SameSite is primarily used to mitigate CSRF, not JS access. Expires and Path help with cookie lifespan and scope but do not affect JS accessibility. Only HttpOnly effectively blocks client-side scripts from reading sensitive cookie data.

  3. CORS Response and Cookie Transmission

    If a frontend application on domain A wants to send and receive cookies containing JWT to an API on domain B, which CORS response header must the API include?

    1. Access-Control-Allow-Credentials: true
    2. Allow-Origin-Credentials: true
    3. Access-Control-Allow-Headers: Cookies
    4. Access-Control-Allow-Cookies: yes

    Explanation: Access-Control-Allow-Credentials: true is required in the CORS response for browsers to send and receive cookies cross-origin. Allow-Origin-Credentials: true and Access-Control-Allow-Cookies: yes are not actual CORS headers, and Access-Control-Allow-Headers controls which headers can be sent, not cookie handling. Without Allow-Credentials, credentials like cookies will not accompany cross-origin requests.

  4. JWT and Local Storage Security Risks

    What is the main security risk of storing JWT access tokens in localStorage during cross-origin authentication scenarios?

    1. Tokens in localStorage can be accessed by injected JavaScript through XSS attacks.
    2. Browsers cache localStorage data even after logout, causing automatic re-authentication.
    3. LocalStorage automatically syncs tokens across different domains, leaking credentials.
    4. Tokens in localStorage are not sent with each HTTP request, causing authentication errors.

    Explanation: The main concern with storing JWTs in localStorage is their exposure to malicious JavaScript from XSS attacks, which can compromise users' tokens. LocalStorage does not sync tokens across domains, so that is incorrect. Tokens are not sent with each HTTP request; they must be manually added, which is an inconvenience but not a direct security risk. Caching after logout is possible, but the risk of JS access is far more severe.

  5. SameSite Cookie Attribute in JWT Authentication

    How does setting a cookie's SameSite attribute to 'Strict' affect cross-origin requests using JWT authentication?

    1. The browser will not send the JWT cookie with cross-origin requests.
    2. The JWT cookie can only be sent over secure HTTPS connections.
    3. The browser will send the JWT cookie but will block the response.
    4. The cookie will be accessible to JavaScript regardless of the origin.

    Explanation: SameSite=Strict instructs browsers not to include the cookie in any cross-origin requests, which prevents automatic JWT cookie transmission from third-party sites. Only the Secure attribute mandates HTTPS, not SameSite. The browser simply withholds the cookie; it does not block responses or bypass JavaScript accessibility rules by itself. For JavaScript access, the HttpOnly flag is more relevant.