-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSkipVulnerabilityScanDeciderTest.java
More file actions
214 lines (171 loc) · 9.05 KB
/
SkipVulnerabilityScanDeciderTest.java
File metadata and controls
214 lines (171 loc) · 9.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package vulnerabilities;
import dev.aikido.agent_api.background.Endpoint;
import dev.aikido.agent_api.context.ContextObject;
import dev.aikido.agent_api.storage.BypassedContextStore;
import dev.aikido.agent_api.vulnerabilities.SkipVulnerabilityScanDecider;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import utils.EmptyAPIResponses;
import utils.EmptySampleContextObject;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
public class SkipVulnerabilityScanDeciderTest {
@BeforeEach
public void setUp() {
BypassedContextStore.clear();
}
@AfterEach
public void tearDown() {
BypassedContextStore.clear();
}
private List<Endpoint> createEndpoints(boolean protectionForcedOff1, boolean protectionForcedOff2) {
List<Endpoint> endpoints = new ArrayList<>();
endpoints.add(new Endpoint("POST", "/api/login", 3, 1000, Collections.emptyList(), false, protectionForcedOff1, true));
endpoints.add(new Endpoint("POST", "/api/*", 1, 1000, Collections.emptyList(), false, protectionForcedOff2, true));
endpoints.add(new Endpoint("GET", "/", 3, 1000, Collections.emptyList(), false, false, false));
return endpoints;
}
private List<Endpoint> createEndpointsWildcardMethod(boolean protectionForcedOff1, boolean protectionForcedOff2) {
List<Endpoint> endpoints = new ArrayList<>();
endpoints.add(new Endpoint("*", "/api/login", 3, 1000, Collections.emptyList(), false, protectionForcedOff1, true));
endpoints.add(new Endpoint("*", "/api/*", 1, 1000, Collections.emptyList(), false, protectionForcedOff2, true));
endpoints.add(new Endpoint("GET", "/", 3, 1000, Collections.emptyList(), false, false, false));
return endpoints;
}
@Test
public void testShouldSkipVulnerabilityScan_NullContext() {
assertTrue(SkipVulnerabilityScanDecider.shouldSkipVulnerabilityScan(null));
}
@Test
public void testShouldSkipVulnerabilityScan_ProtectionForcedOff_1() {
// Update the ServiceConfiguration with endpoints
EmptyAPIResponses.setEmptyConfigWithEndpointList(createEndpoints(true, false));
assertTrue(SkipVulnerabilityScanDecider.shouldSkipVulnerabilityScan(
new EmptySampleContextObject("", "/api/login", "POST")
));
assertFalse(SkipVulnerabilityScanDecider.shouldSkipVulnerabilityScan(
new EmptySampleContextObject("", "/api/login", "GET")
));
assertFalse(SkipVulnerabilityScanDecider.shouldSkipVulnerabilityScan(
new EmptySampleContextObject("", "/api/login2", "POST")
));
EmptyAPIResponses.setEmptyConfigWithEndpointList(createEndpoints(false, false));
assertFalse(SkipVulnerabilityScanDecider.shouldSkipVulnerabilityScan(
new EmptySampleContextObject("", "/api/login", "POST")
));
}
@Test
public void testShouldSkipVulnerabilityScan_ProtectionForcedOff_1_WildCard() {
// Update the ServiceConfiguration with endpoints
EmptyAPIResponses.setEmptyConfigWithEndpointList(createEndpointsWildcardMethod(true, false));
assertTrue(SkipVulnerabilityScanDecider.shouldSkipVulnerabilityScan(
new EmptySampleContextObject("", "/api/login", "POST")
));
assertFalse(SkipVulnerabilityScanDecider.shouldSkipVulnerabilityScan(
new EmptySampleContextObject("", "/api/login2", "POST")
));
EmptyAPIResponses.setEmptyConfigWithEndpointList(createEndpointsWildcardMethod(false, false));
assertFalse(SkipVulnerabilityScanDecider.shouldSkipVulnerabilityScan(
new EmptySampleContextObject("", "/api/login", "POST")
));
}
@Test
public void testShouldSkipVulnerabilityScan_ProtectionForcedOff_2() {
// Update the ServiceConfiguration with endpoints
EmptyAPIResponses.setEmptyConfigWithEndpointList(createEndpoints(false, true));
assertTrue(SkipVulnerabilityScanDecider.shouldSkipVulnerabilityScan(
new EmptySampleContextObject("", "/api/login", "POST")
));
assertFalse(SkipVulnerabilityScanDecider.shouldSkipVulnerabilityScan(
new EmptySampleContextObject("", "/api/login", "GET")
));
assertTrue(SkipVulnerabilityScanDecider.shouldSkipVulnerabilityScan(
new EmptySampleContextObject("", "/api/login2", "POST")
));
assertTrue(SkipVulnerabilityScanDecider.shouldSkipVulnerabilityScan(
new EmptySampleContextObject("", "/api/", "POST")
));
EmptyAPIResponses.setEmptyConfigWithEndpointList(createEndpoints(true, true));
assertTrue(SkipVulnerabilityScanDecider.shouldSkipVulnerabilityScan(
new EmptySampleContextObject("", "/api/login", "POST")
));
assertFalse(SkipVulnerabilityScanDecider.shouldSkipVulnerabilityScan(
new EmptySampleContextObject("", "/api/login", "GET")
));
assertTrue(SkipVulnerabilityScanDecider.shouldSkipVulnerabilityScan(
new EmptySampleContextObject("", "/api/login2", "POST")
));
assertTrue(SkipVulnerabilityScanDecider.shouldSkipVulnerabilityScan(
new EmptySampleContextObject("", "/api/", "POST")
));
}
@Test
public void testShouldSkipVulnerabilityScan_ProtectionForcedOff_WildcardMethod() {
// Update the ServiceConfiguration with endpoints
EmptyAPIResponses.setEmptyConfigWithEndpointList(createEndpointsWildcardMethod(false, true));
assertTrue(SkipVulnerabilityScanDecider.shouldSkipVulnerabilityScan(
new EmptySampleContextObject("", "/api/login", "POST")
));
assertTrue(SkipVulnerabilityScanDecider.shouldSkipVulnerabilityScan(
new EmptySampleContextObject("", "/api/login2", "POST")
));
assertTrue(SkipVulnerabilityScanDecider.shouldSkipVulnerabilityScan(
new EmptySampleContextObject("", "/api/", "POST")
));
EmptyAPIResponses.setEmptyConfigWithEndpointList(createEndpointsWildcardMethod(true, true));
assertTrue(SkipVulnerabilityScanDecider.shouldSkipVulnerabilityScan(
new EmptySampleContextObject("", "/api/login", "POST")
));
assertTrue(SkipVulnerabilityScanDecider.shouldSkipVulnerabilityScan(
new EmptySampleContextObject("", "/api/login2", "POST")
));
assertTrue(SkipVulnerabilityScanDecider.shouldSkipVulnerabilityScan(
new EmptySampleContextObject("", "/api/", "POST")
));
}
@Test
public void testShouldSkipVulnerabilityScan_NoConditionsMet() {
// Update the ServiceConfiguration with endpoints
EmptyAPIResponses.setEmptyConfigWithEndpointList(createEndpoints(false, false));
assertFalse(SkipVulnerabilityScanDecider.shouldSkipVulnerabilityScan(
new EmptySampleContextObject("", "/api/login", "POST")
));
}
@Test
public void testShouldSkipVulnerabilityScan_BypassedIp_NullContext() {
BypassedContextStore.setBypassed(true);
// Even with defaultIfNoContext=false (the Stored SSRF path), a bypassed IP must skip.
assertTrue(SkipVulnerabilityScanDecider.shouldSkipVulnerabilityScan(null, false));
assertTrue(SkipVulnerabilityScanDecider.shouldSkipVulnerabilityScan(null, true));
}
@Test
public void testShouldSkipVulnerabilityScan_BypassedIp_WithContext() {
EmptyAPIResponses.setEmptyConfigWithEndpointList(createEndpoints(false, false));
ContextObject ctx = new EmptySampleContextObject("", "/api/login", "POST");
// Without bypass flag this context would return false (no forced protection off).
assertFalse(SkipVulnerabilityScanDecider.shouldSkipVulnerabilityScan(ctx));
BypassedContextStore.setBypassed(true);
ContextObject freshCtx = new EmptySampleContextObject("", "/api/login", "POST");
assertTrue(SkipVulnerabilityScanDecider.shouldSkipVulnerabilityScan(freshCtx));
}
@Test
public void testShouldSkipVulnerabilityScan_NotBypassed_NullContext() {
// Sanity check: default behavior unchanged when bypass flag is not set.
assertFalse(SkipVulnerabilityScanDecider.shouldSkipVulnerabilityScan(null, false));
assertTrue(SkipVulnerabilityScanDecider.shouldSkipVulnerabilityScan(null, true));
}
@Test
public void testUsesCache() {
ContextObject ctx = new EmptySampleContextObject("", "/api/login", "POST");
assertTrue(ctx.getForcedProtectionOff().isEmpty());
assertFalse(SkipVulnerabilityScanDecider.shouldSkipVulnerabilityScan(ctx));
assertTrue(ctx.getForcedProtectionOff().isPresent());
assertFalse(ctx.getForcedProtectionOff().get());
assertFalse(SkipVulnerabilityScanDecider.shouldSkipVulnerabilityScan(ctx));
ctx.setForcedProtectionOff(true);
assertTrue(SkipVulnerabilityScanDecider.shouldSkipVulnerabilityScan(ctx));
}
}