-
-
Notifications
You must be signed in to change notification settings - Fork 10.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(security): fixed CVE-2023-45857 by backporting
withXSRFToken
op…
…tion to v0.x (#6091)
- Loading branch information
Showing
7 changed files
with
96 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,146 @@ | ||
'use strict'; | ||
|
||
var cookies = require('../../lib/helpers/cookies'); | ||
|
||
describe('xsrf', function () { | ||
beforeEach(function () { | ||
describe('xsrf', function() { | ||
beforeEach(function() { | ||
jasmine.Ajax.install(); | ||
}); | ||
|
||
afterEach(function () { | ||
afterEach(function() { | ||
document.cookie = axios.defaults.xsrfCookieName + '=;expires=' + new Date(Date.now() - 86400000).toGMTString(); | ||
jasmine.Ajax.uninstall(); | ||
}); | ||
|
||
it('should not set xsrf header if cookie is null', function (done) { | ||
it('should not set xsrf header if cookie is null', function(done) { | ||
axios('/foo'); | ||
|
||
getAjaxRequest().then(function (request) { | ||
getAjaxRequest().then(function(request) { | ||
expect(request.requestHeaders[axios.defaults.xsrfHeaderName]).toEqual(undefined); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should set xsrf header if cookie is set', function (done) { | ||
it('should set xsrf header if cookie is set', function(done) { | ||
document.cookie = axios.defaults.xsrfCookieName + '=12345'; | ||
|
||
axios('/foo'); | ||
|
||
getAjaxRequest().then(function (request) { | ||
getAjaxRequest().then(function(request) { | ||
expect(request.requestHeaders[axios.defaults.xsrfHeaderName]).toEqual('12345'); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should not set xsrf header if xsrfCookieName is null', function (done) { | ||
it('should not set xsrf header if xsrfCookieName is null', function(done) { | ||
document.cookie = axios.defaults.xsrfCookieName + '=12345'; | ||
|
||
axios('/foo', { | ||
xsrfCookieName: null | ||
}); | ||
|
||
getAjaxRequest().then(function (request) { | ||
getAjaxRequest().then(function(request) { | ||
expect(request.requestHeaders[axios.defaults.xsrfHeaderName]).toEqual(undefined); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should not read cookies at all if xsrfCookieName is null', function (done) { | ||
spyOn(cookies, "read"); | ||
it('should not read cookies at all if xsrfCookieName is null', function(done) { | ||
spyOn(cookies, 'read'); | ||
|
||
axios('/foo', { | ||
xsrfCookieName: null | ||
}); | ||
|
||
getAjaxRequest().then(function (request) { | ||
getAjaxRequest().then(function() { | ||
expect(cookies.read).not.toHaveBeenCalled(); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should not set xsrf header for cross origin', function (done) { | ||
it('should not set xsrf header for cross origin', function(done) { | ||
document.cookie = axios.defaults.xsrfCookieName + '=12345'; | ||
|
||
axios('http://example.com/'); | ||
|
||
getAjaxRequest().then(function (request) { | ||
getAjaxRequest().then(function(request) { | ||
expect(request.requestHeaders[axios.defaults.xsrfHeaderName]).toEqual(undefined); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should set xsrf header for cross origin when using withCredentials', function (done) { | ||
it('should set xsrf header for cross origin when using withCredentials and withXSRFToken', function(done) { | ||
document.cookie = axios.defaults.xsrfCookieName + '=12345'; | ||
|
||
axios('http://example.com/', { | ||
withCredentials: true | ||
withCredentials: true, | ||
withXSRFToken: true | ||
}); | ||
|
||
getAjaxRequest().then(function (request) { | ||
getAjaxRequest().then(function(request) { | ||
expect(request.requestHeaders[axios.defaults.xsrfHeaderName]).toEqual('12345'); | ||
done(); | ||
}); | ||
}); | ||
describe('withXSRFToken option', function() { | ||
it('should set xsrf header for cross origin when withXSRFToken = true', function(done) { | ||
var token = '12345'; | ||
|
||
document.cookie = axios.defaults.xsrfCookieName + '=' + token; | ||
|
||
axios('http://example.com/', { | ||
withXSRFToken: true | ||
}); | ||
|
||
getAjaxRequest().then(function(request) { | ||
expect(request.requestHeaders[axios.defaults.xsrfHeaderName]).toEqual(token); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should not set xsrf header for the same origin when withXSRFToken = false', function(done) { | ||
var token = '12345'; | ||
|
||
document.cookie = axios.defaults.xsrfCookieName + '=' + token; | ||
|
||
axios('/foo', { | ||
withXSRFToken: false | ||
}); | ||
|
||
getAjaxRequest().then(function(request) { | ||
expect(request.requestHeaders[axios.defaults.xsrfHeaderName]).toEqual(undefined); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should not set xsrf header for the same origin when withXSRFToken = false', function(done) { | ||
var token = '12345'; | ||
|
||
document.cookie = axios.defaults.xsrfCookieName + '=' + token; | ||
|
||
axios('/foo', { | ||
withXSRFToken: false | ||
}); | ||
|
||
getAjaxRequest().then(function(request) { | ||
expect(request.requestHeaders[axios.defaults.xsrfHeaderName]).toEqual(undefined); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should support function resolver', function(done) { | ||
var token = '12345'; | ||
|
||
document.cookie = axios.defaults.xsrfCookieName + '=' + token; | ||
axios('/foo', { | ||
withXSRFToken: function(config) { return config.userFlag === 'yes';}, | ||
userFlag: 'yes' | ||
}); | ||
|
||
getAjaxRequest().then(function(request) { | ||
expect(request.requestHeaders[axios.defaults.xsrfHeaderName]).toEqual(token); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |