Skip to content

Commit 9baad08

Browse files
committed
GUACAMOLE-1855: Use common code for checking for IP in list.
1 parent ef4da2f commit 9baad08

File tree

2 files changed

+30
-70
lines changed

2 files changed

+30
-70
lines changed

extensions/guacamole-auth-duo/src/main/java/org/apache/guacamole/auth/duo/UserVerificationService.java

+13-34
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.apache.guacamole.net.auth.AuthenticatedUser;
4141
import org.apache.guacamole.net.auth.Credentials;
4242
import org.apache.guacamole.net.auth.credentials.CredentialsInfo;
43+
import org.apache.guacamole.properties.IPAddressListProperty;
4344
import org.slf4j.Logger;
4445
import org.slf4j.LoggerFactory;
4546
import org.springframework.web.util.UriComponentsBuilder;
@@ -103,52 +104,30 @@ public void verifyAuthenticatedUser(AuthenticatedUser authenticatedUser)
103104
if (username == null || username.equals(AuthenticatedUser.ANONYMOUS_IDENTIFIER))
104105
return;
105106

106-
// We enforce by default
107-
boolean enforceHost = true;
108-
109-
// Check for a list of addresses that should be bypassed and iterate
107+
// Pull address lists to check from configuration. Note that the enforce
108+
// list will override the bypass list, which means that, if the client
109+
// address happens to be in both lists, Duo MFA will be enforced.
110110
List<IPAddress> bypassAddresses = confService.getBypassHosts();
111-
for (IPAddress bypassAddr : bypassAddresses) {
112-
113-
// If the address contains current client address, flip enforce flag
114-
// and break out
115-
if (clientAddr != null && clientAddr.isIPAddress()
116-
&& bypassAddr.getIPVersion().equals(clientAddr.getIPVersion())
117-
&& bypassAddr.contains(clientAddr)) {
118-
enforceHost = false;
119-
break;
120-
}
121-
}
122-
123-
// Check for a list of addresses that should be enforced and iterate
124111
List<IPAddress> enforceAddresses = confService.getEnforceHosts();
125112

113+
// Check if the bypass list contains the client address, and set the
114+
// enforce flag to the opposite.
115+
boolean enforceHost = !(IPAddressListProperty.addressListContains(bypassAddresses, clientAddr));
116+
126117
// Only continue processing if the list is not empty
127118
if (!enforceAddresses.isEmpty()) {
128119

129120
// If client address is not available or invalid, MFA will
130121
// be enforced.
131-
if (clientAddr == null || !clientAddr.isIPAddress()) {
122+
if (clientAddr == null || !clientAddr.isIPAddress())
132123
enforceHost = true;
133-
}
134124

135-
else {
136-
// With addresses set, this default changes to false.
137-
enforceHost = false;
138-
139-
for (IPAddress enforceAddr : enforceAddresses) {
140-
141-
// If there's a match, flip the enforce flag and break out of the loop
142-
if (enforceAddr.getIPVersion().equals(clientAddr.getIPVersion())
143-
&& enforceAddr.contains(clientAddr)) {
144-
enforceHost = true;
145-
break;
146-
}
147-
}
148-
}
125+
// Check the enforce list for the client address and set enforcement flag.
126+
else
127+
enforceHost = IPAddressListProperty.addressListContains(enforceAddresses, clientAddr);
149128
}
150129

151-
// If the enforce flag has been changed, exit, bypassing Duo MFA.
130+
// If the enforce flag is not true, bypass Duo MFA.
152131
if (!enforceHost)
153132
return;
154133

extensions/guacamole-auth-totp/src/main/java/org/apache/guacamole/auth/totp/user/UserVerificationService.java

+17-36
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.apache.guacamole.net.auth.UserContext;
4848
import org.apache.guacamole.net.auth.UserGroup;
4949
import org.apache.guacamole.net.auth.credentials.CredentialsInfo;
50+
import org.apache.guacamole.properties.IPAddressListProperty;
5051
import org.apache.guacamole.totp.TOTPGenerator;
5152
import org.slf4j.Logger;
5253
import org.slf4j.LoggerFactory;
@@ -296,57 +297,37 @@ public void verifyIdentity(UserContext context,
296297
HttpServletRequest request = credentials.getRequest();
297298

298299
// Get the current client address
299-
IPAddressString clientAddr = new IPAddressString(request.getRemoteAddr());
300+
IPAddress clientAddr = new IPAddressString(request.getRemoteAddr()).getAddress();
300301

301302
// Ignore anonymous users
302303
if (authenticatedUser.getIdentifier().equals(AuthenticatedUser.ANONYMOUS_IDENTIFIER))
303304
return;
304305

305-
// We enforce by default
306-
boolean enforceHost = true;
307-
308-
// Check for a list of addresses that should be bypassed and iterate
306+
// Pull address lists to check from configuration. Note that the enforce
307+
// list will override the bypass list, which means that, if the client
308+
// address happens to be in both lists, Duo MFA will be enforced.
309309
List<IPAddress> bypassAddresses = confService.getBypassHosts();
310-
for (IPAddress bypassAddr : bypassAddresses) {
311-
// If the address contains current client address, flip enforce flag
312-
// and break out
313-
if (clientAddr != null && clientAddr.isIPAddress()
314-
&& bypassAddr.getIPVersion().equals(clientAddr.getIPVersion())
315-
&& bypassAddr.contains(clientAddr.getAddress())) {
316-
enforceHost = false;
317-
break;
318-
}
319-
}
320-
321-
// Check for a list of addresses that should be enforced and iterate
322310
List<IPAddress> enforceAddresses = confService.getEnforceHosts();
323311

312+
// Check the bypass list for the client address, and set the enforce
313+
// flag to the opposite.
314+
boolean enforceHost = !(IPAddressListProperty.addressListContains(bypassAddresses, clientAddr));
315+
324316
// Only continue processing if the list is not empty
325317
if (!enforceAddresses.isEmpty()) {
326318

327-
if (clientAddr == null || !clientAddr.isIPAddress()) {
328-
logger.warn("Client address is not valid, "
329-
+ "MFA will be enforced.");
319+
// If client address is not available or invalid, MFA will
320+
// be enforced.
321+
if (clientAddr == null || !clientAddr.isIPAddress())
330322
enforceHost = true;
331-
}
332323

333-
else {
334-
// With addresses set, this default changes to false.
335-
enforceHost = false;
336-
337-
for (IPAddress enforceAddr : enforceAddresses) {
338-
339-
// If there's a match, flip the enforce flag and break out of the loop
340-
if (enforceAddr.getIPVersion().equals(clientAddr.getIPVersion())
341-
&& enforceAddr.contains(clientAddr.getAddress())) {
342-
enforceHost = true;
343-
break;
344-
}
345-
}
346-
}
324+
// Check the enforce list and set the flag if the client address
325+
// is found in the list.
326+
else
327+
enforceHost = IPAddressListProperty.addressListContains(enforceAddresses, clientAddr);
347328
}
348329

349-
// If the enforce flag has been changed, exit, bypassing TOTP MFA.
330+
// If the enforce flag is not true, bypass TOTP MFA.
350331
if (!enforceHost)
351332
return;
352333

0 commit comments

Comments
 (0)