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

Problem with jquery-file-upload and csrf-magic #6

Open
webbird opened this issue May 20, 2015 · 6 comments
Open

Problem with jquery-file-upload and csrf-magic #6

webbird opened this issue May 20, 2015 · 6 comments

Comments

@webbird
Copy link

webbird commented May 20, 2015

I am having a problem combining csrf-magic and jquery-file-upload by blueimp. When trying to send the files (i.e. send the upload form), the csrf-magic.js prepends the data with it's token. Result is an invalid post:

__csrf_magic=sid:d0a151fd235a4f1302269149a01afe55a45db3de,1432143876&[object FormData]

The server side script now gets nothing as the query string is invalid.

Any ideas?

@webbird
Copy link
Author

webbird commented May 21, 2015

Seems I was able to fix this by extending the process method like so:

CsrfMagic.process = function(base) {
    if(typeof base == 'object') {
        base[csrfMagicName] = csrfMagicToken;
        return base;
    }
    var prepend = csrfMagicName + '=' + csrfMagicToken;
    if (base) return prepend + '&' + base;
    return prepend;
}

Will have to test this a little more.

@samhaldia
Copy link

hi,
Will you be please be precise about it.
Sameer

On 5/21/15, Bianka Martinovic [email protected] wrote:

Seems I was able to fix this by extending the process method like so:

CsrfMagic.process = function(base) {
if(typeof base == 'object') {
base[csrfMagicName] = csrfMagicToken;
return base;
}
var prepend = csrfMagicName + '=' + csrfMagicToken;
if (base) return prepend + '&' + base;
return prepend;
}

Will have to test this a little more.


Reply to this email directly or view it on GitHub:
#6 (comment)

@webbird
Copy link
Author

webbird commented May 21, 2015

Uhm, what do you mean?

@samhaldia
Copy link

i meant will you tell me the steps to reproduce for my php based
website. Actually on not finding any solution i left but again i would
like to work it out

On 5/21/15, Bianka Martinovic [email protected] wrote:

Uhm, what do you mean?


Reply to this email directly or view it on GitHub:
#6 (comment)

@webbird
Copy link
Author

webbird commented May 21, 2015

In my case, the jQuery plugin sends an object instead of a string. When csrf-magic prepends the form data with the token, result is

__csrf_magic=sid:d0a151fd235a4f1302269149a01afe55a45db3de,1432143876&[object FormData]

The part before the & is the token string, the part after would be an object, but now it's cast to a string. The result is an invalid query string, so no data is posted to the PHP script on the server side.

To find this, I used Firebug to see what is sent to the server (console window).

After adding the patch shown above all works fine for me.

NewEraCracker added a commit to NewEraCracker/csrf-magic that referenced this issue Mar 22, 2016
@GreeKatrina
Copy link

I also ran into this issue and used the same fix, but I had to add it in the CsrfMagic.prototype.send() method as well. You could call the process() method, in order to avoid duplicate code.

send: function(data) {
    if (!this.csrf_isPost) return this.csrf_send(data);
    prepend = csrfMagicName + '=' + csrfMagicToken + '&';
    if (this.csrf_purportedLength === undefined) {
        this.csrf_setRequestHeader("Content-length", this.csrf_purportedLength + prepend.length);
        delete this.csrf_purportedLength;
    }
    delete this.csrf_isPost;

    // Fix to work with FormData objects.
    if (typeof data == 'object') {
        data[csrfMagicName] = csrfMagicToken;
        return this.csrf_send(data);
    }

    return this.csrf_send(prepend + data);
},

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants