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

Commit 679741e

Browse files
committed
fixup! feat($http): support sending XSRF token to whitelisted origins
1 parent 34e0440 commit 679741e

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

src/ng/http.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -397,12 +397,14 @@ function $HttpProvider() {
397397
*
398398
* **Note:** An "origin" consists of the [URI scheme](https://en.wikipedia.org/wiki/URI_scheme),
399399
* the [hostname](https://en.wikipedia.org/wiki/Hostname) and the
400-
* [port number](https://en.wikipedia.org/wiki/Port_(computer_networking).
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`
401403
*
402404
* <div class="alert alert-warning">
403405
* It is not possible to whitelist specific URLs/paths. The `path`, `query` and `fragment` parts
404406
* of a URL will be ignored. For example, `https://foo.com/path/bar?query=baz#fragment` will be
405-
* treated as `https://foo.com/`, meaning that **all** requests to URLs starting with
407+
* treated as `https://foo.com`, meaning that **all** requests to URLs starting with
406408
* `https://foo.com/` will include the XSRF token.
407409
* </div>
408410
*
@@ -413,7 +415,7 @@ function $HttpProvider() {
413415
* angular.
414416
* module('xsrfWhitelistedOriginsExample', []).
415417
* config(['$httpProvider', function($httpProvider) {
416-
* $httpProvider.xsrfWhitelistedOrigins.push('https://api.example.com/');
418+
* $httpProvider.xsrfWhitelistedOrigins.push('https://api.example.com');
417419
* }]).
418420
* run(['$http', function($http) {
419421
* // The XSRF token will be sent.

test/ng/httpSpec.js

+22-13
Original file line numberDiff line numberDiff line change
@@ -2213,7 +2213,9 @@ describe('$http', function() {
22132213
var $httpBackend;
22142214

22152215
beforeEach(module(function($httpProvider) {
2216-
$httpProvider.xsrfWhitelistedOrigins.push('https://whitelisted.example.com/');
2216+
$httpProvider.xsrfWhitelistedOrigins.push(
2217+
'https://whitelisted.example.com',
2218+
'https://whitelisted2.example.com:1337/ignored/path');
22172219
}));
22182220

22192221
beforeEach(inject(function(_$http_, _$httpBackend_) {
@@ -2308,15 +2310,19 @@ describe('$http', function() {
23082310
function checkHeaders(headers) {
23092311
return isUndefined(headers['X-XSRF-TOKEN']);
23102312
}
2311-
var currentUrl = 'https://example.com/path';
2312-
var requestUrl = 'https://api.example.com/path';
2313+
var requestUrls = [
2314+
'https://api.example.com/path',
2315+
'http://whitelisted.example.com',
2316+
'https://whitelisted2.example.com:1338'
2317+
];
23132318

23142319
mockedCookies['XSRF-TOKEN'] = 'secret';
2315-
$httpBackend.expect('GET', requestUrl, null, checkHeaders).respond(null);
23162320

2317-
$http.get(requestUrl);
2318-
2319-
$httpBackend.flush();
2321+
requestUrls.forEach(function(url) {
2322+
$httpBackend.expect('GET', url, null, checkHeaders).respond(null);
2323+
$http.get(url);
2324+
$httpBackend.flush();
2325+
});
23202326
});
23212327

23222328

@@ -2326,16 +2332,19 @@ describe('$http', function() {
23262332
return headers['X-XSRF-TOKEN'] === 'secret';
23272333
}
23282334
var currentUrl = 'https://example.com/path';
2329-
var requestUrl = 'https://whitelisted.example.com/path';
2335+
var requestUrls = [
2336+
'https://whitelisted.example.com/path',
2337+
'https://whitelisted2.example.com:1337/path'
2338+
];
23302339

23312340
$browser.url(currentUrl);
2332-
23332341
mockedCookies['XSRF-TOKEN'] = 'secret';
2334-
$httpBackend.expect('GET', requestUrl, null, checkHeaders).respond(null);
23352342

2336-
$http.get(requestUrl);
2337-
2338-
$httpBackend.flush();
2343+
requestUrls.forEach(function(url) {
2344+
$httpBackend.expect('GET', url, null, checkHeaders).respond(null);
2345+
$http.get(url);
2346+
$httpBackend.flush();
2347+
});
23392348
})
23402349
);
23412350
});

0 commit comments

Comments
 (0)