Browse Source

Add more validation to plugin loading.

tags/v1.0.0
Andrew Belt 6 years ago
parent
commit
3f513e1adb
2 changed files with 16 additions and 7 deletions
  1. +7
    -0
      src/plugin.cpp
  2. +9
    -7
      src/plugin/Plugin.cpp

+ 7
- 0
src/plugin.cpp View File

@@ -143,6 +143,13 @@ static Plugin *loadPlugin(std::string path) {
throw UserException(string::f("Plugin %s is already loaded, not attempting to load it again", plugin->slug.c_str()));
}

// Check that all modules have names
for (Model *model : plugin->models) {
if (model->name == "") {
throw UserException(string::f("Module %s has no name", model->slug.c_str()));
}
}

// Normalize tags
for (Model *model : plugin->models) {
std::vector<std::string> normalizedTags;


+ 9
- 7
src/plugin/Plugin.cpp View File

@@ -17,10 +17,6 @@ Plugin::~Plugin() {
void Plugin::addModel(Model *model) {
// Check that the model is not added to a plugin already
assert(!model->plugin);
// Check model slug
if (!isSlugValid(model->slug)) {
throw UserException(string::f("Module slug \"%s\" is invalid", model->slug.c_str()));
}
model->plugin = this;
models.push_back(model);
}
@@ -93,13 +89,19 @@ void Plugin::fromJson(json_t *rootJ) {
json_t *moduleJ;
json_array_foreach(modulesJ, moduleId, moduleJ) {
json_t *modelSlugJ = json_object_get(moduleJ, "slug");
if (!modelSlugJ)
continue;
if (!modelSlugJ) {
throw UserException(string::f("No slug found for module entry %d", moduleId));
}
std::string modelSlug = json_string_value(modelSlugJ);

// Check model slug
if (!isSlugValid(modelSlug)) {
throw UserException(string::f("Module slug \"%s\" is invalid", modelSlug.c_str()));
}

Model *model = getModel(modelSlug);
if (!model) {
throw UserException(string::f("plugin.json of \"%s\" contains module \"%s\" but it is not defined in the plugin", slug.c_str(), modelSlug.c_str()));
throw UserException(string::f("Manifest contains module %s but it is not defined in the plugin", modelSlug.c_str()));
}

model->fromJson(moduleJ);


Loading…
Cancel
Save