TL;DR
Setting a session cookie on another domain requires careful handling of security attributes (Secure, HttpOnly, and SameSite) and potential CORS configuration. This guide explains how to do it safely.
Steps
- Understand the Restrictions: Browsers have strict rules about cookies. A cookie set by domain ‘a.com’ is generally only accessible by ‘a.com’. Cross-domain access requires specific configuration.
- Cookies are not automatically shared between subdomains (e.g.,
www.example.comandapi.example.com). - Directly setting a cookie for another domain from your JavaScript is usually blocked by the browser’s security model.
- Cookies are not automatically shared between subdomains (e.g.,
- Server-Side Setting (Recommended): The most secure method is to set the cookie on the server that controls the target domain.
- When a user authenticates, your authentication server should send an HTTP response header with the
Set-Cookiedirective. This sets the cookie directly for the correct domain. - Example (PHP):
'.targetdomain.com', 'secure' => true, 'httponly' => true, 'samesite' => 'Strict']); ?> - When a user authenticates, your authentication server should send an HTTP response header with the
- Example (Node.js with Express):
- Essential Cookie Attributes: These are crucial for security.
Secure: This attribute ensures the cookie is only sent over HTTPS connections. Always set this in production!HttpOnly: Prevents client-side JavaScript from accessing the cookie, mitigating XSS attacks. Always set this!SameSite: Controls how cookies are sent with cross-site requests. Options:Strict: Only send the cookie for requests originating from the same site. Most secure option if your application doesn’t require cross-site functionality.Lax: Send the cookie for top-level navigation (e.g., clicking a link) and GET requests, but not for POST requests or other potentially unsafe operations. A good balance between security and usability.None: Send the cookie with all cross-site requests. Requires settingSecureto true; otherwise, the browser will reject the cookie. Use this only if absolutely necessary and understand the risks.
- CORS Configuration (If Necessary): If your application involves JavaScript making requests to a different domain, you might need to configure CORS.
- The target domain’s server must include the appropriate
Access-Control-Allow-Credentialsheader in its responses. - Example:
Access-Control-Allow-Credentials: true - The target domain’s server must include the appropriate
- Ensure that your JavaScript code is making requests with the
credentialsoption set to ‘include’. - Subdomain Considerations: If you want a cookie to be accessible across subdomains (e.g., both
www.example.comandapi.example.com), set thedomainattribute to include the base domain.- Example:
domain = '.example.com'
- Example:
- Testing: Thoroughly test your cookie setup in different browsers and scenarios.
- Use browser developer tools to inspect the cookies and verify that they are being set correctly with the expected attributes (
Secure,HttpOnly,SameSite). - Test cross-site requests to ensure the cookie is sent as intended.
- Use browser developer tools to inspect the cookies and verify that they are being set correctly with the expected attributes (
res.cookie('session_id', 'your_session_value', {
domain: '.targetdomain.com',
secure: true,
httpOnly: true,
sameSite: 'Strict'
});
fetch('https://targetdomain.com/api', {
mode: 'cors',
credentials: 'include'
});
Important Security Notes
- Never store sensitive information directly in cookies. Use session IDs or tokens instead.
- Always validate and sanitize any data received from cookies on the server-side to prevent injection attacks.
- Regularly review your cookie policies and security configurations.

