Commit 462bda41 authored by Erase's avatar Erase
Browse files

Add CSRF token for Ajax

parent 23b71f59
<?php
//~ Ajax only
if(!array_key_exists('isAjax', $_POST) || $_POST['isAjax'] !== "true"){
exit('No ajax');
}
//~ Check token
session_start();
if(empty($_SESSION['csrf_token'])){
$_SESSION['csrf_token'] = bin2hex(random_bytes(32));
}
$headers = apache_request_headers();
if(array_key_exists('CsrfToken', $headers) && isset($headers['CsrfToken'])){
if ($headers['CsrfToken'] !== $_SESSION['csrf_token']) {
//~ Wrong token
$_d['sucess'] = false;
$_d['message'] = "Problème lors de la vérification du token";
header('Content-type: text/json');
echo json_encode($_d);
exit;
}
}else {
//~ No token
$_d['sucess'] = false;
$_d['message'] = "Problème de token";
header('Content-type: text/json');
echo json_encode($_d);
exit;
}
//~ Include RSS Fusion
require_once 'vendor/RSS-Fusion/index.php';
......@@ -49,7 +79,6 @@
'_badWords' => \Config::get('_badWords')
));
session_start();
$conf_name = str_replace('.', '', uniqid(rand(), true));
$path = TL_ROOT . '/../../c/';
......
<?php
//~ Generate token
session_start();
if(empty($_SESSION['csrf_token'])) {
$_SESSION['csrf_token'] = bin2hex(random_bytes(32));
}
?>
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<title>RSS-Fusion - Démo</title>
<!--[if IE]><meta http-equiv="X-UA-Compatible" content="chrome=1"><![endif]-->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="format-detection" content="telephone=no"/>
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width" />
<meta name="csrf-token" content="<?= $_SESSION['csrf_token'] ?>">
<link rel="dns-prefetch" href="http://fonts.googleapis.com/">
<link rel="dns-prefetch" href="http://ajax.googleapis.com/">
<link rel="dns-prefetch" href="http://html5shim.googlecode.com/">
......
......@@ -14,7 +14,10 @@ $(document).ready(function(){
$.ajax({
type: 'POST',
url: 'generate.php',
data: $('#contact_form').serialize(),
data: $('#contact_form').serialize()+'&isAjax=true',
headers : {
'CsrfToken': $('meta[name="csrf-token"]').attr('content')
},
success: function(data) {
$this.removeAttr('disabled');
if(!data || !data.sucess){
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment