Skip to content
Snippets Groups Projects
Commit 0f434e0b authored by Thomas Müller's avatar Thomas Müller
Browse files

Implement CSRF protection

parent 4eb15885
Branches
No related tags found
No related merge requests found
......@@ -2,6 +2,9 @@
namespace OCA\DAV\CardDAV\Sharing;
use OCA\DAV\Connector\Sabre\Auth;
use OCP\IRequest;
use Sabre\DAV\Exception\BadRequest;
use Sabre\DAV\Exception\NotFound;
use Sabre\DAV\Server;
use Sabre\DAV\ServerPlugin;
......@@ -11,6 +14,11 @@ use Sabre\HTTP\ResponseInterface;
class Plugin extends ServerPlugin {
public function __construct(Auth $authBackEnd, IRequest $request) {
$this->auth = $authBackEnd;
$this->request = $request;
}
/**
* Reference to SabreDAV server object.
*
......@@ -87,6 +95,9 @@ class Plugin extends ServerPlugin {
return;
}
// CSRF protection
$this->protectAgainstCSRF();
$requestBody = $request->getBodyAsString();
// If this request handler could not deal with this POST request, it
......@@ -190,5 +201,18 @@ class Plugin extends ServerPlugin {
}
private function protectAgainstCSRF() {
$user = $this->auth->getCurrentUser();
if ($this->auth->isDavAuthenticated($user)) {
return true;
}
if ($this->request->passesCSRFCheck()) {
return true;
}
throw new BadRequest();
}
}
......@@ -65,7 +65,7 @@ class Auth extends AbstractBasic {
* @param string $username
* @return bool
*/
protected function isDavAuthenticated($username) {
public function isDavAuthenticated($username) {
return !is_null($this->session->get(self::DAV_AUTHENTICATED)) &&
$this->session->get(self::DAV_AUTHENTICATED) === $username;
}
......
......@@ -50,6 +50,7 @@ class Server {
$this->server->addPlugin(new \Sabre\CalDAV\SharingPlugin());
$this->server->addPlugin(new \Sabre\CalDAV\Subscriptions\Plugin());
$this->server->addPlugin(new \Sabre\CalDAV\Notifications\Plugin());
$this->server->addPlugin(new CardDAV\Sharing\Plugin($authBackend, \OC::$server->getRequest()));
// addressbook plugins
$this->server->addPlugin(new \Sabre\CardDAV\Plugin());
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment