Skip to content

Commit 0a5d58e

Browse files
Cover more configurations in UnsafeSpringExporterInConfigurationClass.ql
1 parent df60268 commit 0a5d58e

File tree

5 files changed

+71
-7
lines changed

5 files changed

+71
-7
lines changed

java/ql/src/experimental/Security/CWE/CWE-502/UnsafeSpringExporterInConfigurationClass.ql

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,22 @@
1414
import java
1515
import UnsafeSpringExporterLib
1616

17+
/**
18+
* Holds if `type` is a Spring configuration that declares beans.
19+
*/
20+
private predicate isConfiguration(RefType type) {
21+
type.hasAnnotation("org.springframework.context.annotation", "Configuration") or
22+
isConfigurationAnnotation(type.getAnAnnotation())
23+
}
24+
25+
/**
26+
* Holds if `annotation` is a Java annotations that declares a Spring configuration.
27+
*/
28+
private predicate isConfigurationAnnotation(Annotation annotation) {
29+
isConfiguration(annotation.getType()) or
30+
isConfigurationAnnotation(annotation.getType().getAnAnnotation())
31+
}
32+
1733
/**
1834
* A method that initializes a unsafe bean based on `RemoteInvocationSerializingExporter`.
1935
*/
@@ -22,11 +38,9 @@ private class UnsafeBeanInitMethod extends Method {
2238

2339
UnsafeBeanInitMethod() {
2440
isRemoteInvocationSerializingExporter(this.getReturnType()) and
25-
this.getDeclaringType().hasAnnotation("org.springframework.context.annotation", "Configuration") and
26-
exists(Annotation a |
27-
a.getType().hasQualifiedName("org.springframework.context.annotation", "Bean")
28-
|
29-
this.getAnAnnotation() = a and
41+
isConfiguration(this.getDeclaringType()) and
42+
exists(Annotation a | this.getAnAnnotation() = a |
43+
a.getType().hasQualifiedName("org.springframework.context.annotation", "Bean") and
3044
if a.getValue("name") instanceof StringLiteral
3145
then identifier = a.getValue("name").(StringLiteral).getRepresentedString()
3246
else identifier = this.getName()

java/ql/test/experimental/query-tests/security/CWE-502/SpringExporterUnsafeDeserialization.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import org.springframework.boot.SpringBootConfiguration;
2+
import org.springframework.boot.autoconfigure.SpringBootApplication;
13
import org.springframework.context.annotation.Bean;
24
import org.springframework.context.annotation.Configuration;
35
import org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter;
@@ -27,6 +29,30 @@ HttpInvokerServiceExporter notABean() {
2729
}
2830
}
2931

32+
@SpringBootApplication
33+
class SpringBootTestApplication {
34+
35+
@Bean(name = "/unsafeHttpInvokerServiceExporter")
36+
HttpInvokerServiceExporter unsafeHttpInvokerServiceExporter() {
37+
HttpInvokerServiceExporter exporter = new HttpInvokerServiceExporter();
38+
exporter.setService(new AccountServiceImpl());
39+
exporter.setServiceInterface(AccountService.class);
40+
return exporter;
41+
}
42+
}
43+
44+
@SpringBootConfiguration
45+
class SpringBootTestConfiguration {
46+
47+
@Bean(name = "/unsafeHttpInvokerServiceExporter")
48+
HttpInvokerServiceExporter unsafeHttpInvokerServiceExporter() {
49+
HttpInvokerServiceExporter exporter = new HttpInvokerServiceExporter();
50+
exporter.setService(new AccountServiceImpl());
51+
exporter.setServiceInterface(AccountService.class);
52+
return exporter;
53+
}
54+
}
55+
3056
class CustomeRemoteInvocationSerializingExporter extends RemoteInvocationSerializingExporter {}
3157

3258
class NotAConfiguration {
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
| SpringExporterUnsafeDeserialization.java:10:32:10:63 | unsafeHttpInvokerServiceExporter | Unsafe deserialization in a Spring exporter bean '/unsafeHttpInvokerServiceExporter' |
2-
| SpringExporterUnsafeDeserialization.java:18:41:18:88 | unsafeCustomeRemoteInvocationSerializingExporter | Unsafe deserialization in a Spring exporter bean '/unsafeCustomeRemoteInvocationSerializingExporter' |
1+
| SpringExporterUnsafeDeserialization.java:12:32:12:63 | unsafeHttpInvokerServiceExporter | Unsafe deserialization in a Spring exporter bean '/unsafeHttpInvokerServiceExporter' |
2+
| SpringExporterUnsafeDeserialization.java:20:41:20:88 | unsafeCustomeRemoteInvocationSerializingExporter | Unsafe deserialization in a Spring exporter bean '/unsafeCustomeRemoteInvocationSerializingExporter' |
3+
| SpringExporterUnsafeDeserialization.java:36:32:36:63 | unsafeHttpInvokerServiceExporter | Unsafe deserialization in a Spring exporter bean '/unsafeHttpInvokerServiceExporter' |
4+
| SpringExporterUnsafeDeserialization.java:48:32:48:63 | unsafeHttpInvokerServiceExporter | Unsafe deserialization in a Spring exporter bean '/unsafeHttpInvokerServiceExporter' |
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.springframework.boot;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Target;
5+
6+
import org.springframework.context.annotation.Configuration;
7+
8+
@Target(ElementType.TYPE)
9+
@Configuration
10+
public @interface SpringBootConfiguration {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.springframework.boot.autoconfigure;
2+
3+
import java.lang.annotation.Target;
4+
import java.lang.annotation.ElementType;
5+
import java.lang.annotation.Inherited;
6+
7+
import org.springframework.boot.SpringBootConfiguration;
8+
9+
@Target(ElementType.TYPE)
10+
@Inherited
11+
@SpringBootConfiguration
12+
public @interface SpringBootApplication {}

0 commit comments

Comments
 (0)