GUACAMOLE-5: Move identifier validity check to ModeledDirectoryObjectService. It must be object-specific, as not all objects have numeric identifiers in the database auth.

This commit is contained in:
Michael Jumper
2016-07-30 10:54:32 -07:00
parent 99c650309a
commit 690e8996e3
2 changed files with 57 additions and 61 deletions

View File

@@ -23,7 +23,6 @@ import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Set; import java.util.Set;
import javax.xml.stream.events.Characters;
import org.apache.guacamole.auth.jdbc.user.ModeledAuthenticatedUser; import org.apache.guacamole.auth.jdbc.user.ModeledAuthenticatedUser;
import org.apache.guacamole.GuacamoleException; import org.apache.guacamole.GuacamoleException;
import org.apache.guacamole.GuacamoleSecurityException; import org.apache.guacamole.GuacamoleSecurityException;
@@ -289,6 +288,62 @@ public abstract class ModeledDirectoryObjectService<InternalType extends Modeled
} }
/**
* Returns whether the given string is a valid identifier within the JDBC
* authentication extension. Invalid identifiers may result in SQL errors
* from the underlying database when used in queries.
*
* @param identifier
* The string to check for validity.
*
* @return
* true if the given string is a valid identifier, false otherwise.
*/
protected boolean isValidIdentifier(String identifier) {
// Empty identifiers are invalid
if (identifier.isEmpty())
return false;
// Identifier is invalid if any non-numeric characters are present
for (int i = 0; i < identifier.length(); i++) {
if (!Character.isDigit(identifier.charAt(i)))
return false;
}
// Identifier is valid - contains only numeric characters
return true;
}
/**
* Filters the given collection of strings, returning a new collection
* containing only those strings which are valid identifiers. If no strings
* within the collection are valid identifiers, the returned collection will
* simply be empty.
*
* @param identifiers
* The collection of strings to filter.
*
* @return
* A new collection containing only the strings within the provided
* collection which are valid identifiers.
*/
protected Collection<String> filterIdentifiers(Collection<String> identifiers) {
// Obtain enough space for a full copy of the given identifiers
Collection<String> validIdentifiers = new ArrayList<String>(identifiers.size());
// Add only valid identifiers to the copy
for (String identifier : identifiers) {
if (isValidIdentifier(identifier))
validIdentifiers.add(identifier);
}
return validIdentifiers;
}
@Override @Override
public InternalType retrieveObject(ModeledAuthenticatedUser user, public InternalType retrieveObject(ModeledAuthenticatedUser user,
String identifier) throws GuacamoleException { String identifier) throws GuacamoleException {
@@ -314,7 +369,7 @@ public abstract class ModeledDirectoryObjectService<InternalType extends Modeled
Collection<String> identifiers) throws GuacamoleException { Collection<String> identifiers) throws GuacamoleException {
// Ignore invalid identifiers // Ignore invalid identifiers
identifiers = ObjectModel.filterIdentifiers(identifiers); identifiers = filterIdentifiers(identifiers);
// Do not query if no identifiers given // Do not query if no identifiers given
if (identifiers.isEmpty()) if (identifiers.isEmpty())

View File

@@ -19,9 +19,6 @@
package org.apache.guacamole.auth.jdbc.base; package org.apache.guacamole.auth.jdbc.base;
import java.util.ArrayList;
import java.util.Collection;
/** /**
* Object representation of a Guacamole object, such as a user or connection, * Object representation of a Guacamole object, such as a user or connection,
* as represented in the database. * as represented in the database.
@@ -87,60 +84,4 @@ public abstract class ObjectModel {
this.objectID = objectID; this.objectID = objectID;
} }
/**
* Returns whether the given string is a valid identifier within the JDBC
* authentication extension. Invalid identifiers may result in SQL errors
* from the underlying database when used in queries.
*
* @param identifier
* The string to check for validity.
*
* @return
* true if the given string is a valid identifier, false otherwise.
*/
public static boolean isValidIdentifier(String identifier) {
// Empty identifiers are invalid
if (identifier.isEmpty())
return false;
// Identifier is invalid if any non-numeric characters are present
for (int i = 0; i < identifier.length(); i++) {
if (!Character.isDigit(identifier.charAt(i)))
return false;
}
// Identifier is valid - contains only numeric characters
return true;
}
/**
* Filters the given collection of strings, returning a new collection
* containing only those strings which are valid identifiers. If no strings
* within the collection are valid identifiers, the returned collection will
* simply be empty.
*
* @param identifiers
* The collection of strings to filter.
*
* @return
* A new collection containing only the strings within the provided
* collection which are valid identifiers.
*/
public static Collection<String> filterIdentifiers(Collection<String> identifiers) {
// Obtain enough space for a full copy of the given identifiers
Collection<String> validIdentifiers = new ArrayList<String>(identifiers.size());
// Add only valid identifiers to the copy
for (String identifier : identifiers) {
if (ObjectModel.isValidIdentifier(identifier))
validIdentifiers.add(identifier);
}
return validIdentifiers;
}
} }