diff --git a/bci/evaluations/custom/custom_evaluation.py b/bci/evaluations/custom/custom_evaluation.py index b359c036..279065e3 100644 --- a/bci/evaluations/custom/custom_evaluation.py +++ b/bci/evaluations/custom/custom_evaluation.py @@ -166,27 +166,43 @@ def add_page(self, project: str, poc: str, domain: str, path: str, file_type: st if os.path.exists(file_path): return False with open(file_path, 'w') as file: - file.write('') - headers_file_path = os.path.join(page_path, 'headers.json') - if not os.path.exists(headers_file_path): - with open(headers_file_path, 'w') as file: - file.write( - textwrap.dedent( - """\ - [ - { - "key": "Header-Name", - "value": "Header-Value" - } - ] - """ + file.write(self.get_default_file_content(file_type)) + + if self.include_file_headers(file_type): + headers_file_path = os.path.join(page_path, 'headers.json') + if not os.path.exists(headers_file_path): + with open(headers_file_path, 'w') as file: + file.write( + textwrap.dedent( + """\ + [ + { + "key": "Header-Name", + "value": "Header-Value" + } + ] + """ + ) ) - ) self.sync_with_folders() # Notify clients of change (an experiment might now be runnable) Clients.push_experiments_to_all() return True + @staticmethod + def get_default_file_content(file_type: str) -> str: + if file_type != 'py': + return '' + + with open('experiments/pages/Support/PythonServer/a.test/py-server/index.py', 'r') as file: + template_content = file.read() + + return template_content + + @staticmethod + def include_file_headers(file_type: str) -> bool: + return file_type != 'py' + def sync_with_folders(self): self.dir_tree = self.initialize_dir_tree() self.tests_per_project = self.initialize_tests_and_url_queues(self.dir_tree) diff --git a/bci/web/blueprints/experiments.py b/bci/web/blueprints/experiments.py index 5ef7af53..2de8daeb 100644 --- a/bci/web/blueprints/experiments.py +++ b/bci/web/blueprints/experiments.py @@ -1,6 +1,8 @@ import datetime import logging import threading +import importlib.util +import sys import requests from flask import Blueprint, make_response, render_template, request, url_for @@ -130,5 +132,25 @@ def report_leak_if_contains(expected_header_name: str, expected_header_value: st ) +@exp.route("////") +def python_evaluation(project: str, experiment: str, directory: str): + """ + Evaluates the python script and returns its result. + """ + host = request.host.lower() + + module_name = f"{host}/{project}/{experiment}/{directory}" + path = f"experiments/pages/{project}/{experiment}/{host}/{directory}/index.py" + + # Dynamically import the file + sys.dont_write_bytecode = True + spec = importlib.util.spec_from_file_location(module_name, path) + module = importlib.util.module_from_spec(spec) + sys.modules[module_name] = module + spec.loader.exec_module(module) + + return module.main(request) + + def get_all_GET_parameters(request): return {k: v for k, v in request.args.items()} diff --git a/bci/web/page_parser.py b/bci/web/page_parser.py index 1cff781d..57dc6186 100644 --- a/bci/web/page_parser.py +++ b/bci/web/page_parser.py @@ -68,6 +68,10 @@ def get_content(subdir_folder_path: str): { "file_name": "index.js", "content_type": "text/javascript" + }, + { + "file_name": "index.py", + "content_type": "text/x-python" } ] content = None diff --git a/bci/web/vue/src/components/poc-editor.vue b/bci/web/vue/src/components/poc-editor.vue index 7bac6b8e..59e894fb 100644 --- a/bci/web/vue/src/components/poc-editor.vue +++ b/bci/web/vue/src/components/poc-editor.vue @@ -1,5 +1,9 @@ + + \ No newline at end of file diff --git a/experiments/pages/Support/PythonServer/a.test/py-server/index.py b/experiments/pages/Support/PythonServer/a.test/py-server/index.py new file mode 100644 index 00000000..91e823b8 --- /dev/null +++ b/experiments/pages/Support/PythonServer/a.test/py-server/index.py @@ -0,0 +1,15 @@ +from flask import Request + +# Make sure that your page directory starts with 'py-' + +def main(req: Request): + # TODO - implement your functionality and return a Flask response + + return { + "agent": req.headers.get("User-Agent"), + "cookies": req.cookies, + "host": req.host, + "path": req.path, + "scheme": req.scheme, + "url": req.url + } \ No newline at end of file diff --git a/nginx/config/experiments.conf b/nginx/config/experiments.conf index 940b5b9f..398dac9c 100644 --- a/nginx/config/experiments.conf +++ b/nginx/config/experiments.conf @@ -21,6 +21,14 @@ location ~ ^/report(/.+)*/?$ { proxy_set_header X-Forwarded-Proto $scheme; } +location ~ ^/(.+)/(.+)/py-(.+)/$ { + proxy_pass http://core:5000; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; +} + location ~ ^/(.+)/(.+)/(.+)/$ { root /www/data/pages; index index.html index.js;