-
Notifications
You must be signed in to change notification settings - Fork 104
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
Fix insert #1465
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')) { | ||
$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; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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... 😕
There was a problem hiding this comment.
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).