From 6117699260b704121813766bbfdbefb28c34163e Mon Sep 17 00:00:00 2001 From: Kevin Chung Date: Sat, 14 Oct 2017 16:37:41 -0400 Subject: [PATCH] Use strings for key type (#409) * Store key_type as string in the database * Give keys plugin the ability to know where the modals are stored and pass this information to the client --- CTFd/admin/challenges.py | 6 +- CTFd/admin/keys.py | 27 +++++--- CTFd/models.py | 2 +- CTFd/plugins/__init__.py | 2 +- .../assets/standard-challenge-create.hbs | 4 +- .../assets/standard-challenge-update.js | 12 ++-- CTFd/plugins/keys/__init__.py | 19 +++++- .../keys/assets/regex/create-regex-modal.hbs} | 0 .../keys/assets}/regex/edit-regex-modal.hbs | 2 +- .../assets/static/create-static-modal.hbs} | 0 .../keys/assets}/static/edit-static-modal.hbs | 2 +- CTFd/themes/admin/static/js/chalboard.js | 4 +- export.zip | Bin 0 -> 1626 bytes .../e62fd69bd417_use_strings_for_key_types.py | 58 ++++++++++++++++++ populate.py | 2 +- tests/helpers.py | 2 +- 16 files changed, 115 insertions(+), 27 deletions(-) rename CTFd/{themes/admin/static/js/templates/keys/regex/regex.hbs => plugins/keys/assets/regex/create-regex-modal.hbs} (100%) rename CTFd/{themes/admin/static/js/templates/keys => plugins/keys/assets}/regex/edit-regex-modal.hbs (97%) rename CTFd/{themes/admin/static/js/templates/keys/static/static.hbs => plugins/keys/assets/static/create-static-modal.hbs} (100%) rename CTFd/{themes/admin/static/js/templates/keys => plugins/keys/assets}/static/edit-static-modal.hbs (97%) create mode 100644 export.zip create mode 100644 migrations/versions/e62fd69bd417_use_strings_for_key_types.py diff --git a/CTFd/admin/challenges.py b/CTFd/admin/challenges.py index 10e3f7d..146d275 100644 --- a/CTFd/admin/challenges.py +++ b/CTFd/admin/challenges.py @@ -199,11 +199,13 @@ def admin_get_values(chalid, prop): chal_keys = Keys.query.filter_by(chal=challenge.id).all() json_data = {'keys': []} for x in chal_keys: + key_class = get_key_class(x.key_type) json_data['keys'].append({ 'id': x.id, 'key': x.flag, 'type': x.key_type, - 'type_name': get_key_class(x.key_type).name + 'type_name': key_class.name, + 'templates': key_class.templates, }) return jsonify(json_data) elif prop == 'tags': @@ -257,7 +259,7 @@ def admin_create_chal(): db.session.add(chal) db.session.flush() - flag = Keys(chal.id, request.form['key'], int(request.form['key_type[0]'])) + flag = Keys(chal.id, request.form['key'], request.form['key_type[0]']) if request.form.get('keydata'): flag.data = request.form.get('keydata') db.session.add(flag) diff --git a/CTFd/admin/keys.py b/CTFd/admin/keys.py index 8a051bb..c3de516 100644 --- a/CTFd/admin/keys.py +++ b/CTFd/admin/keys.py @@ -9,13 +9,23 @@ admin_keys = Blueprint('admin_keys', __name__) @admin_keys.route('/admin/key_types', methods=['GET']) +@admin_keys.route('/admin/key_types/', methods=['GET']) @admins_only -def admin_key_types(): - data = {} - for class_id in KEY_CLASSES: - data[class_id] = KEY_CLASSES.get(class_id).name +def admin_key_types(key_id=None): + if key_id is None: + data = {} + for class_id in KEY_CLASSES: + data[class_id] = KEY_CLASSES.get(class_id).name - return jsonify(data) + return jsonify(data) + else: + key_class = get_key_class(key_id) + data = { + 'id': key_class.id, + 'name': key_class.name, + 'templates': key_class.templates + } + return jsonify(data) @admin_keys.route('/admin/keys', defaults={'keyid': None}, methods=['POST', 'GET']) @@ -25,14 +35,15 @@ def admin_keys_view(keyid): if request.method == 'GET': if keyid: saved_key = Keys.query.filter_by(id=keyid).first_or_404() - + key_class = get_key_class(saved_key.key_type) json_data = { 'id': saved_key.id, 'key': saved_key.flag, 'data': saved_key.data, 'chal': saved_key.chal, 'type': saved_key.key_type, - 'type_name': get_key_class(saved_key.key_type).name + 'type_name': key_class.name, + 'templates': key_class.templates, } return jsonify(json_data) @@ -40,7 +51,7 @@ def admin_keys_view(keyid): chal = request.form.get('chal') flag = request.form.get('key') data = request.form.get('keydata') - key_type = int(request.form.get('key_type')) + key_type = request.form.get('key_type') if not keyid: k = Keys(chal, flag, key_type) k.data = data diff --git a/CTFd/models.py b/CTFd/models.py index 2fe2b17..ffb8ef7 100644 --- a/CTFd/models.py +++ b/CTFd/models.py @@ -127,7 +127,7 @@ class Files(db.Model): class Keys(db.Model): id = db.Column(db.Integer, primary_key=True) chal = db.Column(db.Integer, db.ForeignKey('challenges.id')) - key_type = db.Column(db.Integer) + key_type = db.Column(db.String(80)) flag = db.Column(db.Text) data = db.Column(db.Text) diff --git a/CTFd/plugins/__init__.py b/CTFd/plugins/__init__.py index 2e9eda7..0f5c6af 100644 --- a/CTFd/plugins/__init__.py +++ b/CTFd/plugins/__init__.py @@ -57,7 +57,7 @@ def init_plugins(app): :return: """ modules = glob.glob(os.path.dirname(__file__) + "/*") - blacklist = {'keys', '__pycache__'} + blacklist = {'__pycache__'} for module in modules: module_name = os.path.basename(module) if os.path.isdir(module) and module_name not in blacklist: diff --git a/CTFd/plugins/challenges/assets/standard-challenge-create.hbs b/CTFd/plugins/challenges/assets/standard-challenge-create.hbs index 2731d50..87fcff4 100644 --- a/CTFd/plugins/challenges/assets/standard-challenge-create.hbs +++ b/CTFd/plugins/challenges/assets/standard-challenge-create.hbs @@ -48,13 +48,13 @@
diff --git a/CTFd/plugins/challenges/assets/standard-challenge-update.js b/CTFd/plugins/challenges/assets/standard-challenge-update.js index ebf5dad..265b8b1 100644 --- a/CTFd/plugins/challenges/assets/standard-challenge-update.js +++ b/CTFd/plugins/challenges/assets/standard-challenge-update.js @@ -317,11 +317,13 @@ $('#create-key').click(function(e){ $('#create-keys-select').change(function(){ var key_type_name = $(this).find("option:selected").text(); - $.get(script_root + '/themes/admin/static/js/templates/keys/'+key_type_name +'/'+key_type_name+'.hbs', function(template_data){ - var template = Handlebars.compile(template_data); - $("#create-keys-entry-div").html(template()); - $("#create-keys-button-div").show(); - }); + $.get(script_root + '/admin/key_types/' + key_type_name, function(key_data){ + $.get(script_root + key_data.templates.create, function(template_data){ + var template = Handlebars.compile(template_data); + $("#create-keys-entry-div").html(template()); + $("#create-keys-button-div").show(); + }); + }) }); diff --git a/CTFd/plugins/keys/__init__.py b/CTFd/plugins/keys/__init__.py index 1f8e5d6..3a63440 100644 --- a/CTFd/plugins/keys/__init__.py +++ b/CTFd/plugins/keys/__init__.py @@ -1,3 +1,5 @@ +from CTFd.plugins import register_plugin_assets_directory + import re import string import hmac @@ -6,6 +8,7 @@ import hmac class BaseKey(object): id = None name = None + templates = {} @staticmethod def compare(self, saved, provided): @@ -15,6 +18,10 @@ class BaseKey(object): class CTFdStaticKey(BaseKey): id = 0 name = "static" + templates = { # Handlebars templates used for key editing & viewing + 'create': '/plugins/keys/assets/static/create-static-modal.hbs', + 'update': '/plugins/keys/assets/static/edit-static-modal.hbs', + } @staticmethod def compare(saved, provided): @@ -29,6 +36,10 @@ class CTFdStaticKey(BaseKey): class CTFdRegexKey(BaseKey): id = 1 name = "regex" + templates = { # Handlebars templates used for key editing & viewing + 'create': '/plugins/keys/assets/regex/create-regex-modal.hbs', + 'update': '/plugins/keys/assets/regex/edit-regex-modal.hbs', + } @staticmethod def compare(saved, provided): @@ -37,8 +48,8 @@ class CTFdRegexKey(BaseKey): KEY_CLASSES = { - 0: CTFdStaticKey, - 1: CTFdRegexKey + 'static': CTFdStaticKey, + 'regex': CTFdRegexKey } @@ -47,3 +58,7 @@ def get_key_class(class_id): if cls is None: raise KeyError return cls + + +def load(app): + register_plugin_assets_directory(app, base_path='/plugins/keys/assets/') diff --git a/CTFd/themes/admin/static/js/templates/keys/regex/regex.hbs b/CTFd/plugins/keys/assets/regex/create-regex-modal.hbs similarity index 100% rename from CTFd/themes/admin/static/js/templates/keys/regex/regex.hbs rename to CTFd/plugins/keys/assets/regex/create-regex-modal.hbs diff --git a/CTFd/themes/admin/static/js/templates/keys/regex/edit-regex-modal.hbs b/CTFd/plugins/keys/assets/regex/edit-regex-modal.hbs similarity index 97% rename from CTFd/themes/admin/static/js/templates/keys/regex/edit-regex-modal.hbs rename to CTFd/plugins/keys/assets/regex/edit-regex-modal.hbs index 25d7e8d..d059a78 100644 --- a/CTFd/themes/admin/static/js/templates/keys/regex/edit-regex-modal.hbs +++ b/CTFd/plugins/keys/assets/regex/edit-regex-modal.hbs @@ -6,7 +6,7 @@