14

I am using Laravel 4 with Eloquent. When I get the user input I just use $name=Input::get('name') and then I do $a->name=$name;

I don't know if the function Input::get protect me from SQL Injection and XSS. If it does not, what do I have to do to sanitize the input?

And, when I show the value in my view, shall I use {{$a}} or {{{$a}}}

halfer
  • 19,824
  • 17
  • 99
  • 186
cruster946
  • 475
  • 2
  • 6
  • 16
  • 1
    In addition to the other answers, in case you decide to use something like new Something(Input::all()) make sure to specify the $fillable fields in the Something model, in order to protect against mass assignment. Not strictly pertaining to your questions, but something to be aware of. – Yasen Slavov Oct 05 '14 at 13:03

3 Answers3

18

Eloquent ORM uses PDO's parameter binding, so SQL injection is not something you should worry about. When you're working with raw SQL, you should be using parameter binding as well.

Input::get() does not filter anything.

Double curly braces (triple in the discontinued versions) do the same as e() and HTML::entities(). All of them call htmlspecialchars with UTF-8 support:

htmlspecialchars($your_string, ENT_QUOTES, 'UTF-8', false);
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
cha-cha
  • 328
  • 3
  • 13
  • If I use {{{ to show it, should I use also htmlentities when saving the information? – cruster946 Oct 05 '14 at 13:08
  • 3
    Escape output, not input. – cha-cha Oct 05 '14 at 13:20
  • 2
    There's no need to alter data unless you actually use it. Filter input, escape output. – cha-cha Oct 05 '14 at 13:30
  • It should be pointed out that Laravel provides raw sql query support which isn't protected from injection at all. So there is always the chance that someone may build a site with some raw queries that do need manual cleaning. – AdamJones Feb 11 '20 at 13:33
5

You should use {{{$a}}} because for example Input can has HTML tag. Laravel won't filter it.

To avoid SQL injection you should use bind your parameters running queries like:

$var = 1;
$results = DB::select('select * from users where id = ?', array($var));

and not:

$results = DB::select('select * from users where id = '.$var);
Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
-1

This returns sanitized string enclosed within quotes for safe use in a SQL query:

$safe_string = DB::connection()->getPdo()->quote($unsafe_string);
Ilyich
  • 4,966
  • 3
  • 39
  • 27