Skip to content

Commit 2ecca3f

Browse files
fix(secrets): Disallow empty names
The name of secrets are used as a key when updating or deleting them. Thus, they must not be empty. Signed-off-by: Marcel Bochtler <marcel.bochtler@bosch.com>
1 parent 3b57688 commit 2ecca3f

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

components/secrets/api-model/src/commonMain/kotlin/Secret.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ data class PostSecret(
5252
val description: String?
5353
) {
5454
companion object {
55-
val NAME_PATTERN_REGEX = """^(?!\s)[A-Za-z0-9- ]*(?<!\s)$""".toRegex()
56-
const val NAME_PATTERN_MESSAGE = "The entity name may only contain letters, numbers, hyphen marks and " +
57-
"spaces. Leading and trailing whitespaces are not allowed."
55+
val NAME_PATTERN_REGEX = """^(?!\s)[A-Za-z0-9- ]+(?<!\s)$""".toRegex()
56+
const val NAME_PATTERN_MESSAGE = "The entity name must not be empty and may only contain letters, " +
57+
"numbers, hyphen marks and spaces. Leading and trailing whitespaces are not allowed."
5858
}
5959

6060
fun validate() =

components/secrets/backend/src/test/kotlin/routes/organization/PostOrganizationSecretIntegrationTest.kt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,5 +126,35 @@ class PostOrganizationSecretIntegrationTest : SecretsIntegrationTest({
126126
provider.readSecret(Path("organization_${orgId}_${secret.name}"))?.value.shouldBeNull()
127127
}
128128
}
129+
130+
"respond with 'Bad Request' if the secret's name is empty" {
131+
secretsTestApplication { client ->
132+
install(StatusPages) {
133+
// TODO: This should use the same config as in core.
134+
exception<RequestValidationException> { call, e ->
135+
call.respondError(
136+
HttpStatusCode.BadRequest,
137+
message = "Request validation has failed.",
138+
cause = e.message
139+
)
140+
}
141+
}
142+
143+
val secret = PostSecret("", "value", "description")
144+
145+
val response = client.post("/organizations/$orgId/secrets") {
146+
setBody(secret)
147+
}
148+
149+
response shouldHaveStatus HttpStatusCode.BadRequest
150+
151+
val body = response.body<ErrorResponse>()
152+
body.message shouldBe "Request validation has failed."
153+
body.cause shouldContain "Validation failed for PostSecret"
154+
155+
val provider = SecretsProviderFactoryForTesting.instance()
156+
provider.readSecret(Path("organization_${orgId}_${secret.name}"))?.value.shouldBeNull()
157+
}
158+
}
129159
}
130160
})

0 commit comments

Comments
 (0)