Skip to content

Do not send CSRF token for external requests #46

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

Merged
merged 1 commit into from
Jun 20, 2022

Conversation

t27duck
Copy link
Contributor

@t27duck t27duck commented Jun 16, 2022

This library could be used for any AJAX request, however it was reported that some 3rd party endpoints reject the request if the CSRF token is included in the headers.

This change excludes the CSRF token from the headers by comparing the request URL and window.location.hostname.

  • If the URL is a relative path (ie doesn't begin with "http:"), include the token.
  • If the URL is not parseable with new URL(), then we'll continue on and include the token.
  • If the hostname of the URL and from window.location are equal, include the token.
  • If the hostname of the URL and from window.location are different, do not include the token.

I beleive this covers and maintains the existing expectations of the library so existing applications shouldn't be caught off gaurd as we are including the token more often than not.

Closes #45

This library could be used for any AJAX request, however it was reported that some 3rd party endpoints reject the request if the CSRF token is included in the headers.

This change excludes the CSRF token from the headers by comparing the request URL and `window.location.hostname`.

- If the URL is a relative path (ie doesn't begin with "http:"), include the token.
- If the URL is not parseable with `new URL()`, then we'll continue on and include the token.
- If the hostname of the URL and from `window.location` are equal, include the token.
- If the hostname of the URL and from `window.location` are different, do not include the token.

I beleive this covers and maintains the existing expectations of the library so existing applications shouldn't be caught off gaurd as we are including the token more often than not.
@t27duck t27duck force-pushed the no_csrf_for_external branch from 909de71 to 3c898a0 Compare June 16, 2022 18:01
return new URL(this.originalUrl).hostname === window.location.hostname
} catch (_) {
return true
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This feels ugly, but new URL() bombs on relative paths and invalid URLs. Rather have soemthing further down the line fail because of a bad URL than it throwing an error here.

Copy link
Collaborator

@marcelolx marcelolx left a comment

Choose a reason for hiding this comment

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

@adrienpoly Did you have a chance to test @t27duck changes if they solve your problem? I believe it does, but let us know if it doesn't.

Thanks for the PR @t27duck!

@marcelolx marcelolx merged commit e6d1c01 into rails:main Jun 20, 2022
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.

Using request JS for external APIs : ability to disable csrf token
2 participants