Skip to content

fix(sites): refuse a branch preview domain that is not a hostname - #13957

Merged
levivannoort merged 3 commits into
mainfrom
fix/branch-domain-guard
Sep 28, 2026
Merged

levivannoort merged 3 commits into
mainfrom
fix/branch-domain-guard

Conversation

@levivannoort

@levivannoort levivannoort commented Sep 28, 2026 •

Copy link
Copy Markdown
Member

What does this PR do?

BranchDomain::apply() now checks its result with Utopia\Validator\Domain and throws InvalidArgumentException when the result is not a valid hostname. Each of its three callers catches that, logs a warning and skips only the branch preview rule, so the deployment still goes ahead:

  • VCS\Http\GitHub\Deployment (the push webhook)
  • Compute\Base::redeployVcsSite()
  • Compute\Base::activateBranchPreviewRule(), where manual rules pinned to the branch are still repointed at the new deployment

Why: before #11245, the filter cut a branch name to 16 characters without sanitizing it. A branch like dependabot/npm_and_yarn/... then produced a stored domain such as branch-dependabot/npm_a-eb6baf1-82abd4e.appwrite.network. A browser reads branch-dependabot as the host and the rest as a path, so no such preview was ever reachable. Those rules are still in the table, and on Cloud every invalidation for one of them fails the CDN library's hostname check.

#11245 fixed the sanitizing, but nothing checks the result. A later change to the filter, or a sitesDomain that carries a path, would store unreachable rules again, and the first sign would come weeks later from the edge queue.

The check sits in the filter because that is the only place a rule's domain is built from user input. A format on the rules.domain attribute would need a migration, and it would make every update to the legacy rules still in the table fail.

Test Plan

  • tests/unit/Filter/BranchDomainTest.php: a result that is not a hostname throws. The existing real-world branch cases, such as dependabot/... and user/john/experiment, still pass through the guard.
  • tests/unit/Platform/Modules/Compute/BranchPreviewRuleTest.php: with a refused domain, activateBranchPreviewRule() writes no preview rule, still repoints manual rules pinned to the branch, and does not throw. With a valid domain it creates the rule. The skip test fails against main.
  • The webhook and redeployVcsSite() use the same small try/catch around the filter call. Their normal path is covered by the Sites and VCS E2E runs.
  • Pint and PHPStan pass on the changed files.

To review activateBranchPreviewRule(), hide whitespace changes: most of that hunk is re-indentation.

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? No API change.

Before #11245 sanitized branch names, a "/" in the branch was stored in the
preview rule's domain. No browser, edge or CDN can address such a name, and
the rules still fail CDN purges on Cloud today. BranchDomain now validates its
own result, and its callers skip only the branch preview rule when it throws,
so the deployment itself still goes ahead.
@ghost

ghost commented Sep 28, 2026 •

Copy link
Copy Markdown

🔵 Tier A · Mergeable after minor fixes

The invalid-domain activation test does not exercise the manual-rule update it is intended to protect.

This pull request validates generated branch-preview hostnames and skips invalid preview rules without stopping deployments. The handling covers GitHub push deployments, site redeployments, and build activation, while keeping branch-pinned manual rules eligible for updates. Unit tests cover invalid hostnames and the activation path.

Latest changes: The newest commits replace the separator-only hostname test with activation tests for valid and invalid generated domains.

Verdict New comments Fixed Still open
✅ Approved 1 0 0
Finding Where
🟡 Exercise the manual-rule update in the invalid-domain test tests/unit/Platform/Modules/Compute/BranchPreviewRuleTest.php:30
📂 Walkthrough · 5
File Change
src/Appwrite/Filter/BranchDomain.php Rejects generated branch domains that fail hostname validation.
src/Appwrite/Platform/Modules/Compute/Base.php Skips invalid branch-preview rules on redeploy and activation while retaining manual-rule handling.
src/Appwrite/Platform/Modules/VCS/Http/GitHub/Deployment.php Skips invalid branch-preview rules on GitHub push deployments.
tests/unit/Filter/BranchDomainTest.php Checks invalid-hostname rejection; the latest commits remove the separator-only case.
tests/unit/Platform/Modules/Compute/BranchPreviewRuleTest.php Adds valid- and invalid-domain activation tests.

Reviewed the commits since edda678 · 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

…g it

The catch in the push webhook and redeployVcsSite() also wrapped the rule's
createDocument(), where it could swallow an unrelated InvalidArgumentException.
It now covers the filter call alone, the same as activateBranchPreviewRule(),
and every skipped preview rule leaves a warning.
@github-actions

github-actions Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/branch-domain-guard (after).

Metric Before After Change
🚀 Requests/sec 204.59 206.14 ⚪ +0.8%
⏱️ Latency P50 85.63 ms 83.72 ms ⚪ -2.2%
⏱️ Latency P95 196.06 ms 198.72 ms ⚪ +1.4%
Per-scenario breakdown & investigation details

Metrics below reflect the current branch (after). Δ P95 compares against the base.

Scenario P50 (ms) P95 (ms) Requests RPS Δ P95 (ms)
API total 83.72 198.72 12,768 206.14 +2.66
Account 165.51 312.38 672 11.14 +27.8
TablesDB 79.51 141.98 6,944 113.93 -22.7
Storage 80.56 168.72 3,360 57.21 +6.84
Functions 129.64 245.59 1,792 31.13 +23.1

