forked from cujojs/rest
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue: cujojs#39
- Loading branch information
Showing
4 changed files
with
205 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright 2013 the original author or authors | ||
* @license MIT, see LICENSE.txt for details | ||
* | ||
* @author Scott Andrews | ||
*/ | ||
|
||
(function (define) { | ||
'use strict'; | ||
|
||
define(function (require) { | ||
|
||
var interceptor; | ||
|
||
interceptor = require('../interceptor'); | ||
|
||
/** | ||
* Applies a Cross-Site Request Forgery protection header to a request | ||
* | ||
* CSRF protection helps a server verify that a request came from a | ||
* trusted client and not another client that was able to masquerade | ||
* as an authorized client. Sites that use cookie based authentication | ||
* are particularly vulnerable to request forgeries without extra | ||
* protection. | ||
* | ||
* @see http://en.wikipedia.org/wiki/Cross-site_request_forgery | ||
* | ||
* @param {Client} [client] client to wrap | ||
* @param {string} [config.name='X-Csrf-Token'] name of the request | ||
* header, may be overridden by `request.csrfTokenName` | ||
* @param {string} [config.token] CSRF token, may be overridden by | ||
* `request.csrfToken` | ||
* | ||
* @returns {Client} | ||
*/ | ||
return interceptor({ | ||
init: function (config) { | ||
config.name = config.name || 'X-Csrf-Token'; | ||
return config; | ||
}, | ||
request: function handleRequest(request, config) { | ||
var headers, name, token; | ||
|
||
headers = request.headers || (request.headers = {}); | ||
name = request.csrfTokenName || config.name; | ||
token = request.csrfToken || config.token; | ||
|
||
if (token) { | ||
headers[name] = token; | ||
} | ||
|
||
return request; | ||
} | ||
}); | ||
|
||
}); | ||
|
||
}( | ||
typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); } | ||
// Boilerplate for AMD and Node | ||
)); |
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 |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright 2013 the original author or authors | ||
* @license MIT, see LICENSE.txt for details | ||
* | ||
* @author Scott Andrews | ||
*/ | ||
|
||
(function (buster, define) { | ||
'use strict'; | ||
|
||
var assert, refute, fail; | ||
|
||
assert = buster.assertions.assert; | ||
refute = buster.assertions.refute; | ||
fail = buster.assertions.fail; | ||
|
||
define('rest/interceptor/csrf-test', function (require) { | ||
|
||
var csrf, rest; | ||
|
||
csrf = require('rest/interceptor/csrf'); | ||
rest = require('rest'); | ||
|
||
buster.testCase('rest/interceptor/csrf', { | ||
'should protect the requst from the config': function () { | ||
var client = csrf( | ||
function (request) { return { request: request }; }, | ||
{ token: 'abc123xyz789'} | ||
); | ||
return client({}).then(function (response) { | ||
assert.equals('abc123xyz789', response.request.headers['X-Csrf-Token']); | ||
}).otherwise(fail); | ||
}, | ||
'should protect the requst from the request': function () { | ||
var client = csrf( | ||
function (request) { return { request: request }; } | ||
); | ||
return client({ csrfToken: 'abc123xyz789' }).then(function (response) { | ||
assert.equals('abc123xyz789', response.request.headers['X-Csrf-Token']); | ||
}).otherwise(fail); | ||
}, | ||
'should protect the requst from the config using a custom header': function () { | ||
var client = csrf( | ||
function (request) { return { request: request }; }, | ||
{ token: 'abc123xyz789', name: 'Csrf-Token' } | ||
); | ||
return client({}).then(function (response) { | ||
assert.equals('abc123xyz789', response.request.headers['Csrf-Token']); | ||
}).otherwise(fail); | ||
}, | ||
'should protect the requst from the request using a custom header': function () { | ||
var client = csrf( | ||
function (request) { return { request: request }; } | ||
); | ||
return client({ csrfToken: 'abc123xyz789', csrfTokenName: 'Csrf-Token' }).then(function (response) { | ||
assert.equals('abc123xyz789', response.request.headers['Csrf-Token']); | ||
}).otherwise(fail); | ||
}, | ||
'should not protect without a token': function () { | ||
var client = csrf( | ||
function (request) { return { request: request }; } | ||
); | ||
return client({}).then(function (response) { | ||
refute.defined(response.request.headers['X-Csrf-Token']); | ||
}).otherwise(fail); | ||
}, | ||
'should have the default client as the parent by default': function () { | ||
assert.same(rest, csrf().skip()); | ||
}, | ||
'should support interceptor chaining': function () { | ||
assert(typeof csrf().chain === 'function'); | ||
} | ||
}); | ||
|
||
}); | ||
|
||
}( | ||
this.buster || require('buster'), | ||
typeof define === 'function' && define.amd ? define : function (id, factory) { | ||
var packageName = id.split(/[\/\-]/)[0], pathToRoot = id.replace(/[^\/]+/g, '..'); | ||
pathToRoot = pathToRoot.length > 2 ? pathToRoot.substr(3) : pathToRoot; | ||
factory(function (moduleId) { | ||
return require(moduleId.indexOf(packageName) === 0 ? pathToRoot + moduleId.substr(packageName.length) : moduleId); | ||
}); | ||
} | ||
// Boilerplate for AMD and Node | ||
)); |