Skip to content

Commit

Permalink
having token checks on POST forms
Browse files Browse the repository at this point in the history
  • Loading branch information
YuFei Zhu committed Apr 30, 2012
1 parent 1ab3efb commit b84b439
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 7 deletions.
22 changes: 21 additions & 1 deletion src/messenger/webim/libs/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -688,4 +688,24 @@ function jspath()
return "js/$jsver";
}

?>
/* authorization token check for CSRF attack */
function csrfchecktoken(){
if(!isset($_SESSION['csrf_token'])){
$_SESSION['csrf_token']=sha1(rand(10000000,99999999));
}
// check the turing code
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
//if token match
if(!isset($_POST['csrf_token']) || ($_POST['csrf_token'] != $_SESSION['csrf_token'])){

die("CSRF failure");
}
}
}

/* print csrf token as a hidden field*/
function print_csrf_token_input(){
echo "<input name='csrf_token' type='hidden' value='".$_SESSION['csrf_token']."' />";
}

?>
2 changes: 1 addition & 1 deletion src/messenger/webim/libs/operator_settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ function setup_operator_settings_tabs($opId, $active)
}
}

?>
?>
4 changes: 3 additions & 1 deletion src/messenger/webim/operator/cannededit.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
require_once('../libs/operator.php');
require_once('../libs/pagination.php');

csrfchecktoken();

function load_message($key)
{
global $mysqlprefix;
Expand Down Expand Up @@ -101,4 +103,4 @@ function add_message($locale, $groupid, $message)
start_html_output();
require('../view/cannededit.php');
exit;
?>
?>
2 changes: 2 additions & 0 deletions src/messenger/webim/operator/operator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
require_once('../libs/operator.php');
require_once('../libs/operator_settings.php');

csrfchecktoken();

$operator = check_login();

$page = array('opid' => '');
Expand Down
4 changes: 3 additions & 1 deletion src/messenger/webim/operator/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
require_once('../libs/operator.php');
require_once('../libs/settings.php');

csrfchecktoken();

$operator = check_login();

$page = array('agentId' => '');
Expand Down Expand Up @@ -104,4 +106,4 @@
setup_settings_tabs(0);
start_html_output();
require('../view/settings.php');
?>
?>
6 changes: 5 additions & 1 deletion src/messenger/webim/view/agent.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ function tpl_content() { global $page, $webimroot, $errors;

<?php if( $page['opid'] || $page['canmodify'] ) { ?>
<form name="agentForm" method="post" action="<?php echo $webimroot ?>/operator/operator.php">

<!-- add auth token -->
<?php print_csrf_token_input() ?>

<input type="hidden" name="opid" value="<?php echo $page['opid'] ?>"/>
<div>
<?php if(!$page['needChangePassword']) { print_tabbar(); } ?>
Expand Down Expand Up @@ -130,4 +134,4 @@ function tpl_content() { global $page, $webimroot, $errors;
} /* content */

require_once('inc_main.php');
?>
?>
6 changes: 5 additions & 1 deletion src/messenger/webim/view/cannededit.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ function tpl_content() { global $page, $webimroot, $errors;
?>

<form name="cannedForm" method="post" action="<?php echo $webimroot ?>/operator/cannededit.php">

<!-- add auth token -->
<?php print_csrf_token_input() ?>

<input type="hidden" name="key" value="<?php echo $page['key'] ?>"/>
<?php if(!$page['key']) { ?>
<input type="hidden" name="lang" value="<?php echo $page['locale'] ?>"/>
Expand Down Expand Up @@ -73,4 +77,4 @@ function tpl_content() { global $page, $webimroot, $errors;
} /* content */

require_once('inc_main.php');
?>
?>
5 changes: 4 additions & 1 deletion src/messenger/webim/view/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ function tpl_content() { global $page, $webimroot, $errors;

<form name="settings" method="post" action="<?php echo $webimroot ?>/operator/settings.php">

<!-- add auth token -->
<?php print_csrf_token_input() ?>

<div>
<?php print_tabbar(); ?>
<div class="mform"><div class="formtop"><div class="formtopi"></div></div><div class="forminner">
Expand Down Expand Up @@ -155,4 +158,4 @@ function tpl_content() { global $page, $webimroot, $errors;
} /* content */

require_once('inc_main.php');
?>
?>

0 comments on commit b84b439

Please sign in to comment.