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

Fix insert #1465

Merged
merged 1 commit into from
Jan 31, 2023
Merged
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
2 changes: 2 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use OCA\Forms\Capabilities;
use OCA\Forms\FormsMigrator;
use OCA\Forms\Listener\UserDeletedListener;
use OCA\Forms\Middleware\PublicCorsMiddleware;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IBootContext;
Expand Down Expand Up @@ -59,6 +60,7 @@ public function register(IRegistrationContext $context): void {
$context->registerCapability(Capabilities::class);
$context->registerEventListener(UserDeletedEvent::class, UserDeletedListener::class);
$context->registerUserMigrator(FormsMigrator::class);
$context->registerMiddleware(PublicCorsMiddleware::class);
}

/**
Expand Down
1 change: 1 addition & 0 deletions lib/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,7 @@ public function getSubmissions(string $hash): DataResponse {

/**
* @CORS
* @PublicCORSFix
* @NoAdminRequired
* @PublicPage
*
Expand Down
108 changes: 108 additions & 0 deletions lib/Middleware/PublicCorsMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php
/**
* @copyright Copyright (c) 2023 Jonas Rittershofer <jotoeri@users.noreply.github.com>
*
* @author Jonas Rittershofer <jotoeri@users.noreply.github.com>
*
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\Forms\Middleware;

use OC\AppFramework\Middleware\Security\Exceptions\SecurityException;

use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\Middleware;
use OCP\AppFramework\Utility\IControllerMethodReflector;
use OCP\IUserSession;
use OCP\IRequest;

class PublicCorsMiddleware extends Middleware {
/** @var IRequest */
private $request;
/** @var IControllerMethodReflector */
private $reflector;
/** @var IUserSession */
private $userSession;

/**
* @param IRequest $request
* @param IControllerMethodReflector $reflector
* @param IUserSession $session
*/
public function __construct(IRequest $request,
IControllerMethodReflector $reflector,
IUserSession $userSession) {
$this->request = $request;
$this->reflector = $reflector;
$this->userSession = $userSession;
}

/**
* Copied and modified version of the CORSMiddleware beforeController.
* Most significantly it also works with the PublicPage annotation.
*
* @param Controller $controller the controller that is being called
* @param string $methodName the name of the method that will be called on
* the controller
* @throws SecurityException
*/
public function beforeController($controller, $methodName) {
// ensure that @CORS annotated API routes are not used in conjunction
// with session authentication since this enables CSRF attack vectors
if ($this->reflector->hasAnnotation('PublicCORSFix')) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

If I am correct, this will check every user and even throw if anonymous submit as the user is not set.
In this case we could simply drop @PublicPage.

If we want to support anonymous submission we should change this to:

if ($this->reflector->hasAnnotation('PublicCORSFix') && $this->userSession->isLoggedIn())

Copy link
Member Author

Choose a reason for hiding this comment

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

Public Pages pass the CSRF Check. Thus it works to submit anonymously.

Copy link
Collaborator

Choose a reason for hiding this comment

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

So request->passesCSRFCheck() returns true for anonymous requests without csrf token?
Because if not, anonymous requests would be blocked by line 79.
That's why I suggested the change to only check logged in users, so that anonymous responses are still possible.

Copy link
Member Author

Choose a reason for hiding this comment

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

Nah, csrfcheck result should of course be independent of the user/anonymous. But our own pages pass the csrf check. Didn't test now, but even with the embedding we should still have a valid csrf token.

Indeed, what does not work are anonymous requests from a foreign source. But do we really want to open that? Or shouldn't all external requests be authenticated? Feels a bit like a hole on its own to open that... 😕

Copy link
Collaborator

@susnux susnux Jan 29, 2023

Choose a reason for hiding this comment

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

I would not change the behavior here, as currently anonymous submissions from extern are allowed, so this would break such usage cases. Not sure on how high the threat of CSRF of anonymous submissions is, you could submit with the IP of someone else or send spam with the IP of someone else. (But having a breaking change is the only real reason here for me not disallowing anonymous submissions without session)


While writing this I noticed this is the only endpoint working anonymously, so it does not make much sense having this available from external pages, as you can not even request a public share from external without authentication. (only no admin required and cors is set so the user must be authenticated).

$user = array_key_exists('PHP_AUTH_USER', $this->request->server) ? $this->request->server['PHP_AUTH_USER'] : null;
$pass = array_key_exists('PHP_AUTH_PW', $this->request->server) ? $this->request->server['PHP_AUTH_PW'] : null;

// Allow to use the current session if a CSRF token is provided
if ($this->request->passesCSRFCheck()) {
return;
}
$this->userSession->logout();
if ($user === null || $pass === null || !$this->userSession->login($user, $pass)) {
throw new SecurityException('CORS requires basic auth', Http::STATUS_UNAUTHORIZED);
}
}
}

/**
* If an SecurityException is being caught return a JSON error response
*
* @param Controller $controller the controller that is being called
* @param string $methodName the name of the method that will be called on
* the controller
* @param \Exception $exception the thrown exception
* @throws \Exception the passed in exception if it can't handle it
* @return Response a Response object or null in case that the exception could not be handled
*/
public function afterException($controller, $methodName, \Exception $exception) {
if ($exception instanceof SecurityException) {
$response = new JSONResponse(['message' => $exception->getMessage()]);
if ($exception->getCode() !== 0) {
$response->setStatus($exception->getCode());
} else {
$response->setStatus(Http::STATUS_INTERNAL_SERVER_ERROR);
}
return $response;
}

throw $exception;
}
}