From 233a261a3f353854800f19d049555343d917f7d9 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sat, 9 Mar 2019 00:17:30 -0500 Subject: [PATCH] fix: Strictly validate service names We previously allowed a wide range of service names. As the service name is sometimes used to name databases, the name was actually more restricted than any character, resulting in services that wouldn't start. Going forward, only alphanumeric and underscore characters are allowed. This only impacts service creation. Any services with invalid names should be migrated to a new service, with the data exported and imported as normal. Closes dokku/dokku-redis#99 Closes dokku/dokku-mysql#47 Closes dokku/dokku-mongo#86 Closes dokku/dokku-redis#81 --- common-functions | 12 ++++++++++++ functions | 2 +- tests/service_create.bats | 8 ++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/common-functions b/common-functions index 697fcd7..d215074 100755 --- a/common-functions +++ b/common-functions @@ -731,3 +731,15 @@ verify_service_name() { [[ ! -d "$PLUGIN_DATA_ROOT/$SERVICE" ]] && dokku_log_fail "$PLUGIN_SERVICE service $SERVICE does not exist" return 0 } + +is_valid_service_name() { + declare desc="Validates a service name" + declare SERVICE="$1" + [[ -z "$SERVICE" ]] && dokku_log_fail "SERVICE must not be null" + + if [[ "$SERVICE" =~ ^[A-Za-z0-9_]+$ ]]; then + return 0 + fi + + return 1 +} diff --git a/functions b/functions index 5c02dd7..086535e 100755 --- a/functions +++ b/functions @@ -21,6 +21,7 @@ service_connect() { service_create() { local SERVICE="$1" + is_valid_service_name "$SERVICE" || dokku_log_fail "Please specify a valid name for the service. Valid characters are: [A-Za-z0-9_]+" [[ -z "$SERVICE" ]] && dokku_log_fail "Please specify a name for the service" [[ ! -d "$PLUGIN_DATA_ROOT/$SERVICE" ]] || dokku_log_fail "$PLUGIN_SERVICE service $SERVICE already exists" SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"; LINKS_FILE="$SERVICE_ROOT/LINKS" @@ -39,7 +40,6 @@ service_create() { mkdir -p "$SERVICE_ROOT" || dokku_log_fail "Unable to create service directory" mkdir -p "$SERVICE_ROOT/data" || dokku_log_fail "Unable to create service data directory" - mkdir -p "$SERVICE_ROOT/config" || dokku_log_fail "Unable to create service config directory" touch "$LINKS_FILE" PASSWORD=$(openssl rand -hex 16) diff --git a/tests/service_create.bats b/tests/service_create.bats index 7ecb87b..777441d 100755 --- a/tests/service_create.bats +++ b/tests/service_create.bats @@ -10,3 +10,11 @@ load test_helper run dokku "$PLUGIN_COMMAND_PREFIX:create" assert_contains "${lines[*]}" "Please specify a name for the service" } + +@test "($PLUGIN_COMMAND_PREFIX:create) error when there is an invalid name specified" { + run dokku "$PLUGIN_COMMAND_PREFIX:create" d.erp + assert_failure + + run dokku "$PLUGIN_COMMAND_PREFIX:create" d-erp + assert_failure +}