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.

Safari does not include cookies in JSONP requests

See original GitHub issue

How to reproduce these conditions

Sample name or URL where you found the bug

firebase/functions-samples/spotify-auth, but the other auth examples may be affected too.

Failing Function code used (including require/import commands at the top)

spotify-auth/functions/index.js

const functions = require('firebase-functions');
const cookieParser = require('cookie-parser');
[...]
exports.token = functions.https.onRequest((req, res) => {
  try {
    cookieParser()(req, res, () => {
      console.log('Received verification state:', req.cookies.state);
      console.log('Received state:', req.query.state);
      if (!req.cookies.state) {
        throw new Error('State cookie not set or expired. Maybe you took too long to authorize. Please try again.');
      } else if (req.cookies.state !== req.query.state) {
        throw new Error('State validation failed');
      }
[...]

Steps to set up and reproduce

Setup the Spotify-auth example and use Safari (Version 12.0 14606.1.36.1.9) to test it.

Debug output

Errors in the console logs

State cookie not set or expired. Maybe you took too long to authorize. Please try again.

The req.cookies object is empty.

Expected behavior

No exception.

Actual behavior

As far as I can see, Safari does not include cookies in JSONP request. It works fine in Chrome though.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
kevinguebertcommented, Apr 21, 2021

I think I was finally able to solve this after poking around multiple tickets/PRs.

My solution ended up using code from: #826 #849 #852

The basic rundown:

  1. Change the req.cookies.state to be req.cookies.__session across the files as seen in these updated files
  2. Update the res.cookie object to be
    res.cookie('__session', state.toString(), {
      maxAge: 3600000,
      secure: true,
      httpOnly: true,
      SameSite: 'none'
    });

(note we changed res.cookies('state'... to be res.cookies('__session'... from step 1), also note this comment

  1. Updated my hosting rewrites to support calling the functions like:
// in firebase.json
    "public": "public",
    "rewrites": [
      {
        "source": "/redirect",
        "function": "redirect"
      },
      {
        "source": "/token",
        "function": "token"
      }
    ]

Note, I don’t know if this is the secure/best/optimal way. I attempted each of these on their own and then started combining them to see what worked.

0reactions
MehediHcommented, Dec 12, 2021

@kevinguebert You’re a godsend!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Safari not include COOKIE to second CORS request
Ok - after loooong analysing requests finally I found the problem - in login response the server set in Set-Cookie header the cookie...
Read more >
Accessing cookie not possible on d… | Apple Developer Forums
We perform login to backend server using normal ajax call and in the response the Cookie is read and send in subsequent requests....
Read more >
XMLHttpRequest.withCredentials - Web APIs | MDN
XMLHttpRequest responses from a different domain cannot set cookie values for their own domain unless withCredentials is set to true before ...
Read more >
HTTP cookie - Wikipedia
HTTP cookies are small blocks of data created by a web server while a user is browsing a website and placed on the...
Read more >
Using Postman Interceptor
You can capture requests from your browser and cookies from any domain to use ... [macOS only] Install NodeJS - If Postman detects...
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