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

Catch missing non-optional controller parameter #35599

Closed
Show file tree
Hide file tree
Changes from 1 commit
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
39 changes: 39 additions & 0 deletions lib/private/AppFramework/Exceptions/ParameterMissingException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

/*
* @copyright 2022 Christoph Wurst <[email protected]>
*
* @author 2022 Christoph Wurst <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* 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 OC\AppFramework\Exceptions;

use Exception;

class ParameterMissingException extends Exception {

public function __construct(string $controllerName, string $methodName, string $parameterName) {
parent::__construct("Parameter $parameterName missing for $controllerName::$methodName");
$this->controllerName = $controllerName;
Fixed Show fixed Hide fixed
$this->methodName = $methodName;
Fixed Show fixed Hide fixed
$this->parameterName = $parameterName;
Fixed Show fixed Hide fixed
}

}
33 changes: 26 additions & 7 deletions lib/private/AppFramework/Http/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
*/
namespace OC\AppFramework\Http;

use OC\AppFramework\Exceptions\ParameterMissingException;
use OC\AppFramework\Http;
use OC\AppFramework\Middleware\MiddlewareDispatcher;
use OC\AppFramework\Utility\ControllerMethodReflector;
Expand All @@ -43,6 +44,10 @@
use OCP\IConfig;
use OCP\IRequest;
use Psr\Log\LoggerInterface;
use ReflectionMethod;
use ReflectionNamedType;
use TypeError;
use function get_class;

/**
* Class to dispatch the request to the middleware dispatcher
Expand Down Expand Up @@ -192,20 +197,30 @@ public function dispatch(Controller $controller, string $methodName): array {
*/
private function executeController(Controller $controller, string $methodName): Response {
$arguments = [];
$reflection = new ReflectionMethod($controller, $methodName);

// valid types that will be casted
$types = ['int', 'integer', 'bool', 'boolean', 'float'];

foreach ($this->reflector->getParameters() as $param => $default) {
foreach ($reflection->getParameters() as $param) {
$paramName = $param->name;

// try to get the parameter from the request object and cast
// it to the type annotated in the @param annotation
$value = $this->request->getParam($param, $default);
$type = $this->reflector->getType($param);
// it to the type annotated in the @paramName annotation
$defaultValue = null;
if ($param->isDefaultValueAvailable()) {
$defaultValue = $param->getDefaultValue();
}
$value = $this->request->getParam($paramName, $defaultValue);
$type = $param->getType();
$typeName = null;
if ($type instanceof ReflectionNamedType) {
$typeName = $type->getName();
}

// if this is submitted using GET or a POST form, 'false' should be
// converted to false
if (($type === 'bool' || $type === 'boolean') &&
if (($typeName === 'bool' || $typeName === 'boolean') &&
$value === 'false' &&
(
$this->request->method === 'GET' ||
Expand All @@ -214,8 +229,12 @@ private function executeController(Controller $controller, string $methodName):
)
) {
$value = false;
} elseif ($value !== null && \in_array($type, $types, true)) {
settype($value, $type);
} elseif ($value !== null && \in_array($typeName, $types, true)) {
settype($value, $typeName);
}

if ($value === null && !$param->allowsNull()) {
throw new ParameterMissingException(get_class($controller), $methodName, $paramName);
}

$arguments[] = $value;
Expand Down