Route account lookups through the blind index and keep it fresh on write

These are the paths that must stop depending on deterministic encryption
before storage can move to AES-GCM. Each keeps its original ciphertext
comparison in the same statement, so behaviour is unchanged today and no
account becomes unreachable during the transition.

Lookups:
- auth/login.php — passenger sign-in matched the raw value against the
  encrypted column, which only works because encryptData() is CBC with a
  fixed IV.
- auth/passenger/register.php and auth/driver/register.php — duplicate
  detection. Without the index these would stop detecting existing accounts
  under GCM and allow the same phone to register twice.

Writes now populate the index in the same statement as the value:
- both registration paths write phone/email/name indexes with the row;
  driver indexes are computed before the encryption pass, since the raw
  values are unavailable afterwards.
- passenger profile update and admin driver update refresh the index when
  the underlying field changes. For the composite name index the untouched
  half is read back from the row.

Adds --audit to the backfill script: recomputes every index from its
encrypted value and reports missing or stale entries. Drift here is silent
by nature — it surfaces only when a real search fails.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Hamza-Ayed
2026-07-25 16:04:40 +03:00
co-authored by Claude Opus 5
parent 15f55ff3e4
commit c9b4d14da6
6 changed files with 154 additions and 11 deletions
+51 -1
View File
@@ -25,7 +25,7 @@ if (PHP_SAPI !== 'cli') {
require_once __DIR__ . '/../core/bootstrap.php';
require_once __DIR__ . '/../core/Security/BlindIndex.php';
$options = getopt('', ['dry-run', 'force', 'verify::', 'table::', 'batch::']);
$options = getopt('', ['dry-run', 'force', 'audit', 'verify::', 'table::', 'batch::']);
$dryRun = isset($options['dry-run']);
$force = isset($options['force']);
$only = $options['table'] ?? null;
@@ -79,6 +79,56 @@ if (isset($options['verify'])) {
exit;
}
/**
* --audit: يقارن كل فهرس مخزَّن بالفهرس المحسوب من القيمة المشفّرة.
*
* الهدف كشف "الانحراف": صف عُدِّل هاتفه أو بريده عبر مسار كتابة لا يُحدّث
* الفهرس، فيصبح البحث يجد السجل بقيمته القديمة أو لا يجده إطلاقاً. الانحراف
* صامت بطبيعته، ولن يظهر إلا حين يفشل بحث حقيقي.
*/
if (isset($options['audit'])) {
$con = Database::get('main');
$checks = [
['driver', 'driver.phone', 'phone', 'phone_bidx'],
['driver', 'driver.email', 'email', 'email_bidx'],
['passengers', 'passengers.phone', 'phone', 'phone_bidx'],
['passengers', 'passengers.email', 'email', 'email_bidx'],
['adminUser', 'adminUser.phone', 'phone', 'phone_bidx'],
];
$problems = 0;
foreach ($checks as [$table, $scope, $source, $column]) {
try {
$rows = $con->query("SELECT id, `$source`, `$column` FROM `$table`")->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
echo " ! $table skipped: " . $e->getMessage() . "\n";
continue;
}
$stale = $missing = 0;
foreach ($rows as $row) {
$plain = $encryptionHelper->decryptData($row[$source] ?? null);
if ($plain === false || $plain === '') continue;
$expected = $blind->index($scope, $plain);
if ($expected === null) continue;
if (empty($row[$column])) $missing++;
elseif ($row[$column] !== $expected) $stale++;
}
$problems += $stale + $missing;
$verdict = ($stale + $missing) === 0 ? 'ok' : "MISSING=$missing STALE=$stale";
echo sprintf(" %-24s %s\n", "$table.$column", $verdict);
}
echo $problems === 0
? "\n✔ every index matches its encrypted value.\n"
: "\n✘ $problems row(s) out of sync — re-run with --force to rebuild.\n";
exit($problems === 0 ? 0 : 1);
}
$targets = [
'driver' => [
'phone_bidx' => ['scope' => 'driver.phone', 'columns' => ['phone']],