← All articlesWeb security

How to restrict a session cookie to its intended host and keep it off sibling subdomains

If a session is needed on only one host, start with the Domain attribute: omit it. This makes the cookie host-only, so it is sent only to the host that set it. Below, we explain how to express this boundary in configuration, verify it with a fictional example, and add HTTPS, Secure and HttpOnly restrictions. These measures serve different purposes, so check them separately.

Host-only: the host defines the boundary

The product owner's task is to identify exactly which host needs the session cookie. If authentication applies only to an application on one host, the configuration should reflect that boundary. Omit the Domain attribute from Set-Cookie: the cookie becomes host-only and is returned only to the host that set it.

Distinguish between the application's host and a shared domain scope covering several services. A sibling subdomain should not automatically receive the session cookie simply because it belongs to the same product. First document the required boundary, then compare it with the actual response header.

Why Domain broadens where a cookie is sent

Setting Domain makes the cookie available to the specified domain and all its subdomains. Restricting it to one host therefore requires omitting the attribute entirely. A leading dot in Domain does not narrow the scope: modern rules ignore that dot.

When reviewing configuration, look for the presence of Domain itself, rather than just how its value is written. For example, Domain=example.test and Domain=.example.test do not define different scopes because of the dot. If the goal is a host-only cookie, remove the attribute from the session cookie configuration.

A fictional example: an application and a sibling service

Consider a wholly fictional product with test hosts the team owns: app.example.test and help.example.test. The team wants the application's session cookie to be sent only to app.example.test. If the application sets Domain=example.test, the cookie's scope includes both subdomains. Remove this attribute to meet the stated requirement.

An example header for the test application is: Set-Cookie: __Host-session=fictional-test-value; Secure; HttpOnly; Path=/. The value is fictional and intended only for testing. The header must come from app.example.test over HTTPS. Omitting Domain restricts the cookie to the host, while the other attributes add the separate restrictions described below.

What the __Host- prefix adds

In browsers that support cookie prefixes, a name beginning with __Host- requires the cookie to be set by a secure page over HTTPS, include Secure, omit Domain and use Path=/. For example, __Host-session has the required prefix. This naming convention helps enforce the cookie configuration requirements in a supporting browser.

Prefix support is an important limitation. In browsers that do not support it, you cannot rely on the additional requirements associated with the name. Check the actual header attributes, and do not treat renaming the cookie alone as a sufficient fix.

Secure, HttpOnly and Path serve different purposes

OWASP recommends using HTTPS throughout the entire web session, rather than only during sign-in. OWASP also requires Secure for the session identifier so that it is transmitted only over an encrypted channel. Check these requirements alongside the host scope: correctly omitting Domain does not replace secure transmission.

HttpOnly prevents JavaScript from accessing the cookie, including through Document.cookie. Describe the result in those terms, without promising general protection against XSS. Path should not be treated as a security boundary either: it is not intended to protect a cookie from unauthorized reading through another path on the site. In the example, Path=/ is needed to meet the __Host- requirements.

How to verify the result safely

Test only on hosts you own and with a fictional session value. Prepare a separate browser test environment, set the cookie through the application's response, and inspect the actual Set-Cookie header. Then check whether it is sent in requests to the application host and a sibling test subdomain. For a host-only cookie, the expected result is transmission only to the host that set it.

When testing the prefix, account for support in the selected browser. An observation in one browser does not establish that the additional restrictions work in all others. Record the test conditions and results so the team can repeat the check after configuration changes.

  • Confirm that Set-Cookie omits Domain and includes the intended Secure and HttpOnly attributes.
  • When using __Host-, check HTTPS, Secure, the absence of Domain, and Path=/.
  • Compare whether the test cookie is sent to your application host and a sibling test host.

Fix priorities and audit limitations

For this task, the first priority is to remove Domain from the session cookie and confirm where it is sent. Next, check HTTPS throughout the session and Secure, followed by HttpOnly and correct use of __Host-. This sequence lets you assess the host restriction, secure transmission and JavaScript access separately.

This check covers specific cookie settings and does not establish the security of the entire application. If you need help reviewing them, you can start with a free audit of your website or the web portion of your product: identify the intended session boundary, compare it with the configuration, and prepare a prioritized list of fixes.

Sources and documentation

  1. MDN: Set-Cookie header
  2. OWASP Session Management Cheat Sheet
CheckMyApp

Check your application’s security.

Enter your web application address and email. Get a free preliminary report and choose the depth of analysis.

Check for free