From 4e31f99e098568c5d133125630533dad91d07348 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Thu, 30 Jul 2020 17:21:29 +0200 Subject: [serversetup-bwlp-ipxe/statistics] Sanitize strings from clients Some string from the clients might not be well suited for utf-8 representation. Add wrapper functions that clean utf-8 strings, or convert ANSI strings to UTF-8 while removing problematic chars. --- modules-available/statistics/api.inc.php | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) (limited to 'modules-available/statistics/api.inc.php') diff --git a/modules-available/statistics/api.inc.php b/modules-available/statistics/api.inc.php index f7c0ac32..23b86ef4 100644 --- a/modules-available/statistics/api.inc.php +++ b/modules-available/statistics/api.inc.php @@ -51,8 +51,8 @@ if ($type{0} === '~') { $kvmstate = Request::post('kvmstate', 'UNKNOWN', 'string'); $valid = array('UNKNOWN', 'UNSUPPORTED', 'DISABLED', 'ENABLED'); if (!in_array($kvmstate, $valid)) $kvmstate = 'UNKNOWN'; - $cpumodel = Request::post('cpumodel', '', 'string'); - $systemmodel = Request::post('systemmodel', '', 'string'); + $cpumodel = Util::cleanUtf8(Request::post('cpumodel', '', 'string')); + $systemmodel = Util::cleanUtf8(Request::post('systemmodel', '', 'string')); $id44mb = Request::post('id44mb', 0, 'integer'); if ($id44mb < 0 || $id44mb > 10240000) $id44mb = 0; $badsectors = Request::post('badsectors', 0, 'integer'); @@ -61,7 +61,7 @@ if ($type{0} === '~') { if (!is_string($hostname) || $hostname === $ip) { $hostname = ''; } - $data = Request::post('data', '', 'string'); + $data = Util::cleanUtf8(Request::post('data', '', 'string')); // Prepare insert/update to machine table $new = array( 'uuid' => $uuid, @@ -99,7 +99,7 @@ if ($type{0} === '~') { $moresql .= ' hostname = :hostname,'; } if (($runmode = Request::post('runmode', false, 'string')) !== false) { - $new['currentrunmode'] = $runmode; + $new['currentrunmode'] = Util::cleanUtf8($runmode); $moresql .= ' currentrunmode = :currentrunmode,'; } $new['oldstate'] = $old['state']; @@ -208,7 +208,7 @@ if ($type{0} === '~') { } } if (($runmode = Request::post('runmode', false, 'string')) !== false) { - $params['currentrunmode'] = $runmode; + $params['currentrunmode'] = Util::cleanUtf8($runmode); $strUpdateBoottime .= ' currentrunmode = :currentrunmode, '; } // Figure out what's happening - state changes @@ -227,6 +227,9 @@ if ($type{0} === '~') { if ($sessionLength !== 0 || $old['logintime'] === 0) { // This event is a start of a new session, rather than an update $params['user'] = Request::post('user', null, 'string'); + if (is_string($params['user'])) { + $params['user'] = Util::cleanUtf8($params['user']); + } $res = Database::exec('UPDATE machine SET lastseen = UNIX_TIMESTAMP(),' . $strUpdateBoottime . " logintime = UNIX_TIMESTAMP(), currentuser = :user, currentsession = NULL, state = 'OCCUPIED' " @@ -274,7 +277,8 @@ if ($type{0} === '~') { if (!array_key_exists('name', $screen)) continue; // Filter bogus data - $screen['name'] = iconv('UTF-8', 'UTF-8//IGNORE', $screen['name']); + $screen['name'] = Util::cleanUtf8($screen['name']); + $port = Util::cleanUtf8($port); if (empty($screen['name'])) continue; if (array_key_exists($screen['name'], $hwids)) { @@ -305,7 +309,7 @@ if ($type{0} === '~') { . " VALUES (:id, :key, :value) ON DUPLICATE KEY UPDATE value = VALUES(value)", array( 'id' => $machinehwid, 'key' => $key, - 'value' => $value, + 'value' => Util::cleanUtf8($value), )); } } @@ -423,10 +427,10 @@ function writeClientLog($type, $description) // For backwards compat, we require the . prefix if ($type{0} === '.') { if ($type === '.vmchooser-session') { - $user = Request::post('user', 'unknown', 'string'); + $user = Util::cleanUtf8(Request::post('user', 'unknown', 'string')); $loguser = Request::post('loguser', 0, 'int') !== 0; - $sessionName = Request::post('name', 'unknown', 'string'); - $sessionUuid = Request::post('uuid', '', 'string'); + $sessionName = Util::cleanUtf8(Request::post('name', 'unknown', 'string')); + $sessionUuid = Util::cleanUtf8(Request::post('uuid', '', 'string')); $session = strlen($sessionUuid) === 36 ? $sessionUuid : $sessionName; Database::exec("UPDATE machine SET currentuser = :user, currentsession = :session WHERE clientip = :ip", compact('user', 'session', 'ip')); -- cgit v1.2.3-55-g7522