laravel - securityfeatures
laravel - securityfeatures
Contents
1. Encryption of data
2. Cross site scripting
3. SQL injection
4. Cookies
5. Sessions
1. Encryption
Store data in encrypted form for security purpose,
$hashedpassword = hash::make(‘secret’);
return Redirect::intended('home');
}
2. Cross-site request forgery
(CSRF)
Cross site attacks occur when attackers are able to place client-side Javascript code in a page.
You should never trust user submitted data and verify data by putting it in blade {{$value}}
format.
It will be verified by blade Engine.
{{csrf()}} hidden field is added in Laravel form in order to ensure that data is submitted by filling
in the laravel form and not by some script from another website.
3. SQL injection
SQL inject means to inject wrong query in the application which will jeopardies the safety of the
application.
Laravel by default saves the application from SQL injection. It uses prepared queries and takes
safe input after verification.
4. Cookies
Cookies are used to store server information on client machine. It could be user login or session
information.
Cookies are secure by default in laravel. They are automatically signed and encrypted.
It is not possible to tamper with cookies using Javascript.
<?php
class CookieController extends Controller {
public function setCookie(Request $request){
$minutes = 1;
$response = new Response('Hello World');
$response->withCookie(cookie('name', ‘testname', $minutes));
return $response;
}
public function getCookie(Request $request){
$value = $request->cookie('name');
echo $value;
}
}
?>
5. Sessions
Session variables are created and maintained after user’s log-in. These variables are used to
apply security checks on the access of certain parts of web application.
<script> </html>
function getMessage(){
$.ajax({
type:'POST',
url:'/getmsg',
data:'_token = <?php echo csrf_token() ?>',
success:function(data){
$("#msg").html(data.msg);
}
});
}
</script>
</head>
Ajax Controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;