@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Provide PhabricatorEnv::isSelfURI to test if a URI points at the current install

Summary:
Ref T5378. This repackages an existing check to see if a URI is a URI for the current install into a more reasonable form.

In an upcoming change, I'll use this new check to test whether `http://example.whatever.com/T123` is a link to a task on the current install or not.

Test Plan: This stuff has good test coverage already; added some more.

Reviewers: chad

Reviewed By: chad

Maniphest Tasks: T5378

Differential Revision: https://secure.phabricator.com/D17502

+71 -26
+4 -18
src/applications/differential/field/DifferentialRevisionIDCommitMessageField.php
··· 50 50 $uri = new PhutilURI($uri_string); 51 51 $path = $uri->getPath(); 52 52 53 - $matches = null; 54 - if (preg_match('#^/D(\d+)$#', $path, $matches)) { 55 - $id = (int)$matches[1]; 56 - 57 - $prod_uri = new PhutilURI(PhabricatorEnv::getProductionURI('/D'.$id)); 58 - 59 - // Make sure the URI is the same as our URI. Basically, we want to ignore 60 - // commits from other Phabricator installs. 61 - if ($uri->getDomain() == $prod_uri->getDomain()) { 62 - return $id; 63 - } 64 - 65 - $allowed_uris = PhabricatorEnv::getAllowedURIs('/D'.$id); 66 - 67 - foreach ($allowed_uris as $allowed_uri) { 68 - if ($uri_string == $allowed_uri) { 69 - return $id; 70 - } 53 + if (PhabricatorEnv::isSelfURI($uri_string)) { 54 + $matches = null; 55 + if (preg_match('#^/D(\d+)$#', $path, $matches)) { 56 + return (int)$matches[1]; 71 57 } 72 58 } 73 59
+1
src/applications/differential/field/__tests__/DifferentialCommitMessageFieldTestCase.php
··· 13 13 "D123\nSome-Custom-Field: The End" => 123, 14 14 "{$base_uri}D123" => 123, 15 15 "{$base_uri}D123\nSome-Custom-Field: The End" => 123, 16 + 'https://www.other.com/D123' => null, 16 17 ); 17 18 18 19 $env = PhabricatorEnv::beginScopedEnv();
+31 -8
src/infrastructure/env/PhabricatorEnv.php
··· 406 406 return rtrim($production_domain, '/').$path; 407 407 } 408 408 409 - public static function getAllowedURIs($path) { 410 - $uri = new PhutilURI($path); 411 - if ($uri->getDomain()) { 412 - return $path; 409 + 410 + public static function isSelfURI($raw_uri) { 411 + $uri = new PhutilURI($raw_uri); 412 + 413 + $host = $uri->getDomain(); 414 + if (!strlen($host)) { 415 + return false; 413 416 } 414 417 418 + $host = phutil_utf8_strtolower($host); 419 + 420 + $self_map = self::getSelfURIMap(); 421 + return isset($self_map[$host]); 422 + } 423 + 424 + private static function getSelfURIMap() { 425 + $self_uris = array(); 426 + $self_uris[] = self::getProductionURI('/'); 427 + $self_uris[] = self::getURI('/'); 428 + 415 429 $allowed_uris = self::getEnvConfig('phabricator.allowed-uris'); 416 - $return = array(); 417 430 foreach ($allowed_uris as $allowed_uri) { 418 - $return[] = rtrim($allowed_uri, '/').$path; 431 + $self_uris[] = $allowed_uri; 432 + } 433 + 434 + $self_map = array(); 435 + foreach ($self_uris as $self_uri) { 436 + $host = id(new PhutilURI($self_uri))->getDomain(); 437 + if (!strlen($host)) { 438 + continue; 439 + } 440 + 441 + $host = phutil_utf8_strtolower($host); 442 + $self_map[$host] = $host; 419 443 } 420 444 421 - return $return; 445 + return $self_map; 422 446 } 423 - 424 447 425 448 /** 426 449 * Get the fully-qualified production URI for a static resource path.
+35
src/infrastructure/env/__tests__/PhabricatorEnvTestCase.php
··· 218 218 $this->assertFalse($caught instanceof Exception); 219 219 } 220 220 221 + public function testSelfURI() { 222 + $base_uri = 'https://allowed.example.com/'; 223 + 224 + $allowed_uris = array( 225 + 'https://old.example.com/', 226 + ); 227 + 228 + $env = PhabricatorEnv::beginScopedEnv(); 229 + $env->overrideEnvConfig('phabricator.base-uri', $base_uri); 230 + $env->overrideEnvConfig('phabricator.allowed-uris', $allowed_uris); 231 + 232 + $map = array( 233 + 'https://allowed.example.com/' => true, 234 + 'https://allowed.example.com' => true, 235 + 'https://allowed.EXAMPLE.com' => true, 236 + 'http://allowed.example.com/' => true, 237 + 'https://allowed.example.com/path/to/resource.png' => true, 238 + 239 + 'https://old.example.com/' => true, 240 + 'https://old.example.com' => true, 241 + 'https://old.EXAMPLE.com' => true, 242 + 'http://old.example.com/' => true, 243 + 'https://old.example.com/path/to/resource.png' => true, 244 + 245 + 'https://other.example.com/' => false, 246 + ); 247 + 248 + foreach ($map as $input => $expect) { 249 + $this->assertEqual( 250 + $expect, 251 + PhabricatorEnv::isSelfURI($input), 252 + pht('Is self URI? %s', $input)); 253 + } 254 + } 255 + 221 256 }