5
5
import com .newrelic .agent .security .introspec .SecurityIntrospector ;
6
6
import com .newrelic .api .agent .security .schema .AbstractOperation ;
7
7
import com .newrelic .api .agent .security .schema .VulnerabilityCaseType ;
8
- import com .newrelic .api .agent .security .schema .operation .SecureCookieOperation ;
8
+ import com .newrelic .api .agent .security .schema .operation .SecureCookieOperationSet ;
9
9
import com .newrelic .api .agent .security .schema .operation .TrustBoundaryOperation ;
10
10
import org .junit .Assert ;
11
11
import org .junit .ClassRule ;
18
18
import java .net .HttpURLConnection ;
19
19
import java .net .URISyntaxException ;
20
20
import java .net .URL ;
21
+ import java .util .Iterator ;
21
22
import java .util .List ;
22
23
23
24
@ RunWith (SecurityInstrumentationTestRunner .class )
@@ -29,9 +30,7 @@ public class HttpSessionTest {
29
30
30
31
@ Test
31
32
public void testSessionSetAttribute () throws IOException , URISyntaxException {
32
- String method = "GET" ;
33
- String POST_PARAMS = "hook=readLine" ;
34
- makeRequest (method , POST_PARAMS , "set" );
33
+ makeRequest ("set" );
35
34
36
35
SecurityIntrospector introspector = SecurityInstrumentationTestRunner .getIntrospector ();
37
36
List <AbstractOperation > operations = introspector .getOperations ();
@@ -61,9 +60,7 @@ else if(i==1){
61
60
62
61
@ Test
63
62
public void testSessionPutValue () throws IOException , URISyntaxException {
64
- String method = "GET" ;
65
- String POST_PARAMS = "hook=readLine" ;
66
- makeRequest (method , POST_PARAMS , "put" );
63
+ makeRequest ("put" );
67
64
68
65
SecurityIntrospector introspector = SecurityInstrumentationTestRunner .getIntrospector ();
69
66
List <AbstractOperation > operations = introspector .getOperations ();
@@ -84,56 +81,146 @@ public void testSessionPutValue() throws IOException, URISyntaxException {
84
81
85
82
@ Test
86
83
public void testAddCookie () throws IOException , URISyntaxException {
87
- String method = "GET" ;
88
- String POST_PARAMS = "hook=readLine" ;
89
- makeRequest (method , POST_PARAMS , "cookie" );
84
+ makeRequest ("securecookie" );
90
85
91
86
SecurityIntrospector introspector = SecurityInstrumentationTestRunner .getIntrospector ();
92
87
List <AbstractOperation > operations = introspector .getOperations ();
93
88
Assert .assertTrue ("No operations detected" , operations .size () > 0 );
94
89
Assert .assertTrue ("Unexpected operation count detected" , operations .size () == 1 || operations .size () == 2 );
95
- SecureCookieOperation targetOperation = null ;
96
- for (AbstractOperation operation : operations ) {
97
- if (operation instanceof SecureCookieOperation )
98
- targetOperation = (SecureCookieOperation ) operation ;
99
- };
100
- Assert .assertNotNull ("No target operation detected" , targetOperation );
101
- Assert .assertEquals ("Wrong case-type detected" , VulnerabilityCaseType .SECURE_COOKIE , targetOperation .getCaseType ());
102
- Assert .assertEquals ("Wrong key detected" , "false" , targetOperation .getValue ());
90
+ SecureCookieOperationSet targetOperation = null ;
91
+ targetOperation = verifySecureCookieOp (operations );
92
+
93
+ Assert .assertEquals (1 , targetOperation .getOperations ().size ());
94
+ Iterator <SecureCookieOperationSet .SecureCookieOperation > secureCookieOps = targetOperation .getOperations ().iterator ();
95
+ Assert .assertTrue (secureCookieOps .hasNext ());
96
+
97
+ SecureCookieOperationSet .SecureCookieOperation secureCookieOp = secureCookieOps .next ();
98
+ verifySecureCookie (secureCookieOp , "key" , false , true );
103
99
}
104
100
105
101
@ Test
106
102
public void testAddCookie1 () throws IOException , URISyntaxException {
107
- String method = "GET" ;
108
- String POST_PARAMS = "hook=readLine" ;
109
- makeRequest (method , POST_PARAMS , "securecookie" );
103
+ makeRequest ("cookie" );
110
104
111
105
SecurityIntrospector introspector = SecurityInstrumentationTestRunner .getIntrospector ();
112
106
List <AbstractOperation > operations = introspector .getOperations ();
113
- Assert .assertTrue ("No operations detected" , operations .size () > 0 );
114
- Assert .assertTrue ("Unexpected operation count detected" , operations .size () == 1 || operations .size () == 2 );
115
- SecureCookieOperation targetOperation = null ;
116
- for (AbstractOperation operation : operations ) {
117
- if (operation instanceof SecureCookieOperation )
118
- targetOperation = (SecureCookieOperation ) operation ;
119
- };
120
- Assert .assertNotNull ("No target operation detected" , targetOperation );
121
- Assert .assertEquals ("Wrong case-type detected" , VulnerabilityCaseType .SECURE_COOKIE , targetOperation .getCaseType ());
122
- Assert .assertEquals ("Wrong key detected" , "true" , targetOperation .getValue ());
107
+
108
+ SecureCookieOperationSet targetOperation = verifySecureCookieOp (operations );
109
+ Assert .assertEquals (1 , targetOperation .getOperations ().size ());
110
+
111
+ Iterator <SecureCookieOperationSet .SecureCookieOperation > secureCookieOps = targetOperation .getOperations ().iterator ();
112
+ Assert .assertTrue (secureCookieOps .hasNext ());
113
+
114
+ SecureCookieOperationSet .SecureCookieOperation secureCookieOp = secureCookieOps .next ();
115
+ verifySecureCookie (secureCookieOp , "key" , false , false );
116
+ }
117
+
118
+ @ Test
119
+ public void testAddSecureCookies () throws IOException , URISyntaxException {
120
+ makeRequest ("secure_cookies" );
121
+
122
+ SecurityIntrospector introspector = SecurityInstrumentationTestRunner .getIntrospector ();
123
+ List <AbstractOperation > operations = introspector .getOperations ();
124
+
125
+ SecureCookieOperationSet targetOperation = verifySecureCookieOp (operations );
126
+ Assert .assertEquals (2 , targetOperation .getOperations ().size ());
127
+
128
+ for (SecureCookieOperationSet .SecureCookieOperation secureCookieOp : targetOperation .getOperations ()) {
129
+ if (secureCookieOp .getName ().equals ("secure-cookie-1" )) {
130
+ verifySecureCookie (secureCookieOp , "secure-cookie-1" , false , true );
131
+ } else {
132
+ verifySecureCookie (secureCookieOp , "secure-cookie-2" , true , true );
133
+ }
134
+ }
135
+ }
136
+
137
+ @ Test
138
+ public void testAddInSecureCookies () throws IOException , URISyntaxException {
139
+ makeRequest ("insecure_cookies" );
140
+
141
+ SecurityIntrospector introspector = SecurityInstrumentationTestRunner .getIntrospector ();
142
+ List <AbstractOperation > operations = introspector .getOperations ();
143
+
144
+ SecureCookieOperationSet targetOperation = verifySecureCookieOp (operations );
145
+ Assert .assertEquals (2 , targetOperation .getOperations ().size ());
146
+
147
+ for (SecureCookieOperationSet .SecureCookieOperation secureCookieOp : targetOperation .getOperations ()) {
148
+ if (secureCookieOp .getName ().equals ("insecure-cookie-1" )) {
149
+ verifySecureCookie (secureCookieOp , "insecure-cookie-1" , false , false );
150
+ } else {
151
+ verifySecureCookie (secureCookieOp , "insecure-cookie-2" , false , false );
152
+ }
153
+ }
123
154
}
124
155
125
- private void makeRequest ( String Method , final String POST_PARAMS , String path ) throws URISyntaxException , IOException {
156
+ @ Test
157
+ public void testAddMultiSecureCookies () throws IOException , URISyntaxException {
158
+ makeRequest ("cookies" );
159
+
160
+ SecurityIntrospector introspector = SecurityInstrumentationTestRunner .getIntrospector ();
161
+ List <AbstractOperation > operations = introspector .getOperations ();
162
+
163
+ SecureCookieOperationSet targetOperation = verifySecureCookieOp (operations );
164
+ Assert .assertEquals (2 , targetOperation .getOperations ().size ());
165
+
166
+ for (SecureCookieOperationSet .SecureCookieOperation secureCookieOp : targetOperation .getOperations ()) {
167
+ if (secureCookieOp .getName ().equals ("insecure-cookie" )) {
168
+ verifySecureCookie (secureCookieOp , "insecure-cookie" , false , false );
169
+ } else {
170
+ verifySecureCookie (secureCookieOp , "secure-cookie" , false , true );
171
+ }
172
+ }
173
+ }
174
+
175
+ @ Test
176
+ public void testSingleCookie () throws IOException , URISyntaxException {
177
+ makeRequest ("single-cookie" );
178
+
179
+ SecurityIntrospector introspector = SecurityInstrumentationTestRunner .getIntrospector ();
180
+ List <AbstractOperation > operations = introspector .getOperations ();
181
+
182
+ SecureCookieOperationSet targetOperation = verifySecureCookieOp (operations );
183
+ Assert .assertEquals (1 , targetOperation .getOperations ().size ());
184
+
185
+ Iterator <SecureCookieOperationSet .SecureCookieOperation > secureCookieOps = targetOperation .getOperations ().iterator ();
126
186
187
+ Assert .assertTrue (secureCookieOps .hasNext ());
188
+ SecureCookieOperationSet .SecureCookieOperation secureCookieOp = secureCookieOps .next ();
189
+ verifySecureCookie (secureCookieOp , "cookie" , true , true );
190
+ }
191
+
192
+ private void makeRequest (String path ) throws URISyntaxException , IOException {
127
193
URL u = server .getEndPoint ("session/" +path ).toURL ();
128
194
HttpURLConnection conn = (HttpURLConnection ) u .openConnection ();
129
- conn .setRequestMethod (Method );
130
195
conn .setDoOutput (true );
131
196
conn .setRequestProperty ("Connection" , "Keep-Alive" );
132
197
conn .setRequestProperty ("Cache-Control" , "no-cache" );
133
198
conn .setRequestProperty ("Content-Type" , "multipart/form-data" );
134
199
135
200
conn .connect ();
136
201
conn .getResponseCode ();
202
+ }
203
+
204
+ private SecureCookieOperationSet verifySecureCookieOp (List <AbstractOperation > operations ) {
205
+ SecureCookieOperationSet targetOperation = null ;
206
+ for (AbstractOperation operation : operations ) {
207
+ if (operation instanceof SecureCookieOperationSet ) {
208
+ targetOperation = (SecureCookieOperationSet ) operation ;
209
+ }
210
+ }
211
+
212
+ Assert .assertNotNull ("No target operation detected" , targetOperation );
213
+ Assert .assertEquals ("Wrong method detected" , "addCookie" , targetOperation .getMethodName ());
214
+ Assert .assertEquals ("Wrong case-type detected" , VulnerabilityCaseType .SECURE_COOKIE , targetOperation .getCaseType ());
215
+ Assert .assertTrue ("isLowSeverityHook should be true" , targetOperation .isLowSeverityHook ());
216
+ return targetOperation ;
217
+ }
137
218
219
+ private void verifySecureCookie (SecureCookieOperationSet .SecureCookieOperation secureCookieOp , String key , boolean isHttpOnly , boolean isSecure ) {
220
+ Assert .assertEquals ("Wrong cookie key detected" , key , secureCookieOp .getName ());
221
+ Assert .assertEquals ("Wrong cookie value detected" , "value" , secureCookieOp .getValue ());
222
+ Assert .assertEquals (String .format ("isHttpOnly should be %s" , isHttpOnly ), isHttpOnly , secureCookieOp .isHttpOnly ());
223
+ Assert .assertEquals (String .format ("isSecure should be %s" , isSecure ), isSecure , secureCookieOp .isSecure ());
224
+ Assert .assertTrue (String .format ("isSameSiteStrict should be %s" , true ), secureCookieOp .isSameSiteStrict ());
138
225
}
139
226
}
0 commit comments