Skip to content

fix: success validation in oauth2 redirect#10130

Merged
stnguyen90 merged 4 commits into1.7.xfrom
fix-oauth2-redirect-validation
Jul 10, 2025
Merged

fix: success validation in oauth2 redirect#10130
stnguyen90 merged 4 commits into1.7.xfrom
fix-oauth2-redirect-validation

Conversation

@stnguyen90
Copy link
Contributor

@stnguyen90 stnguyen90 commented Jul 9, 2025

What does this PR do?

We switched to using the Redirect class for validating redirect URLs to cover additional cases like react native expo scheme, but we missed this validation.

Test Plan

Before the change, we would get an error for the success param:

image

After we don't see the error on the success param:

image

And working login using android development build:

Screen.Recording.2025-07-09.at.22.15.25.mov

Related PRs and Issues

Checklist

  • Have you read the Contributing Guidelines on issues?
  • If the PR includes a change to an API's metadata (desc, label, params, etc.), does it also include updated API specs and example docs?

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jul 9, 2025

📝 Walkthrough

Walkthrough

The OAuth2 redirect endpoint for account sessions was updated to accept two new dependencies: an array named platforms and a devKey document. The function signature was modified to include these parameters. The logic for validating the success and failure redirect URLs was changed to use a Redirect validator instantiated with the platforms array, but only when the devKey is empty. If devKey is not empty, URL validation is skipped. Variable names were updated to reflect these changes, with no other logic modified.

Additionally, the URL parsing method was enhanced to handle invalid URLs more robustly by checking the result of parse_url and throwing exceptions when appropriate. The URL unparsing method was refined to better handle empty password fields. Corresponding unit tests were added to verify parsing and unparsing behavior with custom schemes and empty credentials.

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@stnguyen90 stnguyen90 requested review from ChiragAgg5k and loks0n July 9, 2025 19:18
@github-actions
Copy link

github-actions bot commented Jul 9, 2025

Security Scan Results for PR

Docker Image Scan Results

Package Version Vulnerability Severity
binutils 2.42-r0 CVE-2025-0840 HIGH
git 2.45.3-r0 CVE-2025-48384 HIGH
git 2.45.3-r0 CVE-2025-48385 HIGH
git-init-template 2.45.3-r0 CVE-2025-48384 HIGH
git-init-template 2.45.3-r0 CVE-2025-48385 HIGH
libexpat 2.6.4-r0 CVE-2024-8176 HIGH
libxml2 2.12.7-r0 CVE-2024-56171 HIGH
libxml2 2.12.7-r0 CVE-2025-24928 HIGH
libxml2 2.12.7-r0 CVE-2025-27113 HIGH
libxml2 2.12.7-r0 CVE-2025-32414 HIGH
libxml2 2.12.7-r0 CVE-2025-32415 HIGH
pyc 3.12.9-r0 CVE-2024-12718 HIGH
pyc 3.12.9-r0 CVE-2025-4138 HIGH
pyc 3.12.9-r0 CVE-2025-4330 HIGH
pyc 3.12.9-r0 CVE-2025-4517 HIGH
python3 3.12.9-r0 CVE-2024-12718 HIGH
python3 3.12.9-r0 CVE-2025-4138 HIGH
python3 3.12.9-r0 CVE-2025-4330 HIGH
python3 3.12.9-r0 CVE-2025-4517 HIGH
python3-pyc 3.12.9-r0 CVE-2024-12718 HIGH
python3-pyc 3.12.9-r0 CVE-2025-4138 HIGH
python3-pyc 3.12.9-r0 CVE-2025-4330 HIGH
python3-pyc 3.12.9-r0 CVE-2025-4517 HIGH
python3-pycache-pyc0 3.12.9-r0 CVE-2024-12718 HIGH
python3-pycache-pyc0 3.12.9-r0 CVE-2025-4138 HIGH
python3-pycache-pyc0 3.12.9-r0 CVE-2025-4330 HIGH
python3-pycache-pyc0 3.12.9-r0 CVE-2025-4517 HIGH
sqlite-libs 3.45.3-r1 CVE-2025-29087 HIGH
xz 5.6.2-r0 CVE-2025-31115 HIGH
xz-libs 5.6.2-r0 CVE-2025-31115 HIGH
golang.org/x/crypto v0.31.0 CVE-2025-22869 HIGH

Source Code Scan Results

🎉 No vulnerabilities found!

@github-actions
Copy link

github-actions bot commented Jul 9, 2025

✨ Benchmark results

  • Requests per second: 828
  • Requests with 200 status code: 149,082
  • P99 latency: 0.229704892

⚡ Benchmark Comparison

Metric This PR Latest version
RPS 828 1,101
200 149,082 198,289
P99 0.229704892 0.171321825

We switched to using the Redirect class for validating redirect URLs to cover additional cases like react native expo scheme, but we missed this validation.
@stnguyen90 stnguyen90 force-pushed the fix-oauth2-redirect-validation branch from be64005 to 53086fe Compare July 9, 2025 22:11
@stnguyen90 stnguyen90 marked this pull request as ready for review July 9, 2025 22:33
@stnguyen90 stnguyen90 marked this pull request as draft July 10, 2025 03:45
In react native, a redirect URL may only be a url scheme such as appwrite-callback-myproject://. Since parse_url() fails on this type of URL, we need to add this workaround.
So, unparsing should not end up with https://:@appwrite.io just because user and pass are empty strings.
@stnguyen90 stnguyen90 requested review from ChiragAgg5k and loks0n July 10, 2025 05:10
@stnguyen90 stnguyen90 marked this pull request as ready for review July 10, 2025 05:19
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 53086fe and 11332ed.

📒 Files selected for processing (3)
  • app/controllers/api/account.php (3 hunks)
  • src/Appwrite/URL/URL.php (2 hunks)
  • tests/unit/URL/URLTest.php (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: Benchmark
  • GitHub Check: E2E Service Test (Sites)
🔇 Additional comments (8)
src/Appwrite/URL/URL.php (2)

29-42: LGTM! Enhanced URL parsing with proper fallback handling.

The implementation correctly handles the case where parse_url() returns false and provides appropriate fallback logic for scheme-only URLs. The regex pattern properly validates URL schemes according to RFC standards (starting with a letter, followed by letters, digits, plus, period, or hyphen).


71-73: Fix the logic for handling empty passwords.

There's a logical issue in the password handling. Line 73 uses $parts['user'] but should check if the user is not empty, and the condition for appending '@' needs refinement.

Apply this diff to fix the logic:

-$parts['pass'] = !empty($url['pass']) ? ':' . $url['pass'] : '';
-
-$parts['pass'] = ($parts['user'] || !empty($parts['pass'])) ? $parts['pass'] . '@' : '';
+$parts['pass'] = !empty($url['pass']) ? ':' . $url['pass'] : '';
+
+$parts['pass'] = (!empty($parts['user']) || !empty($parts['pass'])) ? $parts['pass'] . '@' : '';

The current logic uses $parts['user'] (which could be an empty string and still be truthy) instead of checking if the user is not empty. This could lead to incorrect URL construction.

Likely an incorrect or invalid review comment.

tests/unit/URL/URLTest.php (2)

30-37: LGTM! Good test coverage for scheme-only URL parsing.

The test case properly validates the new functionality for parsing scheme-only URLs like React Native Expo schemes. All assertions check the correct default values.


99-110: URL::unparse output validated

The test confirms that URL::unparse returns https://appwrite.io/# when all components except scheme and host are empty. No further changes are required.

app/controllers/api/account.php (4)

1319-1320: LGTM! Dependency injections follow established patterns.

The new platforms array and devKey Document injections are properly structured and will support the enhanced redirect validation logic.


1325-1325: Function signature correctly updated.

The addition of array $platforms and Document $devKey parameters properly matches the injected dependencies and maintains type safety.


1329-1329: Core fix implementation looks correct.

The Redirect validator instantiated with the platforms array should properly handle additional URL schemes like React Native Expo, addressing the main issue described in the PR.


1356-1362: No action needed: Dev keys intentionally bypass URL validation in development

The devKey is populated from the x-appwrite-dev-key header (see app/init/resources.php) and is only used to enable “development mode” behaviors—bypassing origin checks, rate limits, and URL validations. When devKey->isEmpty() is false, the code switches to a generic URL validator (allowing any redirect) by design.

• dev keys are created via the Projects API and use a high-entropy secret, so they can’t be forged in production
• All URL validations remain enforced whenever no valid dev key is supplied
• This behavior is documented in the “createDevKey” SDK description and the param definitions in account.php

Comment on lines +1427 to 1434
$userParam = $request->getParam('user');
if (!empty($nameOAuth)) {
$name = $nameOAuth;
} elseif (is_array($userParam)) {
$nameParam = $userParam['name'];
if (is_array($nameParam) && isset($nameParam['firstName']) && isset($nameParam['lastName'])) {
$name = $nameParam['firstName'] . ' ' . $nameParam['lastName'];
} elseif ($userParam !== null) {
$user = \json_decode($userParam, true);
if (isset($user['name']['firstName']) && isset($user['name']['lastName'])) {
$name = $user['name']['firstName'] . ' ' . $user['name']['lastName'];
}
Copy link
Contributor

@coderabbitai coderabbitai bot Jul 10, 2025

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add error handling for JSON decoding.

The restructured user parameter handling is more careful, but lacks error handling for malformed JSON.

-        } elseif ($userParam !== null) {
-            $user = \json_decode($userParam, true);
-            if (isset($user['name']['firstName']) && isset($user['name']['lastName'])) {
-                $name = $user['name']['firstName'] . ' ' . $user['name']['lastName'];
+        } elseif ($userParam !== null) {
+            $user = \json_decode($userParam, true);
+            if ($user !== null && isset($user['name']['firstName']) && isset($user['name']['lastName'])) {
+                $name = $user['name']['firstName'] . ' ' . $user['name']['lastName'];
             }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
$userParam = $request->getParam('user');
if (!empty($nameOAuth)) {
$name = $nameOAuth;
} elseif (is_array($userParam)) {
$nameParam = $userParam['name'];
if (is_array($nameParam) && isset($nameParam['firstName']) && isset($nameParam['lastName'])) {
$name = $nameParam['firstName'] . ' ' . $nameParam['lastName'];
} elseif ($userParam !== null) {
$user = \json_decode($userParam, true);
if (isset($user['name']['firstName']) && isset($user['name']['lastName'])) {
$name = $user['name']['firstName'] . ' ' . $user['name']['lastName'];
}
$userParam = $request->getParam('user');
if (!empty($nameOAuth)) {
$name = $nameOAuth;
} elseif ($userParam !== null) {
$user = \json_decode($userParam, true);
if ($user !== null
&& isset($user['name']['firstName'])
&& isset($user['name']['lastName'])
) {
$name = $user['name']['firstName'] . ' ' . $user['name']['lastName'];
}
🤖 Prompt for AI Agents
In app/controllers/api/account.php around lines 1427 to 1434, the code decodes
JSON from the user parameter without checking for errors, which can cause issues
if the JSON is malformed. Add error handling after json_decode to verify if
decoding was successful by checking for null or json_last_error(), and handle
the error appropriately, such as logging the error or returning a meaningful
response to prevent further processing with invalid data.

Copy link
Member

Choose a reason for hiding this comment

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

might be worth adding @stnguyen90 ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If $userParam is not valid JSON, the isset if block will resolve to false, which is fine.

@stnguyen90 stnguyen90 merged commit 435a319 into 1.7.x Jul 10, 2025
38 checks passed
@stnguyen90 stnguyen90 mentioned this pull request Sep 14, 2025
2 tasks
@stnguyen90 stnguyen90 deleted the fix-oauth2-redirect-validation branch October 1, 2025 23:59
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.

3 participants