Over a million developers have joined DZone.

Cross Domain Arbitrary File Upload Redux

DZone's Guide to

Cross Domain Arbitrary File Upload Redux

· Web Dev Zone ·
Free Resource

Read this guide to learn everything you need to know about RPA, and how it can help you manage and automate your processes.

Remember how it was possible to upload files with arbitrary names & contents cross domain?  The method had one, but crucial limitation - it

did not include any credentials. In other words, the POST message would be sent to server without any cookies / HTTP auth, so it would most likely be discarded by the attacked application. You could upload a file (precisely, that's a CSRF File Upload), but, in most cases, the receiving application would drop it. Until now :)

I can haz cookies!

I still don't know how did I miss this, but it's just a one-line change:
1 xhr.withCredentials = "true";
That's it. With this flag set:
  • CORS simple requests will include cookies / HTTP auth
  • CORS preflighted requests will ask for permission to include them

Luckily for attackers (and unfortunately for the Web), POST request with MIME type multipart/form-data and credentials are still in the 'simple' bucket. So the exact CSRF CORS File Upload attack works like this:

  1. Victim logs in to victim.whatever.com website 
  2. He receives a session cookie for future requests

    "Take those cookies to your grandma", said The Browser"
  3. In the same browser session (e.g. 2nd tab) he visits attacker.reallybad.ly website
  4. Javascript code in attacker silently prepares CORS file upload request with XMLHttpRequest object to victim domain, and asks to include credentials (xhr.withCredentials)

    "Browser, I really need you to send this tiny little harmless POST to victim"

  5. Browser treats this as a simple CORS request, so it attaches the cookie for victim domain to it and sends it.

    "Hey, JS! It's a request to another domain - what are you up to? Oh, just a POST request? No custom headers? Sure thing, here are the cookies and I wish you a pleasant journey!"

  6. victim app receives the POST file upload with the cookie, so it processes the upload and responds.

    "What's this weird Origin header pointing to attacker.reallybad.ly? It must be the new kid in town, but who am I to know?"

  7. Browser looks at the response and, not having appropriate CORS response headers, discards the response.

    "Oh dear! No Access-Control-Allow-Origin header at all! You bad Javascript! I won't give you the response, and you'll get spanked with an exception! Surely that was one nasty hack attack I prevented. Luckily I follow the CORS specification, good work, CORS guys!"

Yeah, exactly. Good work! Now the CSRF File Upload is super-simple. I've updated the examples with the new code. 

Get the senior executive’s handbook of important trends, tips, and strategies to compete and win in the digital economy.


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}