| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Rest\Validator; |
| 4 |
|
| 5 |
use MediaWiki\ParamValidator\TypeDef\ArrayDef; |
| 6 |
use MediaWiki\ParamValidator\TypeDef\TitleDef; |
| 7 |
use MediaWiki\ParamValidator\TypeDef\UserDef; |
| 8 |
use MediaWiki\Permissions\Authority; |
| 9 |
use MediaWiki\Rest\Handler; |
| 10 |
use MediaWiki\Rest\HttpException; |
| 11 |
use MediaWiki\Rest\LocalizedHttpException; |
| 12 |
use MediaWiki\Rest\RequestInterface; |
| 13 |
use Wikimedia\Message\ListParam; |
| 14 |
use Wikimedia\Message\ListType; |
| 15 |
use Wikimedia\Message\MessageValue; |
| 16 |
use Wikimedia\ObjectFactory\ObjectFactory; |
| 17 |
use Wikimedia\ParamValidator\ParamValidator; |
| 18 |
use Wikimedia\ParamValidator\TypeDef; |
| 19 |
use Wikimedia\ParamValidator\TypeDef\BooleanDef; |
| 20 |
use Wikimedia\ParamValidator\TypeDef\EnumDef; |
| 21 |
use Wikimedia\ParamValidator\TypeDef\ExpiryDef; |
| 22 |
use Wikimedia\ParamValidator\TypeDef\FloatDef; |
| 23 |
use Wikimedia\ParamValidator\TypeDef\IntegerDef; |
| 24 |
use Wikimedia\ParamValidator\TypeDef\PasswordDef; |
| 25 |
use Wikimedia\ParamValidator\TypeDef\StringDef; |
| 26 |
use Wikimedia\ParamValidator\TypeDef\TimestampDef; |
| 27 |
use Wikimedia\ParamValidator\TypeDef\UploadDef; |
| 28 |
use Wikimedia\ParamValidator\ValidationException; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
class Validator { |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public const KNOWN_PARAM_SOURCES = [ 'path', 'query', 'body', 'post' ]; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public const PARAM_SOURCE = 'rest-param-source'; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public const PARAM_DESCRIPTION = 'rest-param-description'; |
| 61 |
|
| 62 |
|
| 63 |
private const TYPE_DEFS = [ |
| 64 |
'boolean' => [ 'class' => BooleanDef::class ], |
| 65 |
'enum' => [ 'class' => EnumDef::class ], |
| 66 |
'integer' => [ 'class' => IntegerDef::class ], |
| 67 |
'float' => [ 'class' => FloatDef::class ], |
| 68 |
'double' => [ 'class' => FloatDef::class ], |
| 69 |
'NULL' => [ |
| 70 |
'class' => StringDef::class, |
| 71 |
'args' => [ [ |
| 72 |
StringDef::OPT_ALLOW_EMPTY => true, |
| 73 |
] ], |
| 74 |
], |
| 75 |
'password' => [ 'class' => PasswordDef::class ], |
| 76 |
'string' => [ 'class' => StringDef::class ], |
| 77 |
'timestamp' => [ 'class' => TimestampDef::class ], |
| 78 |
'upload' => [ 'class' => UploadDef::class ], |
| 79 |
'expiry' => [ 'class' => ExpiryDef::class ], |
| 80 |
'title' => [ |
| 81 |
'class' => TitleDef::class, |
| 82 |
'services' => [ 'TitleFactory' ], |
| 83 |
], |
| 84 |
'user' => [ |
| 85 |
'class' => UserDef::class, |
| 86 |
'services' => [ 'UserIdentityLookup', 'TitleParser', 'UserNameUtils' ] |
| 87 |
], |
| 88 |
'array' => [ |
| 89 |
'class' => ArrayDef::class, |
| 90 |
], |
| 91 |
]; |
| 92 |
|
| 93 |
|
| 94 |
private const NO_BODY_METHODS = [ 'GET', 'HEAD' ]; |
| 95 |
|
| 96 |
|
| 97 |
private const BODY_METHODS = [ 'POST', 'PUT' ]; |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
private const FORM_DATA_CONTENT_TYPES = [ |
| 105 |
'application/x-www-form-urlencoded', |
| 106 |
'multipart/form-data', |
| 107 |
]; |
| 108 |
|
| 109 |
private ParamValidator $paramValidator; |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public function __construct( |
| 118 |
ObjectFactory $objectFactory, |
| 119 |
RequestInterface $request, |
| 120 |
Authority $authority |
| 121 |
) { |
| 122 |
$this->paramValidator = new ParamValidator( |
| 123 |
new ParamValidatorCallbacks( $request, $authority ), |
| 124 |
$objectFactory, |
| 125 |
[ |
| 126 |
'typeDefs' => self::TYPE_DEFS, |
| 127 |
] |
| 128 |
); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public function validateParams( array $paramSettings ) { |
| 142 |
$validatedParams = []; |
| 143 |
foreach ( $paramSettings as $name => $settings ) { |
| 144 |
try { |
| 145 |
$source = $settings[Handler::PARAM_SOURCE] ?? 'unspecified'; |
| 146 |
if ( $source === 'body' ) { |
| 147 |
continue; |
| 148 |
} |
| 149 |
|
| 150 |
$validatedParams[$name] = $this->paramValidator->getValue( $name, $settings, [ |
| 151 |
'source' => $source, |
| 152 |
] ); |
| 153 |
} catch ( ValidationException $e ) { |
| 154 |
|
| 155 |
throw new LocalizedHttpException( $e->getFailureMessage(), 400, [ |
| 156 |
'error' => 'parameter-validation-failed', |
| 157 |
'name' => $e->getParamName(), |
| 158 |
'value' => $e->getParamValue(), |
| 159 |
'failureCode' => $e->getFailureMessage()->getCode(), |
| 160 |
'failureData' => $e->getFailureMessage()->getData(), |
| 161 |
] ); |
| 162 |
} |
| 163 |
} |
| 164 |
return $validatedParams; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
public function detectExtraneousBodyFields( array $paramSettings, array $parsedBody ) { |
| 181 |
$validatedKeys = []; |
| 182 |
$remainingBodyFields = $parsedBody; |
| 183 |
foreach ( $paramSettings as $name => $settings ) { |
| 184 |
$source = $settings[Handler::PARAM_SOURCE] ?? 'unspecified'; |
| 185 |
|
| 186 |
if ( $source !== 'body' ) { |
| 187 |
continue; |
| 188 |
} |
| 189 |
|
| 190 |
$validatedKeys[] = $name; |
| 191 |
unset( $remainingBodyFields[$name] ); |
| 192 |
} |
| 193 |
$unvalidatedKeys = array_keys( $remainingBodyFields ); |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
if ( $validatedKeys && $unvalidatedKeys ) { |
| 202 |
throw new LocalizedHttpException( |
| 203 |
new MessageValue( |
| 204 |
'rest-extraneous-body-fields', |
| 205 |
[ new ListParam( ListType::COMMA, $unvalidatedKeys ) ] |
| 206 |
), |
| 207 |
400, |
| 208 |
[ |
| 209 |
'error' => 'parameter-validation-failed', |
| 210 |
'failureCode' => 'extraneous-body-fields', |
| 211 |
'name' => reset( $unvalidatedKeys ), |
| 212 |
'failureData' => $unvalidatedKeys, |
| 213 |
] |
| 214 |
); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
public function validateBodyParams( array $paramSettings, bool $enforceTypes = true ) { |
| 235 |
$validatedParams = []; |
| 236 |
foreach ( $paramSettings as $name => $settings ) { |
| 237 |
$source = $settings[Handler::PARAM_SOURCE] ?? 'body'; |
| 238 |
if ( $source !== 'body' ) { |
| 239 |
continue; |
| 240 |
} |
| 241 |
|
| 242 |
try { |
| 243 |
$validatedParams[ $name ] = $this->paramValidator->getValue( |
| 244 |
$name, |
| 245 |
$settings, |
| 246 |
[ |
| 247 |
'source' => $source, |
| 248 |
TypeDef::OPT_ENFORCE_JSON_TYPES => $enforceTypes, |
| 249 |
StringDef::OPT_ALLOW_EMPTY => $enforceTypes, |
| 250 |
] |
| 251 |
); |
| 252 |
} catch ( ValidationException $e ) { |
| 253 |
$msg = $e->getFailureMessage(); |
| 254 |
$wrappedMsg = new MessageValue( |
| 255 |
'rest-body-validation-error', |
| 256 |
[ $e->getFailureMessage() ] |
| 257 |
); |
| 258 |
|
| 259 |
|
| 260 |
throw new LocalizedHttpException( $wrappedMsg, 400, [ |
| 261 |
'error' => 'parameter-validation-failed', |
| 262 |
'name' => $e->getParamName(), |
| 263 |
'value' => $e->getParamValue(), |
| 264 |
'failureCode' => $msg->getCode(), |
| 265 |
'failureData' => $msg->getData(), |
| 266 |
] ); |
| 267 |
} |
| 268 |
} |
| 269 |
return $validatedParams; |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
public function validateBody( RequestInterface $request, Handler $handler ) { |
| 287 |
wfDeprecated( __METHOD__, '1.43' ); |
| 288 |
|
| 289 |
$method = strtoupper( trim( $request->getMethod() ) ); |
| 290 |
|
| 291 |
|
| 292 |
if ( in_array( $method, self::NO_BODY_METHODS, true ) ) { |
| 293 |
return null; |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
[ $ct ] = explode( ';', $request->getHeaderLine( 'Content-Type' ), 2 ); |
| 298 |
$ct = strtolower( trim( $ct ) ); |
| 299 |
if ( $ct === '' ) { |
| 300 |
t |
| 301 |
|
| 302 |
|
| 303 |
$body = $request->getBody(); |
| 304 |
if ( $body->getSize() === 0 ) { |
| 305 |
return null; |
| 306 |
} else { |
| 307 |
throw new LocalizedHttpException( new MessageValue( "rest-requires-content-type-header" ), 415, [ |
| 308 |
'error' => 'no-content-type', |
| 309 |
] ); |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
if ( in_array( $ct, RequestInterface::FORM_DATA_CONTENT_TYPES, true ) ) { |
| 316 |
return null; |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
return $handler->getBodyValidator( $ct )->validateBody( $request ); |
| 321 |
} |
| 322 |
|
| 323 |
private const PARAM_TYPE_SCHEMAS = [ |
| 324 |
'boolean-param' => [ 'type' => 'boolean' ], |
| 325 |
'enum-param' => [ 'type' => 'string' ], |
| 326 |
'integer-param' => [ 'type' => 'integer' ], |
| 327 |
'float-param' => [ 'type' => 'number', 'format' => 'float' ], |
| 328 |
'double-param' => [ 'type' => 'number', 'format' => 'double' ], |
| 329 |
|
| 330 |
'password-param' => [ 'type' => 'string' ], |
| 331 |
'string-param' => [ 'type' => 'string' ], |
| 332 |
'timestamp-param' => [ 'type' => 'string', 'format' => 'mw-timestamp' ], |
| 333 |
'upload-param' => [ 'type' => 'string', 'format' => 'mw-upload' ], |
| 334 |
'expiry-param' => [ 'type' => 'string', 'format' => 'mw-expiry' ], |
| 335 |
'title-param' => [ 'type' => 'string', 'format' => 'mw-title' ], |
| 336 |
'user-param' => [ 'type' => 'string', 'format' => 'mw-user' ], |
| 337 |
'array-param' => [ 'type' => 'object' ], |
| 338 |
]; |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
public static function getParameterTypeSchemas(): array { |
| 350 |
return self::PARAM_TYPE_SCHEMAS; |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
public static function getParameterSpec( string $name, array $paramSetting ): array { |
| 363 |
$schema = self::getParameterSchema( $paramSetting ); |
| 364 |
|
| 365 |
|
| 366 |
$location = $paramSetting[ self::PARAM_SOURCE ] ?? 'unspecified'; |
| 367 |
|
| 368 |
$param = [ |
| 369 |
'name' => $name, |
| 370 |
'description' => $paramSetting[ self::PARAM_DESCRIPTION ] ?? "$name parameter", |
| 371 |
'in' => $location, |
| 372 |
'schema' => $schema |
| 373 |
]; |
| 374 |
|
| 375 |
|
| 376 |
$param['required'] = $location === 'path' |
| 377 |
|| ( $paramSetting[ ParamValidator::PARAM_REQUIRED ] ?? false ); |
| 378 |
|
| 379 |
return $param; |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
public static function getParameterSchema( array $paramSetting ): array { |
| 391 |
$type = $paramSetting[ ParamValidator::PARAM_TYPE ] ?? 'string'; |
| 392 |
|
| 393 |
if ( is_array( $type ) ) { |
| 394 |
if ( $type === [] ) { |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
$type = [ '' ]; |
| 399 |
} |
| 400 |
|
| 401 |
$schema = [ |
| 402 |
'type' => 'string', |
| 403 |
'enum' => $type |
| 404 |
]; |
| 405 |
} elseif ( isset( $paramSetting[ ArrayDef::PARAM_SCHEMA ] ) ) { |
| 406 |
$schema = $paramSetting[ ArrayDef::PARAM_SCHEMA ]; |
| 407 |
} else { |
| 408 |
|
| 409 |
$schema = self::PARAM_TYPE_SCHEMAS["{$type}-param"] ?? []; |
| 410 |
} |
| 411 |
|
| 412 |
return $schema; |
| 413 |
} |
| 414 |
|
| 415 |
} |