question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cookies on parent domain are also set on subdomain

See original GitHub issue

Hello There,

we are currently using this project to deploy a Serverless application in a dev and prod environment. We are deploying it with AWS SAM directly from the Serverless Application repository with the following configuration (we create the CloudFront distribution and the Cognito UserPool separately). CloudFormation extract we use for the deployment:

    Type: AWS::Serverless::Application
    Properties:
      Location:
        ApplicationId: arn:aws:serverlessrepo:us-east-1:520945424137:applications/cloudfront-authorization-at-edge
        SemanticVersion: 2.0.13
      Parameters: 
        CreateCloudFrontDistribution: false
        UserPoolArn: !Ref UserPoolArn
        UserPoolClientId: !Ref UserPoolClient
        HttpHeaders: |-
            {
              "Content-Security-Policy": "default-src 'self'; img-src 'self'; script-src 'self' https://code.jquery.com https://stackpath.bootstrapcdn.com; style-src 'self' 'unsafe-inline' https://stackpath.bootstrapcdn.com; object-src 'self'; connect-src 'self' https://*.amazonaws.com https://*.amazoncognito.com",
              "Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
              "Referrer-Policy": "same-origin",
              "X-XSS-Protection": "1; mode=block",
              "X-Frame-Options": "DENY",
              "X-Content-Type-Options": "nosniff"
            }
        CookieSettings: |-
            {
              "idToken": "Secure; SameSite=Lax",
              "accessToken": "Secure; SameSite=Lax",
              "refreshToken": "Secure; SameSite=Lax",
              "nonce": "Secure; HttpOnly; Max-Age=300; SameSite=Lax"
            }

We are deploying the code in two different environments (dev and prod) with the domain names “dev.mydomain.net” and “mydomain.net”. The issue we are facing is that the Auth cookies which are set for the prod environment are inherited by the dev environment which causes in a violation of max cookie size in our Web Application Firewall.

image

I believe this is due to the fact that the domain of the Cookies has a leading “.” causing the cookies to be applied to subdomains as well which is not the behavior I would expect. We will on short term fix it by changing our dev domain to something like mydomain-dev.net. However, we would be very grateful if we can get a configuration option to store the cookies under a domain without a leading “.”.

I think it should be enough to either change it in the function “withCookieDomain” or add an option to configure the domain:

function withCookieDomain(
  distributionDomainName: string,
  cookieSettings: string
) {
  // Add the domain to the cookiesetting
  if (cookieSettings.toLowerCase().indexOf("domain") === -1) {
    // Add leading dot for compatibility with Amplify (or js-cookie really)
    return `${cookieSettings}; Domain=.${distributionDomainName}`;
  }
  return cookieSettings;
}

Thanks in advance! Daniel

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:10

github_iconTop GitHub Comments

1reaction
danielbender1989commented, Feb 4, 2022

@ottokruse this was successul! Thanks a lot for your help. To wrap it up this is our working configuration:

  LambdaEdgeProtection:
    Type: AWS::Serverless::Application
    Properties:
      Location:
        ApplicationId: arn:aws:serverlessrepo:us-east-1:520945424137:applications/cloudfront-authorization-at-edge
        SemanticVersion: 2.0.13
      Parameters: 
        CreateCloudFrontDistribution: false
        UserPoolArn: !Ref UserPoolArn
        UserPoolClientId: !Ref UserPoolClient
        HttpHeaders: |-
            {
              "Content-Security-Policy": "default-src 'self'; img-src 'self'; script-src 'self' https://code.jquery.com https://stackpath.bootstrapcdn.com; style-src 'self' 'unsafe-inline' https://stackpath.bootstrapcdn.com; object-src 'self'; connect-src 'self' https://*.amazonaws.com https://*.amazoncognito.com",
              "Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
              "Referrer-Policy": "same-origin",
              "X-XSS-Protection": "1; mode=block",
              "X-Frame-Options": "DENY",
              "X-Content-Type-Options": "nosniff"
            }
        CookieSettings: |-
            {
              "idToken": "Domain=; Secure; SameSite=Lax",
              "accessToken": "Domain=; Secure; SameSite=Lax",
              "refreshToken": "Domain=; Secure; SameSite=Lax",
              "nonce": "Domain=; Secure; HttpOnly; Max-Age=300; SameSite=Lax"
            }
0reactions
danielbender1989commented, Feb 4, 2022

Thanks I will try it. I also figured out now that the Cookie header works like you said but my browser actually adds the ..

set-cookie header of the request:

CognitoIdentityServiceProvider.xxx.LastAuthUser=xxx; Domain=dev.mydomain.net; Secure; SameSite=Lax
Read more comments on GitHub >

github_iconTop Results From Across the Web

Share cookies between subdomain and domain
It's important to note that the behaviour is completely different in Internet Explorer. CMBuckley's very helpful test script demonstrates that in (say) Chrome, ......
Read more >
What is the most secure way to store cross subdomain cookies
If you want to share cookies across subdomains, but leave out other subdomains, you should explicitly state which subdomains you want to read ......
Read more >
Can subdomain.example.com set a cookie that can be read by ...
A Set-Cookie from request-host x.foo.com for Domain=.foo.com would be accepted. So subdomain.example.com can set a cookie for .example.com .
Read more >
Cookie scoped to parent domain - PortSwigger
By default, cookies are scoped to the issuing domain, and on IE/Edge to subdomains. If you remove the explicit domain attribute from your...
Read more >
Setting cookies to subdomains in JavaScript
Top-Level Domains and cookies​​ JavaScript allows you to set a cookie available to all bar.com subdomains from within the foo.bar.com subdomain. ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found