Server error/hint pages with a 500 error code to avoid it being seen instead of the actual resource

* found while reviewing #7205
* allow to specify a special status code

Signed-off-by: Morris Jobke <hey@morrisjobke.de>
This commit is contained in:
Morris Jobke 2018-06-26 09:17:37 +02:00
parent a97cc293b5
commit 8c155cd51c
No known key found for this signature in database
GPG Key ID: FE03C3A163FEDE68
6 changed files with 13 additions and 17 deletions

View File

@ -48,9 +48,8 @@ try {
OC_Response::setStatus(OC_Response::STATUS_SERVICE_UNAVAILABLE); OC_Response::setStatus(OC_Response::STATUS_SERVICE_UNAVAILABLE);
OC_Template::printExceptionErrorPage($ex); OC_Template::printExceptionErrorPage($ex);
} catch (\OC\HintException $ex) { } catch (\OC\HintException $ex) {
OC_Response::setStatus(OC_Response::STATUS_SERVICE_UNAVAILABLE);
try { try {
OC_Template::printErrorPage($ex->getMessage(), $ex->getHint()); OC_Template::printErrorPage($ex->getMessage(), $ex->getHint(), OC_Response::STATUS_SERVICE_UNAVAILABLE);
} catch (Exception $ex2) { } catch (Exception $ex2) {
\OC::$server->getLogger()->logException($ex, array('app' => 'index')); \OC::$server->getLogger()->logException($ex, array('app' => 'index'));
\OC::$server->getLogger()->logException($ex2, array('app' => 'index')); \OC::$server->getLogger()->logException($ex2, array('app' => 'index'));
@ -60,8 +59,7 @@ try {
OC_Template::printExceptionErrorPage($ex); OC_Template::printExceptionErrorPage($ex);
} }
} catch (\OC\User\LoginException $ex) { } catch (\OC\User\LoginException $ex) {
OC_Response::setStatus(OC_Response::STATUS_FORBIDDEN); OC_Template::printErrorPage($ex->getMessage(), $ex->getMessage(), OC_Response::STATUS_FORBIDDEN);
OC_Template::printErrorPage($ex->getMessage(), $ex->getMessage());
} catch (Exception $ex) { } catch (Exception $ex) {
\OC::$server->getLogger()->logException($ex, array('app' => 'index')); \OC::$server->getLogger()->logException($ex, array('app' => 'index'));

View File

@ -260,7 +260,8 @@ class OC {
$l->t('This can usually be fixed by giving the webserver write access to the config directory. See %s', $l->t('This can usually be fixed by giving the webserver write access to the config directory. See %s',
[ $urlGenerator->linkToDocs('admin-dir_permissions') ]) . '. ' [ $urlGenerator->linkToDocs('admin-dir_permissions') ]) . '. '
. $l->t('Or, if you prefer to keep config.php file read only, set the option "config_is_read_only" to true in it. See %s', . $l->t('Or, if you prefer to keep config.php file read only, set the option "config_is_read_only" to true in it. See %s',
[ $urlGenerator->linkToDocs('admin-config') ] ) [ $urlGenerator->linkToDocs('admin-config') ] ),
\OC_Response::STATUS_SERVICE_UNAVAILABLE
); );
} }
} }
@ -750,11 +751,10 @@ class OC {
// Check whether the sample configuration has been copied // Check whether the sample configuration has been copied
if($systemConfig->getValue('copied_sample_config', false)) { if($systemConfig->getValue('copied_sample_config', false)) {
$l = \OC::$server->getL10N('lib'); $l = \OC::$server->getL10N('lib');
header('HTTP/1.1 503 Service Temporarily Unavailable');
header('Status: 503 Service Temporarily Unavailable');
OC_Template::printErrorPage( OC_Template::printErrorPage(
$l->t('Sample configuration detected'), $l->t('Sample configuration detected'),
$l->t('It has been detected that the sample configuration has been copied. This can break your installation and is unsupported. Please read the documentation before performing changes on config.php') $l->t('It has been detected that the sample configuration has been copied. This can break your installation and is unsupported. Please read the documentation before performing changes on config.php'),
\OC_Response::STATUS_SERVICE_UNAVAILABLE
); );
return; return;
} }

View File

@ -198,18 +198,18 @@ class OC_Files {
OC::$server->getLogger()->logException($ex); OC::$server->getLogger()->logException($ex);
$l = \OC::$server->getL10N('core'); $l = \OC::$server->getL10N('core');
$hint = method_exists($ex, 'getHint') ? $ex->getHint() : ''; $hint = method_exists($ex, 'getHint') ? $ex->getHint() : '';
\OC_Template::printErrorPage($l->t('File is currently busy, please try again later'), $hint); \OC_Template::printErrorPage($l->t('File is currently busy, please try again later'), $hint, 200);
} catch (\OCP\Files\ForbiddenException $ex) { } catch (\OCP\Files\ForbiddenException $ex) {
self::unlockAllTheFiles($dir, $files, $getType, $view, $filename); self::unlockAllTheFiles($dir, $files, $getType, $view, $filename);
OC::$server->getLogger()->logException($ex); OC::$server->getLogger()->logException($ex);
$l = \OC::$server->getL10N('core'); $l = \OC::$server->getL10N('core');
\OC_Template::printErrorPage($l->t('Can\'t read file'), $ex->getMessage()); \OC_Template::printErrorPage($l->t('Can\'t read file'), $ex->getMessage(), 200);
} catch (\Exception $ex) { } catch (\Exception $ex) {
self::unlockAllTheFiles($dir, $files, $getType, $view, $filename); self::unlockAllTheFiles($dir, $files, $getType, $view, $filename);
OC::$server->getLogger()->logException($ex); OC::$server->getLogger()->logException($ex);
$l = \OC::$server->getL10N('core'); $l = \OC::$server->getL10N('core');
$hint = method_exists($ex, 'getHint') ? $ex->getHint() : ''; $hint = method_exists($ex, 'getHint') ? $ex->getHint() : '';
\OC_Template::printErrorPage($l->t('Can\'t read file'), $hint); \OC_Template::printErrorPage($l->t('Can\'t read file'), $hint, 200);
} }
} }