Top API waits (after)

API request Max wait (ms)
account.name.update 518.88
account.prefs.update 439.16
functions.create 412
functions.delete 400.28
functions.variables.update 395.65

@greptile-apps

greptile-apps Bot commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 4/5

[Medium risk] Adds validation to branch preview domain generation.

The hostname guard appears sound, but the new tests must satisfy the repository’s testing requirements before merging.

Fix All in Claude CodeFindings

  1. P2 Module behavior tested as unit ▶
  2. P2 Manual rule update goes untested ▶
Fix with agent prompt
### Issue 1
tests/unit/Platform/Modules/Compute/BranchPreviewRuleTest.php:7
This new unit test directly calls `Compute\Base`. The repository’s testing guide limits unit tests to local `src` libraries and directs module behavior to E2E coverage. That requirement must be satisfied before merging.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

### Issue 2
tests/unit/Platform/Modules/Compute/BranchPreviewRuleTest.php:31-34
The test says branch-pinned manual rules still follow the deployment, but its `forEach` mock never runs the callback that updates them. It also expects `updateDocument` never to run, so the test would pass if that update stopped working. The repository requires tests of observable behavior rather than internal call counts; this requirement must be satisfied before merging.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Summary

The PR validates generated branch-preview hostnames and lets deployments continue without an invalid preview rule. The latest revision removes an implementation-mirroring filter test and adds coverage for the site activation path, but that coverage does not exercise manual-rule repointing.

Reviews (2) · Last reviewed commit: "test(sites): cover the skipped branch pr..."

Comment thread tests/unit/Filter/BranchDomainTest.php Resolved
Drops the separator-only filter test, which repeated the validator's own
check. The real-world branch cases already show sanitized names get through
the guard. A new test drives activateBranchPreviewRule() with a domain the
filter refuses: no preview rule is written, and manual rules pinned to the
branch are still repointed.

@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 A · Looks good to merge. Summary

Comment on lines +30 to +34
$dbForPlatform->expects($this->never())->method('createDocument');
$dbForPlatform->expects($this->never())->method('updateDocument');

// Manual rules pinned to the branch still follow the new deployment.
$dbForPlatform->expects($this->once())->method('foreach')->with('rules');

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Exercise the manual-rule update in the invalid-domain test

The forEach mock never invokes its callback, and updateDocument is expected never to run, so this passes even if branch-pinned manual rules stop being repointed when the preview domain is invalid. Have the mock supply a matching manual rule and assert that it is updated to deployment1 while no preview rule is created.

🟡 Minor · testing · Reply if this doesn't apply.


namespace Tests\Unit\Platform\Modules\Compute;

use Appwrite\Platform\Modules\Compute\Base;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Module behavior tested as unit

This new unit test directly calls Compute\Base. The repository’s testing guide limits unit tests to local src libraries and directs module behavior to E2E coverage. That requirement must be satisfied before merging.

Context Used: AGENTS.md (source)

Prompt To Fix With AI
This is a comment left during a code review.
Path: tests/unit/Platform/Modules/Compute/BranchPreviewRuleTest.php
Line: 7

Comment:
**Module behavior tested as unit**

This new unit test directly calls `Compute\Base`. The repository’s testing guide limits unit tests to local `src` libraries and directs module behavior to E2E coverage. That requirement must be satisfied before merging.

**Context Used:** AGENTS.md ([source](https://github.com/appwrite/appwrite/blob/main/AGENTS.md))

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Fix in Claude Code Fix in Codex

Comment on lines +31 to +34
$dbForPlatform->expects($this->never())->method('updateDocument');

// Manual rules pinned to the branch still follow the new deployment.
$dbForPlatform->expects($this->once())->method('foreach')->with('rules');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Manual rule update goes untested

The test says branch-pinned manual rules still follow the deployment, but its forEach mock never runs the callback that updates them. It also expects updateDocument never to run, so the test would pass if that update stopped working. The repository requires tests of observable behavior rather than internal call counts; this requirement must be satisfied before merging.

Context Used: Call out and harshly judge implementation-coupled tests. We don't mirror source code, configuration, or version pins in assertions. We test observable behavior; use linters for syntax and schema checks. (source)

Prompt To Fix With AI
This is a comment left during a code review.
Path: tests/unit/Platform/Modules/Compute/BranchPreviewRuleTest.php
Line: 31-34

Comment:
**Manual rule update goes untested**

The test says branch-pinned manual rules still follow the deployment, but its `forEach` mock never runs the callback that updates them. It also expects `updateDocument` never to run, so the test would pass if that update stopped working. The repository requires tests of observable behavior rather than internal call counts; this requirement must be satisfied before merging.

**Context Used:** Call out and harshly judge implementation-coupled tests. We don't mirror source code, configuration, or version pins in assertions. We test observable behavior; use linters for syntax and schema checks. ([source](https://app.greptile.com/review/custom-context?memory=instruction-0))

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Fix in Claude Code Fix in Codex

@levivannoort
levivannoort merged commit 6d762eb into main Sep 28, 2026
58 of 59 checks passed
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.

1 participant