From 61e7ab47383645850644dcd89e4a995deb74f929 Mon Sep 17 00:00:00 2001 From: Irena Rindos Date: Thu, 6 Jun 2024 13:03:42 -0400 Subject: [PATCH] Allow descriptions to contain newlines and other whitespace (#2599) * Allow names and descriptions to contain newlines and other whitespace * separate validate name from validate description * update changelog --------- Co-authored-by: Danielle Miu <29378233+DanielleMiu@users.noreply.github.com> --- CHANGELOG.md | 5 +++++ .../handlers/scopes/scope_service.go | 4 ++-- .../daemon/controller/handlers/verifiers.go | 18 +++++++++++++----- .../controller/handlers/verifiers_test.go | 13 ++++++++++--- 4 files changed, 30 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b8a82fc9d..69c3497cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ Canonical reference for changes, improvements, and bugfixes for Boundary. ## Next +### New and Improved + +* Allow descriptions to contain newlines and other whitespace + ([PR](https://github.com/hashicorp/boundary/pull/2599)) + ## 0.16.1 (2024/05/30) ### New and Improved diff --git a/internal/daemon/controller/handlers/scopes/scope_service.go b/internal/daemon/controller/handlers/scopes/scope_service.go index fd5d249da..17cdf0ad2 100644 --- a/internal/daemon/controller/handlers/scopes/scope_service.go +++ b/internal/daemon/controller/handlers/scopes/scope_service.go @@ -1006,7 +1006,7 @@ func validateUpdateRequest(req *pbs.UpdateScopeRequest) error { switch { case trimmed == "": badFields["name"] = "Cannot set empty string as name" - case !handlers.ValidNameDescription(trimmed): + case !handlers.ValidName(trimmed): badFields["name"] = "Name contains unprintable characters" default: item.GetName().Value = trimmed @@ -1017,7 +1017,7 @@ func validateUpdateRequest(req *pbs.UpdateScopeRequest) error { switch { case trimmed == "": badFields["description"] = "Cannot set empty string as description" - case !handlers.ValidNameDescription(trimmed): + case !handlers.ValidDescription(trimmed): badFields["description"] = "Description contains unprintable characters" default: item.GetDescription().Value = trimmed diff --git a/internal/daemon/controller/handlers/verifiers.go b/internal/daemon/controller/handlers/verifiers.go index fd6538690..d87139743 100644 --- a/internal/daemon/controller/handlers/verifiers.go +++ b/internal/daemon/controller/handlers/verifiers.go @@ -41,7 +41,7 @@ func ValidateCreateRequest(i ApiResource, fn CustomValidatorFunc) error { switch { case trimmed == "": badFields["name"] = "Cannot set empty string as name" - case !ValidNameDescription(trimmed): + case !ValidName(trimmed): badFields["name"] = "Name contains unprintable characters" default: i.GetName().Value = trimmed @@ -52,7 +52,7 @@ func ValidateCreateRequest(i ApiResource, fn CustomValidatorFunc) error { switch { case trimmed == "": badFields["description"] = "Cannot set empty string as description" - case !ValidNameDescription(trimmed): + case !ValidDescription(trimmed): badFields["description"] = "Description contains unprintable characters" default: i.GetDescription().Value = trimmed @@ -100,7 +100,7 @@ func ValidateUpdateRequest(r UpdateRequest, i ApiResource, fn CustomValidatorFun switch { case trimmed == "": badFields["name"] = "Cannot set empty string as name" - case !ValidNameDescription(trimmed): + case !ValidName(trimmed): badFields["name"] = "Name contains unprintable characters" default: i.GetName().Value = trimmed @@ -111,7 +111,7 @@ func ValidateUpdateRequest(r UpdateRequest, i ApiResource, fn CustomValidatorFun switch { case trimmed == "": badFields["description"] = "Cannot set empty string as description" - case !ValidNameDescription(trimmed): + case !ValidDescription(trimmed): badFields["description"] = "Description contains unprintable characters" default: i.GetDescription().Value = trimmed @@ -193,7 +193,15 @@ func ValidId(i Id, prefixes ...string) bool { return false } -func ValidNameDescription(in string) bool { +func ValidDescription(in string) bool { + idx := strings.IndexFunc(in, func(c rune) bool { + return !(unicode.IsPrint(c) || unicode.IsSpace(c)) + }) + + return idx == -1 +} + +func ValidName(in string) bool { idx := strings.IndexFunc(in, func(c rune) bool { return !unicode.IsPrint(c) }) diff --git a/internal/daemon/controller/handlers/verifiers_test.go b/internal/daemon/controller/handlers/verifiers_test.go index 072c9b04f..214eca684 100644 --- a/internal/daemon/controller/handlers/verifiers_test.go +++ b/internal/daemon/controller/handlers/verifiers_test.go @@ -44,9 +44,16 @@ func TestValidId(t *testing.T) { assert.False(t, ValidId(Id("prefix_short"), "short")) } -func TestValidNameDescription(t *testing.T) { - assert.True(t, ValidNameDescription("foobar")) - assert.False(t, ValidNameDescription("foo\u200Bbar")) +func TestValidDescription(t *testing.T) { + assert.True(t, ValidDescription("foobar")) + assert.True(t, ValidDescription("this is\n a long description")) + assert.False(t, ValidDescription("foo\u200Bbar")) +} + +func TestValidName(t *testing.T) { + assert.True(t, ValidName("foobar")) + assert.False(t, ValidName("this is\n a long description")) + assert.False(t, ValidName("foo\u200Bbar")) } func TestValidateGetRequest(t *testing.T) {