diff --git a/BedrockPlugin.cpp b/BedrockPlugin.cpp index 0c06ddf1b..007261550 100644 --- a/BedrockPlugin.cpp +++ b/BedrockPlugin.cpp @@ -13,6 +13,11 @@ BedrockPlugin::~BedrockPlugin() { } } +bool BedrockPlugin::isValidDate(const string& date) +{ + return SREMatch("^(19|2[0-9])\\d\\d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])( \\d{2}:\\d{2}:\\d{2})?$", date); +} + void BedrockPlugin::verifyAttributeInt64(const SData& request, const string& name, size_t minSize) { if (request[name].size() < minSize) { STHROW("402 Missing " + name); @@ -41,6 +46,17 @@ void BedrockPlugin::verifyAttributeBool(const SData& request, const string& name } } +void BedrockPlugin::verifyAttributeDate(const SData& request, const char* key, bool require) +{ + if (require && request[key].empty()) { + STHROW("402 Missing " + string(key)); + } + + if (!request[key].empty() && !isValidDate(request[key])) { + STHROW("402 Malformed " + string(key)); + } +} + STable BedrockPlugin::getInfo() { return STable(); } diff --git a/BedrockPlugin.h b/BedrockPlugin.h index 547ef9cc9..6a1892ad6 100644 --- a/BedrockPlugin.h +++ b/BedrockPlugin.h @@ -11,10 +11,16 @@ class BedrockPlugin { static constexpr int64_t MAX_SIZE_BLOB = 1024 * 1024; static constexpr int64_t MAX_SIZE_SMALL = 255; + /** + * Check if a date or datetime is valid + */ + static bool isValidDate(const string& date); + // Utility functions for verifying expected input. static void verifyAttributeInt64(const SData& request, const string& name, size_t minSize); static void verifyAttributeSize(const SData& request, const string& name, size_t minSize, size_t maxSize); static void verifyAttributeBool(const SData& request, const string& name, bool require = true); + static void verifyAttributeDate(const SData& request, const char* key, bool require); BedrockPlugin(BedrockServer& s); virtual ~BedrockPlugin();