Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

feat($http): support sending XSRF token to whitelisted origins #14890

Closed

Conversation

gkalpak
Copy link
Member

@gkalpak gkalpak commented Jul 10, 2016

What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)
Feature.

What is the current behavior? (You can also link to an open issue here)
The XSRF token header is not set on requests to cross-origin requests. This is usually desired, but there are cases where you want to whitelist specific origins.
See #7862 for context.

What is the new behavior (if this is a feature change)?
It is possible to whitelist specific origins, so requests to URLs on these origins will also have the XSRF token header set.

Does this PR introduce a breaking change?
No.

Please check if the PR fulfills these requirements

Other information:
Fixes #7862

@gkalpak
Copy link
Member Author

gkalpak commented Jul 10, 2016

/fyi @IgorMinar

customParamSerializer: customParamSerializer
}));
beforeEach(module(function($exceptionHandlerProvider) {
$exceptionHandlerProvider.mode('log');
}));
Copy link
Member Author

Choose a reason for hiding this comment

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

Just refactoring beforeEach blocks merging unnecessarily separate blocks.

@Narretz
Copy link
Contributor

Narretz commented Mar 16, 2018

LGTM but needs a rebase

@gkalpak gkalpak force-pushed the feat-http-xsrf-origin-whitelist branch 2 times, most recently from f5e4abc to 587610f Compare March 16, 2018 11:02
@gkalpak
Copy link
Member Author

gkalpak commented Mar 16, 2018

Rebased. PTAL

@Narretz Narretz modified the milestones: Backlog, 1.6.x Mar 20, 2018
Copy link
Contributor

@Narretz Narretz left a comment

Choose a reason for hiding this comment

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

Only a few cosmetic changes requested, otherwise LGTM

@@ -161,6 +161,7 @@
"urlResolve": false,
"urlIsSameOrigin": false,
"urlIsSameOriginAsBaseUrl": false,
"urlIsAllowedOriginChecker": false,
Copy link
Contributor

Choose a reason for hiding this comment

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

I would prefer a different name here, like isUrlAllowedOrigin. This fits better with our other methods that "check" something.

Copy link
Member Author

Choose a reason for hiding this comment

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

But this one is not like the other functions 😁
This one returns a function that checks (i.e. returns what one would call isUrlAllowedOrigin()).

So, I think naming it isUrlAllowedOrigin would be confusing. Open to other naming suggestions.

Copy link
Member Author

Choose a reason for hiding this comment

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

How about urlIsAllowedOriginFactory()?

src/ng/http.js Outdated
* @name $httpProvider#xsrfWhitelistedOrigins
* @description
*
* Array containing URLs whose origins are considered trusted enough to receive the XSRF token.
Copy link
Contributor

Choose a reason for hiding this comment

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

Could be made simpler as "are trusted to receive the XSRF token"

src/ng/http.js Outdated
* properties of either $httpProvider.defaults at config-time, $http.defaults at run-time,
* or the per-request config object.
* The header will — by default — **not** be set for cross-domain requests. This
* prevents unauthorized servers (e.g. malicious or compromized 3rd-party APIs) from gaining
Copy link
Contributor

Choose a reason for hiding this comment

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

compromize -> compromise

@gkalpak gkalpak force-pushed the feat-http-xsrf-origin-whitelist branch from 587610f to bb17964 Compare March 21, 2018 19:10
@Narretz
Copy link
Contributor

Narretz commented Mar 22, 2018

Factory is good - I noticed that this wasn't the actual function during the review, but then forgot to change my comment 😅

* @name $httpProvider#xsrfWhitelistedOrigins
* @description
*
* Array containing URLs whose origins are trusted to receive the XSRF token. See the
Copy link
Contributor

Choose a reason for hiding this comment

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

Array containing "origins" that are trusted?
Or if you explicitly accept URLs (but ignore the path etc) then change the name to xsrfWhitelistedUrls.

Copy link
Member Author

Choose a reason for hiding this comment

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

I do want to explicitly accept URLs (but ignore the path etc), but I think xsrfWhitelistedUrls will be misleading in that case. It would sound like you can whitelist specific URLs.

How about I change the description to "...containing origins that are trusted..." and keep the name as is (and also mention below that path etc will be ignored)?

Copy link
Contributor

Choose a reason for hiding this comment

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

Changing the description works for me.

src/ng/http.js Outdated
* angular.
* module('xsrfWhitelistedOriginsExample', []).
* config(['$httpProvider', function($httpProvider) {
* $httpProvider.xsrfWhitelistedOrigins.push('https://api.example.com/');
Copy link
Contributor

Choose a reason for hiding this comment

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

Note that the / at the end of the URL is part of the path and not the origin.

* @returns {boolean} - Whether the specified URL is of an allowed origin.
*/
return function urlIsAllowedOrigin(requestUrl) {
var parsedUrl = isString(requestUrl) ? urlResolve(requestUrl) : requestUrl;
Copy link
Contributor

Choose a reason for hiding this comment

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

Not related to this PR but perhaps we should change urlResolve to be a noop if the url is already parsed (i.e. not a string)? This would save some of this boilerplate here and further down.

var $httpBackend;

beforeEach(module(function($httpProvider) {
$httpProvider.xsrfWhitelistedOrigins.push('https://whitelisted.example.com/');
Copy link
Contributor

Choose a reason for hiding this comment

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

Note that the trailing slash is ignored. Should we have tests for URLs with different paths - to demonstrate that they are ignored?

gkalpak added 4 commits March 26, 2018 15:27
Normally, the XSRF token will not be set for cross-origin requests.
With this commit, it is possible to whitelist additional origins, so that requests to these origins
will include the XSRF token header.

Fixes angular#7862
@gkalpak gkalpak force-pushed the feat-http-xsrf-origin-whitelist branch from bb17964 to 45bf5e9 Compare March 26, 2018 14:31
@gkalpak
Copy link
Member Author

gkalpak commented Mar 26, 2018

@petebd, PTAL.

@gkalpak gkalpak closed this in 059d4f6 Apr 4, 2018
gkalpak added a commit that referenced this pull request Apr 5, 2018
@gkalpak gkalpak deleted the feat-http-xsrf-origin-whitelist branch April 5, 2018 07:15
gkalpak added a commit to gkalpak/angular.js that referenced this pull request Apr 5, 2018
Narretz pushed a commit that referenced this pull request Apr 9, 2018
Accidentally broken while backporting #14890.

Since #14890, `$$sanitizeUri()` can no longer handle `null`/`undefined` values.
In 1.7.x, there are no such calls.
In 1.6.x, there is still one such calls inside `Attributes.$set()`, so it needs to be adjusted accordingly.

Closes #16520
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Adding XSRF header when request is NOT same origin, but is TRUSTED origin (subdomain)
4 participants