From 7112d8affda484e1715617f2247e9ee2b98adc62 Mon Sep 17 00:00:00 2001 From: Greg Schueler Date: Thu, 20 Jun 2019 19:40:47 -0700 Subject: [PATCH] Fix #180 prevent invalid project name --- .../rundeck/client/tool/commands/AppCommand.java | 16 +++++++++++++++- .../tool/options/ProjectRequiredNameOptions.java | 4 +++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/rd-cli-tool/src/main/java/org/rundeck/client/tool/commands/AppCommand.java b/rd-cli-tool/src/main/java/org/rundeck/client/tool/commands/AppCommand.java index ecca5350..030636ad 100644 --- a/rd-cli-tool/src/main/java/org/rundeck/client/tool/commands/AppCommand.java +++ b/rd-cli-tool/src/main/java/org/rundeck/client/tool/commands/AppCommand.java @@ -28,6 +28,9 @@ import java.io.IOException; import java.util.function.Function; +import java.util.regex.Pattern; + +import static org.rundeck.client.tool.options.ProjectRequiredNameOptions.PROJECT_NAME_PATTERN; /** * Base type for commands in Rd @@ -168,7 +171,18 @@ public String projectOrEnv(final ProjectNameOptions options) throws InputError { return options.getProject(); } try { - return getAppConfig().require("RD_PROJECT", "or specify as `-p/--project value` : Project name."); + String + rd_project = + getAppConfig().require("RD_PROJECT", "or specify as `-p/--project value` : Project name."); + Pattern pat = Pattern.compile(PROJECT_NAME_PATTERN); + if (!pat.matcher(rd_project).matches()) { + throw new InputError(String.format( + "Cannot match (%s) to pattern: /%s/ : RD_PROJECT", + rd_project, + PROJECT_NAME_PATTERN + )); + } + return rd_project; } catch (ConfigSource.ConfigSourceError configSourceError) { throw new InputError(configSourceError.getMessage()); } diff --git a/rd-cli-tool/src/main/java/org/rundeck/client/tool/options/ProjectRequiredNameOptions.java b/rd-cli-tool/src/main/java/org/rundeck/client/tool/options/ProjectRequiredNameOptions.java index 1ef4087f..a7064651 100644 --- a/rd-cli-tool/src/main/java/org/rundeck/client/tool/options/ProjectRequiredNameOptions.java +++ b/rd-cli-tool/src/main/java/org/rundeck/client/tool/options/ProjectRequiredNameOptions.java @@ -24,6 +24,8 @@ * @since 4/10/17 */ public interface ProjectRequiredNameOptions { - @Option(shortName = "p", longName = "project", description = "Project name") + static final String PROJECT_NAME_PATTERN = "^[-_a-zA-Z0-9+][-\\._a-zA-Z0-9+]*$"; + + @Option(shortName = "p", longName = "project", description = "Project name", pattern = PROJECT_NAME_PATTERN) String getProject(); }