Skip to content

Fix: Platform check for Sites with automatic rule #10043

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 30, 2025

Conversation

Meldiron
Copy link
Contributor

@Meldiron Meldiron commented Jun 23, 2025

What does this PR do?

  • Move CORS domain check to a resource
  • Re-uses same resource in OPTIONS, to ensure same CORS behaviour
  • Re-uses Safe referrer check during platform check

Test Plan

  • Manual QA (site can use web SDK without web platform, but only from same project)

Related PRs and Issues

x

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?

Copy link
Contributor

coderabbitai bot commented Jun 23, 2025

📝 Walkthrough

Walkthrough

The changes remove the use of the clients dependency and associated manual origin/referrer validation logic from the main controller, replacing them with two new injected resources: httpReferrer and httpReferrerSafe. The origin host is now parsed from the injected httpReferrer, and domain verification is simplified to a direct comparison of registerable domains. The CORS Access-Control-Allow-Origin header is consistently set using the sanitized httpReferrerSafe value. Additionally, new resources for httpReferrer and httpReferrerSafe are defined in the application container, handling extraction, validation, and sanitization of the HTTP referrer. Function signatures and dependency injections are updated accordingly. The benchmarking workflow step for installing the "oha" tool is enhanced to include a version output check immediately after installation.

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 phpcs (3.7.2)
app/controllers/general.php

📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between f159065 and ba7903a.

