Get a cookie that was not set by dash

Hi,

I found out how to set a cookie and how to read it afterwards but is it also possible to get/read a cookie that was not set by dash?

My goal is to find out if a specific web page (that embeds my dash app) is calling my apps url or if some other client is calling it to ensure that only this specific page can access my dash app. If there is an easier/better way to ensure that I am open for any suggestions :slight_smile:

Thanks!

Take a look at this code file.

Request object is only available in the callback. Look in the util.py for parsing the cookie.

Hi jnguyen,

thanks for your reply! I am not sure if I am doing this correct but when I try to read the cookies with the following function from your util.py:

def get_user_id(cookies):
    _cookies = dict(cookies)
    print('_cookies: ' + str(_cookies))
    # the id of your cookie
    user_cookie = _cookies.get('_name_of_your_cookie')
    print('user_cookie: ' + str(user_cookie))
    current_user = decodeBase64_message(user_cookie) if user_cookie else None
    return current_user.get('User') if current_user else 'test'

I only get an empty dict as output:

_cookies: {}
user_cookie: None
USER-ID: TEST
ImmutableMultiDict()

When I use the Chrome Web Tools to see all cookies I can see this when I select the page that embeds my dash app in an HTML container (green box):

But when I select my dash app page (red box) I cannot see any cookies here.

How can I access or at least detect if there is a specific cookie in the “superior” web page (green box) that embeds my dash app?

Any idea/help is welcome to get to my goal to find out if a specific web page (that embeds my dash app) is calling my apps url or if some other client is calling it to ensure that only this specific page can access my dash app.

Thanks!

I believe dash can only access the current request cookies. In the code samples I used flask request to access the cookies. You might be able find more info on it on by checking the flask docs. I believe accessing another site cookies could be a security concern and may not be possible.

fyi: I am having a look at this now: CSP: frame-ancestors - HTTP | MDN to check if it can be used for my purpose…

If anyone has an example on how to apply this in dash please let me know :slight_smile:

Thanks!