diff options
| author | Xavier Morel <xmo@odoo.com> | 2015-09-23 15:55:24 +0200 |
|---|---|---|
| committer | Olivier Dony <odo@openerp.com> | 2015-10-01 01:36:50 +0200 |
| commit | cea29e0c0176657f094ab80b460be503affe8a1b (patch) | |
| tree | 445bbe02c60a5f5a3d6638b38888591fe452a08c /addons/web/static | |
| parent | 7753ed7ce4b8e6eb06dc42e284af39fbd9ccefd5 (diff) | |
[ADD] client-side CSRF support
* add CSRF token as core.csrf_token
* add CSRF tokens to client-generated and/or JS-submitted forms
* remove broken "compatibility" mode of web.ajax.post
/cc @dmo-odoo I've no idea how that was supposed to work, from looking
things up all modern browsers seem to support FormData, and old IEs
which don't don't support fallbacks either and would require
submitting an actual form as fallback so...
Diffstat (limited to 'addons/web/static')
| -rw-r--r-- | addons/web/static/src/js/framework/ajax.js | 17 | ||||
| -rw-r--r-- | addons/web/static/src/xml/base.xml | 3 |
2 files changed, 12 insertions, 8 deletions
diff --git a/addons/web/static/src/js/framework/ajax.js b/addons/web/static/src/js/framework/ajax.js index f8392287b26..d6619449a0f 100644 --- a/addons/web/static/src/js/framework/ajax.js +++ b/addons/web/static/src/js/framework/ajax.js @@ -1,6 +1,7 @@ odoo.define('web.ajax', function (require) { "use strict"; +var core = require('web.core'); var time = require('web.time'); var utils = require('web.utils'); @@ -238,6 +239,11 @@ function get_file(options) { method: 'POST' }).appendTo(document.body); } + if (core.csrf_token) { + $('<input type="hidden" name="csrf_token">') + .val(core.csrf_token) + .appendTo($form_data); + } var hparams = _.extend({}, options.data || {}, {token: token}); _.each(hparams, function (value, key) { @@ -292,19 +298,16 @@ function post (controller_url, data) { }; var Def = $.Deferred(); - var compatibility = !(typeof(FormData)); - var postData = compatibility ? new FormDataCompatibility() : new FormData(); + var postData = new FormData(); $.each(data, function(i,val) { postData.append(i, val); }); - - var xhr = new XMLHttpRequest(); - if(compatibility) { - postData.setContentTypeHeader(xhr); - postData = postData.buildBody(); + if (core.csrf_token) { + postData.append('csrf_token', core.csrf_token); } + var xhr = new XMLHttpRequest(); if(xhr.upload) xhr.upload.addEventListener('progress', progressHandler(Def), false); var ajaxDef = $.ajax(controller_url, { diff --git a/addons/web/static/src/xml/base.xml b/addons/web/static/src/xml/base.xml index cae5392cf3b..0ce2ab0f08f 100644 --- a/addons/web/static/src/xml/base.xml +++ b/addons/web/static/src/xml/base.xml @@ -988,7 +988,8 @@ <t t-name="HiddenInputFile"> <div t-attf-class="oe_hidden_input_file #{fileupload_class or ''}" t-att-style="fileupload_style"> <form class="o_form_binary_form" t-att-target="fileupload_id" - method="post" enctype="multipart/form-data" t-att-action="fileupload_action || '/web/binary/upload'"> + method="post" enctype="multipart/form-data" t-att-action="fileupload_action || '/web/binary/upload'"> + <input type="hidden" name="csrf_token" t-att-value="csrf_token"/> <input type="hidden" name="session_id" value="" t-if="widget.session.override_session"/> <input type="hidden" name="callback" t-att-value="fileupload_id"/> <t t-raw="0"/> |
