Skip to content

Commit 9eebbe3

Browse files
authored
Merge pull request #97 from bstoney/additional-config
Config for cookie expire, logDirectory and jsUrl
2 parents 638f411 + e8c0ab7 commit 9eebbe3

13 files changed

+418
-177
lines changed

.gitignore

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
phpunit.phar
2-
coverage/*
2+
coveralls.phar
3+
4+
coverage/
5+
vendor/
6+
build/
7+
log/*.log

js/csrfprotector.js

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ var CSRFP = {
2020
* Array of patterns of url, for which csrftoken need to be added
2121
* In case of GET request also, provided from server
2222
*
23-
* @var string array
23+
* @var {Array}
2424
*/
2525
checkForUrls: [],
2626
/**
2727
* Function to check if a certain url is allowed to perform the request
2828
* With or without csrf token
2929
*
30-
* @param: string, url
30+
* @param {string} url
3131
*
32-
* @return: boolean, true if csrftoken is not needed
32+
* @return {Boolean} true if csrftoken is not needed
3333
* false if csrftoken is needed
3434
*/
3535
_isValidGetRequest: function(url) {
@@ -41,12 +41,12 @@ var CSRFP = {
4141
}
4242
return true;
4343
},
44-
/**
45-
* function to get Auth key from cookie Andreturn it to requesting function
44+
/**
45+
* Function to get Auth key from cookie and return it to requesting function
4646
*
4747
* @param: void
4848
*
49-
* @return: string, csrftoken retrieved from cookie
49+
* @return {string|Boolean} csrftoken retrieved from cookie
5050
*/
5151
_getAuthKey: function() {
5252
var re = new RegExp(CSRFP.CSRFP_TOKEN +"=([^;]+)(;|$)");
@@ -60,9 +60,9 @@ var CSRFP = {
6060
/**
6161
* Function to get domain of any url
6262
*
63-
* @param: string, url
63+
* @param {string} url
6464
*
65-
* @return: string, domain of url
65+
* @return {string} domain of url
6666
*/
6767
_getDomain: function(url) {
6868
if (url.indexOf("http://") !== 0
@@ -72,11 +72,11 @@ var CSRFP = {
7272
},
7373
/**
7474
* Function to create and return a hidden input element
75-
* For stroing the CSRFP_TOKEN
75+
* For storing the CSRFP_TOKEN
7676
*
77-
* @param void
77+
* @param: void
7878
*
79-
* @return input element
79+
* @return {HTMLInputElement} input element
8080
*/
8181
_getInputElt: function() {
8282
var hiddenObj = document.createElement("input");
@@ -88,11 +88,11 @@ var CSRFP = {
8888
},
8989
/**
9090
* Returns absolute path for relative path
91-
*
92-
* @param base, base url
93-
* @param relative, relative url
9491
*
95-
* @return absolute path (string)
92+
* @param {string} base base url
93+
* @param {string} relative relative url
94+
*
95+
* @return {string} absolute path
9696
*/
9797
_getAbsolutePath: function(base, relative) {
9898
var stack = base.split("/");
@@ -102,22 +102,22 @@ var CSRFP = {
102102
stack.pop();
103103

104104
for (var i = 0; i < parts.length; i++) {
105-
if (parts[i] == ".")
105+
if (parts[i] === ".")
106106
continue;
107-
if (parts[i] == "..")
107+
if (parts[i] === "..")
108108
stack.pop();
109109
else
110110
stack.push(parts[i]);
111111
}
112112
return stack.join("/");
113113
},
114-
/**
115-
* Remove jcsrfp-token run fun and then put them back
114+
/**
115+
* Remove jcsrfp-token run fun and then put them back
116116
*
117-
* @param function
118-
* @param reference form obj
117+
* @param {function} fun
118+
* @param {object} obj reference form obj
119119
*
120-
* @retrun function
120+
* @return function
121121
*/
122122
_csrfpWrap: function(fun, obj) {
123123
return function(event) {
@@ -139,7 +139,7 @@ var CSRFP = {
139139
/**
140140
* Initialises the CSRFProtector js script
141141
*
142-
* @param void
142+
* @param: void
143143
*
144144
* @return void
145145
*/
@@ -169,7 +169,7 @@ var CSRFP = {
169169

170170
function csrfprotector_init() {
171171

172-
// Call the init funcion
172+
// Call the init function
173173
CSRFP._init();
174174

175175
// definition of basic FORM submit event handler to intercept the form request
@@ -181,7 +181,7 @@ function csrfprotector_init() {
181181
//modify token to latest value
182182
event.target[CSRFP.CSRFP_TOKEN].value = CSRFP._getAuthKey();
183183
}
184-
}
184+
};
185185

186186
//==================================================================
187187
// Adding csrftoken to request resulting from <form> submissions
@@ -192,10 +192,10 @@ function csrfprotector_init() {
192192
document.querySelector('body').addEventListener('submit', function(event) {
193193
if (event.target.tagName.toLowerCase() === 'form') {
194194
BasicSubmitInterceptor(event);
195-
};
195+
}
196196
});
197197

198-
// intial binding
198+
// initial binding
199199
// for(var i = 0; i < document.forms.length; i++) {
200200
// document.forms[i].addEventListener("submit", BasicSubmitInterceptor);
201201
// }
@@ -211,7 +211,7 @@ function csrfprotector_init() {
211211
if (!this.getElementsByClassName(CSRFP.CSRFP_TOKEN).length)
212212
this.appendChild(CSRFP._getInputElt());
213213
this.submit_();
214-
}
214+
};
215215

216216

217217
/**
@@ -227,12 +227,12 @@ function csrfprotector_init() {
227227
} else {
228228
this.addEventListener_(eventType, fun, bubble);
229229
}
230-
}
230+
};
231231

232232
/**
233233
* Add wrapper for IE's attachEvent
234234
* todo - check for method
235-
* todo - typeof is now obselete for IE 11, use some other method.
235+
* todo - typeof is now obsolete for IE 11, use some other method.
236236
*/
237237
if (typeof HTMLFormElement.prototype.attachEvent !== 'undefined') {
238238
HTMLFormElement.prototype.attachEvent_ = HTMLFormElement.prototype.attachEvent;
@@ -254,13 +254,13 @@ function csrfprotector_init() {
254254

255255
/**
256256
* Wrapper to XHR open method
257-
* Add a property method to XMLHttpRequst class
257+
* Add a property method to XMLHttpRequest class
258258
* @param: all parameters to XHR open method
259259
* @return: object returned by default, XHR open method
260260
*/
261261
function new_open(method, url, async, username, password) {
262262
this.method = method;
263-
var isAbsolute = (url.indexOf("./") === -1) ? true : false;
263+
var isAbsolute = (url.indexOf("./") === -1);
264264
if (!isAbsolute) {
265265
var base = location.protocol +'//' +location.host
266266
+ location.pathname;
@@ -281,7 +281,7 @@ function csrfprotector_init() {
281281

282282
/**
283283
* Wrapper to XHR send method
284-
* Add query paramter to XHR object
284+
* Add query parameter to XHR object
285285
*
286286
* @param: all parameters to XHR send method
287287
*
@@ -313,7 +313,7 @@ function csrfprotector_init() {
313313
// Rules:
314314
// Rewrite those urls which matches the regex sent by Server
315315
// Ignore cross origin urls & internal links (one with hashtags)
316-
// Append the token to those url already containig GET query parameter(s)
316+
// Append the token to those url already containing GET query parameter(s)
317317
// Add the token to those which does not contain GET query parameter(s)
318318
//==================================================================
319319

@@ -322,9 +322,9 @@ function csrfprotector_init() {
322322
var href = event.target.href;
323323
if(typeof href === "string")
324324
{
325-
var urlDisect = href.split('#');
326-
var url = urlDisect[0];
327-
var hash = urlDisect[1];
325+
var urlParts = href.split('#');
326+
var url = urlParts[0];
327+
var hash = urlParts[1];
328328

329329
if(CSRFP._getDomain(url).indexOf(document.domain) === -1
330330
|| CSRFP._isValidGetRequest(url)) {

js/index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
33
* OWASP CSRF Protector Project
4-
* Code to redirect the user to previosus directory
4+
* Code to redirect the user to previous directory
55
* In case a user try to access this directory directly
66
*/
77
header('location: ../index.php');

libs/README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@ CSRFProtector configuration
22
==========================================
33

44
- `CSRFP_TOKEN`: name of the csrf nonce, used for cookie or posting as argument. default: `csrfp_token` (if left blank)
5-
- `logDirectory`: location of the directory at which log files will be saved **relative** to `config.php` file. This is required for file based logging (default), Not needed, in case you override logging function to implement your logging logic. (View [Overriding logging function](https://github.com/mebjas/CSRF-Protector-PHP/wiki/Overriding-logging-function))
5+
- `logDirectory`: location of the directory at which log files will be saved, either **relative** to the default `config.php` file location or an **absolute** path. This is required for file based logging (default), Not needed, in case you override logging function to implement your logging logic. (View [Overriding logging function](https://github.com/mebjas/CSRF-Protector-PHP/wiki/Overriding-logging-function))
66
<br>**Default value:** `../log/`
77
- `failedAuthAction`: Action code (integer) for action to be taken in case of failed validation. Has two different values for bot `GET` and `POST`. Different action codes are specified as follows, (<br>**Default:** `0` for both `GET` & `POST`):
8-
* `0` Send **403, Forbidden** Header
9-
* `1` **Strip the POST/GET query** and forward the request! unset($_POST)
10-
* `2` **Redirect to custom error page** mentioned in `errorRedirectionPage`
11-
* `3` **Show custom error message** to user, mentioned in `customErrorMessage`
12-
* `4` Send **500, Internal Server Error** header
8+
* `0` Send **403, Forbidden** Header
9+
* `1` **Strip the POST/GET query** and forward the request! unset($_POST)
10+
* `2` **Redirect to custom error page** mentioned in `errorRedirectionPage`
11+
* `3` **Show custom error message** to user, mentioned in `customErrorMessage`
12+
* `4` Send **500, Internal Server Error** header
1313

1414
- `errorRedirectionPage`: **Absolute url** of the file to which user should be redirected. <br>**Default: null**
1515
- `customErrorMessage`: **Error Message** to be shown to user. Only this text will be shown!<br>**Default: null**
16-
- `jsUrl`: **Absolute url** of the js file. (See [Setting up](https://github.com/mebjas/CSRF-Protector-PHP/wiki/Setting-up-CSRF-Protector-PHP-in-your-web-application) for more information)
16+
- `jsUrl`: **Absolute url** of the js file or `FALSE` if the js file will be added to the page manually. (See [Setting up](https://github.com/mebjas/CSRF-Protector-PHP/wiki/Setting-up-CSRF-Protector-PHP-in-your-web-application) for more information)
1717
- `tokenLength`: length of csrfp token, Default `10`
18-
- `cookieConfig`: Array of parameter values for set cookie method. supports three properties: `path`, `domain`, `secure`. They have same meaning as respective parameters of `setcookie` method: [learn more - php.net]
18+
- `cookieConfig`: Array of parameter values for set cookie method. supports three properties: `path`, `domain`, `secure` and `expire`. They have same meaning as respective parameters of `setcookie` method: [learn more - php.net]
1919
- `disabledJavascriptMessage`: messaged to be shown if js is disabled (string)
2020
- `verifyGetFor`: regex rules for those urls for which csrfp validation should be enabled for `GET` requests also. (View [verifyGetFor rules](https://github.com/mebjas/CSRF-Protector-PHP/wiki/verifyGetFor-rules) for more information)

libs/config.sample.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"cookieConfig" => array(
2121
"path" => '',
2222
"domain" => '',
23-
"secure" => false
23+
"secure" => false,
24+
"expire" => '',
2425
),
2526
"disabledJavascriptMessage" => "This site attempts to protect users against <a href=\"https://www.owasp.org/index.php/Cross-Site_Request_Forgery_%28CSRF%29\">
2627
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.

0 commit comments

Comments
 (0)