7

I have implemented an OAuth2 client, in which the first step is to send a user to the relevant 3rd party (facebook for this example), I set them a state cookie, and when they return from facebook I validate that state cookie.

In Chrome, everything is great. When I send the user to the redirect URL, I can see (using inspect element) that they have the state cookie I set. However, when I try on (desktop) safari on latest MacOS, I don't see that cookie.

I set the cookie in the response for my redirect request:

res.cookie('state', state.toString(), {
  maxAge: 3600000,
  secure: true,
  httpOnly: true,
});
res.redirect(someRedirectUri);

How can I get those cookies to be saved on Safari as well? Am I just setting the cookies wrong?

Amit
  • 5,118
  • 6
  • 39
  • 82

2 Answers2

7

I think you've found known WebKit issue.

So safari is ignoring the Set-Cookie header when encountering the 302 HTTP status

Yevhen Laichenkov
  • 6,242
  • 2
  • 22
  • 26
0

Late response but I hope this helps anyone else coming across this issue.

I ran into this issue earlier today. It was happening on iOS Safari and Chrome (Chrome on iOS uses WebKit). The workaround I implemented was changing the initial response to a 200 and return a web page which would do a JavaScript redirect in a few seconds.

Here's the HTML I used:

<html lang="en">
<head>
    <title>Redirecting...</title>
</head>
<body>
    <h1>Redirecting...</h1>
    <div>You will be redirected in a moment. If you are not redirected, click the following link: <a id="link" href="https://example.com">Go Now</a></div>
    <script type="text/javascript">
        var host = "https://"+window.location.host;
        document.getElementById("link").setAttribute("href", host);
        setTimeout(function(){
            window.location.href = host;
        }, 3000);
    </script>
</body>
</html>

This way the Cookies will be set with the response, and the redirect will happen a moment later.

In my case, I was completing an OAuth workflow. You should be able to customize/render the page in a number of ways to meet other requirements.

Padge
  • 51
  • 6