$call_at) { echo "FAIL ZM_AUTH_HASH_SECRET_DEFAULT must be defined before the first loadConfig() call\n"; exit(1); } foreach (array("/^define\('ZM_AUTH_HASH_SECRET_DEFAULT'.*?;$/m", '/^function ensureAuthHashSecret\(.*?^}$/ms') as $pattern) { if (!preg_match($pattern, $src, $matches)) { echo "FAIL $pattern not found in config.php.in\n"; exit(1); } eval($matches[0]); } // One Config row. $race, when set, is written just before the UPDATE runs, as // another request that got there first would. class FakeStatement { private $db, $sql; function __construct($db, $sql) { $this->db = $db; $this->sql = $sql; } function execute($params) { if (strpos($this->sql, 'UPDATE') === 0) { if ($this->db->race !== null) { $this->db->value = $this->db->race; $this->db->race = null; } $this->db->updates++; if ($params[1] === 'ZM_AUTH_HASH_SECRET' and $this->db->value === $params[2]) { $this->db->value = $params[0]; if (strpos($this->sql, 'Private=1') !== false) $this->db->private = 1; } } return true; } function fetchColumn() { return $this->db->value; } } class FakeDb { public $value, $race = null, $updates = 0, $private = 0; function __construct($value) { $this->value = $value; } function prepare($sql) { return new FakeStatement($this, $sql); } } $failures = 0; $passes = 0; function check($name, $got, $expected) { global $failures, $passes; if ($got === $expected) { $passes++; echo "ok $name\n"; } else { $failures++; echo "FAIL $name\n got: ".var_export($got, true)."\n expected: ".var_export($expected, true)."\n"; } } $db = new FakeDb(ZM_AUTH_HASH_SECRET_DEFAULT); $got = ensureAuthHashSecret($db, ZM_AUTH_HASH_SECRET_DEFAULT); check('default is replaced with 64 hex characters', (bool)preg_match('/^[0-9a-f]{64}$/', $got), true); check('the new secret is stored', $db->value, $got); check('and marked Private, so skin.js.php never exports it', $db->private, 1); // A secret set by hand on a table whose flags are all 0 is a signing key too. check('loadConfig() marks the loaded secret Private whatever the table says', (bool)preg_match('/if \(\$name == \'ZM_AUTH_HASH_SECRET\'\) \{\s*(#[^\n]*\n\s*)*\$row\[\'Private\'\] = 1;/', $src), true); $db2 = new FakeDb(ZM_AUTH_HASH_SECRET_DEFAULT); check('each server gets a different secret', ensureAuthHashSecret($db2, ZM_AUTH_HASH_SECRET_DEFAULT) !== $got, true); $db = new FakeDb(''); check('empty is replaced', strlen(ensureAuthHashSecret($db, '')), 64); $mine = str_repeat('m', 40); $db = new FakeDb($mine); check('a secret already set is returned unchanged', ensureAuthHashSecret($db, $mine), $mine); check('and the database is not written', $db->updates, 0); $theirs = str_repeat('t', 64); $db = new FakeDb(ZM_AUTH_HASH_SECRET_DEFAULT); $db->race = $theirs; check('a concurrent first request wins and both agree on its secret', ensureAuthHashSecret($db, ZM_AUTH_HASH_SECRET_DEFAULT), $theirs); echo "\n$passes passed, $failures failed\n"; exit($failures ? 1 : 0);