Bug 109533 - CORS requests don't cache authentication
Summary: CORS requests don't cache authentication
Status: UNCONFIRMED
Alias: None
Product: WebKit
Classification: Unclassified
Component: WebCore Misc. (show other bugs)
Version: 528+ (Nightly build)
Hardware: All All
: P2 Normal
Assignee: Nobody
URL: http://lkraider.eipper.com.br/tests/c...
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2013-02-11 18:37 PST by Paul Eipper
Modified: 2021-07-07 15:01 PDT (History)
3 users (show)

See Also:


Attachments
Server side test case (1.93 KB, application/x-tar)
2015-09-24 11:23 PDT, Paul Eipper
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Paul Eipper 2013-02-11 18:37:49 PST
Steps to Reproduce:
1. Construct and open a xhr GET to a CORS (allow-credentials enabled) basic auth endpoint, setting the Authorization header.
2. Open a new xhr with the same parameters, this time without setting the Authorization header.

Actual Results:
Second request fails with 401.

Expected Results:
Browser should have cached the authentication for the domain, as it does for non-CORS requests.
Comment 1 Alexey Proskuryakov 2013-02-12 19:09:28 PST
Can you please provide a complete test case? 

Some comments:
1. A CORS request without withCredentials set would not use cached credentials anyway.
2. I'm surprised that credentials passed via a custom Authorization header ever work on subsequent requests. I would not expect this to work, unlike credentials explicitly passed in XHR.open() call.
Comment 2 Paul Eipper 2013-02-13 13:00:14 PST
Note: I just added a test page on the bug report.

Indeed, setting the Authorization header will not cache, even on same-domain requests. I guess my expectations were wrong there.

Discussing this issue with the Mozilla guys, it seems the issue is CORS specifies that the username and password used on the `open` call should be discarded, which in practice makes CORS authorization uncacheable.

The use case I have in mind is (quoting from my discussion over at Mozilla):

"Take the use case of a website that uses a REST API which requires BasicAuth (everything goes over HTTPS):

1. website presents a form login, which is used to auth user against API;
2. JS performs XHR on API to verify auth;
3. Auth is cached by the browser, which allows the website to perform other requests during that session.

While this works for same-domain, step 3 evidently does not work with CORS, since not `open` nor setting the headers will do the caching.

Is this just not a valid use case for CORS?

Only possible workaround I can see is to store form input in a session cookie to send on all CORS requests.
Pros: lifetime is the same as regular browser auth caching
Cons: credentials are easily readable on the machine

I would appreciate any advice regarding this.
"

Regarding the CORS specification, I don't understand the rationale of discarding the credentials on the open call. I don't see what attacks does that prevent that cannot be achieved by using `setRequestHeader` for the Authorization (the spec is not very clear here).

Regarding Webkit then, I guess the issue is whether it should support some form of caching for CORS credentials, is this a reasonable use-case, and if adhering to the spec makes sense there.
Comment 3 Alexey Proskuryakov 2013-02-14 12:04:46 PST
Will passing credentials with the cross-origin request work for you if you set withCredentials attribute to true?
Comment 4 Paul Eipper 2013-02-14 12:38:44 PST
Does not help(In reply to comment #3)
> Will passing credentials with the cross-origin request work for you if you set withCredentials attribute to true?

Does not help to cache the credentials in the `setRequestHeader` case, or to make Webkit use the username and password on the `open` case.

Essentially, it seems it will only help when sending cookies?
Comment 5 Paul Eipper 2015-09-24 11:23:16 PDT
Created attachment 261878 [details]
Server side test case

Attaching server side code just in case.
Comment 6 Radar WebKit Bug Importer 2021-07-07 15:01:28 PDT
<rdar://problem/80290936>