Migrate remaining encrypted-column lookups to the blind index
Completes the set of queries that matched a freshly encrypted value against a stored one, which only works while encryption is deterministic. Each keeps its original comparison and adds an index comparison in the same WHERE, so nothing changes today. - passenger sign-in by email, service-staff sign-in, Firebase token lookup - driver lookup by phone and by national number - admin ride lookup and ride monitor (both tables) - nabeh: driver status, user resolution, ride history, complaint submission transit_org_admins lives in the transit database and has no index column, so login there falls back to decrypting the small set of active admins and comparing normalised numbers. Schema: adds users.email_bidx/phone_bidx and driver.national_bidx with their indexes. Verified that every :*_bidx placeholder introduced is actually bound — an unbound one is a fatal error at request time, not a silent miss. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5
parent
39b5a7fc7f
commit
8d7e3118b5
@@ -33,17 +33,20 @@ try {
|
||||
global $encryptionHelper;
|
||||
|
||||
$encryptedPhone = $encryptionHelper->encryptData($phone);
|
||||
global $blindIndex;
|
||||
$phoneBidx = $blindIndex ? $blindIndex->index('driver.phone', $phone) : null;
|
||||
|
||||
$stmt = $db->prepare("
|
||||
SELECT d.id, d.phone, d.first_name, d.last_name, d.status, d.created_at,
|
||||
cr.id as car_id, cr.make, cr.model, cr.year, cr.car_plate, cr.status as car_status
|
||||
FROM driver d
|
||||
LEFT JOIN CarRegistration cr ON cr.driverID = d.id
|
||||
WHERE d.phone = :phone
|
||||
WHERE (d.phone = :phone OR (:phone_bidx IS NOT NULL AND d.phone_bidx = :phone_bidx))
|
||||
LIMIT 1
|
||||
");
|
||||
$stmt->execute([
|
||||
':phone' => $encryptedPhone,
|
||||
':phone_bidx' => $phoneBidx,
|
||||
]);
|
||||
$result = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
|
||||
@@ -52,13 +52,16 @@ global $encryptionHelper;
|
||||
|
||||
// Resolve user
|
||||
$encryptedPhone = $encryptionHelper->encryptData($phone);
|
||||
$driver = $mainDb->prepare("SELECT id, 'driver' AS type FROM driver WHERE phone = :p LIMIT 1");
|
||||
$driver->execute([':p' => $encryptedPhone]);
|
||||
global $blindIndex;
|
||||
$dBidx = $blindIndex ? $blindIndex->index('driver.phone', $phone) : null;
|
||||
$pBidx = $blindIndex ? $blindIndex->index('passengers.phone', $phone) : null;
|
||||
$driver = $mainDb->prepare("SELECT id, 'driver' AS type FROM driver WHERE phone = :p OR (:bidx IS NOT NULL AND phone_bidx = :bidx) LIMIT 1");
|
||||
$driver->execute([':p' => $encryptedPhone, ':bidx' => $dBidx]);
|
||||
$user = $driver->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if (!$user) {
|
||||
$passenger = $mainDb->prepare("SELECT id, 'passenger' AS type FROM passengers WHERE phone = :p LIMIT 1");
|
||||
$passenger->execute([':p' => $encryptedPhone]);
|
||||
$passenger = $mainDb->prepare("SELECT id, 'passenger' AS type FROM passengers WHERE phone = :p OR (:bidx IS NOT NULL AND phone_bidx = :bidx) LIMIT 1");
|
||||
$passenger->execute([':p' => $encryptedPhone, ':bidx' => $pBidx]);
|
||||
$user = $passenger->fetch(PDO::FETCH_ASSOC);
|
||||
}
|
||||
|
||||
|
||||
@@ -59,12 +59,15 @@ try {
|
||||
global $encryptionHelper;
|
||||
|
||||
$encryptedPhone = $encryptionHelper->encryptData($phone);
|
||||
global $blindIndex;
|
||||
$dBidx = $blindIndex ? $blindIndex->index('driver.phone', $phone) : null;
|
||||
$pBidx = $blindIndex ? $blindIndex->index('passengers.phone', $phone) : null;
|
||||
|
||||
// Look for driver first
|
||||
$stmt = $db->prepare(
|
||||
"SELECT id, phone, first_name, last_name FROM driver WHERE phone = :phone LIMIT 1"
|
||||
"SELECT id, phone, first_name, last_name FROM driver WHERE phone = :phone OR (:bidx IS NOT NULL AND phone_bidx = :bidx) LIMIT 1"
|
||||
);
|
||||
$stmt->execute([':phone' => $encryptedPhone]);
|
||||
$stmt->execute([':phone' => $encryptedPhone, ':bidx' => $dBidx]);
|
||||
$driver = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if ($driver) {
|
||||
@@ -86,9 +89,9 @@ try {
|
||||
|
||||
// Fallback: look for passenger
|
||||
$stmt = $db->prepare(
|
||||
"SELECT id, phone, first_name, last_name FROM passengers WHERE phone = :phone LIMIT 1"
|
||||
"SELECT id, phone, first_name, last_name FROM passengers WHERE phone = :phone OR (:bidx IS NOT NULL AND phone_bidx = :bidx) LIMIT 1"
|
||||
);
|
||||
$stmt->execute([':phone' => $encryptedPhone]);
|
||||
$stmt->execute([':phone' => $encryptedPhone, ':bidx' => $pBidx]);
|
||||
$passenger = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if ($passenger) {
|
||||
|
||||
@@ -64,14 +64,17 @@ global $encryptionHelper;
|
||||
|
||||
// ── Resolve user by phone ────────────────────────────────────
|
||||
$encryptedPhone = $encryptionHelper->encryptData($phone);
|
||||
$driverRow = $mainDb->prepare("SELECT id, first_name, last_name FROM driver WHERE phone = :p LIMIT 1");
|
||||
$driverRow->execute([':p' => $encryptedPhone]);
|
||||
global $blindIndex;
|
||||
$dBidx = $blindIndex ? $blindIndex->index('driver.phone', $phone) : null;
|
||||
$pBidx = $blindIndex ? $blindIndex->index('passengers.phone', $phone) : null;
|
||||
$driverRow = $mainDb->prepare("SELECT id, first_name, last_name FROM driver WHERE phone = :p OR (:bidx IS NOT NULL AND phone_bidx = :bidx) LIMIT 1");
|
||||
$driverRow->execute([':p' => $encryptedPhone, ':bidx' => $dBidx]);
|
||||
$driver = $driverRow->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
$passengerRow = null;
|
||||
if (!$driver) {
|
||||
$passengerRow = $mainDb->prepare("SELECT id, first_name, last_name FROM passengers WHERE phone = :p LIMIT 1");
|
||||
$passengerRow->execute([':p' => $encryptedPhone]);
|
||||
$passengerRow = $mainDb->prepare("SELECT id, first_name, last_name FROM passengers WHERE phone = :p OR (:bidx IS NOT NULL AND phone_bidx = :bidx) LIMIT 1");
|
||||
$passengerRow->execute([':p' => $encryptedPhone, ':bidx' => $pBidx]);
|
||||
$passenger = $passengerRow->fetch(PDO::FETCH_ASSOC);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user