mirror of
https://we.phorge.it/source/phorge.git
synced 2024-11-14 02:42:40 +01:00
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
This commit is contained in:
parent
20892ae502
commit
688c120f9f
4 changed files with 73 additions and 28 deletions
|
@ -50,24 +50,10 @@ final class DifferentialRevisionIDCommitMessageField
|
||||||
$uri = new PhutilURI($uri_string);
|
$uri = new PhutilURI($uri_string);
|
||||||
$path = $uri->getPath();
|
$path = $uri->getPath();
|
||||||
|
|
||||||
|
if (PhabricatorEnv::isSelfURI($uri_string)) {
|
||||||
$matches = null;
|
$matches = null;
|
||||||
if (preg_match('#^/D(\d+)$#', $path, $matches)) {
|
if (preg_match('#^/D(\d+)$#', $path, $matches)) {
|
||||||
$id = (int)$matches[1];
|
return (int)$matches[1];
|
||||||
|
|
||||||
$prod_uri = new PhutilURI(PhabricatorEnv::getProductionURI('/D'.$id));
|
|
||||||
|
|
||||||
// Make sure the URI is the same as our URI. Basically, we want to ignore
|
|
||||||
// commits from other Phabricator installs.
|
|
||||||
if ($uri->getDomain() == $prod_uri->getDomain()) {
|
|
||||||
return $id;
|
|
||||||
}
|
|
||||||
|
|
||||||
$allowed_uris = PhabricatorEnv::getAllowedURIs('/D'.$id);
|
|
||||||
|
|
||||||
foreach ($allowed_uris as $allowed_uri) {
|
|
||||||
if ($uri_string == $allowed_uri) {
|
|
||||||
return $id;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -13,6 +13,7 @@ final class DifferentialCommitMessageFieldTestCase
|
||||||
"D123\nSome-Custom-Field: The End" => 123,
|
"D123\nSome-Custom-Field: The End" => 123,
|
||||||
"{$base_uri}D123" => 123,
|
"{$base_uri}D123" => 123,
|
||||||
"{$base_uri}D123\nSome-Custom-Field: The End" => 123,
|
"{$base_uri}D123\nSome-Custom-Field: The End" => 123,
|
||||||
|
'https://www.other.com/D123' => null,
|
||||||
);
|
);
|
||||||
|
|
||||||
$env = PhabricatorEnv::beginScopedEnv();
|
$env = PhabricatorEnv::beginScopedEnv();
|
||||||
|
|
37
src/infrastructure/env/PhabricatorEnv.php
vendored
37
src/infrastructure/env/PhabricatorEnv.php
vendored
|
@ -406,21 +406,44 @@ final class PhabricatorEnv extends Phobject {
|
||||||
return rtrim($production_domain, '/').$path;
|
return rtrim($production_domain, '/').$path;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getAllowedURIs($path) {
|
|
||||||
$uri = new PhutilURI($path);
|
public static function isSelfURI($raw_uri) {
|
||||||
if ($uri->getDomain()) {
|
$uri = new PhutilURI($raw_uri);
|
||||||
return $path;
|
|
||||||
|
$host = $uri->getDomain();
|
||||||
|
if (!strlen($host)) {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$host = phutil_utf8_strtolower($host);
|
||||||
|
|
||||||
|
$self_map = self::getSelfURIMap();
|
||||||
|
return isset($self_map[$host]);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static function getSelfURIMap() {
|
||||||
|
$self_uris = array();
|
||||||
|
$self_uris[] = self::getProductionURI('/');
|
||||||
|
$self_uris[] = self::getURI('/');
|
||||||
|
|
||||||
$allowed_uris = self::getEnvConfig('phabricator.allowed-uris');
|
$allowed_uris = self::getEnvConfig('phabricator.allowed-uris');
|
||||||
$return = array();
|
|
||||||
foreach ($allowed_uris as $allowed_uri) {
|
foreach ($allowed_uris as $allowed_uri) {
|
||||||
$return[] = rtrim($allowed_uri, '/').$path;
|
$self_uris[] = $allowed_uri;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $return;
|
$self_map = array();
|
||||||
|
foreach ($self_uris as $self_uri) {
|
||||||
|
$host = id(new PhutilURI($self_uri))->getDomain();
|
||||||
|
if (!strlen($host)) {
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$host = phutil_utf8_strtolower($host);
|
||||||
|
$self_map[$host] = $host;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $self_map;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the fully-qualified production URI for a static resource path.
|
* Get the fully-qualified production URI for a static resource path.
|
||||||
|
|
|
@ -218,4 +218,39 @@ final class PhabricatorEnvTestCase extends PhabricatorTestCase {
|
||||||
$this->assertFalse($caught instanceof Exception);
|
$this->assertFalse($caught instanceof Exception);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testSelfURI() {
|
||||||
|
$base_uri = 'https://allowed.example.com/';
|
||||||
|
|
||||||
|
$allowed_uris = array(
|
||||||
|
'https://old.example.com/',
|
||||||
|
);
|
||||||
|
|
||||||
|
$env = PhabricatorEnv::beginScopedEnv();
|
||||||
|
$env->overrideEnvConfig('phabricator.base-uri', $base_uri);
|
||||||
|
$env->overrideEnvConfig('phabricator.allowed-uris', $allowed_uris);
|
||||||
|
|
||||||
|
$map = array(
|
||||||
|
'https://allowed.example.com/' => true,
|
||||||
|
'https://allowed.example.com' => true,
|
||||||
|
'https://allowed.EXAMPLE.com' => true,
|
||||||
|
'http://allowed.example.com/' => true,
|
||||||
|
'https://allowed.example.com/path/to/resource.png' => true,
|
||||||
|
|
||||||
|
'https://old.example.com/' => true,
|
||||||
|
'https://old.example.com' => true,
|
||||||
|
'https://old.EXAMPLE.com' => true,
|
||||||
|
'http://old.example.com/' => true,
|
||||||
|
'https://old.example.com/path/to/resource.png' => true,
|
||||||
|
|
||||||
|
'https://other.example.com/' => false,
|
||||||
|
);
|
||||||
|
|
||||||
|
foreach ($map as $input => $expect) {
|
||||||
|
$this->assertEqual(
|
||||||
|
$expect,
|
||||||
|
PhabricatorEnv::isSelfURI($input),
|
||||||
|
pht('Is self URI? %s', $input));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue