Skip to content

Commit

Permalink
admin/security: Add SCRAM username validation to create_user_handler
Browse files Browse the repository at this point in the history
Includes simple validator against SASLNAME regex in scram_algorithm.cc
  • Loading branch information
oleiman committed Dec 1, 2023
1 parent 30ce6f1 commit 75b0cb6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/v/redpanda/admin/security.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "security/scram_credential.h"

#include <seastar/coroutine/as_future.hh>
#include <seastar/http/exception.hh>

namespace {

Expand Down Expand Up @@ -172,6 +173,11 @@ admin_server::create_user_handler(std::unique_ptr<ss::http::request> req) {
auto username = security::credential_user(doc["username"].GetString());
validate_no_control(username(), string_conversion_exception{username()});

if (!security::validate_scram_username(username())) {
throw ss::httpd::bad_request_exception(
fmt::format("Invalid SCRAM username {{{}}}", username()));
}

if (is_no_op_user_write(
_controller->get_credential_store().local(), username, credential)) {
vlog(
Expand Down
20 changes: 20 additions & 0 deletions src/v/security/scram_algorithm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
// NOLINTNEXTLINE
#define SASLNAME "(?:" VALUE_SAFE_CHAR "|=2C|=3D)+"

#define BARE_SASLNAME "(" SASLNAME ")"

// value-char = value-safe-char / "="
// value = 1*value-char
// NOLINTNEXTLINE
Expand Down Expand Up @@ -187,6 +189,19 @@ parse_server_final(std::string_view message) {
return server_final_match{.error = ss::sstring(spv(error))};
}

static std::optional<ss::sstring> parse_saslname(std::string_view message) {
static thread_local const re2::RE2 re(BARE_SASLNAME, re2::RE2::Quiet);
vassert(re.ok(), "saslname regex failure: {}", re.error());

re2::StringPiece username;

if (!re2::RE2::FullMatch(message, re, &username)) {
return std::nullopt;
}

return ss::sstring(spv(username));
}

namespace security {

client_first_message::client_first_message(bytes_view data) {
Expand Down Expand Up @@ -343,4 +358,9 @@ server_final_message::server_final_message(bytes_view data) {
_signature = std::move(match->signature);
}

bool validate_scram_username(std::string_view username) {
auto match = parse_saslname(username);
return match.has_value() && match.value() == username;
}

} // namespace security
2 changes: 2 additions & 0 deletions src/v/security/scram_algorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,8 @@ class scram_algorithm {
}
};

bool validate_scram_username(std::string_view username);

// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
using scram_sha512 = scram_algorithm<hmac_sha512, hash_sha512, 130, 4096>;

Expand Down

0 comments on commit 75b0cb6

Please sign in to comment.