Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Browser not keeping cookie from response header

I am trying to do something supposedly simple and easy: set a cookie! But the browser (Chrome and Safari tested) is simply ignoring them. So the response headers look like:

Access-Control-Allow-Credentials:true
Access-Control-Allow-Origin:*
Connection:keep-alive
Content-Encoding:gzip
Content-Type:application/json; charset=utf-8
Date:Wed, 19 Jul 2017 04:51:51 GMT
Server:nginx
Set-Cookie:UserAuth=<some jwt>; Path=/; Domain=10.10.1.110; Expires=Wed, 19 Jul 2017 12:51:51 GMT; HttpOnly; Secure
Transfer-Encoding:chunked
Vary:Origin

The request does include withCredentials=true. But the cookies section in Chrome is empty. I've tried removing the domain altogether, removing the path, every configuration I can think of, but the browser just won't play ball.

What am I missing?

like image 213
see sharper Avatar asked Jul 19 '17 05:07

see sharper


People also ask

What header does the session pass to make the browser store a cookie?

Upon sign in, the server uses the Set-Cookie HTTP-header in the response to set a cookie with a unique “session identifier”. Next time when the request is sent to the same domain, the browser sends the cookie over the net using the Cookie HTTP-header.

How do I set a cookie path?

cookie = "username=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/"; This statement will delete a cookie named “username” if one exists. Some browsers will not allow the deletion of a cookie if the path is not specified. Therefore, it is important to always specify the path when working with cookies.

Is set-cookie a request header a response header or both?

The HTTP header Set-Cookie is a response header and used to send cookies from the server to the user agent.


2 Answers

Your cookie showing HttpOnly; Secure;

Using the HttpOnly flag when generating a cookie helps mitigate the risk of client side script accessing the protected cookie

The purpose of the secure flag is to prevent cookies from being observed by unauthorized parties due to the transmission of a the cookie in clear text. By setting the secure flag, the browser will prevent the transmission of a cookie over an unencrypted channel.

Cookies will be interrupted if travel through HTTP with secure flag in TLS layer. So check your preference and set the configuration of cookies accordingly.

like image 168
Ankit Avatar answered Sep 24 '22 23:09

Ankit


So it turns out that the original request had 'withCredentials=true' as a request header rather than being set on the XMlHttpRequest config object.

like image 25
see sharper Avatar answered Sep 22 '22 23:09

see sharper