1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-29 10:12:41 +01:00

Improve routing of "/robots.txt", "/favicon.ico", "/status/", and 404 on custom Sites

Summary:
Fixes T12919. Fixes T13636. Prior to this change, some well-known resource paths don't route on sites like ResourceSite.

- `/robots.txt`: Make it route on ResourceSite and just deny the whole site.
- `/favicon.ico`: Make it route on ResourceSite.
- `/status/`: Make it route on ResourceSite.
- 404: Make it render a 404 on ResourceSite.

Test Plan:
  - Visited all URIs on ResourceSite, got sensible responses.
  - Visited all URIs on main site.
  - Visited 404 while logged out, got login page.

Maniphest Tasks: T13636, T12919

Differential Revision: https://secure.phabricator.com/D21606
This commit is contained in:
epriestley 2021-03-11 13:07:36 -08:00
parent 4484946cfd
commit 36c6eb9663
12 changed files with 103 additions and 26 deletions

View file

@ -4253,6 +4253,7 @@ phutil_register_library_map(array(
'PhabricatorPhurlURLViewController' => 'applications/phurl/controller/PhabricatorPhurlURLViewController.php', 'PhabricatorPhurlURLViewController' => 'applications/phurl/controller/PhabricatorPhurlURLViewController.php',
'PhabricatorPinnedApplicationsSetting' => 'applications/settings/setting/PhabricatorPinnedApplicationsSetting.php', 'PhabricatorPinnedApplicationsSetting' => 'applications/settings/setting/PhabricatorPinnedApplicationsSetting.php',
'PhabricatorPirateEnglishTranslation' => 'infrastructure/internationalization/translation/PhabricatorPirateEnglishTranslation.php', 'PhabricatorPirateEnglishTranslation' => 'infrastructure/internationalization/translation/PhabricatorPirateEnglishTranslation.php',
'PhabricatorPlatform404Controller' => 'applications/base/controller/PhabricatorPlatform404Controller.php',
'PhabricatorPlatformSite' => 'aphront/site/PhabricatorPlatformSite.php', 'PhabricatorPlatformSite' => 'aphront/site/PhabricatorPlatformSite.php',
'PhabricatorPointsEditField' => 'applications/transactions/editfield/PhabricatorPointsEditField.php', 'PhabricatorPointsEditField' => 'applications/transactions/editfield/PhabricatorPointsEditField.php',
'PhabricatorPointsFact' => 'applications/fact/fact/PhabricatorPointsFact.php', 'PhabricatorPointsFact' => 'applications/fact/fact/PhabricatorPointsFact.php',
@ -4689,6 +4690,8 @@ phutil_register_library_map(array(
'PhabricatorResetPasswordUserLogType' => 'applications/people/userlog/PhabricatorResetPasswordUserLogType.php', 'PhabricatorResetPasswordUserLogType' => 'applications/people/userlog/PhabricatorResetPasswordUserLogType.php',
'PhabricatorResourceSite' => 'aphront/site/PhabricatorResourceSite.php', 'PhabricatorResourceSite' => 'aphront/site/PhabricatorResourceSite.php',
'PhabricatorRobotsController' => 'applications/system/controller/PhabricatorRobotsController.php', 'PhabricatorRobotsController' => 'applications/system/controller/PhabricatorRobotsController.php',
'PhabricatorRobotsPlatformController' => 'applications/system/controller/PhabricatorRobotsPlatformController.php',
'PhabricatorRobotsResourceController' => 'applications/system/controller/PhabricatorRobotsResourceController.php',
'PhabricatorS3FileStorageEngine' => 'applications/files/engine/PhabricatorS3FileStorageEngine.php', 'PhabricatorS3FileStorageEngine' => 'applications/files/engine/PhabricatorS3FileStorageEngine.php',
'PhabricatorSMSAuthFactor' => 'applications/auth/factor/PhabricatorSMSAuthFactor.php', 'PhabricatorSMSAuthFactor' => 'applications/auth/factor/PhabricatorSMSAuthFactor.php',
'PhabricatorSQLPatchList' => 'infrastructure/storage/patch/PhabricatorSQLPatchList.php', 'PhabricatorSQLPatchList' => 'infrastructure/storage/patch/PhabricatorSQLPatchList.php',
@ -10914,6 +10917,7 @@ phutil_register_library_map(array(
'PhabricatorPhurlURLViewController' => 'PhabricatorPhurlController', 'PhabricatorPhurlURLViewController' => 'PhabricatorPhurlController',
'PhabricatorPinnedApplicationsSetting' => 'PhabricatorInternalSetting', 'PhabricatorPinnedApplicationsSetting' => 'PhabricatorInternalSetting',
'PhabricatorPirateEnglishTranslation' => 'PhutilTranslation', 'PhabricatorPirateEnglishTranslation' => 'PhutilTranslation',
'PhabricatorPlatform404Controller' => 'PhabricatorController',
'PhabricatorPlatformSite' => 'PhabricatorSite', 'PhabricatorPlatformSite' => 'PhabricatorSite',
'PhabricatorPointsEditField' => 'PhabricatorEditField', 'PhabricatorPointsEditField' => 'PhabricatorEditField',
'PhabricatorPointsFact' => 'PhabricatorFact', 'PhabricatorPointsFact' => 'PhabricatorFact',
@ -11471,6 +11475,8 @@ phutil_register_library_map(array(
'PhabricatorResetPasswordUserLogType' => 'PhabricatorUserLogType', 'PhabricatorResetPasswordUserLogType' => 'PhabricatorUserLogType',
'PhabricatorResourceSite' => 'PhabricatorSite', 'PhabricatorResourceSite' => 'PhabricatorSite',
'PhabricatorRobotsController' => 'PhabricatorController', 'PhabricatorRobotsController' => 'PhabricatorController',
'PhabricatorRobotsPlatformController' => 'PhabricatorRobotsController',
'PhabricatorRobotsResourceController' => 'PhabricatorRobotsController',
'PhabricatorS3FileStorageEngine' => 'PhabricatorFileStorageEngine', 'PhabricatorS3FileStorageEngine' => 'PhabricatorFileStorageEngine',
'PhabricatorSMSAuthFactor' => 'PhabricatorAuthFactor', 'PhabricatorSMSAuthFactor' => 'PhabricatorAuthFactor',
'PhabricatorSQLPatchList' => 'Phobject', 'PhabricatorSQLPatchList' => 'Phobject',

View file

@ -32,10 +32,6 @@ final class AphrontApplicationConfiguration
return $request; return $request;
} }
public function build404Controller() {
return array(new Phabricator404Controller(), array());
}
public function buildRedirectController($uri, $external) { public function buildRedirectController($uri, $external) {
return array( return array(
new PhabricatorRedirectController(), new PhabricatorRedirectController(),
@ -504,7 +500,10 @@ final class AphrontApplicationConfiguration
return array($result, array()); return array($result, array());
} }
return $this->build404Controller(); throw new Exception(
pht(
'Aphront site ("%s") failed to build a 404 controller.',
get_class($site)));
} }
/** /**

View file

@ -10,10 +10,17 @@ final class Aphront404Response extends AphrontHTMLResponse {
$request = $this->getRequest(); $request = $this->getRequest();
$viewer = $request->getViewer(); $viewer = $request->getViewer();
// See T13636. Note that this response may be served from a Site other than
// the primary PlatformSite. For now, always link to the PlatformSite.
// (This may not be the best possible place to send users who are currently
// on "real" sites, like the BlogSite.)
$return_uri = PhabricatorEnv::getURI('/');
$dialog = id(new AphrontDialogView()) $dialog = id(new AphrontDialogView())
->setViewer($viewer) ->setViewer($viewer)
->setTitle(pht('404 Not Found')) ->setTitle(pht('404 Not Found'))
->addCancelButton('/', pht('Return to Charted Waters')) ->addCancelButton($return_uri, pht('Return to Charted Waters'))
->appendParagraph( ->appendParagraph(
pht( pht(
'You arrive at your destination, but there is nothing here.')) 'You arrive at your destination, but there is nothing here.'))

View file

@ -10,7 +10,7 @@ abstract class AphrontSite extends Phobject {
abstract public function getRoutingMaps(); abstract public function getRoutingMaps();
public function new404Controller(AphrontRequest $request) { public function new404Controller(AphrontRequest $request) {
return null; return new Phabricator404Controller();
} }
protected function isHostMatch($host, array $uris) { protected function isHostMatch($host, array $uris) {

View file

@ -50,4 +50,8 @@ final class PhabricatorPlatformSite extends PhabricatorSite {
return $maps; return $maps;
} }
public function new404Controller(AphrontRequest $request) {
return new PhabricatorPlatform404Controller();
}
} }

View file

@ -39,7 +39,13 @@ final class PhabricatorAuthMainMenuBarExtension
private function buildLoginMenu() { private function buildLoginMenu() {
$controller = $this->getController(); $controller = $this->getController();
$uri = new PhutilURI('/auth/start/'); // See T13636. This button may be rendered by the 404 controller on sites
// other than the primary PlatformSite. Link the button to the primary
// site.
$uri = '/auth/start/';
$uri = PhabricatorEnv::getURI($uri);
$uri = new PhutilURI($uri);
if ($controller) { if ($controller) {
$path = $controller->getRequest()->getPath(); $path = $controller->getRequest()->getPath();
$uri->replaceQueryParam('next', $path); $uri->replaceQueryParam('next', $path);

View file

@ -1,6 +1,11 @@
<?php <?php
final class Phabricator404Controller extends PhabricatorController { final class Phabricator404Controller
extends PhabricatorController {
public function shouldRequireLogin() {
return false;
}
public function processRequest() { public function processRequest() {
return new Aphront404Response(); return new Aphront404Response();

View file

@ -0,0 +1,10 @@
<?php
final class PhabricatorPlatform404Controller
extends PhabricatorController {
public function processRequest() {
return new Aphront404Response();
}
}

View file

@ -25,7 +25,7 @@ final class PhabricatorSystemApplication extends PhabricatorApplication {
'/status/' => 'PhabricatorStatusController', '/status/' => 'PhabricatorStatusController',
'/debug/' => 'PhabricatorDebugController', '/debug/' => 'PhabricatorDebugController',
'/favicon.ico' => 'PhabricatorFaviconController', '/favicon.ico' => 'PhabricatorFaviconController',
'/robots.txt' => 'PhabricatorRobotsController', '/robots.txt' => 'PhabricatorRobotsPlatformController',
'/services/' => array( '/services/' => array(
'encoding/' => 'PhabricatorSystemSelectEncodingController', 'encoding/' => 'PhabricatorSystemSelectEncodingController',
'highlight/' => 'PhabricatorSystemSelectHighlightController', 'highlight/' => 'PhabricatorSystemSelectHighlightController',
@ -38,4 +38,12 @@ final class PhabricatorSystemApplication extends PhabricatorApplication {
); );
} }
public function getResourceRoutes() {
return array(
'/status/' => 'PhabricatorStatusController',
'/favicon.ico' => 'PhabricatorFaviconController',
'/robots.txt' => 'PhabricatorRobotsResourceController',
);
}
} }

View file

@ -1,26 +1,13 @@
<?php <?php
final class PhabricatorRobotsController extends PhabricatorController { abstract class PhabricatorRobotsController extends PhabricatorController {
public function shouldRequireLogin() { public function shouldRequireLogin() {
return false; return false;
} }
public function processRequest() { final public function processRequest() {
$out = array(); $out = $this->newRobotsRules();
// Prevent indexing of '/diffusion/', since the content is not generally
// useful to index, web spiders get stuck scraping the history of every
// file, and much of the content is Ajaxed in anyway so spiders won't even
// see it. These pages are also relatively expensive to generate.
// Note that this still allows commits (at '/rPxxxxx') to be indexed.
// They're probably not hugely useful, but suffer fewer of the problems
// Diffusion suffers and are hard to omit with 'robots.txt'.
$out[] = 'User-Agent: *';
$out[] = 'Disallow: /diffusion/';
$out[] = 'Disallow: /source/';
// Add a small crawl delay (number of seconds between requests) for spiders // Add a small crawl delay (number of seconds between requests) for spiders
// which respect it. The intent here is to prevent spiders from affecting // which respect it. The intent here is to prevent spiders from affecting
@ -36,4 +23,7 @@ final class PhabricatorRobotsController extends PhabricatorController {
->setCacheDurationInSeconds(phutil_units('2 hours in seconds')) ->setCacheDurationInSeconds(phutil_units('2 hours in seconds'))
->setCanCDN(true); ->setCanCDN(true);
} }
abstract protected function newRobotsRules();
} }

View file

@ -0,0 +1,25 @@
<?php
final class PhabricatorRobotsPlatformController
extends PhabricatorRobotsController {
protected function newRobotsRules() {
$out = array();
// Prevent indexing of '/diffusion/', since the content is not generally
// useful to index, web spiders get stuck scraping the history of every
// file, and much of the content is Ajaxed in anyway so spiders won't even
// see it. These pages are also relatively expensive to generate.
// Note that this still allows commits (at '/rPxxxxx') to be indexed.
// They're probably not hugely useful, but suffer fewer of the problems
// Diffusion suffers and are hard to omit with 'robots.txt'.
$out[] = 'User-Agent: *';
$out[] = 'Disallow: /diffusion/';
$out[] = 'Disallow: /source/';
return $out;
}
}

View file

@ -0,0 +1,17 @@
<?php
final class PhabricatorRobotsResourceController
extends PhabricatorRobotsController {
protected function newRobotsRules() {
$out = array();
// See T13636. Prevent indexing of any content on resource domains.
$out[] = 'User-Agent: *';
$out[] = 'Disallow: /';
return $out;
}
}