From 69c5ec8983ead07475752cbadba82373636a7a21 Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Fri, 31 May 2019 20:40:26 -0400 Subject: [PATCH] Normalize plugin and model slugs when loading patches. Fail with warning when model slug is invalid, instead of crashing. --- include/plugin.hpp | 2 ++ src/plugin.cpp | 13 ++++++++++++- src/plugin/Plugin.cpp | 8 +++++++- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/include/plugin.hpp b/include/plugin.hpp index b53a8b11..f5621949 100644 --- a/include/plugin.hpp +++ b/include/plugin.hpp @@ -27,6 +27,8 @@ Model *getModel(const std::string &pluginSlug, const std::string &modelSlug); std::string normalizeTag(const std::string &tag); /** Checks that the slug contains only alphanumeric characters, "-", and "_" */ bool isSlugValid(const std::string &slug); +/** Returns a string containing only the valid slug characters. */ +std::string normalizeSlug(const std::string &slug); struct Update { diff --git a/src/plugin.cpp b/src/plugin.cpp index 8e87b231..c4e80269 100644 --- a/src/plugin.cpp +++ b/src/plugin.cpp @@ -485,8 +485,9 @@ bool isLoggedIn() { } Plugin *getPlugin(const std::string &pluginSlug) { + std::string slug = normalizeSlug(pluginSlug); for (Plugin *plugin : plugins) { - if (plugin->slug == pluginSlug) { + if (plugin->slug == slug) { return plugin; } } @@ -610,6 +611,16 @@ bool isSlugValid(const std::string &slug) { return true; } +std::string normalizeSlug(const std::string &slug) { + std::string s; + for (char c : slug) { + if (!(std::isalnum(c) || c == '-' || c == '_')) + continue; + s += c; + } + return s; +} + std::vector plugins; diff --git a/src/plugin/Plugin.cpp b/src/plugin/Plugin.cpp index e7da130f..f5c10ca5 100644 --- a/src/plugin/Plugin.cpp +++ b/src/plugin/Plugin.cpp @@ -14,13 +14,19 @@ Plugin::~Plugin() { } void Plugin::addModel(Model *model) { - assert(isSlugValid(model->slug)); + // Check that the model is not added to a plugin already assert(!model->plugin); + // Check model slug + if (!isSlugValid(model->slug)) { + WARN("Module slug \"%s\" is invalid", model->slug.c_str()); + return; + } model->plugin = this; models.push_back(model); } Model *Plugin::getModel(std::string slug) { + slug = normalizeSlug(slug); for (Model *model : models) { if (model->slug == slug) { return model;