11<?php
22
3+ declare (strict_types=1 );
4+
35/**
46 * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
57 * SPDX-License-Identifier: AGPL-3.0-or-later
1012use OC \AppFramework \Middleware \Security \Exceptions \LaxSameSiteCookieFailedException ;
1113use OC \AppFramework \Utility \ControllerMethodReflector ;
1214use OCP \AppFramework \Http ;
15+ use OCP \AppFramework \Http \Attribute \NoSameSiteCookieRequired ;
1316use OCP \AppFramework \Http \Response ;
1417use OCP \AppFramework \Middleware ;
18+ use Psr \Log \LoggerInterface ;
19+ use ReflectionMethod ;
1520
1621class SameSiteCookieMiddleware extends Middleware {
1722 public function __construct (
18- private Request $ request ,
19- private ControllerMethodReflector $ reflector ,
23+ private readonly Request $ request ,
24+ private readonly ControllerMethodReflector $ reflector ,
25+ private readonly LoggerInterface $ logger ,
2026 ) {
2127 }
2228
@@ -29,7 +35,8 @@ public function beforeController($controller, $methodName) {
2935 return ;
3036 }
3137
32- $ noSSC = $ this ->reflector ->hasAnnotation ('NoSameSiteCookieRequired ' );
38+ $ reflectionMethod = new ReflectionMethod ($ controller , $ methodName );
39+ $ noSSC = $ this ->hasAnnotationOrAttribute ($ reflectionMethod , 'NoSameSiteCookieRequired ' , NoSameSiteCookieRequired::class);
3340 if ($ noSSC ) {
3441 return ;
3542 }
@@ -80,4 +87,25 @@ protected function setSameSiteCookie(): void {
8087 );
8188 }
8289 }
90+
91+ /**
92+ * @template T
93+ *
94+ * @param ReflectionMethod $reflectionMethod
95+ * @param ?string $annotationName
96+ * @param class-string<T> $attributeClass
97+ * @return boolean
98+ */
99+ protected function hasAnnotationOrAttribute (ReflectionMethod $ reflectionMethod , ?string $ annotationName , string $ attributeClass ): bool {
100+ if (!empty ($ reflectionMethod ->getAttributes ($ attributeClass ))) {
101+ return true ;
102+ }
103+
104+ if ($ annotationName && $ this ->reflector ->hasAnnotation ($ annotationName )) {
105+ $ this ->logger ->debug ($ reflectionMethod ->getDeclaringClass ()->getName () . ':: ' . $ reflectionMethod ->getName () . ' uses the @ ' . $ annotationName . ' annotation and should use the #[ ' . $ attributeClass . '] attribute instead ' );
106+ return true ;
107+ }
108+
109+ return false ;
110+ }
83111}
0 commit comments