Skip to content

Commit 0f9961d

Browse files
committed
csrf 预防
1 parent 7a72799 commit 0f9961d

File tree

8 files changed

+49
-47
lines changed

8 files changed

+49
-47
lines changed

application/controllers/Admin.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ public function __construct(){
1010
$this->objOfJwt = new ImplementJwt();
1111
}
1212

13-
public function index()
14-
{
13+
public function index(){
14+
// CREATE A RANDOM SESSION TOKEN
15+
session_start();
16+
$length = 32;
17+
$_SESSION['nonces'] = substr(base_convert(sha1(uniqid(mt_rand())), 16, 36), 0, $length);
18+
1519
if(isset($_COOKIE['auth'])){
1620
$decodeToken = $this->objOfJwt->DecodeToken($_COOKIE['auth']);
1721
$user = $decodeToken;

application/controllers/Category.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,20 @@
33

44
class Category extends CI_Controller {
55

6-
public function index()
7-
{
6+
public function index(){
87
}
98

109
public function AddCategory() {
11-
session_start();
12-
$post = $this->input->post(NULL, TRUE);
13-
$data = array(
14-
'name' => $post['cat_name'],
15-
);
16-
$nonces = $_SESSION['nonces'];
10+
session_start();
11+
$post = $this->input->post(NULL, TRUE);
12+
$data = array(
13+
'name' => $post['cat_name'],
14+
);
15+
if ($_SESSION['nonces']==$post['nonces']) {
1716
$this->load->model('Category_model');
18-
$category_list = $this->Category_model->add_category($data, $nonces);
17+
$category_list = $this->Category_model->add_category($data);
1918
echo json_encode($category_list);
20-
// var_dump($post);
21-
// echo $post->;
19+
}
2220
}
2321

2422
public function DeleteCategory() {

application/controllers/Login.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ public function __construct(){
1111
}
1212

1313
public function index(){
14+
// CREATE A RANDOM SESSION TOKEN
15+
session_start();
16+
$length = 32;
17+
$_SESSION['nonces'] = substr(base_convert(sha1(uniqid(mt_rand())), 16, 36), 0, $length);
18+
1419
$this->load->helper('url');
1520
$this->load->helper('cookie');
1621
$this->load->library('layout');
@@ -50,13 +55,14 @@ public function Login() {
5055
);
5156
// set cookie + set session
5257
$this->input->set_cookie($cookie);
53-
$_SESSION['nonces'] = md5(rand(1,10000));
54-
// $decodeToken = $this->objOfJwt->DecodeToken($jwtToken);
55-
// echo $decodeToken;
56-
// var_dump($_COOKIE);
57-
58-
$response = array('status'=>'2','msg'=>'success','data'=>$user['data']);
59-
echo json_encode($response);
58+
if ($_SESSION['nonces']==$post['nonces']) {
59+
// VALID TOKEN PROVIDED - PROCEED WITH PROCESS
60+
$response = array('status'=>'2','msg'=>'success','data'=>$user['data']);
61+
echo json_encode($response);
62+
} else {
63+
$response = array('status'=>'0','msg'=>'failed','data'=>'nonces error');
64+
echo json_encode($response);
65+
}
6066
}else {
6167
$response = array('status'=>'0','msg'=>'failed','data'=>'pwd or email error');
6268
echo json_encode($response);

application/controllers/ProductAPI.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ public function AddProduct() {
5454
'catid' => $post['catid'],
5555
'description' => $post['description']
5656
);
57-
$nonces = $_SESSION['nonces'];
58-
$this->load->model('Product_model');
59-
$product_list = $this->Product_model->add_product($data,$nonces);
60-
echo json_encode($product_list);
61-
// var_dump($post);
62-
// echo $post->;
57+
if ($_SESSION['nonces']==$post['nonces']) {
58+
$this->load->model('Product_model');
59+
$product_list = $this->Product_model->add_product($data);
60+
echo json_encode($product_list);
61+
}
62+
6363
}
6464

6565
public function DeleteProduct() {

application/models/Category_model.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,16 @@ public function __construct()
77
}
88

99

10-
public function add_category($data, $nonces){
10+
public function add_category($data){
1111
$response = array('status'=>'0','msg'=>'failed','data'=>'');
1212
try{
1313
// 防止 sql 注入。
1414
// $dataEscape = $this->db->escape($data);
15-
if(isset($nonces) && $nonces == $_SESSION['nonces']){
16-
$query = $this->db->insert( 'categories' , $data );
17-
// $response['data'] = $query;
18-
$response['status'] = '2';
19-
$response['msg'] = 'success';
20-
return $response;
21-
}else {
22-
echo 'error';
23-
}
15+
$query = $this->db->insert( 'categories' , $data );
16+
// $response['data'] = $query;
17+
$response['status'] = '2';
18+
$response['msg'] = 'success';
19+
return $response;
2420
}catch(PDOEXCEPTION $e){
2521
echo $e->getMessage();
2622
}

application/models/Product_model.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,17 @@ public function get_product_detail($productid){
1818
}
1919
}
2020

21-
public function add_product($data, $nonces){
21+
public function add_product($data){
2222
// session_start();
2323
$response = array('status'=>'0','msg'=>'failed','id'=>'');
2424
try{
2525
// 防止 sql 注入。 需要每个字段单独拿出来判断。
2626
// $dataEscape = $this->db->escape($data);
27-
// Apply and validate secret nonces for every form
28-
if(isset($nonces) && $nonces == $_SESSION['nonces']){
29-
$query = $this->db->insert( 'products' , $data );
30-
$response['id'] = $this->db->insert_id();
31-
$response['status'] = '2';
32-
$response['msg'] = 'success';
33-
return $response;
34-
}else {
35-
echo 'error';
36-
}
27+
$query = $this->db->insert( 'products' , $data );
28+
$response['id'] = $this->db->insert_id();
29+
$response['status'] = '2';
30+
$response['msg'] = 'success';
31+
return $response;
3732
}catch(PDOEXCEPTION $e){
3833
echo $e->getMessage();
3934
}

application/views/admin.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
<input type="file" name="file" class="form-control-file" accept="image/jpeg, image/jpg, image/png" id="prod_img" required="true">
4646
<!-- <button type="button" class="upload-file">upload-pid:3</button> -->
4747
</div>
48+
<input id="nonces" type="hidden" name="nonces" value="<?=$_SESSION['nonces']?>">
4849
<button type="button" class="btn btn-primary btn-product" name="reg_prod" value="Submit">Submit</button>
4950
</form>
5051
</div>
@@ -57,6 +58,7 @@
5758
<label>Category Name</label>
5859
<input id="cat_name" type="text" class="form-control" name="cat_name" pattern="^[\w\- ]+$" required>
5960
</div>
61+
<input id="nonces" type="hidden" name="nonces" value="<?=$_SESSION['nonces']?>">
6062
<button type="button" class="btn btn-primary btn-category" name="reg_prod" value="Submit">Submit</button>
6163
</form>
6264
</div>

application/views/login.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<label>Pwd</label>
1313
<input id="pwd" type="password" class="form-control" name="pwd">
1414
</div>
15+
<input id="nonces" type="hidden" name="nonces" value="<?=$_SESSION['nonces']?>">
1516
<button type="button" class="btn btn-primary btn-login" >Login</button>
1617
<button type="button" class="btn btn-primary btn-signUp" >SignUp</button>
1718
</form>

0 commit comments

Comments
 (0)