From cda8aaa41eb5884229496a73061d86997369e5d0 Mon Sep 17 00:00:00 2001 From: Kevin Bates Date: Mon, 5 Aug 2019 13:52:26 -0700 Subject: [PATCH] Fix extension_name validation Because there is intentionally no default value and extension_name is not a configurable trait (not sure that's the right terminology), the @validate decorator won't be called. This change fixes up the validate function and adds a call to it during extension initialization. Also raise ValueError rather than the base Exception if no value is given (via the @default decorator). --- jupyter_server/extension/application.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/jupyter_server/extension/application.py b/jupyter_server/extension/application.py index a20031535a..791bc9fca6 100644 --- a/jupyter_server/extension/application.py +++ b/jupyter_server/extension/application.py @@ -80,16 +80,21 @@ class method. This method can be set as a entry_point in @default("extension_name") def _default_extension_name(self): - raise Exception("The extension must be given a `name`.") + raise ValueError("The extension must be given a `name`.") - @validate("extension_name") - def _valid_extension_name(self, obj, value): - if isinstance(name, str): + INVALID_EXTENSION_NAME_CHARS = [' ', '.', '+', '/'] + + def _validate_extension_name(self): + value = self.extension_name + if isinstance(value, str): # Validate that extension_name doesn't contain any invalid characters. - for char in [' ', '.', '+', '/']: - self.error(obj, value) + for c in ExtensionApp.INVALID_EXTENSION_NAME_CHARS: + if c in value: + raise ValueError("Extension name '{name}' cannot contain any of the following characters: " + "{invalid_chars}.". + format(name=value, invalid_chars=ExtensionApp.INVALID_EXTENSION_NAME_CHARS)) return value - self.error(obj, value) + raise ValueError("Extension name must be a string, found {type}.".format(type=type(value))) # Extension can configure the ServerApp from the command-line classes = [ @@ -235,11 +240,12 @@ def initialize(self, serverapp, argv=[]): - Passes settings to webapp - Appends handlers to webapp. """ + self._validate_extension_name() # Initialize the extension application super(ExtensionApp, self).initialize(argv=argv) self.serverapp = serverapp - # Intialize config, settings, templates, and handlers. + # Initialize config, settings, templates, and handlers. self._prepare_config() self._prepare_templates() self._prepare_settings()