Skip to content

Commit

Permalink
Null injection hardening
Browse files Browse the repository at this point in the history
In NuProcessBuilder throw an IllegalArgumentException if commands that
include a null character are passed. This hardens against command line
injection type attacks in applications that use NuProcess. Throwing a
IllegalArgumentException is what Java's ProcessBuilder does when nulls
are passed so this solution was chosen to maintain some consistency with
that.
  • Loading branch information
benhumphreys-atlassian committed Sep 19, 2022
1 parent 4953d61 commit d4005b6
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/main/java/com/zaxxer/nuprocess/NuProcessBuilder.java
Expand Up @@ -107,6 +107,7 @@ public NuProcessBuilder(List<String> commands, Map<String, String> environment)
if (commands == null || commands.isEmpty()) {
throw new IllegalArgumentException("List of commands may not be null or empty");
}
ensureNoNullCharacters(commands);

this.environment = new TreeMap<String, String>(environment);
this.command = new ArrayList<String>(commands);
Expand All @@ -126,6 +127,7 @@ public NuProcessBuilder(List<String> commands)
if (commands == null || commands.isEmpty()) {
throw new IllegalArgumentException("List of commands may not be null or empty");
}
ensureNoNullCharacters(commands);

this.environment = new TreeMap<String, String>(System.getenv());
this.command = new ArrayList<String>(commands);
Expand All @@ -144,9 +146,11 @@ public NuProcessBuilder(String... commands)
if (commands == null || commands.length == 0) {
throw new IllegalArgumentException("List of commands may not be null or empty");
}
List<String> commandsList = Arrays.asList(commands);
ensureNoNullCharacters(commandsList);

this.environment = new TreeMap<String, String>(System.getenv());
this.command = new ArrayList<String>(Arrays.asList(commands));
this.command = new ArrayList<String>(commandsList);
}

/**
Expand Down Expand Up @@ -280,6 +284,14 @@ private void ensureListener()
}
}

private void ensureNoNullCharacters(List<String> commands) {
for (String command : commands) {
if (command.indexOf('\u0000') >= 0) {
throw new IllegalArgumentException("Commands may not contain null characters");
}
}
}

private String[] prepareEnvironment()
{
String[] env = new String[environment.size()];
Expand Down

0 comments on commit d4005b6

Please sign in to comment.