📒 Files selected for processing (2)
  • .github/workflows/benchmark.yml (2 hunks)
  • app/controllers/general.php (4 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • .github/workflows/benchmark.yml
  • app/controllers/general.php
⏰ Context from checks skipped due to timeout of 90000ms (20)
  • GitHub Check: E2E Service Test (VCS)
  • GitHub Check: E2E Service Test (Migrations)
  • GitHub Check: E2E Service Test (Teams)
  • GitHub Check: E2E Service Test (Proxy)
  • GitHub Check: E2E Service Test (Sites)
  • GitHub Check: E2E Service Test (Users)
  • GitHub Check: E2E Service Test (Databases)
  • GitHub Check: E2E Service Test (GraphQL)
  • GitHub Check: E2E Service Test (Functions)
  • GitHub Check: E2E Service Test (Console)
  • GitHub Check: E2E Service Test (Storage)
  • GitHub Check: E2E Service Test (Health)
  • GitHub Check: E2E Service Test (Projects)
  • GitHub Check: E2E Service Test (Tokens)
  • GitHub Check: E2E Service Test (Account)
  • GitHub Check: E2E Service Test (Avatars)
  • GitHub Check: E2E General Test
  • GitHub Check: E2E Service Test (Dev Keys)
  • GitHub Check: Unit Test
  • GitHub Check: Setup & Build Appwrite Image
✨ 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.

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: 0

🧹 Nitpick comments (1)
app/init/resources.php (1)

953-997: Refactor goto statements for better code readability.

The validation logic is sound, but the use of goto statements reduces code readability and maintainability. Consider refactoring to use early returns or a helper method.

-App::setResource('httpReferrerSafe', function (Request $request, string $httpReferrer, array $clients, Database $dbForPlatform, Document $project, App $utopia): string {
-    $origin = \parse_url($request->getOrigin($httpReferrer), PHP_URL_HOST);
-
-    // Safe if route is public
-    $route = $utopia->getRoute();
-    if (!$route->getLabel('origin', false)) {
-        goto originToUrl;
-    }
-
-    // Safe if added as web platform
-    $validator = new Hostname($clients);
-    if ($validator->isValid($origin)) {
-        goto originToUrl;
-    }
-
-    // Safe if rule with same project ID exists
-    if (!empty($origin)) {
-        if (System::getEnv('_APP_RULES_FORMAT') === 'md5') {
-            $rule = Authorization::skip(fn () => $dbForPlatform->getDocument('rules', md5($origin ?? '')));
-        } else {
-            $rule = Authorization::skip(
-                fn () => $dbForPlatform->find('rules', [
-                    Query::equal('domain', [$origin]),
-                    Query::limit(1)
-                ])
-            )[0] ?? new Document();
-        }
-
-        if (!$rule->isEmpty() && $rule->getAttribute('projectInternalId') === $project->getSequence()) {
-            goto originToUrl;
-        }
-    }
-
-    // Unsafe; Localhost is always safe for ease of local development
-    $origin = 'localhost';
-
-    originToUrl:
-
-    $protocol = \parse_url($request->getOrigin($httpReferrer), PHP_URL_SCHEME);
-    $port = \parse_url($request->getOrigin($httpReferrer), PHP_URL_PORT);
-
-    $referrer = (!empty($protocol) ? $protocol : $request->getProtocol()) . '://' . $origin . (!empty($port) ? ':' . $port : '');
-
-    return $referrer;
+App::setResource('httpReferrerSafe', function (Request $request, string $httpReferrer, array $clients, Database $dbForPlatform, Document $project, App $utopia): string {
+    $origin = \parse_url($request->getOrigin($httpReferrer), PHP_URL_HOST);
+
+    $isSafe = false;
+
+    // Safe if route is public
+    $route = $utopia->getRoute();
+    if (!$route->getLabel('origin', false)) {
+        $isSafe = true;
+    }
+
+    // Safe if added as web platform
+    if (!$isSafe) {
+        $validator = new Hostname($clients);
+        if ($validator->isValid($origin)) {
+            $isSafe = true;
+        }
+    }
+
+    // Safe if rule with same project ID exists
+    if (!$isSafe && !empty($origin)) {
+        if (System::getEnv('_APP_RULES_FORMAT') === 'md5') {
+            $rule = Authorization::skip(fn () => $dbForPlatform->getDocument('rules', md5($origin ?? '')));
+        } else {
+            $rule = Authorization::skip(
+                fn () => $dbForPlatform->find('rules', [
+                    Query::equal('domain', [$origin]),
+                    Query::limit(1)
+                ])
+            )[0] ?? new Document();
+        }
+
+        if (!$rule->isEmpty() && $rule->getAttribute('projectInternalId') === $project->getSequence()) {
+            $isSafe = true;
+        }
+    }
+
+    // Unsafe; Localhost is always safe for ease of local development
+    if (!$isSafe) {
+        $origin = 'localhost';
+    }
+
+    $protocol = \parse_url($request->getOrigin($httpReferrer), PHP_URL_SCHEME);
+    $port = \parse_url($request->getOrigin($httpReferrer), PHP_URL_PORT);
+
+    $referrer = (!empty($protocol) ? $protocol : $request->getProtocol()) . '://' . $origin . (!empty($port) ? ':' . $port : '');
+
+    return $referrer;
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between ffc7f4c and c1bd493.

📒 Files selected for processing (2)
  • app/controllers/general.php (6 hunks)
  • app/init/resources.php (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Setup & Build Appwrite Image
  • GitHub Check: Setup & Build Appwrite Image
  • GitHub Check: scan
🔇 Additional comments (7)
app/init/resources.php (1)

948-951: LGTM - Simple and effective implementation.

The httpReferrer resource correctly extracts the raw HTTP referrer from the request.

app/controllers/general.php (6)

806-808: LGTM - Proper dependency injection of new resources.

The function signature correctly injects the new httpReferrer and httpReferrerSafe resources, replacing the previous manual validation logic.


940-940: LGTM - Consistent use of centralized referrer handling.

Using the httpReferrer resource instead of directly calling $request->getReferer('') maintains consistency with the new centralized approach.


1014-1014: LGTM - Consistent CORS header handling.

Setting the CORS Access-Control-Allow-Origin header to the sanitized httpReferrerSafe value ensures consistent and secure CORS behavior.


1035-1035: LGTM - Simplified and secure origin validation.

The simplified check against httpReferrerSafe leverages the centralized validation logic while maintaining security by checking if the safe referrer defaults to 'localhost' (indicating an untrusted origin).


1058-1059: LGTM - Consistent dependency injection in OPTIONS route.

The OPTIONS route correctly injects the httpReferrerSafe resource with updated function signature, maintaining consistency with the main API route.


1077-1077: LGTM - Consistent CORS header in OPTIONS route.

The OPTIONS route now consistently uses httpReferrerSafe for the CORS header, matching the behavior in the main API route.

Copy link

github-actions bot commented Jun 23, 2025

Security Scan Results for PR

Docker Image Scan Results

Package Version Vulnerability Severity
binutils 2.42-r0 CVE-2025-0840 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-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-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-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-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!

Copy link

github-actions bot commented Jun 25, 2025

✨ Benchmark results

  • Requests per second: 907
  • Requests with 200 status code: 163,385
  • P99 latency: 0.210927914

⚡ Benchmark Comparison

Metric This PR Latest version
RPS 907 1,135
200 163,385 204,298
P99 0.210927914 0.166217048

@Meldiron Meldiron merged commit 36aa227 into 1.7.x Jun 30, 2025
38 checks passed
@christyjacob4 christyjacob4 deleted the fix-rules-platform-check branch June 30, 2025 12:10
@coderabbitai coderabbitai bot mentioned this pull request Jul 21, 2025
2 tasks
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