Files
Siro/backend/auth/login.php
T
Hamza-AyedandClaude Opus 5 2135edcf43 Close the remaining ciphertext joins and a SQL injection in email verification
- Customer-service notes joined to the account by comparing encrypted phone
  columns. Both notes tables now carry phone_key, written when a note is
  saved, and the three joins match on it.
- The email_verifications join was comparing a plaintext column against an
  encrypted one, so it never matched and `verified` was always NULL in both
  passenger and driver sign-in. It is now resolved in PHP against the
  decrypted address, which fixes a pre-existing bug rather than only
  preparing for GCM.
- auth/sendVerifyEmail.php built all three of its statements by interpolating
  the request values into SQL. Any caller could inject through the email or
  token field. Now parameterised.
- serviceapp/register.php duplicate detection consults the users indexes and
  writes them with the row.

Sweep confirms no join or lookup compares two encrypted columns any more.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-25 16:48:10 +03:00

127 lines
3.9 KiB
PHP

<?php
require_once __DIR__ . '/../connect.php';
$email = filterRequest('email');
$phone = filterRequest('phone');
$password = filterRequest('password');
if (empty($phone) && empty($email)) {
echo json_encode(["status" => "Failure", "data" => "Phone or email is required."]);
exit;
}
/**
* البحث عن الحساب.
*
* سابقاً كان يقارن القيمة الخام بالعمود المشفّر مباشرةً، وهو ما ينجح فقط لأن
* التشفير الحالي حتمي (CBC بـ IV ثابت). الفهرس الأعمى يجعل هذا الاستعلام
* مستقلاً عن أسلوب التشفير، فلا ينكسر تسجيل الدخول عند الانتقال إلى AES-GCM.
*
* تُبقى المقارنتان القديمتان في نفس الشرط كاحتياط للحسابات التي لم تُفهرس بعد.
*/
global $blindIndex;
$conditions = [];
$params = [':password' => $password];
if (!empty($phone)) {
$conditions[] = "passengers.phone = :phone";
$params[':phone'] = $phone;
$phoneBidx = $blindIndex ? $blindIndex->index('passengers.phone', $phone) : null;
if ($phoneBidx) {
$conditions[] = "passengers.phone_bidx = :phone_bidx";
$params[':phone_bidx'] = $phoneBidx;
}
}
if (!empty($email)) {
$conditions[] = "passengers.email = :email";
$params[':email'] = $email;
$emailBidx = $blindIndex ? $blindIndex->index('passengers.email', $email) : null;
if ($emailBidx) {
$conditions[] = "passengers.email_bidx = :email_bidx";
$params[':email_bidx'] = $emailBidx;
}
}
$where = implode(' OR ', $conditions);
$sql = "SELECT
passengers.`id`,
passengers.`phone`,
passengers.`email`,
passengers.`password`,
passengers.`gender`,
passengers.`birthdate`,
passengers.`site`,
passengers.`first_name`,
passengers.`last_name`,
passengers.`education`,
passengers.`employmentType`,
passengers.`maritalStatus`,
passengers.`created_at`,
passengers.`updated_at`,
passengers.`email` AS `_email_enc`
FROM
`passengers`
WHERE
$where";
/**
* حالة توثيق البريد.
*
* كان الاستعلام يربط email_verifications.email بعمود البريد في الحساب، لكن
* الأول يُخزَّن نصاً صريحاً والثاني مشفّراً — فالربط لم يكن يطابق شيئاً أصلاً
* وكانت verified تعود NULL دائماً. نجلبها هنا بالبريد الأصلي.
*/
function fetchEmailVerified(PDO $con, ?string $plainEmail): ?int
{
if (!$plainEmail) return null;
try {
$st = $con->prepare("SELECT verified FROM email_verifications WHERE email = ? LIMIT 1");
$st->execute([$plainEmail]);
$v = $st->fetchColumn();
return $v === false ? null : (int) $v;
} catch (PDOException $e) {
error_log('[email_verifications] ' . $e->getMessage());
return null;
}
}
$stmt = $con->prepare($sql);
$stmt->execute($params);
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
$count = $stmt->rowCount();
if ($count > 0) {
$plainEmail = $encryptionHelper->decryptData($data[0]['_email_enc'] ?? null) ?: null;
$data[0]['verified'] = fetchEmailVerified($con, $plainEmail);
unset($data[0]['_email_enc']);
$stored_password = $data[0]['password'];
if (password_verify($password, $stored_password)) {
unset($data[0]['password']);
echo json_encode([
"status" => "success",
"count" => $count,
"data" => $data
]);
} else {
// The password is incorrect
echo json_encode([
"status" => "Failure",
"data" => "Incorrect password."
]);
// jsonError("Incorrect password.");
}
} else {
echo json_encode([
"status" => "Failure",
"data" => "Invalid credentials."
]);
}
$con = null;
?>