forked from Waffle/waffle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NegotiateSecurityFilterTests.java
429 lines (397 loc) · 19.1 KB
/
NegotiateSecurityFilterTests.java
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/**
* Waffle (https://github.com/Waffle/waffle)
*
* Copyright (c) 2010-2017 Application Security, Inc.
*
* All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse
* Public License v1.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v10.html.
*
* Contributors: Application Security, Inc.
*/
package waffle.servlet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Base64;
import javax.security.auth.Subject;
import javax.servlet.ServletException;
import org.assertj.core.api.Assertions;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import com.sun.jna.platform.win32.Advapi32Util;
import com.sun.jna.platform.win32.Secur32.EXTENDED_NAME_FORMAT;
import com.sun.jna.platform.win32.Secur32Util;
import com.sun.jna.platform.win32.Sspi;
import com.sun.jna.platform.win32.Sspi.SecBufferDesc;
import waffle.mock.MockWindowsAuthProvider;
import waffle.mock.MockWindowsIdentity;
import waffle.mock.http.SimpleFilterChain;
import waffle.mock.http.SimpleFilterConfig;
import waffle.mock.http.SimpleHttpRequest;
import waffle.mock.http.SimpleHttpResponse;
import waffle.windows.auth.IWindowsCredentialsHandle;
import waffle.windows.auth.PrincipalFormat;
import waffle.windows.auth.impl.WindowsAccountImpl;
import waffle.windows.auth.impl.WindowsAuthProviderImpl;
import waffle.windows.auth.impl.WindowsCredentialsHandleImpl;
import waffle.windows.auth.impl.WindowsSecurityContextImpl;
/**
* Waffle Tomcat Security Filter Tests.
*
* @author dblock[at]dblock[dot]org
*/
public class NegotiateSecurityFilterTests {
/** The Constant NEGOTIATE. */
private static final String NEGOTIATE = "Negotiate";
/** The Constant NTLM. */
private static final String NTLM = "NTLM";
/** The filter. */
private NegotiateSecurityFilter filter;
/**
* Sets the up.
*
* @throws ServletException
* the servlet exception
*/
@Before
public void setUp() throws ServletException {
this.filter = new NegotiateSecurityFilter();
this.filter.setAuth(new WindowsAuthProviderImpl());
this.filter.init(null);
}
/**
* Tear down.
*/
@After
public void tearDown() {
this.filter.destroy();
}
/**
* Test challenge get.
*
* @throws IOException
* Signals that an I/O exception has occurred.
* @throws ServletException
* the servlet exception
*/
@Test
public void testChallengeGET() throws IOException, ServletException {
final SimpleHttpRequest request = new SimpleHttpRequest();
request.setMethod("GET");
final SimpleHttpResponse response = new SimpleHttpResponse();
this.filter.doFilter(request, response, null);
final String[] wwwAuthenticates = response.getHeaderValues("WWW-Authenticate");
Assert.assertEquals(3, wwwAuthenticates.length);
Assert.assertEquals(NegotiateSecurityFilterTests.NEGOTIATE, wwwAuthenticates[0]);
Assert.assertEquals(NegotiateSecurityFilterTests.NTLM, wwwAuthenticates[1]);
Assert.assertTrue(wwwAuthenticates[2].startsWith("Basic realm=\""));
Assert.assertEquals(2, response.getHeaderNamesSize());
Assert.assertEquals("keep-alive", response.getHeader("Connection"));
Assert.assertEquals(401, response.getStatus());
}
/**
* Test challenge post.
*
* @throws IOException
* Signals that an I/O exception has occurred.
* @throws ServletException
* the servlet exception
*/
@Test
public void testChallengePOST() throws IOException, ServletException {
final String securityPackage = NegotiateSecurityFilterTests.NEGOTIATE;
IWindowsCredentialsHandle clientCredentials = null;
WindowsSecurityContextImpl clientContext = null;
try {
// client credentials handle
clientCredentials = WindowsCredentialsHandleImpl.getCurrent(securityPackage);
clientCredentials.initialize();
// initial client security context
clientContext = new WindowsSecurityContextImpl();
clientContext.setPrincipalName(WindowsAccountImpl.getCurrentUsername());
clientContext.setCredentialsHandle(clientCredentials);
clientContext.setSecurityPackage(securityPackage);
clientContext.initialize(null, null, WindowsAccountImpl.getCurrentUsername());
final SimpleHttpRequest request = new SimpleHttpRequest();
request.setMethod("POST");
request.setContentLength(0);
final String clientToken = Base64.getEncoder().encodeToString(clientContext.getToken());
request.addHeader("Authorization", securityPackage + " " + clientToken);
final SimpleHttpResponse response = new SimpleHttpResponse();
this.filter.doFilter(request, response, null);
Assert.assertTrue(response.getHeader("WWW-Authenticate").startsWith(securityPackage + " "));
Assert.assertEquals("keep-alive", response.getHeader("Connection"));
Assert.assertEquals(3, response.getHeaderNamesSize());
Assert.assertEquals(401, response.getStatus());
} finally {
if (clientContext != null) {
clientContext.dispose();
}
if (clientCredentials != null) {
clientCredentials.dispose();
}
}
}
/**
* Test negotiate.
*
* @throws IOException
* Signals that an I/O exception has occurred.
* @throws ServletException
* the servlet exception
*/
@Test
public void testNegotiate() throws IOException, ServletException {
final String securityPackage = NegotiateSecurityFilterTests.NEGOTIATE;
// client credentials handle
IWindowsCredentialsHandle clientCredentials = null;
WindowsSecurityContextImpl clientContext = null;
// role will contain both Everyone and SID
this.filter.setRoleFormat("both");
try {
// client credentials handle
clientCredentials = WindowsCredentialsHandleImpl.getCurrent(securityPackage);
clientCredentials.initialize();
// initial client security context
clientContext = new WindowsSecurityContextImpl();
clientContext.setPrincipalName(WindowsAccountImpl.getCurrentUsername());
clientContext.setCredentialsHandle(clientCredentials);
clientContext.setSecurityPackage(securityPackage);
clientContext.initialize(null, null, WindowsAccountImpl.getCurrentUsername());
// filter chain
final SimpleFilterChain filterChain = new SimpleFilterChain();
// negotiate
boolean authenticated = false;
final SimpleHttpRequest request = new SimpleHttpRequest();
while (true) {
final String clientToken = Base64.getEncoder().encodeToString(clientContext.getToken());
request.addHeader("Authorization", securityPackage + " " + clientToken);
final SimpleHttpResponse response = new SimpleHttpResponse();
this.filter.doFilter(request, response, filterChain);
final Subject subject = (Subject) request.getSession().getAttribute("javax.security.auth.subject");
authenticated = subject != null && subject.getPrincipals().size() > 0;
if (authenticated) {
Assertions.assertThat(response.getHeaderNamesSize()).isGreaterThanOrEqualTo(0);
break;
}
Assert.assertEquals(401, response.getStatus());
// security package requested is one negotiate continues with
Assert.assertTrue(response.getHeader("WWW-Authenticate").startsWith(securityPackage + " "));
// keep-alive, NTLM is a connection-oriented protocol
Assert.assertEquals("keep-alive", response.getHeader("Connection"));
// Connection: keep-alive
// WWW-Authenticate: ...
// Content-Length: ...
Assert.assertEquals(3, response.getHeaderNamesSize());
// response has a body and a content length (.NET clients require this)
int contentLength = Integer.parseInt(response.getHeader("Content-Length"));
Assert.assertTrue(contentLength > 0);
String content = response.getOutputText();
Assert.assertEquals(contentLength, content.length());
// continue token
final String continueToken = response.getHeader("WWW-Authenticate")
.substring(securityPackage.length() + 1);
final byte[] continueTokenBytes = Base64.getDecoder().decode(continueToken);
Assertions.assertThat(continueTokenBytes.length).isGreaterThan(0);
final SecBufferDesc continueTokenBuffer = new SecBufferDesc(Sspi.SECBUFFER_TOKEN, continueTokenBytes);
clientContext.initialize(clientContext.getHandle(), continueTokenBuffer, "localhost");
}
Assert.assertTrue(authenticated);
Assert.assertTrue(filterChain.getRequest() instanceof NegotiateRequestWrapper);
Assert.assertTrue(filterChain.getResponse() instanceof SimpleHttpResponse);
final NegotiateRequestWrapper wrappedRequest = (NegotiateRequestWrapper) filterChain.getRequest();
Assert.assertEquals(NegotiateSecurityFilterTests.NEGOTIATE.toUpperCase(), wrappedRequest.getAuthType());
Assert.assertEquals(Secur32Util.getUserNameEx(EXTENDED_NAME_FORMAT.NameSamCompatible),
wrappedRequest.getRemoteUser());
Assert.assertTrue(wrappedRequest.getUserPrincipal() instanceof WindowsPrincipal);
final String everyoneGroupName = Advapi32Util.getAccountBySid("S-1-1-0").name;
Assert.assertTrue(wrappedRequest.isUserInRole(everyoneGroupName));
Assert.assertTrue(wrappedRequest.isUserInRole("S-1-1-0"));
} finally {
if (clientContext != null) {
clientContext.dispose();
}
if (clientCredentials != null) {
clientCredentials.dispose();
}
}
}
/**
* Test negotiate previous auth with windows principal.
*
* @throws IOException
* Signals that an I/O exception has occurred.
* @throws ServletException
* the servlet exception
*/
@Test
public void testNegotiatePreviousAuthWithWindowsPrincipal() throws IOException, ServletException {
final MockWindowsIdentity mockWindowsIdentity = new MockWindowsIdentity("user", new ArrayList<String>());
final SimpleHttpRequest request = new SimpleHttpRequest();
final WindowsPrincipal windowsPrincipal = new WindowsPrincipal(mockWindowsIdentity);
request.setUserPrincipal(windowsPrincipal);
final SimpleFilterChain filterChain = new SimpleFilterChain();
final SimpleHttpResponse response = new SimpleHttpResponse();
this.filter.doFilter(request, response, filterChain);
Assert.assertTrue(filterChain.getRequest() instanceof NegotiateRequestWrapper);
final NegotiateRequestWrapper wrappedRequest = (NegotiateRequestWrapper) filterChain.getRequest();
Assert.assertTrue(wrappedRequest.getUserPrincipal() instanceof WindowsPrincipal);
Assert.assertEquals(windowsPrincipal, wrappedRequest.getUserPrincipal());
}
/**
* Test challenge ntlmpost.
*
* @throws IOException
* Signals that an I/O exception has occurred.
* @throws ServletException
* the servlet exception
*/
@Test
public void testChallengeNTLMPOST() throws IOException, ServletException {
final MockWindowsIdentity mockWindowsIdentity = new MockWindowsIdentity("user", new ArrayList<String>());
final SimpleHttpRequest request = new SimpleHttpRequest();
final WindowsPrincipal windowsPrincipal = new WindowsPrincipal(mockWindowsIdentity);
request.setUserPrincipal(windowsPrincipal);
request.setMethod("POST");
request.setContentLength(0);
request.addHeader("Authorization", "NTLM TlRMTVNTUAABAAAABzIAAAYABgArAAAACwALACAAAABXT1JLU1RBVElPTkRPTUFJTg==");
final SimpleFilterChain filterChain = new SimpleFilterChain();
final SimpleHttpResponse response = new SimpleHttpResponse();
this.filter.doFilter(request, response, filterChain);
Assert.assertEquals(401, response.getStatus());
final String[] wwwAuthenticates = response.getHeaderValues("WWW-Authenticate");
Assert.assertEquals(1, wwwAuthenticates.length);
Assert.assertTrue(wwwAuthenticates[0].startsWith("NTLM "));
Assert.assertEquals(3, response.getHeaderNamesSize());
Assert.assertEquals("keep-alive", response.getHeader("Connection"));
Assert.assertEquals(401, response.getStatus());
}
/**
* Test challenge ntlmput.
*
* @throws IOException
* Signals that an I/O exception has occurred.
* @throws ServletException
* the servlet exception
*/
@Test
public void testChallengeNTLMPUT() throws IOException, ServletException {
final MockWindowsIdentity mockWindowsIdentity = new MockWindowsIdentity("user", new ArrayList<String>());
final SimpleHttpRequest request = new SimpleHttpRequest();
final WindowsPrincipal windowsPrincipal = new WindowsPrincipal(mockWindowsIdentity);
request.setUserPrincipal(windowsPrincipal);
request.setMethod("PUT");
request.setContentLength(0);
request.addHeader("Authorization", "NTLM TlRMTVNTUAABAAAABzIAAAYABgArAAAACwALACAAAABXT1JLU1RBVElPTkRPTUFJTg==");
final SimpleFilterChain filterChain = new SimpleFilterChain();
final SimpleHttpResponse response = new SimpleHttpResponse();
this.filter.doFilter(request, response, filterChain);
Assert.assertEquals(401, response.getStatus());
final String[] wwwAuthenticates = response.getHeaderValues("WWW-Authenticate");
Assert.assertEquals(1, wwwAuthenticates.length);
Assert.assertTrue(wwwAuthenticates[0].startsWith("NTLM "));
Assert.assertEquals(3, response.getHeaderNamesSize());
Assert.assertEquals("keep-alive", response.getHeader("Connection"));
Assert.assertEquals(401, response.getStatus());
}
/**
* Test init basic security filter provider.
*
* @throws ServletException
* the servlet exception
*/
@Test
public void testInitBasicSecurityFilterProvider() throws ServletException {
final SimpleFilterConfig filterConfig = new SimpleFilterConfig();
filterConfig.setParameter("principalFormat", "sid");
filterConfig.setParameter("roleFormat", "none");
filterConfig.setParameter("allowGuestLogin", "true");
filterConfig.setParameter("securityFilterProviders", "waffle.servlet.spi.BasicSecurityFilterProvider\n");
filterConfig.setParameter("waffle.servlet.spi.BasicSecurityFilterProvider/realm", "DemoRealm");
filterConfig.setParameter("authProvider", MockWindowsAuthProvider.class.getName());
this.filter.init(filterConfig);
Assert.assertEquals(this.filter.getPrincipalFormat(), PrincipalFormat.SID);
Assert.assertEquals(this.filter.getRoleFormat(), PrincipalFormat.NONE);
Assert.assertTrue(this.filter.isAllowGuestLogin());
Assert.assertEquals(1, this.filter.getProviders().size());
Assert.assertTrue(this.filter.getAuth() instanceof MockWindowsAuthProvider);
}
/**
* Test init two security filter providers.
*
* @throws ServletException
* the servlet exception
*/
@Test
public void testInitTwoSecurityFilterProviders() throws ServletException {
// make sure that providers can be specified separated by any kind of space
final SimpleFilterConfig filterConfig = new SimpleFilterConfig();
filterConfig.setParameter("securityFilterProviders", "waffle.servlet.spi.BasicSecurityFilterProvider\n"
+ "waffle.servlet.spi.NegotiateSecurityFilterProvider waffle.servlet.spi.BasicSecurityFilterProvider");
this.filter.init(filterConfig);
Assert.assertEquals(3, this.filter.getProviders().size());
}
/**
* Test init negotiate security filter provider.
*
* @throws ServletException
* the servlet exception
*/
@Test
public void testInitNegotiateSecurityFilterProvider() throws ServletException {
final SimpleFilterConfig filterConfig = new SimpleFilterConfig();
filterConfig.setParameter("securityFilterProviders", "waffle.servlet.spi.NegotiateSecurityFilterProvider\n");
filterConfig.setParameter("waffle.servlet.spi.NegotiateSecurityFilterProvider/protocols",
"NTLM\nNegotiate NTLM");
this.filter.init(filterConfig);
Assert.assertEquals(this.filter.getPrincipalFormat(), PrincipalFormat.FQN);
Assert.assertEquals(this.filter.getRoleFormat(), PrincipalFormat.FQN);
Assert.assertTrue(this.filter.isAllowGuestLogin());
Assert.assertEquals(1, this.filter.getProviders().size());
}
/**
* Test init negotiate security filter provider invalid protocol.
*/
@Test
public void testInitNegotiateSecurityFilterProviderInvalidProtocol() {
final SimpleFilterConfig filterConfig = new SimpleFilterConfig();
filterConfig.setParameter("securityFilterProviders", "waffle.servlet.spi.NegotiateSecurityFilterProvider\n");
filterConfig.setParameter("waffle.servlet.spi.NegotiateSecurityFilterProvider/protocols", "INVALID");
try {
this.filter.init(filterConfig);
Assert.fail("expected ServletException");
} catch (final ServletException e) {
Assert.assertEquals("java.lang.RuntimeException: Unsupported protocol: INVALID", e.getMessage());
}
}
/**
* Test init invalid parameter.
*/
@Test
public void testInitInvalidParameter() {
try {
final SimpleFilterConfig filterConfig = new SimpleFilterConfig();
filterConfig.setParameter("invalidParameter", "random");
this.filter.init(filterConfig);
Assert.fail("expected ServletException");
} catch (final ServletException e) {
Assert.assertEquals("Invalid parameter: invalidParameter", e.getMessage());
}
}
/**
* Test init invalid class in parameter.
*/
@Test
public void testInitInvalidClassInParameter() {
try {
final SimpleFilterConfig filterConfig = new SimpleFilterConfig();
filterConfig.setParameter("invalidClass/invalidParameter", "random");
this.filter.init(filterConfig);
Assert.fail("expected ServletException");
} catch (final ServletException e) {
Assert.assertEquals("java.lang.ClassNotFoundException: invalidClass", e.getMessage());
}
}
}