row = $row; } function Password() { return $this->row['Password']; } } } namespace { $failures = 0; $passes = 0; function check($name, $ok, $detail = '') { global $failures, $passes; if ($ok) { $passes++; echo "ok - $name\n"; } else { $failures++; echo "FAIL - $name".($detail !== '' ? " ($detail)" : '')."\n"; } } // The log lines recorded since the last call that contain any of $needles. function leaked(array $needles) { $hits = array(); foreach ($GLOBALS['logged'] as $line) { foreach ($needles as $needle) { if ($needle !== '' and stripos($line, $needle) !== false) $hits[] = $line; } } $GLOBALS['logged'] = array(); return $hits; } $stubdir = sys_get_temp_dir().'/zm_auth_log_redaction_stubs_'.getmypid(); @mkdir($stubdir, 0700, true); foreach (array('session.php', 'Network.php', 'User.php', 'Group_Permission.php', 'Monitor_Permission.php', 'User_Role.php', 'Role_Group_Permission.php', 'Role_Monitor_Permission.php') as $stub) { file_put_contents($stubdir.'/'.$stub, " $mysql_encoded, 'bcrypt' => $bcrypt, 'mysql+bcrypt' => '-ZM-'.$overlay, ) as $type => $stored) { $users = array(array('Id' => 1, 'Username' => 'alice', 'Password' => $stored, 'Enabled' => 1)); $GLOBALS['logged'] = array(); $ret = validateUser('alice', $password); check("$type: the right password is accepted", $ret[0] !== false); $hits = leaked(array($password, $mysql_encoded, $stored, substr($stored, 4), $bcrypt)); check("$type: no password, encoded password or stored hash in the log", !count($hits), implode(' | ', $hits)); $ret = validateUser('alice', 'wrong-password'); check("$type: a wrong password is refused", $ret[0] === false); $hits = leaked(array('wrong-password', '*'.strtoupper(sha1(sha1('wrong-password', true))), $stored)); check("$type: nothing of a wrong password in the log", !count($hits), implode(' | ', $hits)); } echo "\ngetAuthUser() logs a fingerprint of the auth hash, not the hash\n"; $users = array(array('Id' => 1, 'Username' => 'alice', 'Password' => $bcrypt, 'Enabled' => 1)); $hashFor = function ($addr) use ($bcrypt) { $t = localtime(time()); return md5(ZM_AUTH_HASH_SECRET.'alice'.$bcrypt.$addr.$t[2].$t[3].$t[4].$t[5]); }; $good = $hashFor(''); $GLOBALS['logged'] = array(); check('a valid hash authenticates', getAuthUser($good) instanceof ZM\User); $lines = $GLOBALS['logged']; $hits = leaked(array($good, ZM_AUTH_HASH_SECRET, $bcrypt)); check('valid hash: not in the log', !count($hits), implode(' | ', $hits)); check('valid hash: its fingerprint is', count(preg_grep('/'.preg_quote(ZM\logFingerprint($good), '/').'/', $lines)) > 0); // Valid for the address it was issued to, refused here: the line that says // so is an Info line and lands in the Logs table by default. $elsewhere = $hashFor('198.51.100.9'); check('a hash for another address is refused', getAuthUser($elsewhere) === null); $lines = $GLOBALS['logged']; $hits = leaked(array($elsewhere, ZM_AUTH_HASH_SECRET, $bcrypt)); check('refused hash: not in the log', !count($hits), implode(' | ', $hits)); check('refused hash: the Info line carries its fingerprint', count(preg_grep('/^INF Unable to authenticate user from auth hash '.preg_quote(ZM\logFingerprint($elsewhere), '/').' /', $lines)) === 1); echo "\n$passes passed, $failures failed\n"; exit($failures ? 1 : 0); }