Welcome! Log In Create A New Profile

Advanced

Cookies are sometimes empty

Posted by Tyler_durden_83 
Cookies are sometimes empty
October 08, 2020 12:30PM
Dear friends,
I'm using a nginx dockerized image with this server block:

server {
listen 80;
listen [::]:80;
server_name localhost;

location / {
add_header Set-Cookie "username=$http_matricola; Domain=localhost; SameSite=Lax; Path=/" always;
root /usr/share/nginx/html;
index index.html index.htm;
}
}

My nginx homepage was modified to do this:

<body>
<h1>Welcome to nginx!</h1>
<script>function listCookies() {var theCookies = document.cookie.split(';'); var aString = ''; for (var i = 1 ; i <= theCookies.length; i++) { aString += i + ' ' + theCookies[i-1] ; } return aString;} console.log(listCookies()); </script>
</body>

I am setting a custom request header via the modheader chrome extention. The odd thing that happens is that if I refresh the page, approximately 25% of the time the cookie is printed with an empty value.

In other words, 75% of the times that I press the page refresh button, it prints the cookie key and value correctly, while the other 25% it prints the cookie key, but with no value next to it. If I open the web console and go to Application and Cookie, the cookie is there and always has the value set, even when it's not printed via javascript.

Is there a race condition between nginx setting the cookie and the javascript printing it?

Thank you very much,
Roberto
Re: Cookies are sometimes empty
October 13, 2020 08:48AM
I've been able to pinpoint the cause, but I don't really understand it too well.

add_header Set-Cookie "username=$http_matricola; Path=/" always;

when a file like this is requested

/static/js/xxx.js

it generates the username=yyy header under the /static/js path, but then the file

/static/js/xxx.js.map

is authomatically requested and served by nginx, and that overrides the cookie emptying it.
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 230
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready