Skip to content

Commit 0da9ae7

Browse files
committed
refactor: early return
1 parent ba09b87 commit 0da9ae7

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

‎system/HTTP/Cors.php‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,18 @@ private function setAllowOrigin(RequestInterface $request, ResponseInterface $re
128128
$this->checkWildcard('allowedOrigins', $originCount);
129129
$this->checkWildcardAndCredentials('allowedOrigins', 'Access-Control-Allow-Origin');
130130

131+
// Single Origin.
131132
if ($originCount === 1 && $originPatternCount === 0) {
132133
$response->setHeader('Access-Control-Allow-Origin', $this->config['allowedOrigins'][0]);
133134

134135
return;
135136
}
136137

138+
// Multiple Origins.
139+
if (! $request->hasHeader('Origin')) {
140+
return;
141+
}
142+
137143
$origin = $request->getHeaderLine('Origin');
138144

139145
if ($originCount > 1 && in_array($origin, $this->config['allowedOrigins'], true)) {
@@ -150,6 +156,8 @@ private function setAllowOrigin(RequestInterface $request, ResponseInterface $re
150156
if (preg_match($regex, $origin)) {
151157
$response->setHeader('Access-Control-Allow-Origin', $origin);
152158
$response->appendHeader('Vary', 'Origin');
159+
160+
return;
153161
}
154162
}
155163
}

‎tests/system/Filters/CorsTest.php‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,18 @@ public function testItDoesModifyOnARequestWithoutOrigin()
120120
);
121121
}
122122

123+
public function testItDoesNotModifyOnARequestWithoutOrigin()
124+
{
125+
$this->cors = $this->createCors([
126+
'allowedOrigins' => ['https://www.example.com', 'https://app.example.com'],
127+
]);
128+
$request = $this->createRequest();
129+
130+
$response = $this->handle($request);
131+
132+
$this->assertFalse($response->hasHeader('Access-Control-Allow-Origin'));
133+
}
134+
123135
private function handle($request): ResponseInterface
124136
{
125137
$response = $this->cors->before($request);

0 commit comments

Comments
 (0)