Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v22.3.x] Validation and url_decode for SCRAM usernames #15284

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/v/redpanda/admin_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
#include <seastar/core/with_scheduling_group.hh>
#include <seastar/coroutine/maybe_yield.hh>
#include <seastar/http/api_docs.hh>
#include <seastar/http/exception.hh>
#include <seastar/http/httpd.hh>
#include <seastar/http/reply.hh>
#include <seastar/http/request.hh>
Expand Down Expand Up @@ -1611,6 +1612,11 @@ admin_server::create_user_handler(std::unique_ptr<ss::httpd::request> req) {

auto username = security::credential_user(doc["username"].GetString());

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 Expand Up @@ -1650,7 +1656,12 @@ admin_server::delete_user_handler(std::unique_ptr<ss::httpd::request> req) {
throw co_await redirect_to_leader(*req, model::controller_ntp);
}

auto user = security::credential_user(req->param["user"]);
ss::sstring user_v;
if (!ss::http::internal::url_decode(req->param["user"], user_v)) {
throw ss::httpd::bad_param_exception{fmt::format(
"Invalid parameter 'user' got {{{}}}", req->param["user"])};
}
auto user = security::credential_user(user_v);

if (!_controller->get_credential_store().local().contains(user)) {
vlog(logger.debug, "User '{}' already gone during deletion", user);
Expand All @@ -1677,7 +1688,12 @@ admin_server::update_user_handler(std::unique_ptr<ss::httpd::request> req) {
throw co_await redirect_to_leader(*req, model::controller_ntp);
}

auto user = security::credential_user(req->param["user"]);
ss::sstring user_v;
if (!ss::http::internal::url_decode(req->param["user"], user_v)) {
throw ss::httpd::bad_param_exception{fmt::format(
"Invalid parameter 'user' got {{{}}}", req->param["user"])};
}
auto user = security::credential_user(user_v);

auto doc = parse_json_body(*req);

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 @@ -64,6 +64,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 @@ -327,6 +329,19 @@ parse_server_final(std::string_view message) {
#endif
}

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 @@ -479,4 +494,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
Loading