mirror of
https://we.phorge.it/source/phorge.git
synced 2025-03-20 16:20:09 +01:00
Convert some whiny exceptions into quiet MalformedRequest exceptions
Summary: Fixes T11480. This cleans up the error logs a little by quieting three common errors which are really malformed requests: - The CSRF error happens when bots hit anything which does write checks. - The "wrong cookie domain" errors happen when bots try to use the `security.alternate-file-domain` to browse stuff like `/auth/start/`. - The "no phcid" errors happen when bots try to go through the login flow. All of these are clearly communicated to human users, commonly encountered by bots, and not useful to log. I collapsed the `CSRFException` type into a standard malformed request exception, since nothing catches it and I can't really come up with a reason why anything would ever care. Test Plan: Hit each error through some level of `curl -H ...` and/or fakery. Verified that they showed to users before/after, but no longer log. Hit some other real errors, verified that they log. Reviewers: chad Reviewed By: chad Maniphest Tasks: T11480 Differential Revision: https://secure.phabricator.com/D16402
This commit is contained in:
parent
f50e550c9e
commit
95cf83f14e
5 changed files with 37 additions and 23 deletions
|
@ -132,7 +132,6 @@ phutil_register_library_map(array(
|
|||
'AphrontApplicationConfiguration' => 'aphront/configuration/AphrontApplicationConfiguration.php',
|
||||
'AphrontBarView' => 'view/widget/bars/AphrontBarView.php',
|
||||
'AphrontBoolHTTPParameterType' => 'aphront/httpparametertype/AphrontBoolHTTPParameterType.php',
|
||||
'AphrontCSRFException' => 'aphront/exception/AphrontCSRFException.php',
|
||||
'AphrontCalendarEventView' => 'applications/calendar/view/AphrontCalendarEventView.php',
|
||||
'AphrontController' => 'aphront/AphrontController.php',
|
||||
'AphrontCursorPagerView' => 'view/control/AphrontCursorPagerView.php',
|
||||
|
@ -4566,7 +4565,6 @@ phutil_register_library_map(array(
|
|||
'AphrontApplicationConfiguration' => 'Phobject',
|
||||
'AphrontBarView' => 'AphrontView',
|
||||
'AphrontBoolHTTPParameterType' => 'AphrontHTTPParameterType',
|
||||
'AphrontCSRFException' => 'AphrontException',
|
||||
'AphrontCalendarEventView' => 'AphrontView',
|
||||
'AphrontController' => 'Phobject',
|
||||
'AphrontCursorPagerView' => 'AphrontView',
|
||||
|
|
|
@ -261,25 +261,30 @@ final class AphrontRequest extends Phobject {
|
|||
// Add some diagnostic details so we can figure out if some CSRF issues
|
||||
// are JS problems or people accessing Ajax URIs directly with their
|
||||
// browsers.
|
||||
$more_info = array();
|
||||
$info = array();
|
||||
|
||||
$info[] = pht(
|
||||
'You are trying to save some data to Phabricator, but the request '.
|
||||
'your browser made included an incorrect token. Reload the page '.
|
||||
'and try again. You may need to clear your cookies.');
|
||||
|
||||
if ($this->isAjax()) {
|
||||
$more_info[] = pht('This was an Ajax request.');
|
||||
$info[] = pht('This was an Ajax request.');
|
||||
} else {
|
||||
$more_info[] = pht('This was a Web request.');
|
||||
$info[] = pht('This was a Web request.');
|
||||
}
|
||||
|
||||
if ($token) {
|
||||
$more_info[] = pht('This request had an invalid CSRF token.');
|
||||
$info[] = pht('This request had an invalid CSRF token.');
|
||||
} else {
|
||||
$more_info[] = pht('This request had no CSRF token.');
|
||||
$info[] = pht('This request had no CSRF token.');
|
||||
}
|
||||
|
||||
// Give a more detailed explanation of how to avoid the exception
|
||||
// in developer mode.
|
||||
if (PhabricatorEnv::getEnvConfig('phabricator.developer-mode')) {
|
||||
// TODO: Clean this up, see T1921.
|
||||
$more_info[] = pht(
|
||||
$info[] = pht(
|
||||
"To avoid this error, use %s to construct forms. If you are already ".
|
||||
"using %s, make sure the form 'action' uses a relative URI (i.e., ".
|
||||
"begins with a '%s'). Forms using absolute URIs do not include CSRF ".
|
||||
|
@ -299,16 +304,16 @@ final class AphrontRequest extends Phobject {
|
|||
'setRenderAsForm(true)');
|
||||
}
|
||||
|
||||
$message = implode("\n", $info);
|
||||
|
||||
// This should only be able to happen if you load a form, pull your
|
||||
// internet for 6 hours, and then reconnect and immediately submit,
|
||||
// but give the user some indication of what happened since the workflow
|
||||
// is incredibly confusing otherwise.
|
||||
throw new AphrontCSRFException(
|
||||
pht(
|
||||
'You are trying to save some data to Phabricator, but the request '.
|
||||
'your browser made included an incorrect token. Reload the page '.
|
||||
'and try again. You may need to clear your cookies.')."\n\n".
|
||||
implode("\n", $more_info));
|
||||
throw new AphrontMalformedRequestException(
|
||||
pht('Invalid Request (CSRF)'),
|
||||
$message,
|
||||
true);
|
||||
}
|
||||
|
||||
return true;
|
||||
|
@ -480,7 +485,8 @@ final class AphrontRequest extends Phobject {
|
|||
$configured_as = PhabricatorEnv::getEnvConfig('phabricator.base-uri');
|
||||
$accessed_as = $this->getHost();
|
||||
|
||||
throw new Exception(
|
||||
throw new AphrontMalformedRequestException(
|
||||
pht('Bad Host Header'),
|
||||
pht(
|
||||
'This Phabricator install is configured as "%s", but you are '.
|
||||
'using the domain name "%s" to access a page which is trying to '.
|
||||
|
@ -488,7 +494,8 @@ final class AphrontRequest extends Phobject {
|
|||
'domain or a configured alternate domain. Phabricator will not '.
|
||||
'set cookies on other domains for security reasons.',
|
||||
$configured_as,
|
||||
$accessed_as));
|
||||
$accessed_as),
|
||||
true);
|
||||
}
|
||||
|
||||
$base_domain = $base_domain_uri->getDomain();
|
||||
|
|
|
@ -1,3 +0,0 @@
|
|||
<?php
|
||||
|
||||
final class AphrontCSRFException extends AphrontException {}
|
|
@ -28,8 +28,18 @@ final class PhabricatorDefaultRequestExceptionHandler
|
|||
|
||||
$viewer = $this->getViewer($request);
|
||||
|
||||
// Always log the unhandled exception.
|
||||
phlog($ex);
|
||||
// Some types of uninteresting request exceptions don't get logged, usually
|
||||
// because they are caused by the background radiation of bot traffic on
|
||||
// the internet. These include requests with bad CSRF tokens and
|
||||
// questionable "Host" headers.
|
||||
$should_log = true;
|
||||
if ($ex instanceof AphrontMalformedRequestException) {
|
||||
$should_log = !$ex->getIsUnlogged();
|
||||
}
|
||||
|
||||
if ($should_log) {
|
||||
phlog($ex);
|
||||
}
|
||||
|
||||
$class = get_class($ex);
|
||||
$message = $ex->getMessage();
|
||||
|
|
|
@ -464,12 +464,14 @@ abstract class PhabricatorAuthProvider extends Phobject {
|
|||
public function getAuthCSRFCode(AphrontRequest $request) {
|
||||
$phcid = $request->getCookie(PhabricatorCookies::COOKIE_CLIENTID);
|
||||
if (!strlen($phcid)) {
|
||||
throw new Exception(
|
||||
throw new AphrontMalformedRequestException(
|
||||
pht('Missing Client ID Cookie'),
|
||||
pht(
|
||||
'Your browser did not submit a "%s" cookie with client state '.
|
||||
'information in the request. Check that cookies are enabled. '.
|
||||
'If this problem persists, you may need to clear your cookies.',
|
||||
PhabricatorCookies::COOKIE_CLIENTID));
|
||||
PhabricatorCookies::COOKIE_CLIENTID),
|
||||
true);
|
||||
}
|
||||
|
||||
return PhabricatorHash::digest($phcid);
|
||||
|
|
Loading…
Add table
Reference in a new issue