Skip to content

Commit 6fe7c7a

Browse files
Jami CogswellJami Cogswell
authored andcommitted
Java: some refactoring
1 parent f65a5b9 commit 6fe7c7a

File tree

2 files changed

+69
-72
lines changed

2 files changed

+69
-72
lines changed

java/ql/lib/semmle/code/java/frameworks/spring/SpringSecurity.qll

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ class TypeAbstractRequestMatcherRegistry extends Class {
4040
}
4141

4242
/**
43-
* A call to `HttpSecurity.authorizeRequests` method.
43+
* A call to the `HttpSecurity.authorizeRequests` method.
4444
*
45-
* Note: this API is deprecated and scheduled for removal
45+
* Note: this method is deprecated and scheduled for removal
4646
* in Spring Security 7.0.
4747
*/
4848
class AuthorizeRequestsCall extends MethodCall {
@@ -53,9 +53,9 @@ class AuthorizeRequestsCall extends MethodCall {
5353
}
5454

5555
/**
56-
* A call to `HttpSecurity.authorizeHttpRequests` method.
56+
* A call to the `HttpSecurity.authorizeHttpRequests` method.
5757
*
58-
* Note: the no-argument version of this API is deprecated
58+
* Note: the no-argument version of this method is deprecated
5959
* and scheduled for removal in Spring Security 7.0.
6060
*/
6161
class AuthorizeHttpRequestsCall extends MethodCall {
@@ -65,15 +65,57 @@ class AuthorizeHttpRequestsCall extends MethodCall {
6565
}
6666
}
6767

68-
/** A call to `AuthorizedUrl.permitAll` method. */
68+
/**
69+
* A call to the `HttpSecurity.requestMatcher` method.
70+
*
71+
* Note: this method was removed in Spring Security 6.0.
72+
* It was replaced by `securityMatcher`.
73+
*/
74+
class RequestMatcherCall extends MethodCall {
75+
RequestMatcherCall() {
76+
this.getMethod().hasName("requestMatcher") and
77+
this.getMethod().getDeclaringType() instanceof TypeHttpSecurity
78+
}
79+
}
80+
81+
/**
82+
* A call to the `HttpSecurity.requestMatchers` method.
83+
*
84+
* Note: this method was removed in Spring Security 6.0.
85+
* It was replaced by `securityMatchers`.
86+
*/
87+
class RequestMatchersCall extends MethodCall {
88+
RequestMatchersCall() {
89+
this.getMethod().hasName("requestMatchers") and
90+
this.getMethod().getDeclaringType() instanceof TypeHttpSecurity
91+
}
92+
}
93+
94+
/** A call to the `HttpSecurity.securityMatcher` method. */
95+
class SecurityMatcherCall extends MethodCall {
96+
SecurityMatcherCall() {
97+
this.getMethod().hasName("securityMatcher") and
98+
this.getMethod().getDeclaringType() instanceof TypeHttpSecurity
99+
}
100+
}
101+
102+
/** A call to the `HttpSecurity.securityMatchers` method. */
103+
class SecurityMatchersCall extends MethodCall {
104+
SecurityMatchersCall() {
105+
this.getMethod().hasName("securityMatchers") and
106+
this.getMethod().getDeclaringType() instanceof TypeHttpSecurity
107+
}
108+
}
109+
110+
/** A call to the `AuthorizedUrl.permitAll` method. */
69111
class PermitAllCall extends MethodCall {
70112
PermitAllCall() {
71113
this.getMethod().hasName("permitAll") and
72114
this.getMethod().getDeclaringType() instanceof TypeAuthorizedUrl
73115
}
74116
}
75117

76-
/** A call to `AbstractRequestMatcherRegistry.anyRequest` method. */
118+
/** A call to the `AbstractRequestMatcherRegistry.anyRequest` method. */
77119
class AnyRequestCall extends MethodCall {
78120
AnyRequestCall() {
79121
this.getMethod().hasName("anyRequest") and

java/ql/lib/semmle/code/java/security/SpringBootActuatorsQuery.qll

Lines changed: 21 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,36 @@ private import semmle.code.java.frameworks.spring.SpringSecurity
55
private import semmle.code.java.frameworks.spring.SpringBoot
66

77
/**
8-
* A call to `HttpSecurity.requestMatcher` method with argument
8+
* A call to an `HttpSecurity` matcher method with argument
99
* `EndpointRequest.toAnyEndpoint()`.
1010
*/
11-
private class RequestMatcherCall extends MethodCall {
12-
RequestMatcherCall() {
13-
this.getMethod().hasName("requestMatcher") and
14-
this.getMethod().getDeclaringType() instanceof TypeHttpSecurity and
11+
private class HttpSecurityMatcherCall extends MethodCall {
12+
HttpSecurityMatcherCall() {
13+
(
14+
this instanceof RequestMatcherCall or
15+
this instanceof SecurityMatcherCall
16+
) and
1517
this.getArgument(0) instanceof ToAnyEndpointCall
1618
}
1719
}
1820

1921
/**
20-
* A call to `HttpSecurity.requestMatchers` method with lambda argument
21-
* `EndpointRequest.toAnyEndpoint()`.
22+
* A call to an `HttpSecurity` matchers method with lambda
23+
* argument `EndpointRequest.toAnyEndpoint()`.
2224
*/
23-
private class RequestMatchersCall extends MethodCall {
24-
RequestMatchersCall() {
25-
this.getMethod().hasName("requestMatchers") and
26-
this.getMethod().getDeclaringType() instanceof TypeHttpSecurity and
25+
private class HttpSecurityMatchersCall extends MethodCall {
26+
HttpSecurityMatchersCall() {
27+
(
28+
this instanceof RequestMatchersCall or
29+
this instanceof SecurityMatchersCall
30+
) and
2731
this.getArgument(0).(LambdaExpr).getExprBody() instanceof ToAnyEndpointCall
2832
}
2933
}
3034

3135
/**
32-
* A call to `AbstractRequestMatcherRegistry.requestMatchers` method with an argument
33-
* `RequestMatcher.toAnyEndpoint()`.
36+
* A call to an `AbstractRequestMatcherRegistry.requestMatchers` method with
37+
* argument `EndpointRequest.toAnyEndpoint()`.
3438
*/
3539
private class RegistryRequestMatchersCall extends MethodCall {
3640
RegistryRequestMatchersCall() {
@@ -40,71 +44,22 @@ private class RegistryRequestMatchersCall extends MethodCall {
4044
}
4145
}
4246

43-
/**
44-
* A call to `HttpSecurity.securityMatcher` method with argument
45-
* `EndpointRequest.toAnyEndpoint()`.
46-
*/
47-
private class SecurityMatcherCall extends MethodCall {
48-
SecurityMatcherCall() {
49-
this.getMethod().hasName("securityMatcher") and
50-
this.getMethod().getDeclaringType() instanceof TypeHttpSecurity and
51-
this.getArgument(0) instanceof ToAnyEndpointCall
52-
}
53-
}
54-
55-
/**
56-
* A call to `HttpSecurity.securityMatchers` method with lambda argument
57-
* `EndpointRequest.toAnyEndpoint()`.
58-
*/
59-
private class SecurityMatchersCall extends MethodCall {
60-
SecurityMatchersCall() {
61-
this.getMethod().hasName("securityMatchers") and
62-
this.getMethod().getDeclaringType() instanceof TypeHttpSecurity and
63-
this.getArgument(0).(LambdaExpr).getExprBody() instanceof ToAnyEndpointCall
64-
}
65-
}
66-
67-
/**
68-
* A call to a method that authorizes requests, e.g. `authorizeRequests` or
69-
* `authorizeHttpRequests`.
70-
*/
47+
/** A call to an `HttpSecurity` method that authorizes requests. */
7148
private class AuthorizeCall extends MethodCall {
7249
AuthorizeCall() {
7350
this instanceof AuthorizeRequestsCall or
7451
this instanceof AuthorizeHttpRequestsCall
7552
}
7653
}
7754

78-
/**
79-
* A call to a matcher method with argument
80-
* `EndpointRequest.toAnyEndpoint()`.
81-
*/
82-
private class MatcherCall extends MethodCall {
83-
MatcherCall() {
84-
this instanceof RequestMatcherCall or
85-
this instanceof SecurityMatcherCall
86-
}
87-
}
88-
89-
/**
90-
* A call to a matchers method with argument
91-
* `EndpointRequest.toAnyEndpoint()`.
92-
*/
93-
private class MatchersCall extends MethodCall {
94-
MatchersCall() {
95-
this instanceof RequestMatchersCall or
96-
this instanceof SecurityMatchersCall
97-
}
98-
}
99-
10055
/** Holds if `permitAllCall` is called on request(s) mapped to actuator endpoint(s). */
10156
predicate permitsSpringBootActuators(PermitAllCall permitAllCall) {
10257
exists(AuthorizeCall authorizeCall |
10358
// .requestMatcher(EndpointRequest).authorizeRequests([...]).[...]
104-
authorizeCall.getQualifier() instanceof MatcherCall
59+
authorizeCall.getQualifier() instanceof HttpSecurityMatcherCall
10560
or
10661
// .requestMatchers(matcher -> EndpointRequest).authorizeRequests([...]).[...]
107-
authorizeCall.getQualifier() instanceof MatchersCall
62+
authorizeCall.getQualifier() instanceof HttpSecurityMatchersCall
10863
|
10964
// [...].authorizeRequests(r -> r.anyRequest().permitAll()) or
11065
// [...].authorizeRequests(r -> r.requestMatchers(EndpointRequest).permitAll())
@@ -143,7 +98,7 @@ predicate permitsSpringBootActuators(PermitAllCall permitAllCall) {
14398
permitAllCall.getQualifier() = registryRequestMatchersCall
14499
)
145100
or
146-
exists(Variable v, MatcherCall matcherCall |
101+
exists(Variable v, HttpSecurityMatcherCall matcherCall |
147102
// http.securityMatcher(EndpointRequest.toAnyEndpoint());
148103
// http.authorizeRequests([...].permitAll())
149104
v.getAnAccess() = authorizeCall.getQualifier() and

0 commit comments

Comments
 (0)