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