Skip to content

Commit 102c827

Browse files
committed
feat: referer -> origin fallback
Signed-off-by: romanetar <roman_ag@hotmail.com>
1 parent fd2a84a commit 102c827

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

app/Http/Middleware/OAuth2BearerAccessTokenRequestValidator.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,8 @@ public function handle($request, Closure $next)
132132
}
133133

134134
Log::debug($request->headers->__toString());
135-
// http://tools.ietf.org/id/draft-abarth-origin-03.html
136-
$origin = $request->headers->has('Origin') ? $request->headers->get('Origin') : null;
137-
if (!empty($origin)) {
138-
$nm = new Normalizer($origin);
139-
$origin = $nm->normalize();
140-
}
135+
136+
$origin = RequestUtils::getOrigin($request);
141137

142138
//check first http basic auth header
143139
$auth_header = isset($this->headers['authorization']) ? $this->headers['authorization'] : null;

app/libs/Utils/RequestUtils.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,22 @@
1111
* See the License for the specific language governing permissions and
1212
* limitations under the License.
1313
**/
14+
15+
use Illuminate\Http\Request;
1416
use Illuminate\Support\Facades\Route;
1517
use Illuminate\Support\Facades\Log;
18+
use OAuth2\Exceptions\OAuth2ResourceServerException;
19+
use OAuth2\OAuth2Protocol;
20+
use URL\Normalizer;
21+
1622
/**
1723
* Class RequestUtils
1824
* @package libs\utils
1925
*/
2026
final class RequestUtils {
2127

2228
/**
23-
* @param \Illuminate\Http\Request $request
29+
* @param Request $request
2430
* @return bool|string
2531
*/
2632
public static function getCurrentRoutePath($request)
@@ -42,4 +48,37 @@ public static function getCurrentRoutePath($request)
4248
return false;
4349
}
4450

51+
/**
52+
* @param Request $request
53+
* @return string|null
54+
* @throws OAuth2ResourceServerException
55+
*/
56+
public static function getOrigin(Request $request): ?string
57+
{
58+
// http://tools.ietf.org/id/draft-abarth-origin-03.html
59+
$origin = $request->headers->get('Origin');
60+
$referer = $request->headers->get('Referer');
61+
62+
if (!empty($origin) && !empty($referer) &&
63+
parse_url($origin, PHP_URL_HOST) != parse_url($referer, PHP_URL_HOST))
64+
{
65+
Log::warning('OAuth2BearerAccessTokenRequestValidator::handle Origin and Referrer mismatch');
66+
throw new OAuth2ResourceServerException(
67+
403,
68+
OAuth2Protocol::OAuth2Protocol_Error_InvalidRequest,
69+
'Origin and Referrer mismatch'
70+
);
71+
}
72+
if (empty($origin) && !empty($referer)) {
73+
$referer_parts = parse_url($referer);
74+
$origin = $referer_parts['scheme'] . '://' . $referer_parts['host'];
75+
if (!empty($origin)) {
76+
Log::warning('OAuth2BearerAccessTokenRequestValidator::Origin header not present. Using normalized Referer as fallback: ' . $origin);
77+
}
78+
}
79+
if (!empty($origin)) {
80+
$origin = (new Normalizer($origin))->normalize();
81+
}
82+
return $origin;
83+
}
4584
}

0 commit comments

Comments
 (0)