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.

Cookie not saved in browser

See original GitHub issue

Expected behavior

I expected to use the proxy middleware so that when I query http://localhost:8080/api/myurl with ajax it will actually send the request to https://dev.mydomain.com/api/myurl, and return a header with set-cookie that will be saved in my browser and used the next time I run another ajax query to that endpoint.

Actual behavior

The proxy seems to be working and reaching the proper endpoint correctly and set-cookie returns a correct looking domain=dev.mydomain.com without secure but it is not saved in my browser and therefore not included with the following request. I’ve tried changing the target and cookieDomainRewrite options but have not successfully had a cookie saved from my response. I tried the method outlined in issue #137 and repeated below. This seems to manually read and write the cookie relative headers? However, when I do this I receive a response from the correct domain but the wrong (missing) subdomain. How can I preserve the subdomain? Or am I doing something else wrong with this setup?

Setup

proxy middleware configuration

var proxyOptions = {
    target: 'https://dev.mydomain.com/',
    changeOrigin: true,
    ws: true,
    secure: false,
    cookieDomainRewrite: "dev.mydomain.com",
    debug: true,
    onProxyReq: relayRequestHeaders,
    onProxyRes: relayResponseHeaders
}
function relayRequestHeaders(proxyReq, req) {
   Object.keys(req.headers).forEach(function (key) {
     proxyReq.setHeader(key, req.headers[key]);
   });
 }
function relayResponseHeaders(proxyRes, req, res) {
   Object.keys(proxyRes.headers).forEach(function (key) {
     res.append(key, proxyRes.headers[key]);
   });
 }
var apiProxy = proxy('/api', proxyOptions);

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

7reactions
chimuraicommented, Jun 6, 2017

Since you are trying to proxy between http <-> https; You might want to check your cookie flags. (Especially the secure flag);

The cookie will not be set if this flag is present in the response, because you’re running http on localhost.

More info: https://blog.dareboost.com/en/2016/12/secure-cookies-secure-httponly-flags/

5reactions
p2yangcommented, Jan 16, 2020

It works for me.

onProxyRes: (proxyRes, req, res) => {
  const sc = proxyRes.headers['set-cookie'];
  if (Array.isArray(sc)) {
    proxyRes.headers['set-cookie'] = sc.map(sc => {
      return sc.split(';')
        .filter(v => v.trim().toLowerCase() !== 'secure')
        .join('; ')
    });
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Cookies not saved in the browser - python - Stack Overflow
Cookies not saved in the browser · Cookies are just key value pairs that may have an expiration which makes them persistent, otherwise...
Read more >
7 Keys to the Mystery of a Missing Cookie - Medium
7 Keys to the Mystery of a Missing Cookie · 1. SameSite attribute Defaults to Lax · 2. withCredentials is not Set to...
Read more >
Turn cookies on or off - Computer - Google Account Help
On your computer, open Chrome. · At the top right, click More More and then Settings. · Under "Privacy and security," click Site...
Read more >
Cookies headers are present but Cookies are not stored in ...
So I send POST from sub1.local to sub2.local . I recevie Cookie header: Set-Cookie: name=value; expires=Sun, 20-May-2018 21:38:08 GMT; Max-Age= ...
Read more >
Browser not saving cookie when request is made from frontend
Even with the set-cookie header in the initial response from the server, no cookies actually get saved in the cookies list displayed in...
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