Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't send errors as HTML if request looks like a json request #15357

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,20 @@

//show the user a detailed error page
OC_Response::setStatus(OC_Response::STATUS_SERVICE_UNAVAILABLE);
OC_Template::printExceptionErrorPage($ex);
if (\OC::$server->getRequest()->isJSONRequest()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about moving this check inside printExceptionErrorPage() adn even send back the error message as json?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this looks ugly. But as a developer you expect printExceptionErrorPage() to output something ... it starts with print.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it starts with print.

in case of json request it will print out json - on cli it will print out plain text - html for all the rest

OC_Template::printExceptionErrorPage($ex);
}
} catch (\OC\HintException $ex) {
OC_Response::setStatus(OC_Response::STATUS_SERVICE_UNAVAILABLE);
OC_Template::printErrorPage($ex->getMessage(), $ex->getHint());
if (\OC::$server->getRequest()->isJSONRequest()) {
OC_Template::printErrorPage($ex->getMessage(), $ex->getHint());
}
} catch (Exception $ex) {
\OCP\Util::logException('index', $ex);

//show the user a detailed error page
OC_Response::setStatus(OC_Response::STATUS_INTERNAL_SERVER_ERROR);
OC_Template::printExceptionErrorPage($ex);
if (\OC::$server->getRequest()->isJSONRequest()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be inverted - right?

If it's NOT a json request the exception page is displayed

@butonic

OC_Template::printExceptionErrorPage($ex);
}
}
14 changes: 14 additions & 0 deletions lib/private/appframework/http/request.php
Original file line number Diff line number Diff line change
Expand Up @@ -758,4 +758,18 @@ private function getOverwriteHost() {
return null;
}

/**
* Checks whether the request originates from jQuery
* or if the Accept Header contains application json
* @return boolean true if at least one of the headers is set, false otherwise
*/
public function isJSONRequest() {
if (stristr($this->server['HTTP_X_REQUEST_WITH'], 'XMLHttpRequest')
|| stristr($this->server['HTTP_ACCEPT'],'application/json')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will this work for use out of the box? We send no accept headers in our ajax calls as of now - what we probably should do.

relying on X_REQUEST_WITH seems a bit fragile to me

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jQuery sends it by default. Nearly no one sends the Accept: application/json header. So really no way we can detect JSON requests... this is the best we can do for now.

) {
return true;
}
return false;
}

}
7 changes: 7 additions & 0 deletions lib/public/irequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,11 @@ public function getInsecureServerHost();
* @since 8.1.0
*/
public function getServerHost();

/**
* Checks whether the request originates from jQuery
* or if the Accept Header contains application json
* @return boolean true if at least one of the headers is set, false otherwise
*/
public function isJSONRequest();
}