Skip to content

Commit

Permalink
Rewrite OCS CSRF check to be readable
Browse files Browse the repository at this point in the history
Signed-off-by: jld3103 <jld3103yt@gmail.com>
  • Loading branch information
provokateurin committed Aug 16, 2023
1 parent f3fa006 commit 12f8543
Showing 1 changed file with 15 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public function beforeController($controller, $methodName) {
}
// CSRF check - also registers the CSRF token since the session may be closed later
Util::callRegister();
if (!$this->hasAnnotationOrAttribute($reflectionMethod, 'NoCSRFRequired', NoCSRFRequired::class)) {
if ($this->isInvalidCSRFRequired($reflectionMethod)) {
/*
* Only allow the CSRF check to fail on OCS Requests. This kind of
* hacks around that we have no full token auth in place yet and we
Expand All @@ -215,12 +215,7 @@ public function beforeController($controller, $methodName) {
* Additionally we allow Bearer authenticated requests to pass on OCS routes.
* This allows oauth apps (e.g. moodle) to use the OCS endpoints
*/
if (!$this->request->passesCSRFCheck() && !(
$controller instanceof OCSController && (
$this->request->getHeader('OCS-APIREQUEST') === 'true' ||
str_starts_with($this->request->getHeader('Authorization'), 'Bearer ')
)
)) {
if (!$controller instanceof OCSController || !$this->isValidOCSRequest()) {
throw new CrossSiteRequestForgeryException();
}
}
Expand All @@ -242,6 +237,19 @@ public function beforeController($controller, $methodName) {
}
}

private function isInvalidCSRFRequired(ReflectionMethod $reflectionMethod): bool {
if ($this->hasAnnotationOrAttribute($reflectionMethod, 'NoCSRFRequired', NoCSRFRequired::class)) {
return false;
}

return !$this->request->passesCSRFCheck();
}

private function isValidOCSRequest(): bool {
return $this->request->getHeader('OCS-APIREQUEST') === 'true'
|| str_starts_with($this->request->getHeader('Authorization'), 'Bearer ');
}

/**
* @template T
*
Expand Down

0 comments on commit 12f8543

Please sign in to comment.