88 */
99namespace OC \AppFramework \Middleware \Security ;
1010
11+ use OC \AppFramework \Middleware \Security \Exceptions \AdminIpNotAllowedException ;
1112use OC \AppFramework \Middleware \Security \Exceptions \AppNotEnabledException ;
1213use OC \AppFramework \Middleware \Security \Exceptions \CrossSiteRequestForgeryException ;
1314use OC \AppFramework \Middleware \Security \Exceptions \ExAppRequiredException ;
1617use OC \AppFramework \Middleware \Security \Exceptions \SecurityException ;
1718use OC \AppFramework \Middleware \Security \Exceptions \StrictCookieMissingException ;
1819use OC \AppFramework \Utility \ControllerMethodReflector ;
20+ use OC \Security \RemoteIpAddress ;
1921use OC \Settings \AuthorizedGroupMapper ;
2022use OC \User \Session ;
2123use OCP \App \AppPathNotFoundException ;
5052 * check fails
5153 */
5254class SecurityMiddleware extends Middleware {
53- /** @var INavigationManager */
54- private $ navigationManager ;
55- /** @var IRequest */
56- private $ request ;
57- /** @var ControllerMethodReflector */
58- private $ reflector ;
59- /** @var string */
60- private $ appName ;
61- /** @var IURLGenerator */
62- private $ urlGenerator ;
63- /** @var LoggerInterface */
64- private $ logger ;
65- /** @var bool */
66- private $ isLoggedIn ;
67- /** @var bool */
68- private $ isAdminUser ;
69- /** @var bool */
70- private $ isSubAdmin ;
71- /** @var IAppManager */
72- private $ appManager ;
73- /** @var IL10N */
74- private $ l10n ;
75- /** @var AuthorizedGroupMapper */
76- private $ groupAuthorizationMapper ;
77- /** @var IUserSession */
78- private $ userSession ;
79-
80- public function __construct (IRequest $ request ,
81- ControllerMethodReflector $ reflector ,
82- INavigationManager $ navigationManager ,
83- IURLGenerator $ urlGenerator ,
84- LoggerInterface $ logger ,
85- string $ appName ,
86- bool $ isLoggedIn ,
87- bool $ isAdminUser ,
88- bool $ isSubAdmin ,
89- IAppManager $ appManager ,
90- IL10N $ l10n ,
91- AuthorizedGroupMapper $ mapper ,
92- IUserSession $ userSession
55+ public function __construct (
56+ private IRequest $ request ,
57+ private ControllerMethodReflector $ reflector ,
58+ private INavigationManager $ navigationManager ,
59+ private IURLGenerator $ urlGenerator ,
60+ private LoggerInterface $ logger ,
61+ private string $ appName ,
62+ private bool $ isLoggedIn ,
63+ private bool $ isAdminUser ,
64+ private bool $ isSubAdmin ,
65+ private IAppManager $ appManager ,
66+ private IL10N $ l10n ,
67+ private AuthorizedGroupMapper $ groupAuthorizationMapper ,
68+ private IUserSession $ userSession ,
69+ private RemoteIpAddress $ remoteIpAddress ,
9370 ) {
94- $ this ->navigationManager = $ navigationManager ;
95- $ this ->request = $ request ;
96- $ this ->reflector = $ reflector ;
97- $ this ->appName = $ appName ;
98- $ this ->urlGenerator = $ urlGenerator ;
99- $ this ->logger = $ logger ;
100- $ this ->isLoggedIn = $ isLoggedIn ;
101- $ this ->isAdminUser = $ isAdminUser ;
102- $ this ->isSubAdmin = $ isSubAdmin ;
103- $ this ->appManager = $ appManager ;
104- $ this ->l10n = $ l10n ;
105- $ this ->groupAuthorizationMapper = $ mapper ;
106- $ this ->userSession = $ userSession ;
10771 }
10872
10973 /**
@@ -161,6 +125,9 @@ public function beforeController($controller, $methodName) {
161125 if (!$ authorized ) {
162126 throw new NotAdminException ($ this ->l10n ->t ('Logged in account must be an admin, a sub admin or gotten special right to access this setting ' ));
163127 }
128+ if (!$ this ->remoteIpAddress ->allowsAdminActions ()) {
129+ throw new AdminIpNotAllowedException ($ this ->l10n ->t ('Your current IP address doesn’t allow you to perform admin actions ' ));
130+ }
164131 }
165132 if ($ this ->hasAnnotationOrAttribute ($ reflectionMethod , 'SubAdminRequired ' , SubAdminRequired::class)
166133 && !$ this ->isSubAdmin
@@ -174,6 +141,16 @@ public function beforeController($controller, $methodName) {
174141 && !$ authorized ) {
175142 throw new NotAdminException ($ this ->l10n ->t ('Logged in account must be an admin ' ));
176143 }
144+ if ($ this ->hasAnnotationOrAttribute ($ reflectionMethod , 'SubAdminRequired ' , SubAdminRequired::class)
145+ && !$ this ->remoteIpAddress ->allowsAdminActions ()) {
146+ throw new AdminIpNotAllowedException ($ this ->l10n ->t ('Your current IP address doesn’t allow you to perform admin actions ' ));
147+ }
148+ if (!$ this ->hasAnnotationOrAttribute ($ reflectionMethod , 'SubAdminRequired ' , SubAdminRequired::class)
149+ && !$ this ->hasAnnotationOrAttribute ($ reflectionMethod , 'NoAdminRequired ' , NoAdminRequired::class)
150+ && !$ this ->remoteIpAddress ->allowsAdminActions ()) {
151+ throw new AdminIpNotAllowedException ($ this ->l10n ->t ('Your current IP address doesn’t allow you to perform admin actions ' ));
152+ }
153+
177154 }
178155
179156 // Check for strict cookie requirement
0 commit comments