Skip to content

Commit

Permalink
Do not trim user names, passwords, and file paths or file names
Browse files Browse the repository at this point in the history
Use ValidateUtils.hasContent(String, ...) instead of the trimming
ValidateUtils.checkNotNullAndNotEmpty(String, ...) for all strings
that are user names, passwords, or file paths or file names. All of
these might contain leading or trailing blanks that actually are
significant.
  • Loading branch information
tomaswolf committed Nov 8, 2024
1 parent 827e81e commit 0114a5f
Show file tree
Hide file tree
Showing 19 changed files with 32 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ public String getName() {
public boolean executeCommand(
String args, BufferedReader stdin, PrintStream stdout, PrintStream stderr)
throws Exception {
ValidateUtils.checkNotNullAndNotEmpty(args, "No remote directory specified");
ValidateUtils.hasContent(args, "No remote directory specified");

String newPath = resolveRemotePath(args);
SftpClient sftp = getClient();
Expand Down Expand Up @@ -695,7 +695,7 @@ public String getName() {
public boolean executeCommand(
String args, BufferedReader stdin, PrintStream stdout, PrintStream stderr)
throws Exception {
ValidateUtils.checkNotNullAndNotEmpty(args, "No remote directory specified");
ValidateUtils.hasContent(args, "No remote directory specified");

String path = resolveRemotePath(args);
SftpClient sftp = getClient();
Expand Down Expand Up @@ -908,7 +908,7 @@ public String getName() {
public boolean executeCommand(
String args, BufferedReader stdin, PrintStream stdout, PrintStream stderr)
throws Exception {
ValidateUtils.checkNotNullAndNotEmpty(args, "No remote directory specified");
ValidateUtils.hasContent(args, "No remote directory specified");

String path = resolveRemotePath(args);
SftpClient sftp = getClient();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ public void addIdentity(Path path) {
* @param id The identity path to add - never {@code null}
*/
public void addIdentity(String id) {
String path = ValidateUtils.checkNotNullAndNotEmpty(id, "No identity provided");
String path = ValidateUtils.hasContent(id, "No identity provided");
identities.add(path);
appendPropertyValue(IDENTITY_FILE_CONFIG_PROP, id);
}
Expand Down Expand Up @@ -890,8 +890,7 @@ public static String resolveIdentityFilePath(String id, String host, int port, S
PathUtils.appendUserHome(sb);
break;
case LOCAL_USER_MACRO:
sb.append(ValidateUtils.checkNotNullAndNotEmpty(OsUtils.getCurrentUser(),
"No local user name value"));
sb.append(OsUtils.getCurrentUser());
break;
case LOCAL_HOST_MACRO: {
InetAddress address = Objects.requireNonNull(InetAddress.getLocalHost(), "No local address");
Expand All @@ -902,7 +901,7 @@ public static String resolveIdentityFilePath(String id, String host, int port, S
sb.append(ValidateUtils.checkNotNullAndNotEmpty(host, "No remote host provided"));
break;
case REMOTE_USER_MACRO:
sb.append(ValidateUtils.checkNotNullAndNotEmpty(username, "No remote user provided"));
sb.append(ValidateUtils.hasContent(username, "No remote user provided"));
break;
case REMOTE_PORT_MACRO:
ValidateUtils.checkTrue(port > 0, "Bad remote port value: %d", port);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private Path toPath(NamedResource location) {
Objects.requireNonNull(location, "No location provided");

Path path = Paths
.get(ValidateUtils.checkNotNullAndNotEmpty(location.getName(), "No location value for %s", location));
.get(ValidateUtils.hasContent(location.getName(), "No location value for %s", location));
path = path.toAbsolutePath();
path = path.normalize();
return path;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ protected byte[] deriveEncryptionKey(PrivateKeyEncryptionContext encContext, int
byte[] initVector = Objects.requireNonNull(encContext.getInitVector(), "No encryption init vector");
ValidateUtils.checkTrue(initVector.length > 0, "Empty encryption init vector");

String password = ValidateUtils.checkNotNullAndNotEmpty(encContext.getPassword(), "No encryption password");
String password = ValidateUtils.hasContent(encContext.getPassword(), "No encryption password");
byte[] passBytes = password.getBytes(StandardCharsets.UTF_8);
byte[] prevHash = GenericUtils.EMPTY_BYTE_ARRAY;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public String getUsersHomeDir() {
* @see #isCreateHome()
*/
public void setUsersHomeDir(String usersHomeDir) {
this.usersHomeDir = ValidateUtils.checkNotNullAndNotEmpty(usersHomeDir, "No users home dir");
this.usersHomeDir = ValidateUtils.hasContent(usersHomeDir, "No users home dir");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ public Path getDefaultHomeDir() {
}

public void setUserHomeDir(String userName, Path userHomeDir) {
homeDirs.put(ValidateUtils.checkNotNullAndNotEmpty(userName, "No username"),
homeDirs.put(ValidateUtils.hasContent(userName, "No username"),
Objects.requireNonNull(userHomeDir, "No home dir"));
}

public Path getUserHomeDir(String userName) {
return homeDirs.get(ValidateUtils.checkNotNullAndNotEmpty(userName, "No username"));
return homeDirs.get(ValidateUtils.hasContent(userName, "No username"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ protected void resetCacheMap(Collection<?> resources) {
}

for (Object r : resources) {
String resourceKey = ValidateUtils.checkNotNullAndNotEmpty(Objects.toString(r, null), "No resource key value");
String resourceKey = ValidateUtils.hasContent(Objects.toString(r, null), "No resource key value");
if (cacheMap.containsKey(resourceKey)) {
continue;
}
Expand Down Expand Up @@ -126,7 +126,7 @@ protected Iterable<KeyPair> doLoadKeys(SessionContext session, R resource)
IoResource<?> ioResource
= ValidateUtils.checkNotNull(getIoResource(session, resource), "No I/O resource available for %s", resource);
String resourceKey
= ValidateUtils.checkNotNullAndNotEmpty(ioResource.getName(), "No resource string value for %s", resource);
= ValidateUtils.hasContent(ioResource.getName(), "No resource string value for %s", resource);
Iterable<KeyPair> ids;
synchronized (cacheMap) {
// check if lucky enough to have already loaded this file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ public static String getCurrentUser() {
}

username = getCanonicalUser(System.getProperty(CURRENT_USER_OVERRIDE_PROP, System.getProperty("user.name")));
ValidateUtils.checkNotNullAndNotEmpty(username, "No username available");
ValidateUtils.hasContent(username, "No username available");
CURRENT_USER_HOLDER.set(username);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public DefaultAuthorizedKeysAuthenticator(Path path, boolean strict, LinkOption.

public DefaultAuthorizedKeysAuthenticator(String user, Path path, boolean strict, LinkOption... options) {
super(path, options);
this.user = ValidateUtils.checkNotNullAndNotEmpty(user, "No username provided");
this.user = ValidateUtils.hasContent(user, "No username provided");
this.strict = strict;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ protected Path resolveRootDirectory(String command, String[] args) throws IOExce
pathArg = pathArg.substring(1);
}

ValidateUtils.checkNotNullAndNotEmpty(pathArg, "No %s command sub-path specified", args[0]);
ValidateUtils.hasContent(pathArg, "No %s command sub-path specified", args[0]);
return rootDir.resolve(pathArg);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,10 @@ static byte[] decodePrivateKeyBytes(
Map<String, String> headers)
throws GeneralSecurityException {
Objects.requireNonNull(prvBytes, "No encrypted key bytes");
ValidateUtils.checkNotNullAndNotEmpty(algName, "No encryption algorithm", GenericUtils.EMPTY_OBJECT_ARRAY);
ValidateUtils.checkNotNullAndNotEmpty(algName, "No encryption algorithm");
ValidateUtils.checkTrue(numBits > 0, "Invalid encryption key size: %d", numBits);
ValidateUtils.checkNotNullAndNotEmpty(algMode, "No encryption mode", GenericUtils.EMPTY_OBJECT_ARRAY);
ValidateUtils.checkNotNullAndNotEmpty(password, "No encryption password", GenericUtils.EMPTY_OBJECT_ARRAY);
ValidateUtils.checkNotNullAndNotEmpty(algMode, "No encryption mode");
ValidateUtils.hasContent(password, "No encryption password");

if (!"AES".equalsIgnoreCase(algName)) {
throw new NoSuchAlgorithmException("decodePrivateKeyBytes(" + algName + "-" + numBits + "-" + algMode + ") N/A");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public boolean isOpen() {

@Override
public void download(String[] remote, String local, Collection<Option> options) throws IOException {
local = ValidateUtils.checkNotNullAndNotEmpty(local, "Invalid argument local: %s", local);
local = ValidateUtils.hasContent(local, "Invalid argument local: %s", local);
remote = ValidateUtils.checkNotNullAndNotEmpty(remote, "Invalid argument remote: %s", (Object) remote);

if (remote.length > 1) {
Expand Down Expand Up @@ -87,7 +87,7 @@ public void download(String[] remote, Path local, Collection<Option> options) th
@Override
public void download(String remote, Path local, Collection<Option> options) throws IOException {
local = ValidateUtils.checkNotNull(local, "Invalid argument local: %s", local);
remote = ValidateUtils.checkNotNullAndNotEmpty(remote, "Invalid argument remote: %s", remote);
remote = ValidateUtils.hasContent(remote, "Invalid argument remote: %s", remote);

LinkOption[] opts = IoUtils.getLinkOptions(true);
if (Files.isDirectory(local, opts)) {
Expand All @@ -114,7 +114,7 @@ public void download(String remote, Path local, Collection<Option> options) thro

@Override
public void download(String remote, String local, Collection<Option> options) throws IOException {
local = ValidateUtils.checkNotNullAndNotEmpty(local, "Invalid argument local: %s", local);
local = ValidateUtils.hasContent(local, "Invalid argument local: %s", local);

ClientSession session = getClientSession();
FactoryManager manager = session.getFactoryManager();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ protected void download(String remote, FileSystem fs, Path local, Collection<Opt
public void upload(
InputStream local, String remote, long size, Collection<PosixFilePermission> perms, ScpTimestampCommandDetails time)
throws IOException {
int namePos = ValidateUtils.checkNotNullAndNotEmpty(remote, "No remote location specified").lastIndexOf('/');
int namePos = ValidateUtils.hasContent(remote, "No remote location specified").lastIndexOf('/');
String name = (namePos < 0)
? remote
: ValidateUtils.checkNotNullAndNotEmpty(remote.substring(namePos + 1), "No name value in remote=%s", remote);
: ValidateUtils.hasContent(remote.substring(namePos + 1), "No name value in remote=%s", remote);
Collection<Option> options = (time != null) ? EnumSet.of(Option.PreserveAttributes) : Collections.emptySet();
String cmd = ScpClient.createSendCommand(remote, options);
ClientSession session = getClientSession();
Expand All @@ -134,7 +134,7 @@ protected <T> void runUpload(
String remote, Collection<Option> options, Collection<T> local, AbstractScpClient.ScpOperationExecutor<T> executor)
throws IOException {
local = ValidateUtils.checkNotNullAndNotEmpty(local, "Invalid argument local: %s", local);
remote = ValidateUtils.checkNotNullAndNotEmpty(remote, "Invalid argument remote: %s", remote);
remote = ValidateUtils.hasContent(remote, "Invalid argument remote: %s", remote);
if (local.size() > 1) {
options = addTargetIsDirectory(options);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ default void upload(String local, String remote, Option... options) throws IOExc
}

default void upload(String local, String remote, Collection<Option> options) throws IOException {
upload(new String[] { ValidateUtils.checkNotNullAndNotEmpty(local, "Invalid argument local: %s", local) }, remote,
upload(new String[] { ValidateUtils.hasContent(local, "Invalid argument local: %s", local) }, remote,
options);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class Handle {

Handle(String path, byte[] id) {
// clone the original so the handle is immutable
this.path = ValidateUtils.checkNotNullAndNotEmpty(path, "No remote path");
this.path = ValidateUtils.hasContent(path, "No remote path");
this.id = ValidateUtils.checkNotNullAndNotEmpty(id, "No handle ID").clone();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public String getBasedir() {
* form when scanning. May not be {@code null}/empty
*/
public void setBasedir(String basedir) {
this.basedir = ValidateUtils.checkNotNullAndNotEmpty(basedir, "No base directory provided");
this.basedir = ValidateUtils.hasContent(basedir, "No base directory provided");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1568,7 +1568,7 @@ public static URI createFileSystemURI(String host, int port, String username, St
}

public static String encodeCredentials(String username, String password) {
ValidateUtils.checkNotNullAndNotEmpty(username, "No username provided");
ValidateUtils.hasContent(username, "No username provided");

/*
* There is no way to properly encode/decode credentials that already contain colon. See also
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public SftpRemotePathChannel(String path, SftpClient sftp, boolean closeOnExit,
Collection<OpenMode> modes)
throws IOException {
this.log = LoggerFactory.getLogger(getClass());
this.path = ValidateUtils.checkNotNullAndNotEmpty(path, "No remote file path specified");
this.path = ValidateUtils.hasContent(path, "No remote file path specified");
this.modes = Collections.unmodifiableSet(EnumSet.copyOf(modes));
if (this.modes.isEmpty()) {
throw new IllegalArgumentException("At least one OpenMode is required for a SftpRemotePathChannel");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public String getUsername() {
*/
@Override
public void setUsername(String user) {
this.userValue = ValidateUtils.checkNotNullAndNotEmpty(user, "No user specified: %s", user);
this.userValue = ValidateUtils.hasContent(user, "No user specified: %s", user);
}

@Override
Expand Down Expand Up @@ -447,7 +447,7 @@ protected ClientSession resolveClientSession(boolean sharedInstance) throws Exce

protected ClientSession createClientSession() throws Exception {
String hostname = ValidateUtils.checkNotNullAndNotEmpty(getHost(), "Host must not be empty");
String username = ValidateUtils.checkNotNullAndNotEmpty(getUsername(), "User must not be empty");
String username = ValidateUtils.hasContent(getUsername(), "User must not be empty");
ClientSession session
= createClientSession(hostname, username, getPort(), getEffectiveTimeoutValue(getConnectTimeout()));
try {
Expand Down

0 comments on commit 0114a5f

Please sign in to comment.