@@ -29,6 +29,43 @@ function parseHeaders(headers) {
29
29
}
30
30
31
31
32
+ var IS_SAME_DOMAIN_URL_MATCH = / ^ ( ( [ ^ : ] + ) : ) ? \/ \/ ( \w + : { 0 , 1 } \w * @ ) ? ( [ \w \. - ] * ) ? ( : ( [ 0 - 9 ] + ) ) ? ( .* ) $ / ;
33
+
34
+
35
+ /**
36
+ * Parse a request and location URL and determine whether this is a same-domain request.
37
+ *
38
+ * @param {string } requestUrl The url of the request.
39
+ * @param {string } locationUrl The current browser location url.
40
+ * @returns {boolean } Whether the request is for the same domain.
41
+ */
42
+ function isSameDomain ( requestUrl , locationUrl ) {
43
+ var match = IS_SAME_DOMAIN_URL_MATCH . exec ( requestUrl ) ;
44
+ // if requestUrl is relative, the regex does not match.
45
+ if ( match == null ) return true ;
46
+
47
+ var domain1 = {
48
+ protocol : match [ 2 ] ,
49
+ host : match [ 4 ] ,
50
+ port : int ( match [ 6 ] ) || DEFAULT_PORTS [ match [ 2 ] ] || null ,
51
+ // IE8 sets unmatched groups to '' instead of undefined.
52
+ relativeProtocol : match [ 2 ] === undefined || match [ 2 ] === ''
53
+ } ;
54
+
55
+ match = URL_MATCH . exec ( locationUrl ) ;
56
+ var domain2 = {
57
+ protocol : match [ 1 ] ,
58
+ host : match [ 3 ] ,
59
+ port : int ( match [ 5 ] ) || DEFAULT_PORTS [ match [ 1 ] ] || null
60
+ } ;
61
+
62
+ return ( domain1 . protocol == domain2 . protocol || domain1 . relativeProtocol ) &&
63
+ domain1 . host == domain2 . host &&
64
+ ( domain1 . port == domain2 . port || ( domain1 . relativeProtocol &&
65
+ domain2 . port == DEFAULT_PORTS [ domain2 . protocol ] ) ) ;
66
+ }
67
+
68
+
32
69
/**
33
70
* Returns a function that provides access to parsed headers.
34
71
*
@@ -345,7 +382,7 @@ function $HttpProvider() {
345
382
* to counter XSRF. When performing XHR requests, the $http service reads a token from a cookie
346
383
* called `XSRF-TOKEN` and sets it as the HTTP header `X-XSRF-TOKEN`. Since only JavaScript that
347
384
* runs on your domain could read the cookie, your server can be assured that the XHR came from
348
- * JavaScript running on your domain.
385
+ * JavaScript running on your domain. The header will not be set for cross-domain requests.
349
386
*
350
387
* To take advantage of this, your server needs to set a token in a JavaScript readable session
351
388
* cookie called `XSRF-TOKEN` on first HTTP GET request. On subsequent non-GET requests the
@@ -476,7 +513,9 @@ function $HttpProvider() {
476
513
var reqTransformFn = config . transformRequest || defaults . transformRequest ,
477
514
respTransformFn = config . transformResponse || defaults . transformResponse ,
478
515
defHeaders = defaults . headers ,
479
- reqHeaders = extend ( { 'X-XSRF-TOKEN' : $browser . cookies ( ) [ 'XSRF-TOKEN' ] } ,
516
+ xsrfToken = isSameDomain ( config . url , $browser . url ( ) ) ?
517
+ $browser . cookies ( ) [ 'XSRF-TOKEN' ] : undefined ,
518
+ reqHeaders = extend ( { 'X-XSRF-TOKEN' : xsrfToken } ,
480
519
defHeaders . common , defHeaders [ lowercase ( config . method ) ] , config . headers ) ,
481
520
reqData = transformData ( config . data , headersGetter ( reqHeaders ) , reqTransformFn ) ,
482
521
promise ;
0 commit comments