Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(csrf) enable csrf token in other way than ruby's one #134

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions ng-upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ angular.module('ngUpload', [])
}
};
}])
.directive('ngUpload', ["$log", "$parse", "$document",
function ($log, $parse, $document) {
.directive('ngUpload', ["$log", "$parse", "$document", "$browser", "$http",
function ($log, $parse, $document, $browser, $http) {
var iframeID = 1;
// Utility function to get meta tag with a given name attribute
function getMetaTagWithName(name) {
Expand All @@ -75,6 +75,10 @@ angular.module('ngUpload', [])
return angular.element(match);
}

function getCsrfTokenValue() {
return $browser.cookies()[$http.defaults.xsrfCookieName || 'X-XSRF-TOKEN'];
}

return {
restrict: 'AC',
link: function (scope, element, attrs) {
Expand Down Expand Up @@ -110,6 +114,11 @@ angular.module('ngUpload', [])
options.beforeSubmit = $parse(attrs.uploadOptionsBeforeSubmit);
}

if ( attrs.hasOwnProperty( "uploadOptionsEnableCsrf" ) ) {
// allow for blank or true
options.enableCsrf = attrs.uploadOptionsEnableCsrf != "false";
}

element.attr({
'target': 'upload-iframe-' + iframeID,
'method': 'post',
Expand All @@ -133,6 +142,17 @@ angular.module('ngUpload', [])

element.append(input);
}

if ( options.enableCsrf ) {
var input = angular.element("<input />");
input.attr("class", "upload-csrf-token");
input.attr("type", "hidden");
input.attr("name", attrs.uploadOptionsCsrfParam || 'CSRFToken');
input.val(getCsrfTokenValue());

element.append(input);
}

element.after(iframe);

setLoadingState(false);
Expand Down
2 changes: 1 addition & 1 deletion ng-upload.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ In order, for ngUpload to respond correctly for IE, your server needs to return
* `upload-options-enable-rails-csrf`: Turns on support for [Rails' CSRF](http://guides.rubyonrails.org/security.html#cross-site-request-forgery-csrf)
by adding a hidden form field with the csrf token.

* `upload-options-enable-csrf`: Turns on support for CSRF by adding a hidden form field with the csrf token fetched from cookies as configured in [$httpProvider.defaults.xsrfCookieName](https://docs.angularjs.org/api/ng/provider/$httpProvider).

* `upload-options-csrf-param`: Name of the csrf parameter to be sent while submitting form (Default value: "CSRFToken"). Only usable with `upload-options-enable-csrf` option.

* `upload-options-before-submit`: function that gets triggered before the upload starts and if the function returns false it will cancel the submit.

### uploadSubmit
Expand Down
71 changes: 71 additions & 0 deletions test/ngUploadCsrfSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// ngUpload as directive and input submit
describe('ngUpload', function() {
var scope, $compile, $http, $browser;
beforeEach(module('ngUpload'));
beforeEach(inject(function($rootScope, _$compile_, _$browser_, _$http_) {
$browser = _$browser_;
$browser.cookies('X-XSRF-TOKEN', 'the-token');
scope = $rootScope;
$compile = _$compile_;
$http = _$http_;
}));

function submit(element) {
element[0].getElementsByClassName('submit-button')[0].click();
}

function compile(template) {
var elm = angular.element(template);
$compile(elm)(scope);
return elm;
}

function getHiddenField(element) {
return element[0].getElementsByClassName('upload-csrf-token')[0];
}

it('should set csrf hidden field with csrf token from cookie', function() {
var form = compile(
'<div>' +
'<form action="/upload" ng-upload="foo()" upload-options-enable-csrf>' +
'<input type="file" name="foo"></input>' +
'<input class="submit-button" type="submit" value="submit"></input>' +
'</form>' +
'</div>');
submit(form);

var hiddenField = getHiddenField(form);
expect(hiddenField).toBeDefined();
expect(hiddenField.getAttribute('value')).toBe('the-token');
expect(hiddenField.getAttribute('name')).toBe('CSRFToken');
});

it('should get csrf param name from a specific attribute', function() {
var form = compile(
'<div>' +
'<form action="/upload" ng-upload="foo()" upload-options-enable-csrf upload-options-csrf-param="csrf-token-parameter">' +
'<input type="file" name="foo"></input>' +
'<input class="submit-button" type="submit" value="submit"></input>' +
'</form>' +
'</div>');
submit(form);

expect(getHiddenField(form).getAttribute('name')).toBe('csrf-token-parameter');
});

it('should get csrf token from cookies according to configured xsrfCookieName', function() {
$http.defaults.xsrfCookieName = 'another.cookie.name';
$browser.cookies('another.cookie.name', 'another-token');
var form = compile(
'<div>' +
'<form action="/upload" ng-upload="foo()" upload-options-enable-csrf upload-options-csrf-param="csrf-token-parameter">' +
'<input type="file" name="foo"></input>' +
'<input class="submit-button" type="submit" value="submit"></input>' +
'</form>' +
'</div>');
submit(form);

expect(getHiddenField(form).getAttribute('value')).toBe('another-token');
});

});