Repository navigation
feat: Add RFC 6761–compliant localhost loopback checks so secure cookies work on localhost (fixes: #382) - #498
Conversation
|
@Chriss4123 thanks for this pull request! I try to review it by the end of the week. |
…tps and wss protocols.
|
@colincasey I've completed all the requested changes and also added a comment linking a potentially trustworthy origin and the notion of a "secure" connection in draft-ietf-httpbis-rfc6265bis-19. I'm ready for you to re-review the PR. |
|
Will look at the integration tests later and find what is causing these issues. Will let you know once all is good. |
|
Thanks @Chriss4123, I'll hold off on approval until it's clearer why |
|
@colincasey The reason The assertCookies(cookieHeader, [
"Test1=Basic",
"Test2=PathMatch",
"Test6=HttpOnly",
// "Test7=Secure; expires=Wed, 13-Jan-2051 22:23:01 GMT; path=/; Secure", (This cookie is present)
"Test9=Duplicate",
"Test10={\"prop1\":5,\"prop2\":\"value\"}",
"Malformed"
]);due to the scheme being This is unfortunately something out of our control, but as their goal states:
I do believe that part of that should be accepting I'll open a PR in As it stands What do you think of this? |
|
By the way, when I run
(taken from https://stackoverflow.com/a/5834094) This is because I am on Linux and the last person to run |
This is expected @Chriss4123. There's a bug in the api-extractor that seems to be at the root of this. Our automation doc checks should catch if there's any problems here so if that passes you should be all good. As for the code, I have one question for you on the addition of |
|
That is a great point @colincasey. The only reason I didn't do this is because the cookie jar doesn't actually follow this step in the algorithm: > jar.setCookie('ipv6cookie=ipv6val; Secure; Path=/', 'http://[::ffff:7f00:1]:5000/')
Promise {
Cookie="ipv6cookie=ipv6val; Path=/; Secure; hostOnly=true; aAge=0ms; cAge=0ms",
[Symbol(async_id_symbol)]: 137,
[Symbol(trigger_async_id_symbol)]: 6
}
> jar.store.idx
[Object: null prototype] {
'::ffff:7f00:1': [Object: null prototype] {
'/': [Object: null prototype] {
ipv6cookie: Cookie="ipv6cookie=ipv6val; Path=/; Secure; hostOnly=true; aAge=142897ms; cAge=142897ms"
}
}
}If it did, this cookie wouldn't be present in the store at all. Testing with this code: from flask import Flask, make_response, request
app = Flask(__name__)
@app.route('/setcookie')
def set_cookie():
resp = make_response('')
resp.set_cookie('example', '', secure=True)
return resp
@app.route('/getcookie')
def get_cookie():
return 'Cookie is set' if 'example' in request.cookies else 'No cookie set'
if __name__ == '__main__':
app.run()and Firefox on If you think this is a flaw too, let me know and I can open a new PR for it. |
This is what I was hinting at. I noticed in the tests added to support the What do you think of the following alternatives @Chriss4123? Expose the
|
@colincasey Sorry, didn't realize you actually answered my question, guess I was too tired 😅. In the process, I had to modify multiple current and legacy tests to reflect that setting a cookie with the Previously, those tests expected no error when setting the cookie and instead not retrieving the cookie. |
|
@Chriss4123 discussed this PR with the other maintainers today. We're looking at shipping this in a v6 release. @awaterma is also going to give it a review and we're just figuring out if there are other work items that we'd like to include in a new major release so don't be surprised if it takes a while longer for it to be merged. Thanks so much for this contribution! |
|
@colincasey got it. For now I can just use my branch in my projects where I need this functionality. If you need any further changes let me know. Do you have a broad ETA in mind for this being merged (i.e. v6 release)? |
|
@Chriss4123 the ETA would definitely be a broad one 😅 My guess is somewhere between 2 - 4 weeks. |
|
@colincasey that ETA is definitely quicker than I anticipated which is good to hear! I realized I made a small mistake on a test case: Pre it('should fail silently when no Domain or Path', async () => {
await cookieJar.setCookie('__Host-SID=12345; Secure', insecureUrl, {})
const cookies = await cookieJar.getCookies(insecureUrl)
expect(cookies).toEqual([])
})Post it('should fail silently when no Domain or Path', async () => {
await cookieJar.setCookie('__Host-SID=12345', insecureUrl, {})
const cookies = await cookieJar.getCookies(insecureUrl)
expect(cookies).toEqual([])
})I think there was an issue with the initial test case: |
awaterma
left a comment
There was a problem hiding this comment.
I think this looks good to me; let's discuss today and see if we can move this forward. :)
|
@Chriss4123 we're going to include this in a new major release ( |
Happy this has been merged! On the Apart from that happy I could help! |
|
@Chriss4123 Once I publish a pre-release, it'll be possible to open PRs against
I think that would be useful in getting a conversation started around how this change affects I don't mind doing that work but I also won't stop you if you wanted to tackle that before I get around to it 😄 |
The upcoming release of `tough-cookie` contains a [breaking change](salesforce/tough-cookie#498) for how `Secure` cookies are treated in secure contexts such as `localhost`. This PR updates the cookie processing tests with: - A cookie jar configured to simulate an insecure context to match the expected behavior of the `http`-based server tests - A correction of the `https`-based server test which should allow for `Secure` cookies
|
@colincasey When is the official release of version 6.x scheduled? Are there any known downsides to using the release candidate? We're considering it urgently due to the need for the new secure behavior. |
|
@ertl There are no major downsides to using the release candidate. There is one minor detail that I've got to sort out first though and it's whether to enforce the secure behavior on both cookie storage and retrieval. RFC 6265 doesn't state it should be enforced during storage but httpbis-rfc6265bis (draft 20) does and that is currently causing an issue with one of the web platform tests for websockets that jsdom uses. Removing the code here is more in alignment with RFC 6265 and this also seems to satisfy the web platform tests. I just haven't had the time yet to double-check the expected websocket cookie behavior in different browsers to confirm if that's the correct action to take or not. |
|
@colincasey Thanks for the update. We'll go ahead and give the release candidate a try. |
This change was introduce in #498. As this new feature was driven by current browser behaviors + updates to the terminology for "secure" connections from RFC6265bis-19, it seemed appropriate to also apply the following check from RFC6265bis-19 when setting a cookie: > If the request-uri does not denote a "secure" connection (as defined by the user agent), and the cookie's secure-only-flag is true, then abort these steps and ignore the cookie entirely. > - https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis-19#section-5.7-3.13.1 This deviates from RFC6265 which does not describe any check for a "secure" connection when setting a cookie, only when retrieving the cookie. In our integration tests for JSDOM, this code also causes a failure in their Web Platform Test suite around passing cookies to Websocket connections. Testing the behavior for this in various browsers showed inconsistent results which I'll summarize below: | Browser | HTTP → WSS | HTTPS → WSS | |---------|------------|-------------| | Chrome | Fail | Pass | | Firefox | Pass | Pass | | Safari | Fail | Pass | | Edge | Fail | Pass | As it's unclear what the correct behavior should be here, I think we should revert this check to restore the previous behavior in https://github.com/salesforce/tough-cookie/blob/v5.1.2/lib/cookie/cookieJar.ts#L595 to what RFC6265 defines.
This change was introduce in #498. As this new feature was driven by current browser behaviors + updates to the terminology for "secure" connections from RFC6265bis-19, it seemed appropriate to also apply the following check from RFC6265bis-19 when setting a cookie: > If the request-uri does not denote a "secure" connection (as defined by the user agent), and the cookie's secure-only-flag is true, then abort these steps and ignore the cookie entirely. > - https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis-19#section-5.7-3.13.1 This deviates from RFC6265 which does not describe any check for a "secure" connection when setting a cookie, only when retrieving the cookie. In our integration tests for JSDOM, this code also causes a failure in their Web Platform Test suite around passing cookies to Websocket connections. Testing the behavior for this in various browsers showed inconsistent results which I'll summarize below: | Browser | HTTP → WSS | HTTPS → WSS | |---------|------------|-------------| | Chrome | Fail | Pass | | Firefox | Pass | Pass | | Safari | Fail | Pass | | Edge | Fail | Pass | As it's unclear what the correct behavior should be here, I think we should revert this check to restore the previous behavior in https://github.com/salesforce/tough-cookie/blob/v5.1.2/lib/cookie/cookieJar.ts#L595 to what RFC6265 defines.
The upcoming release of `tough-cookie` contains a [breaking change](salesforce/tough-cookie#498) for how `Secure` cookies are treated in secure contexts such as `localhost`. This PR updates the cookie processing tests with: - A cookie jar configured to simulate an insecure context to match the expected behavior of the `http`-based server tests - A correction of the `https`-based server test which should allow for `Secure` cookies
The upcoming release of `tough-cookie` contains a [breaking change](salesforce/tough-cookie#498) for how `Secure` cookies are treated in secure contexts such as `localhost`. This PR updates the cookie processing tests with: - A cookie jar configured to simulate an insecure context to match the expected behavior of the `http`-based server tests - A correction of the `https`-based server test which should allow for `Secure` cookies
fixes: #382
Description
This commit extends how cookies are treated in secure contexts by fully recognizing
localhostand loopback IPs as trustworthy origins, matching the de facto behavior of all modern browsers and RFC 6761. Previously,tough-cookiedefaultedsecuretotrueonly forhttps:andwss:URLs, causing cookies withsecureto not work withlocalhost.What Changed
New
secureContext.tsisPotentiallyTrustworthy(url)by checking:httpsorwss127.0.0.1/8and::1localhostand*.localhostIsLocalhost,IsLoopbackandHostNoBracketsPiece, located at:IsLocalhostIsLoopbackHostNoBracketsPiececookieJar.tsUpdatehttpsandwssschemes:isPotentiallyTrustworthyfunction which accounts for secure schemes,localhostaddresses and loopback addresses.No existing functionality is removed. Where the old check would return true, so does the new check. The only added functionality is treating
localhostaddresses and loopback addresses as secure contexts which was previously not present.All modern browsers (Chrome et. al) support secure cookies on localhost so it only makes sense
tough-cookiesupports this too.