35 $this->options = $options;
36 $this->responseFactory = $responseFactory;
54 && !$this->user->isRegistered()
56 $origin = Origin::parseHeaderList( $request->
getHeader(
'Origin' ) );
58 if ( !$this->allowOrigin( $origin ) ) {
59 return 'rest-cross-origin-anon-write';
66 private function allowOrigin(
Origin $origin ): bool {
67 $allowed = array_merge( [ $this->getCanonicalDomain() ],
71 return $origin->
match( $allowed, $excluded );
74 private function getCanonicalDomain(): string {
75 $res = parse_url( $this->options->get(
MainConfigNames::CanonicalServer ) );
76 '@phan-var array $res';
78 $host = $res[
'host'] ??
'';
79 $port = $res[
'port'] ??
null;
81 return $port ?
"$host:$port" : $host;
101 if ( $this->options->get( MainConfigNames::RestAllowCrossOriginCookieAuth ) ) {
110 $response->
addHeader(
'Vary',
'Origin' );
113 if ( $request->hasHeader(
'Origin' ) ) {
114 $origin = Origin::parseHeaderList( $request->getHeader(
'Origin' ) );
115 if ( $this->allowOrigin( $origin ) ) {
119 if ( $request->getMethod() ===
'OPTIONS' || $this->user->isRegistered() ) {
134 if ( $allowedOrigin !==
'*' ) {
135 $response->setHeader(
'Access-Control-Allow-Credentials',
'true' );
138 $response->setHeader(
'Access-Control-Allow-Origin', $allowedOrigin );
150 $response = $this->responseFactory->createNoContent();
151 $response->
setHeader(
'Access-Control-Allow-Methods', $allowedMethods );
153 $allowedHeaders = $this->options->get( MainConfigNames::AllowedCorsHeaders );
154 $allowedHeaders = array_merge( $allowedHeaders, array_diff( [
163 ], $allowedHeaders ) );
164 $response->setHeader(
'Access-Control-Allow-Headers', $allowedHeaders );
if(!defined('MW_SETUP_CALLBACK'))
A class containing constants representing the names of configuration variables.
const CrossSiteAJAXdomainExceptions
Name constant for the CrossSiteAJAXdomainExceptions setting, for use with Config::get()
const CanonicalServer
Name constant for the CanonicalServer setting, for use with Config::get()
const AllowCrossOrigin
Name constant for the AllowCrossOrigin setting, for use with Config::get()
const AllowedCorsHeaders
Name constant for the AllowedCorsHeaders setting, for use with Config::get()
const CrossSiteAJAXdomains
Name constant for the CrossSiteAJAXdomains setting, for use with Config::get()
const RestAllowCrossOriginCookieAuth
Name constant for the RestAllowCrossOriginCookieAuth setting, for use with Config::get()