View File

@ -306,7 +306,7 @@ class OC_Template extends \OC\Template\Base {
* @param string $hint An optional hint message - needs to be properly escape * @param string $hint An optional hint message - needs to be properly escape
* @suppress PhanAccessMethodInternal * @suppress PhanAccessMethodInternal
*/ */
public static function printErrorPage( $error_msg, $hint = '' ) { public static function printErrorPage( $error_msg, $hint = '', $statusCode = \OC_Response::STATUS_INTERNAL_SERVER_ERROR ) {
if (\OC::$server->getAppManager()->isEnabledForUser('theming') && !\OC_App::isAppLoaded('theming')) { if (\OC::$server->getAppManager()->isEnabledForUser('theming') && !\OC_App::isAppLoaded('theming')) {
\OC_App::loadApp('theming'); \OC_App::loadApp('theming');
} }
@ -317,6 +317,7 @@ class OC_Template extends \OC\Template\Base {
$hint = ''; $hint = '';
} }
http_response_code($statusCode);
try { try {
$content = new \OC_Template( '', 'error', 'error', false ); $content = new \OC_Template( '', 'error', 'error', false );
$errors = array(array('error' => $error_msg, 'hint' => $hint)); $errors = array(array('error' => $error_msg, 'hint' => $hint));
@ -327,7 +328,6 @@ class OC_Template extends \OC\Template\Base {
$logger->error("$error_msg $hint", ['app' => 'core']); $logger->error("$error_msg $hint", ['app' => 'core']);
$logger->logException($e, ['app' => 'core']); $logger->logException($e, ['app' => 'core']);
header(self::getHttpProtocol() . ' 500 Internal Server Error');
header('Content-Type: text/plain; charset=utf-8'); header('Content-Type: text/plain; charset=utf-8');
print("$error_msg $hint"); print("$error_msg $hint");
} }

View File

@ -36,8 +36,7 @@ try {
if (\OCP\Util::needUpgrade()) { if (\OCP\Util::needUpgrade()) {
// since the behavior of apps or remotes are unpredictable during // since the behavior of apps or remotes are unpredictable during
// an upgrade, return a 503 directly // an upgrade, return a 503 directly
OC_Response::setStatus(OC_Response::STATUS_SERVICE_UNAVAILABLE); OC_Template::printErrorPage('Service unavailable', '', OC_Response::STATUS_SERVICE_UNAVAILABLE);
OC_Template::printErrorPage('Service unavailable');
exit; exit;
} }

View File

@ -77,8 +77,7 @@ function handleException($e) {
} }
if ($e instanceof RemoteException) { if ($e instanceof RemoteException) {
// we shall not log on RemoteException // we shall not log on RemoteException
OC_Response::setStatus($e->getCode()); OC_Template::printErrorPage($e->getMessage(), '', $e->getCode());
OC_Template::printErrorPage($e->getMessage());
} else { } else {
\OC::$server->getLogger()->logException($e, ['app' => 'remote']); \OC::$server->getLogger()->logException($e, ['app' => 'remote']);
OC_Response::setStatus($statusCode); OC_Response::setStatus($statusCode);