is \Base::clean enough for sanitization?

245 views
Skip to first unread message

AM

unread,
Aug 18, 2016, 10:42:18 AM8/18/16
to Fat-Free Framework
Hi,

is \Base::clean 100% secure for sanitizing user input?

thanks

Anatol Buchholz

unread,
Aug 19, 2016, 3:47:41 PM8/19/16
to Fat-Free Framework
Do you ask if it does this automatically or if it´s provided methods are bullet proof?

vijinho

unread,
Aug 19, 2016, 6:04:15 PM8/19/16
to f3-fra...@googlegroups.com
It should be.  Maybe it's overkill but I do this to clean and normalize all input:

// clean ALL incoming user input by default
$request = [];
$utf = \UTF::instance();
foreach (['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'COOKIE'] as $var) {
$f3->copy($var, $var . '_UNCLEAN'); $input = $f3->get($var);
if (is_array($input) && count($input)) {
$cleaned = [];
foreach ($input as $k => $v) {
$cleaned[strtolower($utf->trim($f3->clean($k)))] = $f3->recursive($v, function ($v) use ($f3, $utf) {
return $utf->trim($f3->clean($v));
});
}
ksort($cleaned);
$request = array_merge_recursive($request, $cleaned);
$f3->set($var, $cleaned);
}
}
unset($cleaned);
// we don't want to include the session name in the request data
$session_name = strtolower(session_name());
if (array_key_exists($session_name, $request)) {
unset($request[$session_name]);
}
ksort($request); $f3->copy('REQUEST', 'REQUEST_UNCLEAN');
$f3->set('REQUEST', $request);
unset($request);

Rayne

unread,
Aug 20, 2016, 3:25:21 AM8/20/16
to Fat-Free Framework

vijinho

unread,
Aug 20, 2016, 7:02:09 AM8/20/16
to Fat-Free Framework
That's interesting.  I also use https://github.com/Wixel/GUMP as a second layer of filtering (and validation) before any fields are updated within my database.

AM

unread,
Sep 9, 2016, 3:22:28 AM9/9/16
to Fat-Free Framework
Well, if I do something like this:

$item = new \DB\SQL\Mapper($db, 'item');
$item
->variable =$f3->get('POST.variable');
$item
->save();


1. Could the database be hacked using that code by a SQL iinjection or something?
2. Is this safe for output when the output is being escaped (which is the default in the f3 template engine)?

Thanks

Anatol Buchholz

unread,
Sep 11, 2016, 8:49:36 AM9/11/16
to f3-fra...@googlegroups.com

$item
= new \DB\SQL\Mapper($db, 'item');
$item
->variable =$f3->get('POST.variable');
$item
->save();

1. Could the database be hacked using that code by a SQL iinjection or something?

I think yes and would suggest to sanatize data and/or use parameterized queries

It is recommended to use parameterized queries for all where conditions that may include user input data.
 

2. Is this safe for output when the output is being escaped (which is the default in the f3 template engine)?

Yes, in past projects I´ve trusted F3 here: https://fatfreeframework.com/views-and-templates#DataSanitation
 

AM

unread,
Sep 12, 2016, 2:56:00 AM9/12/16
to Fat-Free Framework


Am Sonntag, 11. September 2016 14:49:36 UTC+2 schrieb Anatol Buchholz:

$item
= new \DB\SQL\Mapper($db, 'item');
$item
->variable =$f3->get('POST.variable');
$item
->save();

1. Could the database be hacked using that code by a SQL iinjection or something?

I think yes and would suggest to sanatize data and/or use parameterized queries

It is recommended to use parameterized queries for all where conditions that may include user input data.
 


 I would have thought that F3 internally uses parameterized queries when using $mapper->save()?

it seems it does?
See \DB\SQL\Mapper::update(), line 456:
foreach ($this->fields as $key=>$field)
if ($field['changed']) {
$pairs.=($pairs?',':'').$this->db->quotekey($key).'=?';
$args[$ctr+1]=array($field['value'],$field['pdo_type']);
$ctr++;
}


ikkez

unread,
Sep 12, 2016, 3:40:33 AM9/12/16
to f3-fra...@googlegroups.com
yes it does.
this is not the point where the bind happens. it's here:
https://github.com/bcosca/fatfree-core/blob/master/db/sql.php#L201

the mapper always uses prepared statements, which is relativly safe against sql injections. The only point where you should be careful is the $options array you can put into for limit, offset, sorting and group. these things cannot use prepared statements, so better check twice if you want to use dynamic values here.

AM

unread,
Sep 12, 2016, 3:58:12 AM9/12/16
to f3-fra...@googlegroups.com
Great!
Thanks ikkez!
Reply all
Reply to author
Forward
0 new messages