Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to set cookie from the express app hosted on Heroku

I have hosted both frontend and backend in Heroku.

  • Frontend - xxxxxx.herokuapp.com (react app)
  • Backend - yyyyyy.herokuapp.com (express)

I'm trying to implement Google authentication. After getting the token from Google OAuth2, I'm trying to set the id_token and user details in the cookie through the express app.

Below is the piece of code that I have in the backend,

authRouter.get('/token', async (req, res) => {
    try {
        const result = await getToken(String(req.query.code))
        const { id_token, userId, name, exp } = result;
        const cookieConfig = { domain: '.herokuapp.com', expires: new Date(exp * 1000), secure: true }
        res.status(201)
            .cookie('auth_token', id_token, {
                httpOnly: true,
                ...cookieConfig

            })
            .cookie('user_id', userId, cookieConfig)
            .cookie('user_name', name, cookieConfig)
            .send("Login succeeded")
    } catch (err) {
        res.status(401).send("Login failed");
    }
});

It is working perfectly for me on my local but it is not working on heroku.

These are the domains I tried out already - .herokuapp.com herokuapp.com. Also, I tried out without specifying the domain field itself.

I can see the Set-Cookie details on the response headers but the /token endpoint is failing without returning any status code and I can't see the cookies set on the application tab.

Please see the below images,

enter image description here I can't see any status code here but it says it is failed. I can't see any status code here These are cookie information that I can see but it is not available if I check via application tab. enter image description here

What am I missing here? Could someone help me?

like image 835
Manoj Kumar S Avatar asked May 30 '20 17:05

Manoj Kumar S


People also ask

Does heroku allow cookies?

It seems to me that, as long as you set the cookie for example.herokuapp.com , then the cookie is safe from manipulation. The cookie will only be presented to the app running on example.herokuapp.com and to herokuapp.com (where no app runs).

Does heroku Support Express?

Below are the steps to follow to deploy a simple Express app to Heroku: Create a new directory and initialise a Git repository. Login to the Heroku CLI and create a new project. Initialise a new npm project and install Express.

How do cookies work in Express?

Cookies are simple, small files/data that are sent to client with a server request and stored on the client side. Every time the user loads the website back, this cookie is sent with the request. This helps us keep track of the user's actions.


2 Answers

May you should try secure as: secure: req.secure || req.headers['x-forwarded-proto'] === 'https'

like image 124
Pardeep Baboria Avatar answered Oct 20 '22 10:10

Pardeep Baboria


You are right, this should technically work.

Except that if it did work, this could lead to a massive security breach since anyone able to create a Heroku subdomain could generate a session cookie for all other subdomains.

It's not only a security issue for Heroku but also for any other service that lets you have a subdomain.

This is why a list of domains has been created and been maintained since then to list public domains where cookies should not be shared amongst the subdomains. This list is usually used by browsers.

As you can imagine, the domain heroku.com is part of this list.

If you want to know more, this list is known as the Mozilla Foundation’s Public Suffix List.

like image 45
SugarMouse Avatar answered Oct 20 '22 09:10

SugarMouse