Add a temporary fix/hack for running scripts in flag editor templates (#1783)

* Works on #1779 
* Adds a temporary hack for re-enabling Javascript snippets in Flag editor templates. 
    * In the future this will probably be separated out into a separate scripts file or something like using web components
This commit is contained in:
Kevin Chung
2021-01-27 02:58:23 -05:00
committed by GitHub
parent 566c16a9df
commit 2e6ce0f695
4 changed files with 25 additions and 3 deletions

View File

@@ -86,6 +86,17 @@ export default {
const template = nunjucks.compile(template_data);
this.selectedType = flagType;
this.createForm = template.render();
// TODO: See https://github.com/CTFd/CTFd/issues/1779
if (this.createForm.includes("<script")) {
setTimeout(() => {
$(`<div>` + this.createForm + `</div>`)
.find("script")
.each(function() {
eval($(this).html());
});
}, 100);
}
});
},
loadTypes: function() {

View File

@@ -72,6 +72,17 @@ export default {
$.get(CTFd.config.urlRoot + editFormURL, template_data => {
const template = nunjucks.compile(template_data);
this.editForm = template.render(this.flag);
// TODO: See https://github.com/CTFd/CTFd/issues/1779
if (this.createForm.includes("<script")) {
setTimeout(() => {
$(`<div>` + this.editForm + `</div>`)
.find("script")
.each(function() {
eval($(this).html());
});
}, 100);
}
});
});
},

View File

