Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F4011837
D25847.1746626756.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Flag For Later
Advanced/Developer...
View Handle
View Hovercard
Size
25 KB
Referenced Files
None
Subscribers
None
D25847.1746626756.diff
View Options
diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php
--- a/src/__phutil_library_map__.php
+++ b/src/__phutil_library_map__.php
@@ -12681,7 +12681,7 @@
'PhutilRemarkupDelRule' => 'PhutilRemarkupRule',
'PhutilRemarkupDocumentLinkRule' => 'PhutilRemarkupRule',
'PhutilRemarkupEngine' => 'PhutilMarkupEngine',
- 'PhutilRemarkupEngineTestCase' => 'PhutilTestCase',
+ 'PhutilRemarkupEngineTestCase' => 'PhabricatorTestCase',
'PhutilRemarkupEscapeRemarkupRule' => 'PhutilRemarkupRule',
'PhutilRemarkupEvalRule' => 'PhutilRemarkupRule',
'PhutilRemarkupHeaderBlockRule' => 'PhutilRemarkupBlockRule',
diff --git a/src/infrastructure/env/PhabricatorEnv.php b/src/infrastructure/env/PhabricatorEnv.php
--- a/src/infrastructure/env/PhabricatorEnv.php
+++ b/src/infrastructure/env/PhabricatorEnv.php
@@ -11,7 +11,7 @@
* The primary role of this class is to provide an API for reading
* Phabricator configuration, @{method:getEnvConfig}:
*
- * $value = PhabricatorEnv::getEnvConfig('some.key', $default);
+ * $value = PhabricatorEnv::getEnvConfig('some.key');
*
* The class also handles some URI construction based on configuration, via
* the methods @{method:getURI}, @{method:getProductionURI},
@@ -428,14 +428,31 @@
}
+ /**
+ * Check whenever an URI points to this very same Phorge installation.
+ * @param string|PhutilURI $raw_uri Raw string URI, or PhutilURI.
+ * @return bool True, if this URI points to Phorge itself.
+ */
public static function isSelfURI($raw_uri) {
+ // The constructor of PhutilURI has always accepted both a string or
+ // a PhutilURI, and it's much faster with a PhutilURI.
$uri = new PhutilURI($raw_uri);
+ // Relative paths always are self-URIs.
$host = $uri->getDomain();
- if (!phutil_nonempty_string($host)) {
+ $prot = $uri->getProtocol();
+ $empty_host = !phutil_nonempty_string($host);
+ $empty_prot = !phutil_nonempty_string($prot);
+ if ($empty_host && $empty_prot) {
return true;
}
+ // When we have a protocol, but no host,
+ // this is probably a 'mailto' or another external thing.
+ if ($empty_host) {
+ return false;
+ }
+
$host = phutil_utf8_strtolower($host);
$self_map = self::getSelfURIMap();
@@ -443,6 +460,10 @@
}
private static function getSelfURIMap() {
+ // Note: here we always re-build some PhutilURI(s), but this is not
+ // blazing fast, so, we could add a small cache for our result.
+ // https://we.phorge.it/T16061
+
$self_uris = array();
$self_uris[] = self::getProductionURI('/');
$self_uris[] = self::getURI('/');
diff --git a/src/infrastructure/markup/markuprule/PhutilRemarkupDocumentLinkRule.php b/src/infrastructure/markup/markuprule/PhutilRemarkupDocumentLinkRule.php
--- a/src/infrastructure/markup/markuprule/PhutilRemarkupDocumentLinkRule.php
+++ b/src/infrastructure/markup/markuprule/PhutilRemarkupDocumentLinkRule.php
@@ -44,7 +44,13 @@
protected function renderHyperlink($link, $name) {
$engine = $this->getEngine();
+ // Setup the utility that recognizes internal/external URIs.
$uri = new PhutilURIHelper($link);
+ $uri_here = $engine->getConfig('uri.here');
+ if ($uri_here) {
+ $uri->addExtraTrustedURI($uri_here);
+ }
+
$is_anchor = $uri->isAnchor();
$starts_with_slash = $uri->isStartingWithSlash();
if ($starts_with_slash) {
@@ -52,7 +58,7 @@
$base = rtrim($base, '/');
$link = $base.$link;
} else if ($is_anchor) {
- $here = $engine->getConfig('uri.here');
+ $here = $uri_here;
$link = $here.$link;
}
diff --git a/src/infrastructure/markup/remarkup/__tests__/PhutilRemarkupEngineTestCase.php b/src/infrastructure/markup/remarkup/__tests__/PhutilRemarkupEngineTestCase.php
--- a/src/infrastructure/markup/remarkup/__tests__/PhutilRemarkupEngineTestCase.php
+++ b/src/infrastructure/markup/remarkup/__tests__/PhutilRemarkupEngineTestCase.php
@@ -2,12 +2,15 @@
/**
* Test cases for @{class:PhutilRemarkupEngine}.
+ * This needs to be a PhabricatorTestCase since it may want
+ * to access some Phorge URI configurations.
* @TODO: This unit is not always triggered when you need it.
* https://we.phorge.it/T15500
*/
-final class PhutilRemarkupEngineTestCase extends PhutilTestCase {
+final class PhutilRemarkupEngineTestCase extends PhabricatorTestCase {
public function testEngine() {
+ // Test each '.txt' test file.
$root = dirname(__FILE__).'/remarkup/';
foreach (Filesystem::listDirectory($root, $hidden = false) as $file) {
$this->markupText($root.$file);
@@ -48,6 +51,7 @@
$engine->setConfig('uri.same-window', true);
break;
case 'link-square.txt':
+ // Setup URI used as base in this Remarkup document.
$engine->setConfig('uri.base', 'http://www.example.com/');
$engine->setConfig('uri.here', 'http://www.example.com/page/');
break;
diff --git a/src/infrastructure/markup/remarkup/__tests__/remarkup/link-brackets.txt b/src/infrastructure/markup/remarkup/__tests__/remarkup/link-brackets.txt
--- a/src/infrastructure/markup/remarkup/__tests__/remarkup/link-brackets.txt
+++ b/src/infrastructure/markup/remarkup/__tests__/remarkup/link-brackets.txt
@@ -1,5 +1,5 @@
<http://www.zany.com/omg/weird_url,,,>
~~~~~~~~~~
-<p><a href="http://www.zany.com/omg/weird_url,,," class="remarkup-link" target="_blank" rel="noreferrer">http://www.zany.com/omg/weird_url,,,</a></p>
+<p><a href="http://www.zany.com/omg/weird_url,,," class="remarkup-link remarkup-link-ext" target="_blank" rel="noreferrer">http://www.zany.com/omg/weird_url,,,</a></p>
~~~~~~~~~~
http://www.zany.com/omg/weird_url,,,
diff --git a/src/infrastructure/markup/remarkup/__tests__/remarkup/link-edge-cases.txt b/src/infrastructure/markup/remarkup/__tests__/remarkup/link-edge-cases.txt
--- a/src/infrastructure/markup/remarkup/__tests__/remarkup/link-edge-cases.txt
+++ b/src/infrastructure/markup/remarkup/__tests__/remarkup/link-edge-cases.txt
@@ -10,17 +10,17 @@
Quick! http://www.example.com/!
~~~~~~~~~~
-<p><a href="http://www.example.com/" class="remarkup-link" target="_blank" rel="noreferrer">http://www.example.com/</a></p>
+<p><a href="http://www.example.com/" class="remarkup-link remarkup-link-ext" target="_blank" rel="noreferrer">http://www.example.com/</a></p>
-<p>(<a href="http://www.example.com/" class="remarkup-link" target="_blank" rel="noreferrer">http://www.example.com/</a>)</p>
+<p>(<a href="http://www.example.com/" class="remarkup-link remarkup-link-ext" target="_blank" rel="noreferrer">http://www.example.com/</a>)</p>
-<p><a href="http://www.example.com/" class="remarkup-link" target="_blank" rel="noreferrer">http://www.example.com/</a></p>
+<p><a href="http://www.example.com/" class="remarkup-link remarkup-link-ext" target="_blank" rel="noreferrer">http://www.example.com/</a></p>
-<p><a href="http://www.example.com/wiki/example_(disambiguation)" class="remarkup-link" target="_blank" rel="noreferrer">http://www.example.com/wiki/example_(disambiguation)</a></p>
+<p><a href="http://www.example.com/wiki/example_(disambiguation)" class="remarkup-link remarkup-link-ext" target="_blank" rel="noreferrer">http://www.example.com/wiki/example_(disambiguation)</a></p>
-<p>(example <a href="http://www.example.com/" class="remarkup-link" target="_blank" rel="noreferrer">http://www.example.com/</a>)</p>
+<p>(example <a href="http://www.example.com/" class="remarkup-link remarkup-link-ext" target="_blank" rel="noreferrer">http://www.example.com/</a>)</p>
-<p>Quick! <a href="http://www.example.com/" class="remarkup-link" target="_blank" rel="noreferrer">http://www.example.com/</a>!</p>
+<p>Quick! <a href="http://www.example.com/" class="remarkup-link remarkup-link-ext" target="_blank" rel="noreferrer">http://www.example.com/</a>!</p>
~~~~~~~~~~
http://www.example.com/
diff --git a/src/infrastructure/markup/remarkup/__tests__/remarkup/link-mailto.txt b/src/infrastructure/markup/remarkup/__tests__/remarkup/link-mailto.txt
--- a/src/infrastructure/markup/remarkup/__tests__/remarkup/link-mailto.txt
+++ b/src/infrastructure/markup/remarkup/__tests__/remarkup/link-mailto.txt
@@ -5,11 +5,11 @@
[[mailto:alincoln@example.com]]
~~~~~~~~~~
-<p><a href="mailto:alincoln@example.com" class="remarkup-link" target="_blank" rel="noreferrer">mail me</a></p>
+<p><a href="mailto:alincoln@example.com" class="remarkup-link remarkup-link-ext" target="_blank" rel="noreferrer">mail me</a></p>
-<p><a href="mailto:alincoln@example.com" class="remarkup-link" target="_blank" rel="noreferrer">mail me</a></p>
+<p><a href="mailto:alincoln@example.com" class="remarkup-link remarkup-link-ext" target="_blank" rel="noreferrer">mail me</a></p>
-<p><a href="mailto:alincoln@example.com" class="remarkup-link" target="_blank" rel="noreferrer">alincoln@example.com</a></p>
+<p><a href="mailto:alincoln@example.com" class="remarkup-link remarkup-link-ext" target="_blank" rel="noreferrer">alincoln@example.com</a></p>
~~~~~~~~~~
mail me <alincoln@example.com>
diff --git a/src/infrastructure/markup/remarkup/__tests__/remarkup/link-mixed.txt b/src/infrastructure/markup/remarkup/__tests__/remarkup/link-mixed.txt
--- a/src/infrastructure/markup/remarkup/__tests__/remarkup/link-mixed.txt
+++ b/src/infrastructure/markup/remarkup/__tests__/remarkup/link-mixed.txt
@@ -5,11 +5,11 @@
<http://www.example.com/ [[http://www.example.net/ | Example]]>
~~~~~~~~~~
-<p><a href="http://www.example.com/" class="remarkup-link" target="_blank" rel="noreferrer">Example</a>(<a href="http://www.alternate.org/" class="remarkup-link" target="_blank" rel="noreferrer">http://www.alternate.org/</a>)</p>
+<p><a href="http://www.example.com/" class="remarkup-link remarkup-link-ext" target="_blank" rel="noreferrer">Example</a>(<a href="http://www.alternate.org/" class="remarkup-link remarkup-link-ext" target="_blank" rel="noreferrer">http://www.alternate.org/</a>)</p>
-<p>(<a href="http://www.alternate.org/" class="remarkup-link" target="_blank" rel="noreferrer">http://www.alternate.org/</a>)<a href="http://www.example.com/" class="remarkup-link" target="_blank" rel="noreferrer">Example</a></p>
+<p>(<a href="http://www.alternate.org/" class="remarkup-link remarkup-link-ext" target="_blank" rel="noreferrer">http://www.alternate.org/</a>)<a href="http://www.example.com/" class="remarkup-link remarkup-link-ext" target="_blank" rel="noreferrer">Example</a></p>
-<p><<a href="http://www.example.com/" class="remarkup-link" target="_blank" rel="noreferrer">http://www.example.com/</a> <a href="http://www.example.net/" class="remarkup-link" target="_blank" rel="noreferrer">Example</a>></p>
+<p><<a href="http://www.example.com/" class="remarkup-link remarkup-link-ext" target="_blank" rel="noreferrer">http://www.example.com/</a> <a href="http://www.example.net/" class="remarkup-link remarkup-link-ext" target="_blank" rel="noreferrer">Example</a>></p>
~~~~~~~~~~
Example <http://www.example.com/>(http://www.alternate.org/)
diff --git a/src/infrastructure/markup/remarkup/__tests__/remarkup/link-noreferrer.txt b/src/infrastructure/markup/remarkup/__tests__/remarkup/link-noreferrer.txt
--- a/src/infrastructure/markup/remarkup/__tests__/remarkup/link-noreferrer.txt
+++ b/src/infrastructure/markup/remarkup/__tests__/remarkup/link-noreferrer.txt
@@ -1,15 +1,27 @@
+I have no idea what should happen in this case.
+Technically it does not seem to be an external link.
+Browsers may make it external, though?
+But I guess that the TARGET attribute is not the important part here...
+
[[ /\evil.com ]]
[[ /
/evil.com ]]
~~~~~~~~~~
-<p><a href="/\evil.com" class="remarkup-link" target="_blank" rel="noreferrer">/\evil.com</a></p>
+<p>I have no idea what should happen in this case.
+Technically it does not seem to be an external link.
+Browsers may make it external, though?
+But I guess that the TARGET attribute is not the important part here...</p>
+
+<p><a href="/\evil.com" class="remarkup-link" rel="noreferrer">/\evil.com</a></p>
<p><a href="/
-/evil.com" class="remarkup-link" target="_blank" rel="noreferrer">/
+/evil.com" class="remarkup-link" rel="noreferrer">/
/evil.com</a></p>
~~~~~~~~~~
+I have no idea what should happen in this case. Technically it does not seem to be an external link. Browsers may make it external, though? But I guess that the TARGET attribute is not the important part here...
+
/\evil.com
/
diff --git a/src/infrastructure/markup/remarkup/__tests__/remarkup/link-same-window.txt b/src/infrastructure/markup/remarkup/__tests__/remarkup/link-same-window.txt
--- a/src/infrastructure/markup/remarkup/__tests__/remarkup/link-same-window.txt
+++ b/src/infrastructure/markup/remarkup/__tests__/remarkup/link-same-window.txt
@@ -2,9 +2,9 @@
http://www.example.com/
~~~~~~~~~~
-<p><a href="http://www.example.com/" class="remarkup-link" rel="noreferrer">http://www.example.com/</a></p>
+<p><a href="http://www.example.com/" class="remarkup-link remarkup-link-ext" rel="noreferrer">http://www.example.com/</a></p>
-<p><a href="http://www.example.com/" class="remarkup-link" rel="noreferrer">http://www.example.com/</a></p>
+<p><a href="http://www.example.com/" class="remarkup-link remarkup-link-ext" rel="noreferrer">http://www.example.com/</a></p>
~~~~~~~~~~
http://www.example.com/
diff --git a/src/infrastructure/markup/remarkup/__tests__/remarkup/link-square.txt b/src/infrastructure/markup/remarkup/__tests__/remarkup/link-square.txt
--- a/src/infrastructure/markup/remarkup/__tests__/remarkup/link-square.txt
+++ b/src/infrastructure/markup/remarkup/__tests__/remarkup/link-square.txt
@@ -1,3 +1,5 @@
+This specific test assumes that www.example.com is our Phorge.
+
[[http://www.example.com/]]
[[http://www.example.com/ | example.com]]
@@ -8,16 +10,20 @@
[[#anchor | Anchors ]]
~~~~~~~~~~
-<p><a href="http://www.example.com/" class="remarkup-link" target="_blank" rel="noreferrer">http://www.example.com/</a></p>
+<p>This specific test assumes that www.example.com is our Phorge.</p>
+
+<p><a href="http://www.example.com/" class="remarkup-link" rel="noreferrer">http://www.example.com/</a></p>
-<p><a href="http://www.example.com/" class="remarkup-link" target="_blank" rel="noreferrer">example.com</a></p>
+<p><a href="http://www.example.com/" class="remarkup-link" rel="noreferrer">example.com</a></p>
-<p><a href="http://www.example.com/x/" class="remarkup-link" target="_blank" rel="noreferrer">http://www.example.com/x/</a></p>
+<p><a href="http://www.example.com/x/" class="remarkup-link" rel="noreferrer">http://www.example.com/x/</a></p>
<p><a href="http://www.example.com/page/#anchor" class="remarkup-link" rel="noreferrer">http://www.example.com/page/#anchor</a></p>
<p><a href="http://www.example.com/page/#anchor" class="remarkup-link" rel="noreferrer">Anchors</a></p>
~~~~~~~~~~
+This specific test assumes that www.example.com is our Phorge.
+
http://www.example.com/
example.com <http://www.example.com/>
diff --git a/src/infrastructure/markup/remarkup/__tests__/remarkup/link-tel.txt b/src/infrastructure/markup/remarkup/__tests__/remarkup/link-tel.txt
--- a/src/infrastructure/markup/remarkup/__tests__/remarkup/link-tel.txt
+++ b/src/infrastructure/markup/remarkup/__tests__/remarkup/link-tel.txt
@@ -5,11 +5,11 @@
[[tel:18005555555]]
~~~~~~~~~~
-<p><a href="tel:18005555555" class="remarkup-link" target="_blank" rel="noreferrer">call me</a></p>
+<p><a href="tel:18005555555" class="remarkup-link remarkup-link-ext" target="_blank" rel="noreferrer">call me</a></p>
-<p><a href="tel:18005555555" class="remarkup-link" target="_blank" rel="noreferrer">call me</a></p>
+<p><a href="tel:18005555555" class="remarkup-link remarkup-link-ext" target="_blank" rel="noreferrer">call me</a></p>
-<p><a href="tel:18005555555" class="remarkup-link" target="_blank" rel="noreferrer">18005555555</a></p>
+<p><a href="tel:18005555555" class="remarkup-link remarkup-link-ext" target="_blank" rel="noreferrer">18005555555</a></p>
~~~~~~~~~~
call me <18005555555>
diff --git a/src/infrastructure/markup/remarkup/__tests__/remarkup/link-with-punctuation.txt b/src/infrastructure/markup/remarkup/__tests__/remarkup/link-with-punctuation.txt
--- a/src/infrastructure/markup/remarkup/__tests__/remarkup/link-with-punctuation.txt
+++ b/src/infrastructure/markup/remarkup/__tests__/remarkup/link-with-punctuation.txt
@@ -2,8 +2,8 @@
http://www.example.com/..
http://www.example.com/!!!
~~~~~~~~~~
-<p><a href="http://www.example.com/" class="remarkup-link" target="_blank" rel="noreferrer">http://www.example.com/</a>,
-<a href="http://www.example.com/" class="remarkup-link" target="_blank" rel="noreferrer">http://www.example.com/</a>..
-<a href="http://www.example.com/" class="remarkup-link" target="_blank" rel="noreferrer">http://www.example.com/</a>!!!</p>
+<p><a href="http://www.example.com/" class="remarkup-link remarkup-link-ext" target="_blank" rel="noreferrer">http://www.example.com/</a>,
+<a href="http://www.example.com/" class="remarkup-link remarkup-link-ext" target="_blank" rel="noreferrer">http://www.example.com/</a>..
+<a href="http://www.example.com/" class="remarkup-link remarkup-link-ext" target="_blank" rel="noreferrer">http://www.example.com/</a>!!!</p>
~~~~~~~~~~
http://www.example.com/, http://www.example.com/.. http://www.example.com/!!!
diff --git a/src/infrastructure/markup/remarkup/__tests__/remarkup/link-with-tilde.txt b/src/infrastructure/markup/remarkup/__tests__/remarkup/link-with-tilde.txt
--- a/src/infrastructure/markup/remarkup/__tests__/remarkup/link-with-tilde.txt
+++ b/src/infrastructure/markup/remarkup/__tests__/remarkup/link-with-tilde.txt
@@ -1,5 +1,5 @@
http://www.example.com/~~
~~~~~~~~~~
-<p><a href="http://www.example.com/~" class="remarkup-link" target="_blank" rel="noreferrer">http://www.example.com/~</a></p>
+<p><a href="http://www.example.com/~" class="remarkup-link remarkup-link-ext" target="_blank" rel="noreferrer">http://www.example.com/~</a></p>
~~~~~~~~~~
http://www.example.com/~~
diff --git a/src/infrastructure/markup/remarkup/__tests__/remarkup/link.txt b/src/infrastructure/markup/remarkup/__tests__/remarkup/link.txt
--- a/src/infrastructure/markup/remarkup/__tests__/remarkup/link.txt
+++ b/src/infrastructure/markup/remarkup/__tests__/remarkup/link.txt
@@ -1,5 +1,5 @@
http://www.example.com/
~~~~~~~~~~
-<p><a href="http://www.example.com/" class="remarkup-link" target="_blank" rel="noreferrer">http://www.example.com/</a></p>
+<p><a href="http://www.example.com/" class="remarkup-link remarkup-link-ext" target="_blank" rel="noreferrer">http://www.example.com/</a></p>
~~~~~~~~~~
http://www.example.com/
diff --git a/src/infrastructure/markup/remarkup/__tests__/remarkup/percent-block-multiline.txt b/src/infrastructure/markup/remarkup/__tests__/remarkup/percent-block-multiline.txt
--- a/src/infrastructure/markup/remarkup/__tests__/remarkup/percent-block-multiline.txt
+++ b/src/infrastructure/markup/remarkup/__tests__/remarkup/percent-block-multiline.txt
@@ -10,7 +10,7 @@
<br />- second
<br />- third</p>
-<p><a href="http://hello" class="remarkup-link" target="_blank" rel="noreferrer">world</a></p>
+<p><a href="http://hello" class="remarkup-link remarkup-link-ext" target="_blank" rel="noreferrer">world</a></p>
~~~~~~~~~~
**foo**
diff --git a/src/infrastructure/markup/remarkup/__tests__/remarkup/simple-table-with-link.txt b/src/infrastructure/markup/remarkup/__tests__/remarkup/simple-table-with-link.txt
--- a/src/infrastructure/markup/remarkup/__tests__/remarkup/simple-table-with-link.txt
+++ b/src/infrastructure/markup/remarkup/__tests__/remarkup/simple-table-with-link.txt
@@ -1,7 +1,7 @@
| [[ http://example.com | name ]] | [x] |
~~~~~~~~~~
<div class="remarkup-table-wrap"><table class="remarkup-table">
-<tr><td><a href="http://example.com" class="remarkup-link" target="_blank" rel="noreferrer">name</a></td><td>[x]</td></tr>
+<tr><td><a href="http://example.com" class="remarkup-link remarkup-link-ext" target="_blank" rel="noreferrer">name</a></td><td>[x]</td></tr>
</table></div>
~~~~~~~~~~
| name <http://example.com> | [x] |
diff --git a/src/infrastructure/markup/remarkup/__tests__/remarkup/toc.txt b/src/infrastructure/markup/remarkup/__tests__/remarkup/toc.txt
--- a/src/infrastructure/markup/remarkup/__tests__/remarkup/toc.txt
+++ b/src/infrastructure/markup/remarkup/__tests__/remarkup/toc.txt
@@ -13,11 +13,11 @@
<li><a href="#http-www-example-com">http://www.example.com</a></li>
</ul>
-<h2 class="remarkup-header"><a name="link-name"></a><a href="http://www.example.com/" class="remarkup-link" target="_blank" rel="noreferrer">link_name</a></h2>
+<h2 class="remarkup-header"><a name="link-name"></a><a href="http://www.example.com/" class="remarkup-link remarkup-link-ext" target="_blank" rel="noreferrer">link_name</a></h2>
<h3 class="remarkup-header"><a name="bold"></a><strong>bold</strong></h3>
-<h2 class="remarkup-header"><a name="http-www-example-com"></a><a href="http://www.example.com" class="remarkup-link" target="_blank" rel="noreferrer">http://www.example.com</a></h2>
+<h2 class="remarkup-header"><a name="http-www-example-com"></a><a href="http://www.example.com" class="remarkup-link remarkup-link-ext" target="_blank" rel="noreferrer">http://www.example.com</a></h2>
~~~~~~~~~~
[[ http://www.example.com/ | link_name ]]
=========================================
diff --git a/src/infrastructure/parser/PhutilURIHelper.php b/src/infrastructure/parser/PhutilURIHelper.php
--- a/src/infrastructure/parser/PhutilURIHelper.php
+++ b/src/infrastructure/parser/PhutilURIHelper.php
@@ -2,7 +2,8 @@
/**
* A simple wrapper for PhutilURI, to be aware of the
- * relative/absolute context, and other minor things.
+ * relative/absolute context, being internal/external,
+ * and other minor things.
*/
final class PhutilURIHelper extends Phobject {
@@ -18,6 +19,15 @@
*/
private $phutilUri;
+ /**
+ * Extra trusted domains.
+ * This is useful because your Phorge environment may have some globally
+ * trusted domains, but you also can temporarily register a domain as trusted,
+ * for example in a specific Remarkup engine instance.
+ * @var array Map of trusted domains, in both the key and the value.
+ */
+ private $extraTrustedDomains = [];
+
/**
* @param string|PhutilURI $uri
*/
@@ -40,7 +50,7 @@
public function isSelf() {
// The backend prefers a PhutilURI object, if available.
$uri = $this->phutilUri ? $this->phutilUri : $this->uriStr;
- return PhabricatorEnv::isSelfURI($uri);
+ return PhabricatorEnv::isSelfURI($uri) || $this->isExtraSelfURI();
}
/**
@@ -59,6 +69,25 @@
return $this->isStartingWithChar('/');
}
+ /**
+ * Add an extra trusted URI string.
+ * Note that you may want to use addExtraTrustedPhutilURI() instead
+ * if you already have a PhutilURI object.
+ * @param $uri string URI e.g. 'https://example.com/'
+ */
+ public function addExtraTrustedURI(string $uri) {
+ $this->addExtraTrustedPhutilURI(new PhutilURI($uri));
+ }
+
+ /**
+ * Add an extra trusted URI, using a PhutilURI.
+ * @param $uri PhutilURI Extra URI to be considered trusted.
+ */
+ public function addExtraTrustedPhutilURI(PhutilURI $uri) {
+ $domain = $uri->getDomain();
+ $this->extraTrustedDomains[$domain] = $domain;
+ }
+
/**
* A sane default.
*/
@@ -66,6 +95,31 @@
return $this->uriStr;
}
+ /**
+ * Check whenever the specified URI is an "extra" self URI.
+ * @return bool
+ */
+ private function isExtraSelfURI(): bool {
+ // Micro-optimization to do not always create a PhutilURI object.
+ if (!$this->extraTrustedDomains) {
+ return false;
+ }
+
+ $domain = $this->getPhutilURI()->getDomain();
+ return isset($this->extraTrustedDomains[$domain]);
+ }
+
+ /**
+ * Get or create a PhutilURI object.
+ * @return PhutilURI
+ */
+ private function getPhutilURI(): PhutilURI {
+ if (!$this->phutilUri) {
+ $this->phutilUri = new PhutilURI($this->uriStr);
+ }
+ return $this->phutilUri;
+ }
+
/**
* Check whenever the URI starts with the provided character.
* @param string $char String that MUST have length of 1.
diff --git a/src/infrastructure/parser/__tests__/PhutilURIHelperTestCase.php b/src/infrastructure/parser/__tests__/PhutilURIHelperTestCase.php
--- a/src/infrastructure/parser/__tests__/PhutilURIHelperTestCase.php
+++ b/src/infrastructure/parser/__tests__/PhutilURIHelperTestCase.php
@@ -3,6 +3,9 @@
final class PhutilURIHelperTestCase extends PhabricatorTestCase {
public function testPhutilURIHelper() {
+ // Setup the test environment.
+ $env = PhabricatorEnv::beginScopedEnv();
+ $env->overrideEnvConfig('phabricator.base-uri', 'https://phorge.localhost');
// Every row is a test. Every column is:
// - 0: name of the test
@@ -20,16 +23,26 @@
array('internal root dir', '/#asd', true, false, true),
array('external', 'https://gnu.org/', false, false, false),
array('external anchor', 'https://gnu.org/#asd', false, false, false),
+ array('mail', 'mailto:info@wikipedia.org', false, false, false),
+ array('tel', 'tel:+555555555', false, false, false),
+ array('alien protocol', 'foo://whatever', false, false, false),
+ array('base uri', 'https://phorge.localhost', true, false, false),
+ array(
+ 'base uri anchor',
+ 'https://phorge.localhost/#asd',
+ true,
+ false,
+ false,
+ ),
+ array(
+ 'base uri path',
+ 'https://phorge.localhost/something',
+ true,
+ false,
+ false,
+ ),
);
- // Add additional self-tests if base URI is available.
- $base = PhabricatorEnv::getEnvConfigIfExists('phabricator.base-uri');
- if ($base) {
- $domain = id(new PhutilURI($base))->getDomain();
- $tests[] = array('base uri', $base, true, false, false);
- $tests[] = array('base uri anchor', "{$base}#asd", true, false, false);
- }
-
foreach ($tests as $test) {
$name = $test[0];
$uri = $test[1];
@@ -59,5 +72,8 @@
pht('%s - is starting with slash', $test_name));
}
}
+
+ // Clear the test environment.
+ unset($env);
}
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Wed, May 7, 14:05 (18 h, 8 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1511975
Default Alt Text
D25847.1746626756.diff (25 KB)
Attached To
Mode
D25847: Remarkup: harden how we recognize internal/external URIs (mailto, mobile phones, ...)
Attached
Detach File
Event Timeline
Log In to Comment