Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ajax.ts): send XSRF cookies in a custom header #5702

Merged
merged 19 commits into from
Sep 27, 2020
Merged

feat(ajax.ts): send XSRF cookies in a custom header #5702

merged 19 commits into from
Sep 27, 2020

Conversation

DCtheTall
Copy link
Contributor

@DCtheTall DCtheTall commented Sep 8, 2020

Description:

Add the ability to send XSRF cookies in a custom header. This implementation is based off of Axios' own implementation of the same feature.

Related issue (if exists):

This fixes #4003.

@DCtheTall DCtheTall marked this pull request as draft September 8, 2020 14:58
src/internal/ajax/ajax.ts Outdated Show resolved Hide resolved
Copy link
Member

@benlesh benlesh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is just a draft, and it looks okay. Nonetheless, I'm leaving comments.

As much as possible, I would look to other ajax libraries like axios for guidance here. We should probably set the same defaults with regards to this.

In particular, you'll notice that axios will check to see not only if withCredentials is true, but also to see if the URL is from the same origin.. I think that's expected for this sort of functionality.

@DCtheTall
Copy link
Contributor Author

I know this is just a draft, and it looks okay. Nonetheless, I'm leaving comments.

They're appreciated 😄

In particular, you'll notice that axios will check to see not only if withCredentials is true, but also to see if the URL is from the same origin. I think that's expected for this sort of functionality.

Done.

@DCtheTall
Copy link
Contributor Author

@benlesh I am working through unit tests, but if you get to this before they're up feel free to leave any comments.

@DCtheTall DCtheTall marked this pull request as ready for review September 10, 2020 17:10
@DCtheTall
Copy link
Contributor Author

@benlesh a bit confused by the failing checks since they don't seem to have to do with this change?

@benlesh
Copy link
Member

benlesh commented Sep 14, 2020

The API guardian tests are to show if there's been any changes to the public API. which there were in this.

If you run npm run api_guardian:update, it will update the appropriate files.

@DCtheTall
Copy link
Contributor Author

If you run npm run api_guardian:update, it will update the appropriate files.

Done.

I am not sure why the expected throttleTime operator type needed to change for this change to pass checks, is that expected?

@DCtheTall
Copy link
Contributor Author

@benlesh friendly ping

@benlesh
Copy link
Member

benlesh commented Sep 18, 2020

Sorry, big conference day today. I will get back to this soon.

src/internal/ajax/ajax.ts Outdated Show resolved Hide resolved
try {
return document?.cookie.match(new RegExp(`(^|;\\s*)(${name})=([^;]*)`))?.pop() ?? '';
} catch (err) {
if (err instanceof ReferenceError) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Further explanation of the in-lined stuff above. The try/catch isn't necessary because it's really just here to catch the ReferenceError and then rethrow. If we check cookie inline like document?.cookie?.match, then there's no possibility of a ReferenceError and the try/catch has no purpose (because we're just rethrowing).

Copy link
Contributor Author

@DCtheTall DCtheTall Sep 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional chaining defends against TypeErrors that get thrown due to accessing properties of already initialized variables whose value is null or undefined.

In Node.js, the document variable is not even initialized, so accessing the variable document itself (even without accessing properties on that object!) throws a ReferenceError that optional chaining will not prevent.

That being said, I have thought about this some and decided that I am probably being overly-defensive. I think that it is reasonable to leave it to the user to only use xsrf*Name options in the DOM, and to allow the ReferenceError to throw if they try to use it in Node. If someone really wants it in Node then the library can always change later.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see what you're getting at. Thank you for the explanation. I'll try to revisit this in another pass. I suspect there are some places where we need to just do a sanity check on references for environments like node and older runtimes. And I also suspect that there's a compact way to do this.

@benlesh
Copy link
Member

benlesh commented Sep 19, 2020

So so close. Just one last set of changes to help reduce the size impact of the feature.

@DCtheTall
Copy link
Contributor Author

@benlesh I addressed your comments and left a bit of explanation for my reasoning for the code I had before, hope this is what you had in mind 😄

@DCtheTall
Copy link
Contributor Author

@benlesh friendly ping

@benlesh benlesh merged commit 1a2c2e4 into ReactiveX:master Sep 27, 2020
@benlesh
Copy link
Member

benlesh commented Sep 27, 2020

Merged! Thanks for all of your hard work, @DCtheTall! We'll have this in the next 7 beta release!

🎉 🎉 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ajax with PUT and CORS
2 participants