| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Rest; |
| 4 |
|
| 5 |
use MediaWiki\Config\ServiceOptions; |
| 6 |
use MediaWiki\MainConfigNames; |
| 7 |
use MediaWiki\Rest\BasicAccess\BasicAuthorizerInterface; |
| 8 |
use MediaWiki\Rest\HeaderParser\Origin; |
| 9 |
use MediaWiki\User\UserIdentity; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
class CorsUtils implements BasicAuthorizerInterface { |
| 15 |
|
| 16 |
public const CONSTRUCTOR_OPTIONS = [ |
| 17 |
MainConfigNames::AllowedCorsHeaders, |
| 18 |
MainConfigNames::AllowCrossOrigin, |
| 19 |
MainConfigNames::RestAllowCrossOriginCookieAuth, |
| 20 |
MainConfigNames::CanonicalServer, |
| 21 |
MainConfigNames::CrossSiteAJAXdomains, |
| 22 |
MainConfigNames::CrossSiteAJAXdomainExceptions, |
| 23 |
]; |
| 24 |
|
| 25 |
private ServiceOptions $options; |
| 26 |
private ResponseFactory $responseFactory; |
| 27 |
private UserIdentity $user; |
| 28 |
|
| 29 |
public function __construct( |
| 30 |
ServiceOptions $options, |
| 31 |
ResponseFactory $responseFactory, |
| 32 |
UserIdentity $user |
| 33 |
) { |
| 34 |
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); |
| 35 |
$this->options = $options; |
| 36 |
$this->responseFactory = $responseFactory; |
| 37 |
$this->user = $user; |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public function authorize( RequestInterface $request, Handler $handler ) { |
| 49 |
|
| 50 |
|
| 51 |
if ( |
| 52 |
$handler->needsWriteAccess() |
| 53 |
&& $request->hasHeader( 'Origin' ) |
| 54 |
&& !$this->user->isRegistered() |
| 55 |
) { |
| 56 |
$origin = Origin::parseHeaderList( $request->getHeader( 'Origin' ) ); |
| 57 |
|
| 58 |
if ( !$this->allowOrigin( $origin ) ) { |
| 59 |
return 'rest-cross-origin-anon-write'; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
return null; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
private function allowOrigin( Origin $origin ): bool { |
| 71 |
$allowed = array_merge( [ $this->getCanonicalDomain() ], |
| 72 |
$this->options->get( MainConfigNames::CrossSiteAJAXdomains ) ); |
| 73 |
$excluded = $this->options->get( MainConfigNames::CrossSiteAJAXdomainExceptions ); |
| 74 |
|
| 75 |
return $origin->match( $allowed, $excluded ); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
private function getCanonicalDomain(): string { |
| 82 |
$res = parse_url( $this->options->get( MainConfigNames::CanonicalServer ) ); |
| 83 |
'@phan-var array $res'; |
| 84 |
|
| 85 |
$host = $res['host'] ?? ''; |
| 86 |
$port = $res['port'] ?? null; |
| 87 |
|
| 88 |
return $port ? "$host:$port" : $host; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public function modifyResponse( RequestInterface $request, ResponseInterface $response ): ResponseInterface { |
| 102 |
if ( !$this->options->get( MainConfigNames::AllowCrossOrigin ) ) { |
| 103 |
return $response; |
| 104 |
} |
| 105 |
|
| 106 |
$allowedOrigin = '*'; |
| 107 |
|
| 108 |
if ( $this->options->get( MainConfigNames::RestAllowCrossOriginCookieAuth ) ) { |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
$response->addHeader( 'Vary', 'Origin' ); |
| 118 |
|
| 119 |
|
| 120 |
if ( $request->hasHeader( 'Origin' ) ) { |
| 121 |
$origin = Origin::parseHeaderList( $request->getHeader( 'Origin' ) ); |
| 122 |
if ( $this->allowOrigin( $origin ) ) { |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
if ( $request->getMethod() === 'OPTIONS' || $this->user->isRegistered() ) { |
| 127 |
$allowedOrigin = $origin->getSingleOrigin(); |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
if ( $allowedOrigin !== '*' ) { |
| 142 |
$response->setHeader( 'Access-Control-Allow-Credentials', 'true' ); |
| 143 |
} |
| 144 |
|
| 145 |
$response->setHeader( 'Access-Control-Allow-Origin', $allowedOrigin ); |
| 146 |
|
| 147 |
return $response; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
public function createPreflightResponse( array $allowedMethods ): Response { |
| 157 |
$response = $this->responseFactory->createNoContent(); |
| 158 |
$response->setHeader( 'Access-Control-Allow-Methods', $allowedMethods ); |
| 159 |
|
| 160 |
$allowedHeaders = $this->options->get( MainConfigNames::AllowedCorsHeaders ); |
| 161 |
$allowedHeaders = array_merge( $allowedHeaders, array_diff( [ |
| 162 |
|
| 163 |
'Authorization', |
| 164 |
|
| 165 |
'Content-Type', |
| 166 |
|
| 167 |
'If-Mach', |
| 168 |
'If-None-Match', |
| 169 |
'If-Modified-Since', |
| 170 |
], $allowedHeaders ) ); |
| 171 |
$response->setHeader( 'Access-Control-Allow-Headers', $allowedHeaders ); |
| 172 |
|
| 173 |
return $response; |
| 174 |
} |
| 175 |
} |