@@ -584,7 +584,7 @@ eval("\n\nObject.defineProperty(exports, \"__esModule\", {\n value: true\n});\n
/***/ (function(module, exports, __webpack_require__) {
;
eval("\n\nObject.defineProperty(exports, \"__esModule\", {\n value: true\n});\nexports[\"default\"] = void 0;\n\nvar _jquery = _interopRequireDefault(__webpack_require__(/*! jquery */ \"./node_modules/jquery/dist/jquery.js\"));\n\nvar _CTFd = _interopRequireDefault(__webpack_require__(/*! core/CTFd */ \"./CTFd/themes/core/assets/js/CTFd.js\"));\n\nvar _nunjucks = _interopRequireDefault(__webpack_require__(/*! nunjucks */ \"./node_modules/nunjucks/browser/nunjucks.js\"));\n\nfunction _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { \"default\": obj }; }\n\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\nvar _default = {\n name: \"FlagCreationForm\",\n props: {\n challenge_id: Number\n },\n data: function data() {\n return {\n types: {},\n selectedType: null,\n createForm: \"\"\n };\n },\n methods: {\n selectType: function selectType(event) {\n var _this = this;\n\n var flagType = event.target.value;\n\n if (this.types[flagType] === undefined) {\n this.selectedType = null;\n this.createForm = \"\";\n return;\n }\n\n var createFormURL = this.types[flagType][\"templates\"][\"create\"];\n\n _jquery[\"default\"].get(_CTFd[\"default\"].config.urlRoot + createFormURL, function (template_data) {\n var template = _nunjucks[\"default\"].compile(template_data);\n\n _this.selectedType = flagType;\n _this.createForm = template.render();\n });\n },\n loadTypes: function loadTypes() {\n var _this2 = this;\n\n _CTFd[\"default\"].fetch(\"/api/v1/flags/types\", {\n method: \"GET\"\n }).then(function (response) {\n return response.json();\n }).then(function (response) {\n _this2.types = response.data;\n });\n },\n submitFlag: function submitFlag(event) {\n var _this3 = this;\n\n var form = (0, _jquery[\"default\"])(event.target);\n var params = form.serializeJSON(true);\n params[\"challenge\"] = this.$props.challenge_id;\n\n _CTFd[\"default\"].fetch(\"/api/v1/flags\", {\n method: \"POST\",\n credentials: \"same-origin\",\n headers: {\n Accept: \"application/json\",\n \"Content-Type\": \"application/json\"\n },\n body: JSON.stringify(params)\n }).then(function (response) {\n return response.json();\n }).then(function (_response) {\n _this3.$emit(\"refreshFlags\", _this3.$options.name);\n });\n }\n },\n created: function created() {\n this.loadTypes();\n }\n};\nexports[\"default\"] = _default;\n\n//# sourceURL=webpack:///./CTFd/themes/admin/assets/js/components/flags/FlagCreationForm.vue?./node_modules/babel-loader/lib??ref--0!./node_modules/vue-loader/lib??vue-loader-options");
eval("\n\nObject.defineProperty(exports, \"__esModule\", {\n value: true\n});\nexports[\"default\"] = void 0;\n\nvar _jquery = _interopRequireDefault(__webpack_require__(/*! jquery */ \"./node_modules/jquery/dist/jquery.js\"));\n\nvar _CTFd = _interopRequireDefault(__webpack_require__(/*! core/CTFd */ \"./CTFd/themes/core/assets/js/CTFd.js\"));\n\nvar _nunjucks = _interopRequireDefault(__webpack_require__(/*! nunjucks */ \"./node_modules/nunjucks/browser/nunjucks.js\"));\n\nfunction _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { \"default\": obj }; }\n\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\nvar _default = {\n name: \"FlagCreationForm\",\n props: {\n challenge_id: Number\n },\n data: function data() {\n return {\n types: {},\n selectedType: null,\n createForm: \"\"\n };\n },\n methods: {\n selectType: function selectType(event) {\n var _this = this;\n\n var flagType = event.target.value;\n\n if (this.types[flagType] === undefined) {\n this.selectedType = null;\n this.createForm = \"\";\n return;\n }\n\n var createFormURL = this.types[flagType][\"templates\"][\"create\"];\n\n _jquery[\"default\"].get(_CTFd[\"default\"].config.urlRoot + createFormURL, function (template_data) {\n var template = _nunjucks[\"default\"].compile(template_data);\n\n _this.selectedType = flagType;\n _this.createForm = template.render(); // TODO: See https://github.com/CTFd/CTFd/issues/1779\n\n if (_this.createForm.includes(\"<script\")) {\n setTimeout(function () {\n (0, _jquery[\"default\"])(\"<div>\" + _this.createForm + \"</div>\").find(\"script\").each(function () {\n eval((0, _jquery[\"default\"])(this).html());\n });\n }, 100);\n }\n });\n },\n loadTypes: function loadTypes() {\n var _this2 = this;\n\n _CTFd[\"default\"].fetch(\"/api/v1/flags/types\", {\n method: \"GET\"\n }).then(function (response) {\n return response.json();\n }).then(function (response) {\n _this2.types = response.data;\n });\n },\n submitFlag: function submitFlag(event) {\n var _this3 = this;\n\n var form = (0, _jquery[\"default\"])(event.target);\n var params = form.serializeJSON(true);\n params[\"challenge\"] = this.$props.challenge_id;\n\n _CTFd[\"default\"].fetch(\"/api/v1/flags\", {\n method: \"POST\",\n credentials: \"same-origin\",\n headers: {\n Accept: \"application/json\",\n \"Content-Type\": \"application/json\"\n },\n body: JSON.stringify(params)\n }).then(function (response) {\n return response.json();\n }).then(function (_response) {\n _this3.$emit(\"refreshFlags\", _this3.$options.name);\n });\n }\n },\n created: function created() {\n this.loadTypes();\n }\n};\nexports[\"default\"] = _default;\n\n//# sourceURL=webpack:///./CTFd/themes/admin/assets/js/components/flags/FlagCreationForm.vue?./node_modules/babel-loader/lib??ref--0!./node_modules/vue-loader/lib??vue-loader-options");
/***/ }),
@@ -596,7 +596,7 @@ eval("\n\nObject.defineProperty(exports, \"__esModule\", {\n value: true\n});\n
/***/ (function(module, exports, __webpack_require__) {
;
eval("\n\nObject.defineProperty(exports, \"__esModule\", {\n value: true\n});\nexports[\"default\"] = void 0;\n\nvar _jquery = _interopRequireDefault(__webpack_require__(/*! jquery */ \"./node_modules/jquery/dist/jquery.js\"));\n\nvar _CTFd = _interopRequireDefault(__webpack_require__(/*! core/CTFd */ \"./CTFd/themes/core/assets/js/CTFd.js\"));\n\nvar _nunjucks = _interopRequireDefault(__webpack_require__(/*! nunjucks */ \"./node_modules/nunjucks/browser/nunjucks.js\"));\n\nfunction _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { \"default\": obj }; }\n\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\nvar _default = {\n name: \"FlagEditForm\",\n props: {\n flag_id: Number\n },\n data: function data() {\n return {\n flag: {},\n editForm: \"\"\n };\n },\n watch: {\n flag_id: {\n immediate: true,\n handler: function handler(val, oldVal) {\n if (val !== null) {\n this.loadFlag();\n }\n }\n }\n },\n methods: {\n loadFlag: function loadFlag() {\n var _this = this;\n\n _CTFd[\"default\"].fetch(\"/api/v1/flags/\".concat(this.$props.flag_id), {\n method: \"GET\"\n }).then(function (response) {\n return response.json();\n }).then(function (response) {\n _this.flag = response.data;\n var editFormURL = _this.flag[\"templates\"][\"update\"];\n\n _jquery[\"default\"].get(_CTFd[\"default\"].config.urlRoot + editFormURL, function (template_data) {\n var template = _nunjucks[\"default\"].compile(template_data);\n\n _this.editForm = template.render(_this.flag);\n });\n });\n },\n updateFlag: function updateFlag(event) {\n var _this2 = this;\n\n var form = (0, _jquery[\"default\"])(event.target);\n var params = form.serializeJSON(true);\n\n _CTFd[\"default\"].fetch(\"/api/v1/flags/\".concat(this.$props.flag_id), {\n method: \"PATCH\",\n credentials: \"same-origin\",\n headers: {\n Accept: \"application/json\",\n \"Content-Type\": \"application/json\"\n },\n body: JSON.stringify(params)\n }).then(function (response) {\n return response.json();\n }).then(function (response) {\n _this2.$emit(\"refreshFlags\", _this2.$options.name);\n });\n }\n },\n mounted: function mounted() {\n if (this.flag_id) {\n this.loadFlag();\n }\n },\n created: function created() {\n if (this.flag_id) {\n this.loadFlag();\n }\n }\n};\nexports[\"default\"] = _default;\n\n//# sourceURL=webpack:///./CTFd/themes/admin/assets/js/components/flags/FlagEditForm.vue?./node_modules/babel-loader/lib??ref--0!./node_modules/vue-loader/lib??vue-loader-options");
eval("\n\nObject.defineProperty(exports, \"__esModule\", {\n value: true\n});\nexports[\"default\"] = void 0;\n\nvar _jquery = _interopRequireDefault(__webpack_require__(/*! jquery */ \"./node_modules/jquery/dist/jquery.js\"));\n\nvar _CTFd = _interopRequireDefault(__webpack_require__(/*! core/CTFd */ \"./CTFd/themes/core/assets/js/CTFd.js\"));\n\nvar _nunjucks = _interopRequireDefault(__webpack_require__(/*! nunjucks */ \"./node_modules/nunjucks/browser/nunjucks.js\"));\n\nfunction _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { \"default\": obj }; }\n\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\n//\nvar _default = {\n name: \"FlagEditForm\",\n props: {\n flag_id: Number\n },\n data: function data() {\n return {\n flag: {},\n editForm: \"\"\n };\n },\n watch: {\n flag_id: {\n immediate: true,\n handler: function handler(val, oldVal) {\n if (val !== null) {\n this.loadFlag();\n }\n }\n }\n },\n methods: {\n loadFlag: function loadFlag() {\n var _this = this;\n\n _CTFd[\"default\"].fetch(\"/api/v1/flags/\".concat(this.$props.flag_id), {\n method: \"GET\"\n }).then(function (response) {\n return response.json();\n }).then(function (response) {\n _this.flag = response.data;\n var editFormURL = _this.flag[\"templates\"][\"update\"];\n\n _jquery[\"default\"].get(_CTFd[\"default\"].config.urlRoot + editFormURL, function (template_data) {\n var template = _nunjucks[\"default\"].compile(template_data);\n\n _this.editForm = template.render(_this.flag); // TODO: See https://github.com/CTFd/CTFd/issues/1779\n\n if (_this.createForm.includes(\"<script\")) {\n setTimeout(function () {\n (0, _jquery[\"default\"])(\"<div>\" + _this.editForm + \"</div>\").find(\"script\").each(function () {\n eval((0, _jquery[\"default\"])(this).html());\n });\n }, 100);\n }\n });\n });\n },\n updateFlag: function updateFlag(event) {\n var _this2 = this;\n\n var form = (0, _jquery[\"default\"])(event.target);\n var params = form.serializeJSON(true);\n\n _CTFd[\"default\"].fetch(\"/api/v1/flags/\".concat(this.$props.flag_id), {\n method: \"PATCH\",\n credentials: \"same-origin\",\n headers: {\n Accept: \"application/json\",\n \"Content-Type\": \"application/json\"\n },\n body: JSON.stringify(params)\n }).then(function (response) {\n return response.json();\n }).then(function (response) {\n _this2.$emit(\"refreshFlags\", _this2.$options.name);\n });\n }\n },\n mounted: function mounted() {\n if (this.flag_id) {\n this.loadFlag();\n }\n },\n created: function created() {\n if (this.flag_id) {\n this.loadFlag();\n }\n }\n};\nexports[\"default\"] = _default;\n\n//# sourceURL=webpack:///./CTFd/themes/admin/assets/js/components/flags/FlagEditForm.vue?./node_modules/babel-loader/lib??ref--0!./node_modules/vue-loader/lib??vue-loader-options");
/***/ }),

File diff suppressed because one or more lines are too long