Skip to content

fix(auth): prevent timing attacks in MFA challenge verification using hash_equals (CWE-208) - #14320

Closed
halilyilmz wants to merge 5 commits into
appwrite:mainfrom
halilyilmz:fix/mfa-timing-attack-hardening
Closed

halilyilmz wants to merge 5 commits into
appwrite:mainfrom
halilyilmz:fix/mfa-timing-attack-hardening

Conversation

@halilyilmz

Copy link
Copy Markdown

What does this PR do?

Hardens Multi-Factor Authentication (MFA) challenge verification against timing side-channel attacks (CWE-208: Observable Timing Discrepancy) by replacing non-constant-time string comparison (===) with constant-time \hash_equals().

Technical & Security Details

  • Vulnerability: In Email.php, Phone.php, and Custom.php, OTP verification previously used:
    return $challenge->getAttribute('code') === $otp;
    And in TOTP.php:
    return $instance->now() === $otp;
    In PHP, the identity comparison operator (===) terminates evaluation at the first non-matching byte. This introduces variable execution time proportional to the length of matching characters, enabling remote timing side-channel attacks to deduce OTP digits.
  • Compliance & Security Standards:
    • NIST SP 800-63B Section 5.1.3.2: Mandates constant-time comparison for authenticator verification.
    • OWASP ASVS v4.0 (V2.8.2 / V13.2.3): Requires timing-safe string comparison for authentication secrets and codes.
    • CWE-208: Observable Timing Discrepancy.
  • Semgrep Baseline Remediation:
    • Appwrite's internal static analysis rule .semgrep/secret-compare-timing.yml already flags non-constant-time comparisons.
    • These 4 locations were previously tracked as suppressed technical debt in .semgrep/baseline.json.
    • With this fix, all 4 locations now comply with the rule and have been removed from .semgrep/baseline.json.

Test Plan

  • Verify that valid OTP codes for Email, Phone, Custom, and TOTP challenges authenticate successfully.
  • Verify that invalid OTPs are rejected safely.
  • Verify .semgrep/secret-compare-timing.yml passes without baseline exceptions.

Checklist

  • Have you read the Contributing Guidelines?
  • If the PR includes a change to an API's metadata, does it include updated specs? (N/A)

@ghost

ghost commented Oct 9, 2026

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

MFA challenge verification for email, phone, custom, and TOTP now uses hash_equals() instead of strict string comparison. The Semgrep baseline removes the corresponding four timing-comparison suppressions.

Verdict New comments Fixed Still open
💬 Commented 0 0 0

Note

@halilyilmz does not have write access to this repository, so Hansi does not approve automatically. A maintainer can review and approve.

📂 Walkthrough · 5
File Change
.semgrep/baseline.json Removes the four resolved MFA timing-comparison findings from the baseline.
src/Appwrite/Auth/MFA/Challenge/Custom.php Uses hash_equals() for custom challenge OTP verification.
src/Appwrite/Auth/MFA/Challenge/Email.php Uses hash_equals() for email challenge OTP verification.
src/Appwrite/Auth/MFA/Challenge/Phone.php Uses hash_equals() for phone challenge OTP verification.
src/Appwrite/Auth/MFA/Challenge/TOTP.php Uses hash_equals() for TOTP verification.

Reviewed da2b258 · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Tier S · Looks good to merge. Summary

@HarshMN2345

Copy link
Copy Markdown
Member

Closing since this doesn't match the direction we're taking for this area. Thanks for the contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants