Skip to content

Commit 330f4b7

Browse files
committed
adopted hidden data field insertion
now rather than creating a new js file each time the config file is modified csrfp inserts the cookie name and url patterns as hidden fields in HTML output and js code fetches them
1 parent 9b9e40d commit 330f4b7

File tree

4 files changed

+29
-400
lines changed

4 files changed

+29
-400
lines changed

js/csrfprotector.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
* =================================================================
1212
*/
1313

14+
var CSRFP_FIELD_TOKEN_NAME = 'csrfp_hidden_data_token';
15+
var CSRFP_FIELD_URLS = 'csrfp_hidden_data_urls';
16+
1417
var CSRFP = {
1518
CSRFP_TOKEN: 'csrfp_token',
1619
/**
@@ -19,10 +22,7 @@ var CSRFP = {
1922
*
2023
* @var string array
2124
*/
22-
checkForUrls: Array.prototype.slice.call(document.getElementsByName("CSRFP_checkForUrls"))
23-
.map(function (element) {
24-
return element.value;
25-
}),
25+
checkForUrls: [],
2626
/**
2727
* Function to check if a certain url is allowed to perform the request
2828
* With or without csrf token
@@ -143,6 +143,14 @@ var CSRFP = {
143143
* @return void
144144
*/
145145
_init: function() {
146+
CSRFP.CSRFP_TOKEN = document.getElementById(CSRFP_FIELD_TOKEN_NAME).value;
147+
try {
148+
CSRFP.checkForUrls = JSON.parse(document.getElementById(CSRFP_FIELD_URLS).value);
149+
} catch (err) {
150+
console.error(err);
151+
console.error('[ERROR] [CSRF Protector] unable to parse blacklisted url fields.');
152+
}
153+
146154
//convert these rules received from php lib to regex objects
147155
for (var i = 0; i < CSRFP.checkForUrls.length; i++) {
148156
CSRFP.checkForUrls[i] = CSRFP.checkForUrls[i].replace(/\*/g, '(.*)')

libs/config.sample.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@
2323
Cross-Site Request Forgeries </a> attacks. In order to do so, you must have JavaScript enabled in your web browser otherwise this site will fail to work correctly for you.
2424
See details of your web browser for how to enable JavaScript.",
2525
"verifyGetFor" => array()
26-
);
26+
);

libs/csrf/csrfpJsFileBase.php

Lines changed: 0 additions & 316 deletions
This file was deleted.

0 commit comments

Comments
 (0)