Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetch API not sending session cookies when used inside a Chrome Extension

I'm trying to make a Chrome Extension which scrapes some details from Pull Requests on Github using the Fetch API, and then displays them elsewhere. I'm running into some problems when I try to use this with a non-public repository on Github. I believe this is related to CSRF protection, and the rules that govern Chrome extensions having access to session cookies.

I have the following in my extension's manifest.json:

"content_scripts": [{
    "matches": [
        "*://github.com/*/*/pulls"
    ],
    "js": ["script/underscore-1.8.3.min.js", "script/content.js"]
}],
"permissions": [
    "tabs",
    "activeTab",
    "*://github.com/*",
    "webNavigation"
]

But when I run the following from within my script/content.js:

fetch('/redacted/redacted/pull/4549', {credentials: 'same-origin'}).then((response) => {
    return response.text();
}).then((text) => {
    // do cool stuff
})

This produces a 404 response from Github. Inspecting this request with Chrome Inspector's network tab, I can see it is not sending my GitHub session header with the request.

If I make the very same request using the Javascript prompt in the Inspector, I can see a 200 response, and I can see that it is sending my session cookies.

My understanding was that specifying the Github domain in my manifest.json would mean my extension would have access to my session data in my content scripts, is this not correct? What should I be doing to make a valid request to this protected content?

like image 841
majackson Avatar asked Feb 21 '16 21:02

majackson


People also ask

Why does fetch not send cookies?

By default, fetch won’t send or receive any cookies from the server, resulting in unauthenticated requests if the site relies on maintaining a user session (to send cookies, the credentials init option must be set). Since Aug 25, 2017. The spec changed the default credentials policy to same-origin .

How to enable cookie for HTTP request in URL fetch?

Fetch does not use cookie by default. To enable cookie, do this: fetch (url, { credentials: "same-origin" }).then (...).catch (...); Show activity on this post. In addition to @Khanetor's answer, for those who are working with cross-origin requests: credentials: 'include'

What went wrong with the session cookie request?

What went wrong was that in his case session cookie was not passed along the request to the backend service and the backend replied with http status code 401 — Unauthorized. There are a couple of reasons why the browser would not attach a cookie to the request even if we are expecting it to do so.

Why is fetch unauthenticated on my website?

@chrisdavidmills Thank you for asking. By default, fetch won’t send or receive any cookies from the server, resulting in unauthenticated requests if the site relies on maintaining a user session (to send cookies, the credentials init option must be set). Since Aug 25, 2017. The spec changed the default credentials policy to same-origin .


1 Answers

According to Chrome blog, to include cookies you need credentials: 'include' instead of credentials: 'same-origin'.

like image 92
Daiwei Avatar answered Sep 28 '22 22:09

Daiwei