@@ -80,7 +80,8 @@ var CSRFP = {
80
80
*/
81
81
_getInputElt : function ( ) {
82
82
var hiddenObj = document . createElement ( "input" ) ;
83
- hiddenObj . name = CSRFP . CSRFP_TOKEN ;
83
+ hiddenObj . setAttribute ( 'name' , CSRFP . CSRFP_TOKEN ) ;
84
+ hiddenObj . setAttribute ( 'class' , CSRFP . CSRFP_TOKEN ) ;
84
85
hiddenObj . type = 'hidden' ;
85
86
hiddenObj . value = CSRFP . _getAuthKey ( ) ;
86
87
return hiddenObj ;
@@ -174,6 +175,7 @@ function csrfprotector_init() {
174
175
//==================================================================
175
176
// Adding csrftoken to request resulting from <form> submissions
176
177
// Add for each POST, while for mentioned GET request
178
+ // TODO - check for method
177
179
//==================================================================
178
180
for ( var i = 0 ; i < document . forms . length ; i ++ ) {
179
181
document . forms [ i ] . addEventListener ( "submit" , function ( event ) {
@@ -186,9 +188,24 @@ function csrfprotector_init() {
186
188
} ) ;
187
189
}
188
190
191
+ //==================================================================
192
+ // Adding csrftoken to request resulting from direct form.submit() call
193
+ // Add for each POST, while for mentioned GET request
194
+ // TODO - check for form method
195
+ //==================================================================
196
+ HTMLFormElement . prototype . submit_ = HTMLFormElement . prototype . submit ;
197
+ HTMLFormElement . prototype . submit = function ( ) {
198
+ // check if the FORM already contains the token element
199
+ if ( ! this . getElementsByClassName ( CSRFP . CSRFP_TOKEN ) . length )
200
+ this . appendChild ( CSRFP . _getInputElt ( ) ) ;
201
+ this . submit_ ( ) ;
202
+ }
203
+
204
+
189
205
/**
190
206
* Add wrapper for HTMLFormElements addEventListener so that any further
191
207
* addEventListens won't have trouble with CSRF token
208
+ * todo - check for method
192
209
*/
193
210
HTMLFormElement . prototype . addEventListener_ = HTMLFormElement . prototype . addEventListener ;
194
211
HTMLFormElement . prototype . addEventListener = function ( eventType , fun , bubble ) {
@@ -202,6 +219,8 @@ function csrfprotector_init() {
202
219
203
220
/**
204
221
* Add wrapper for IE's attachEvent
222
+ * todo - check for method
223
+ * todo - typeof is now obselete for IE 11, use some other method.
205
224
*/
206
225
if ( typeof HTMLFormElement . prototype . attachEvent !== 'undefined' ) {
207
226
HTMLFormElement . prototype . attachEvent_ = HTMLFormElement . prototype . attachEvent ;
0 commit comments