99
1010namespace OC \AppFramework \Middleware \Security ;
1111
12- use OC \AppFramework \Middleware \MiddlewareUtils ;
1312use OC \AppFramework \Middleware \Security \Exceptions \AdminIpNotAllowedException ;
1413use OC \AppFramework \Middleware \Security \Exceptions \AppNotEnabledException ;
1514use OC \AppFramework \Middleware \Security \Exceptions \CrossSiteRequestForgeryException ;
1918use OC \AppFramework \Middleware \Security \Exceptions \NotLoggedInException ;
2019use OC \AppFramework \Middleware \Security \Exceptions \SecurityException ;
2120use OC \AppFramework \Middleware \Security \Exceptions \StrictCookieMissingException ;
21+ use OC \AppFramework \Utility \ControllerMethodReflector ;
2222use OC \Security \CSRF \CsrfTokenManager ;
2323use OC \Settings \AuthorizedGroupMapper ;
2424use OC \User \Session ;
@@ -64,7 +64,7 @@ class SecurityMiddleware extends Middleware {
6464
6565 public function __construct (
6666 private readonly IRequest $ request ,
67- private readonly MiddlewareUtils $ middlewareUtils ,
67+ private readonly ControllerMethodReflector $ reflector ,
6868 private readonly INavigationManager $ navigationManager ,
6969 private readonly IURLGenerator $ urlGenerator ,
7070 private readonly LoggerInterface $ logger ,
@@ -118,18 +118,16 @@ public function beforeController(Controller $controller, string $methodName): vo
118118 $ this ->navigationManager ->setActiveEntry ('spreed ' );
119119 }
120120
121- $ reflectionMethod = new ReflectionMethod ($ controller , $ methodName );
122-
123121 // security checks
124- $ isPublicPage = $ this ->middlewareUtils ->hasAnnotationOrAttribute ($ reflectionMethod , 'PublicPage ' , PublicPage::class);
122+ $ isPublicPage = $ this ->reflector ->hasAnnotationOrAttribute ('PublicPage ' , PublicPage::class);
125123
126- if ($ this ->middlewareUtils ->hasAnnotationOrAttribute ($ reflectionMethod , 'ExAppRequired ' , ExAppRequired::class)) {
124+ if ($ this ->reflector ->hasAnnotationOrAttribute ('ExAppRequired ' , ExAppRequired::class)) {
127125 if (!$ this ->userSession instanceof Session || $ this ->userSession ->getSession ()->get ('app_api ' ) !== true ) {
128126 throw new ExAppRequiredException ();
129127 }
130128 } elseif (!$ isPublicPage ) {
131129 $ authorized = false ;
132- if ($ this ->middlewareUtils ->hasAnnotationOrAttribute ($ reflectionMethod , null , AppApiAdminAccessWithoutUser::class)) {
130+ if ($ this ->reflector ->hasAnnotationOrAttribute (null , AppApiAdminAccessWithoutUser::class)) {
133131 // this attribute allows ExApp to access admin endpoints only if "userId" is "null"
134132 if ($ this ->userSession instanceof Session && $ this ->userSession ->getSession ()->get ('app_api ' ) === true && $ this ->userSession ->getUser () === null ) {
135133 $ authorized = true ;
@@ -140,15 +138,15 @@ public function beforeController(Controller $controller, string $methodName): vo
140138 throw new NotLoggedInException ();
141139 }
142140
143- if (!$ authorized && $ this ->middlewareUtils ->hasAnnotationOrAttribute ($ reflectionMethod , 'AuthorizedAdminSetting ' , AuthorizedAdminSetting::class)) {
141+ if (!$ authorized && $ this ->reflector ->hasAnnotationOrAttribute ('AuthorizedAdminSetting ' , AuthorizedAdminSetting::class)) {
144142 $ authorized = $ this ->isAdminUser ();
145143
146- if (!$ authorized && $ this ->middlewareUtils ->hasAnnotationOrAttribute ($ reflectionMethod , 'SubAdminRequired ' , SubAdminRequired::class)) {
144+ if (!$ authorized && $ this ->reflector ->hasAnnotationOrAttribute ('SubAdminRequired ' , SubAdminRequired::class)) {
147145 $ authorized = $ this ->isSubAdmin ();
148146 }
149147
150148 if (!$ authorized ) {
151- $ settingClasses = $ this ->middlewareUtils -> getAuthorizedAdminSettingClasses ($ reflectionMethod );
149+ $ settingClasses = $ this ->getAuthorizedAdminSettingClasses ();
152150 $ authorizedClasses = $ this ->groupAuthorizationMapper ->findAllClassesForUser ($ this ->userSession ->getUser ());
153151 foreach ($ settingClasses as $ settingClass ) {
154152 $ authorized = in_array ($ settingClass , $ authorizedClasses , true );
@@ -165,40 +163,40 @@ public function beforeController(Controller $controller, string $methodName): vo
165163 throw new AdminIpNotAllowedException ($ this ->l10n ->t ('Your current IP address doesn \'t allow you to perform admin actions ' ));
166164 }
167165 }
168- if ($ this ->middlewareUtils ->hasAnnotationOrAttribute ($ reflectionMethod , 'SubAdminRequired ' , SubAdminRequired::class)
166+ if ($ this ->reflector ->hasAnnotationOrAttribute ('SubAdminRequired ' , SubAdminRequired::class)
169167 && !$ this ->isSubAdmin ()
170168 && !$ this ->isAdminUser ()
171169 && !$ authorized ) {
172170 throw new NotAdminException ($ this ->l10n ->t ('Logged in account must be an admin or sub admin ' ));
173171 }
174- if (!$ this ->middlewareUtils ->hasAnnotationOrAttribute ($ reflectionMethod , 'SubAdminRequired ' , SubAdminRequired::class)
175- && !$ this ->middlewareUtils ->hasAnnotationOrAttribute ($ reflectionMethod , 'NoAdminRequired ' , NoAdminRequired::class)
172+ if (!$ this ->reflector ->hasAnnotationOrAttribute ('SubAdminRequired ' , SubAdminRequired::class)
173+ && !$ this ->reflector ->hasAnnotationOrAttribute ('NoAdminRequired ' , NoAdminRequired::class)
176174 && !$ this ->isAdminUser ()
177175 && !$ authorized ) {
178176 throw new NotAdminException ($ this ->l10n ->t ('Logged in account must be an admin ' ));
179177 }
180- if ($ this ->middlewareUtils ->hasAnnotationOrAttribute ($ reflectionMethod , 'SubAdminRequired ' , SubAdminRequired::class)
178+ if ($ this ->reflector ->hasAnnotationOrAttribute ('SubAdminRequired ' , SubAdminRequired::class)
181179 && !$ this ->remoteAddress ->allowsAdminActions ()) {
182180 throw new AdminIpNotAllowedException ($ this ->l10n ->t ('Your current IP address doesn \'t allow you to perform admin actions ' ));
183181 }
184- if (!$ this ->middlewareUtils ->hasAnnotationOrAttribute ($ reflectionMethod , 'SubAdminRequired ' , SubAdminRequired::class)
185- && !$ this ->middlewareUtils ->hasAnnotationOrAttribute ($ reflectionMethod , 'NoAdminRequired ' , NoAdminRequired::class)
182+ if (!$ this ->reflector ->hasAnnotationOrAttribute ('SubAdminRequired ' , SubAdminRequired::class)
183+ && !$ this ->reflector ->hasAnnotationOrAttribute ('NoAdminRequired ' , NoAdminRequired::class)
186184 && !$ this ->remoteAddress ->allowsAdminActions ()) {
187185 throw new AdminIpNotAllowedException ($ this ->l10n ->t ('Your current IP address doesn \'t allow you to perform admin actions ' ));
188186 }
189187
190188 }
191189
192190 // Check for strict cookie requirement
193- if ($ this ->middlewareUtils ->hasAnnotationOrAttribute ($ reflectionMethod , 'StrictCookieRequired ' , StrictCookiesRequired::class)
194- || !$ this ->middlewareUtils ->hasAnnotationOrAttribute ($ reflectionMethod , 'NoCSRFRequired ' , NoCSRFRequired::class)) {
191+ if ($ this ->reflector ->hasAnnotationOrAttribute ('StrictCookieRequired ' , StrictCookiesRequired::class)
192+ || !$ this ->reflector ->hasAnnotationOrAttribute ('NoCSRFRequired ' , NoCSRFRequired::class)) {
195193 if (!$ this ->request ->passesStrictCookieCheck ()) {
196194 throw new StrictCookieMissingException ();
197195 }
198196 }
199197 // CSRF check - also registers the CSRF token since the session may be closed later
200198 Server::get (CsrfTokenManager::class)->generateSessionToken ();
201- if ($ this ->isInvalidCSRFRequired ($ reflectionMethod )) {
199+ if ($ this ->isInvalidCSRFRequired ()) {
202200 /*
203201 * Only allow the CSRF check to fail on OCS Requests. This kind of
204202 * hacks around that we have no full token auth in place yet and we
@@ -229,8 +227,8 @@ public function beforeController(Controller $controller, string $methodName): vo
229227 }
230228 }
231229
232- private function isInvalidCSRFRequired (ReflectionMethod $ reflectionMethod ): bool {
233- if ($ this ->middlewareUtils ->hasAnnotationOrAttribute ($ reflectionMethod , 'NoCSRFRequired ' , NoCSRFRequired::class)) {
230+ private function isInvalidCSRFRequired (): bool {
231+ if ($ this ->reflector ->hasAnnotationOrAttribute ('NoCSRFRequired ' , NoCSRFRequired::class)) {
234232 return false ;
235233 }
236234
@@ -296,4 +294,24 @@ public function afterException(Controller $controller, string $methodName, \Exce
296294
297295 throw $ exception ;
298296 }
297+
298+ /**
299+ * @param ReflectionMethod $reflectionMethod
300+ * @return string[]
301+ */
302+ public function getAuthorizedAdminSettingClasses (): array {
303+ $ classes = [];
304+ if ($ this ->reflector ->hasAnnotation ('AuthorizedAdminSetting ' )) {
305+ $ classes = explode ('; ' , $ this ->reflector ->getAnnotationParameter ('AuthorizedAdminSetting ' , 'settings ' ));
306+ }
307+
308+ $ attribute = $ this ->reflector ->getAttribute (AuthorizedAdminSetting::class);
309+ if ($ attribute !== null ) {
310+ /** @var AuthorizedAdminSetting $setting */
311+ $ setting = $ attribute ->newInstance ();
312+ $ classes [] = $ setting ->getSettings ();
313+ }
314+
315+ return $ classes ;
316+ }
299317}
0 commit comments