@@ -34,7 +34,7 @@ function $HttpParamSerializerProvider() {
34
34
* * `{'foo': {'bar':'baz'}}` results in `foo=%7B%22bar%22%3A%22baz%22%7D` (stringified and encoded representation of an object)
35
35
*
36
36
* Note that serializer will sort the request parameters alphabetically.
37
- * * /
37
+ */
38
38
39
39
this . $get = function ( ) {
40
40
return function ngParamSerializer ( params ) {
@@ -101,7 +101,7 @@ function $HttpParamSerializerJQLikeProvider() {
101
101
* });
102
102
* ```
103
103
*
104
- * * /
104
+ */
105
105
this . $get = function ( ) {
106
106
return function jQueryLikeParamSerializer ( params ) {
107
107
if ( ! params ) return '' ;
@@ -261,7 +261,7 @@ function isSuccess(status) {
261
261
*
262
262
* @description
263
263
* Use `$httpProvider` to change the default behavior of the {@link ng.$http $http} service.
264
- * * /
264
+ */
265
265
function $HttpProvider ( ) {
266
266
/**
267
267
* @ngdoc property
@@ -315,7 +315,7 @@ function $HttpProvider() {
315
315
* - **`defaults.xsrfHeaderName`** - {string} - Name of HTTP header to populate with the
316
316
* XSRF token. Defaults value is `'X-XSRF-TOKEN'`.
317
317
*
318
- ** /
318
+ */
319
319
var defaults = this . defaults = {
320
320
// transform incoming response data
321
321
transformResponse : [ defaultHttpResponseTransform ] ,
@@ -362,7 +362,7 @@ function $HttpProvider() {
362
362
*
363
363
* @returns {boolean|Object } If a value is specified, returns the $httpProvider for chaining.
364
364
* otherwise, returns the current configured value.
365
- ** /
365
+ */
366
366
this . useApplyAsync = function ( value ) {
367
367
if ( isDefined ( value ) ) {
368
368
useApplyAsync = ! ! value ;
@@ -383,9 +383,51 @@ function $HttpProvider() {
383
383
* array, on request, but reverse order, on response.
384
384
*
385
385
* {@link ng.$http#interceptors Interceptors detailed info}
386
- ** /
386
+ */
387
387
var interceptorFactories = this . interceptors = [ ] ;
388
388
389
+ /**
390
+ * @ngdoc property
391
+ * @name $httpProvider#xsrfWhitelistedOrigins
392
+ * @description
393
+ *
394
+ * Array containing URLs whose origins are trusted to receive the XSRF token. See the
395
+ * {@link ng.$http#security-considerations Security Considerations} sections for more details on
396
+ * XSRF.
397
+ *
398
+ * **Note:** An "origin" consists of the [URI scheme](https://en.wikipedia.org/wiki/URI_scheme),
399
+ * the [hostname](https://en.wikipedia.org/wiki/Hostname) and the
400
+ * [port number](https://en.wikipedia.org/wiki/Port_(computer_networking). For `http:` and
401
+ * `https:`, the port number can be omitted if using th default ports (80 and 443 respectively).
402
+ * Examples: `http://example.com`, `https://api.example.com:9876`
403
+ *
404
+ * <div class="alert alert-warning">
405
+ * It is not possible to whitelist specific URLs/paths. The `path`, `query` and `fragment` parts
406
+ * of a URL will be ignored. For example, `https://foo.com/path/bar?query=baz#fragment` will be
407
+ * treated as `https://foo.com`, meaning that **all** requests to URLs starting with
408
+ * `https://foo.com/` will include the XSRF token.
409
+ * </div>
410
+ *
411
+ * @example
412
+ *
413
+ * ```js
414
+ * // App served from `https://example.com/`.
415
+ * angular.
416
+ * module('xsrfWhitelistedOriginsExample', []).
417
+ * config(['$httpProvider', function($httpProvider) {
418
+ * $httpProvider.xsrfWhitelistedOrigins.push('https://api.example.com');
419
+ * }]).
420
+ * run(['$http', function($http) {
421
+ * // The XSRF token will be sent.
422
+ * $http.get('https://api.example.com/preferences').then(...);
423
+ *
424
+ * // The XSRF token will NOT be sent.
425
+ * $http.get('https://stats.example.com/activity').then(...);
426
+ * }]);
427
+ * ```
428
+ */
429
+ var xsrfWhitelistedOrigins = this . xsrfWhitelistedOrigins = [ ] ;
430
+
389
431
this . $get = [ '$browser' , '$httpBackend' , '$$cookieReader' , '$cacheFactory' , '$rootScope' , '$q' , '$injector' , '$sce' ,
390
432
function ( $browser , $httpBackend , $$cookieReader , $cacheFactory , $rootScope , $q , $injector , $sce ) {
391
433
@@ -409,6 +451,11 @@ function $HttpProvider() {
409
451
? $injector . get ( interceptorFactory ) : $injector . invoke ( interceptorFactory ) ) ;
410
452
} ) ;
411
453
454
+ /**
455
+ * A function to check request URLs against a list of allowed origins.
456
+ */
457
+ var urlIsAllowedOrigin = urlIsAllowedOriginFactory ( xsrfWhitelistedOrigins ) ;
458
+
412
459
/**
413
460
* @ngdoc service
414
461
* @kind function
@@ -765,25 +812,42 @@ function $HttpProvider() {
765
812
* which the attacker can trick an authenticated user into unknowingly executing actions on your
766
813
* website. AngularJS provides a mechanism to counter XSRF. When performing XHR requests, the
767
814
* $http service reads a token from a cookie (by default, `XSRF-TOKEN`) and sets it as an HTTP
768
- * header (`X-XSRF-TOKEN`). Since only JavaScript that runs on your domain could read the
769
- * cookie, your server can be assured that the XHR came from JavaScript running on your domain.
770
- * The header will not be set for cross- domain requests .
815
+ * header (by default `X-XSRF-TOKEN`). Since only JavaScript that runs on your domain could read
816
+ * the cookie, your server can be assured that the XHR came from JavaScript running on your
817
+ * domain.
771
818
*
772
819
* To take advantage of this, your server needs to set a token in a JavaScript readable session
773
820
* cookie called `XSRF-TOKEN` on the first HTTP GET request. On subsequent XHR requests the
774
- * server can verify that the cookie matches `X-XSRF-TOKEN` HTTP header, and therefore be sure
775
- * that only JavaScript running on your domain could have sent the request. The token must be
776
- * unique for each user and must be verifiable by the server (to prevent the JavaScript from
821
+ * server can verify that the cookie matches the `X-XSRF-TOKEN` HTTP header, and therefore be
822
+ * sure that only JavaScript running on your domain could have sent the request. The token must
823
+ * be unique for each user and must be verifiable by the server (to prevent the JavaScript from
777
824
* making up its own tokens). We recommend that the token is a digest of your site's
778
825
* authentication cookie with a [salt](https://en.wikipedia.org/wiki/Salt_(cryptography))
779
826
* for added security.
780
827
*
781
- * The name of the headers can be specified using the xsrfHeaderName and xsrfCookieName
782
- * properties of either $httpProvider.defaults at config-time, $http.defaults at run-time,
783
- * or the per-request config object.
828
+ * The header will — by default — **not** be set for cross-domain requests. This
829
+ * prevents unauthorized servers (e.g. malicious or compromised 3rd-party APIs) from gaining
830
+ * access to your users' XSRF tokens and exposing them to Cross Site Request Forgery. If you
831
+ * want to, you can whitelist additional origins to also receive the XSRF token, by adding them
832
+ * to {@link ng.$httpProvider#xsrfWhitelistedOrigins xsrfWhitelistedOrigins}. This might be
833
+ * useful, for example, if your application, served from `example.com`, needs to access your API
834
+ * at `api.example.com`.
835
+ * See {@link ng.$httpProvider#xsrfWhitelistedOrigins $httpProvider.xsrfWhitelistedOrigins} for
836
+ * more details.
837
+ *
838
+ * <div class="alert alert-danger">
839
+ * **Warning**<br />
840
+ * Only whitelist origins that you have control over and make sure you understand the
841
+ * implications of doing so.
842
+ * </div>
843
+ *
844
+ * The name of the cookie and the header can be specified using the `xsrfCookieName` and
845
+ * `xsrfHeaderName` properties of either `$httpProvider.defaults` at config-time,
846
+ * `$http.defaults` at run-time, or the per-request config object.
784
847
*
785
848
* In order to prevent collisions in environments where multiple AngularJS apps share the
786
- * same domain or subdomain, we recommend that each application uses unique cookie name.
849
+ * same domain or subdomain, we recommend that each application uses a unique cookie name.
850
+ *
787
851
*
788
852
* @param {object } config Object describing the request to be made and how it should be
789
853
* processed. The object has following properties:
@@ -1343,7 +1407,7 @@ function $HttpProvider() {
1343
1407
// if we won't have the response in cache, set the xsrf headers and
1344
1408
// send the request to the backend
1345
1409
if ( isUndefined ( cachedResp ) ) {
1346
- var xsrfValue = urlIsSameOrigin ( config . url )
1410
+ var xsrfValue = urlIsAllowedOrigin ( config . url )
1347
1411
? $$cookieReader ( ) [ config . xsrfCookieName || defaults . xsrfCookieName ]
1348
1412
: undefined ;
1349
1413
if ( xsrfValue ) {
0 commit comments