From 35bc488f5bb6aa33fdb2a01aebee5e5754d9590f Mon Sep 17 00:00:00 2001 From: Nadav Har'El Date: Mon, 6 May 2019 20:11:24 +0300 Subject: [PATCH] alternator: better key type parsing The supported key types are just S(tring), B(lob), or N(umber). Other types are valid for attributes, but not for keys, and should not be accepted. And wrong types used should result in the appropriate user-visible error. Signed-off-by: Nadav Har'El --- alternator/executor.cc | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/alternator/executor.cc b/alternator/executor.cc index 9babcad3bc..88e3b2cbf9 100644 --- a/alternator/executor.cc +++ b/alternator/executor.cc @@ -64,6 +64,7 @@ public: } }; +#if 0 /* not used yet */ /* * Full representation should cover: "B": blob, @@ -96,6 +97,7 @@ static data_type parse_type(sstring type) { } return it->second; } +#endif static sstring type_to_sstring(data_type type) { static thread_local std::unordered_map types = { @@ -190,11 +192,26 @@ future executor::delete_table(sstring content) { }); } +static data_type parse_key_type(const std::string& type) { + // Note that keys are only allowed to be string, blob or number (S/B/N). + // The other types: boolean and various lists or sets - are not allowed. + if (type.length() == 1) { + switch (type[0]) { + case 'S': return utf8_type; + case 'B': return bytes_type; + case 'N': return long_type; // FIXME: this actually needs to be a new number type, not long + } + } + throw api_error(reply::status_type::bad_request, "ValidationException", + format("Invalid key type '{}', can only be S, B or N.", type)); +} + + static void add_column(schema_builder& builder, const std::string& name, const Json::Value& attribute_definitions, column_kind kind) { for (const Json::Value& attribute_info : attribute_definitions) { if (attribute_info["AttributeName"].asString() == name) { - sstring type = attribute_info["AttributeType"].asString(); - builder.with_column(to_bytes(name), parse_type(type), kind); + auto type = attribute_info["AttributeType"].asString(); + builder.with_column(to_bytes(name), parse_key_type(type), kind); return; } }