Skip to content

Commit 14b7b0a

Browse files
Fix Neural Solution SQL/CMD injection (#1627)
Signed-off-by: Kaihui-intel <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 8ddd755 commit 14b7b0a

File tree

12 files changed

+187
-13
lines changed

12 files changed

+187
-13
lines changed

neural_solution/backend/scheduler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def prepare_task(self, task: Task):
154154
if not task.optimized:
155155
# Generate quantization code with Neural Coder API
156156
neural_coder_cmd = ["python -m neural_coder --enable --approach"]
157-
# for users to define approach: "static, ""static_ipex", "dynamic", "auto"
157+
# for users to define approach: "static", "static_ipex", "dynamic", "auto"
158158
approach = task.approach
159159
neural_coder_cmd.append(approach)
160160
if is_remote_url(task.script_url):

neural_solution/examples/custom_models_optimized/tf_example1/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ optional arguments:
9191
"script_url": "tf_example1",
9292
"optimized": "True",
9393
"arguments": [
94-
"--dataset_location=dataset --model_path=model"
94+
"--dataset_location=dataset", "--model_path=model"
9595
],
9696
"approach": "static",
9797
"requirements": [
@@ -106,7 +106,7 @@ When using distributed quantization, the `workers` needs to be set to greater th
106106
"script_url": "tf_example1",
107107
"optimized": "True",
108108
"arguments": [
109-
"--dataset_location=dataset --model_path=model"
109+
"--dataset_location=dataset", "--model_path=model"
110110
],
111111
"approach": "static",
112112
"requirements": [

neural_solution/examples/custom_models_optimized/tf_example1/task_request.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"script_url": "custom_models_optimized/tf_example1",
33
"optimized": "True",
44
"arguments": [
5-
"--dataset_location=dataset --model_path=model"
5+
"--dataset_location=dataset", "--model_path=model"
66
],
77
"approach": "static",
88
"requirements": ["tensorflow"

neural_solution/examples/custom_models_optimized/tf_example1/task_request_distributed.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"script_url": "custom_models_optimized/tf_example1",
33
"optimized": "True",
44
"arguments": [
5-
"--dataset_location=dataset --model_path=model"
5+
"--dataset_location=dataset", "--model_path=model"
66
],
77
"approach": "static",
88
"requirements": ["tensorflow"

neural_solution/examples/hf_models/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ optional arguments:
7272
"script_url": "https://github.com/huggingface/transformers/blob/v4.21-release/examples/pytorch/text-classification/run_glue.py",
7373
"optimized": "False",
7474
"arguments": [
75-
"--model_name_or_path bert-base-cased --task_name mrpc --do_eval --output_dir result"
75+
"--model_name_or_path=bert-base-cased", "--task_name=mrpc", "--do_eval", "--output_dir=result"
7676
],
7777
"approach": "static",
7878
"requirements": [],

neural_solution/examples/hf_models/task_request.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"script_url": "https://github.com/huggingface/transformers/blob/v4.21-release/examples/pytorch/text-classification/run_glue.py",
33
"optimized": "False",
44
"arguments": [
5-
"--model_name_or_path bert-base-cased --task_name mrpc --do_eval --output_dir result"
5+
"--model_name_or_path=bert-base-cased", "--task_name=mrpc", "--do_eval", "--output_dir=result"
66
],
77
"approach": "static",
88
"requirements": ["datasets", "transformers=4.21.0", "torch"],

neural_solution/examples/hf_models_grpc/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ optional arguments:
6868
"script_url": "https://github.com/huggingface/transformers/blob/v4.21-release/examples/pytorch/text-classification/run_glue.py",
6969
"optimized": "False",
7070
"arguments": [
71-
"--model_name_or_path bert-base-cased --task_name mrpc --do_eval --output_dir result"
71+
"--model_name_or_path=bert-base-cased", "--task_name=mrpc", "--do_eval", "--output_dir=result"
7272
],
7373
"approach": "static",
7474
"requirements": [],

neural_solution/examples/hf_models_grpc/task_request.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"script_url": "https://github.com/huggingface/transformers/blob/v4.21-release/examples/pytorch/text-classification/run_glue.py",
33
"optimized": "False",
44
"arguments": [
5-
"--model_name_or_path bert-base-cased --task_name mrpc --do_eval --output_dir result"
5+
"--model_name_or_path=bert-base-cased", "--task_name=mrpc", "--do_eval", "--output_dir=result"
66
],
77
"approach": "static",
88
"requirements": ["datasets", "transformers=4.21.0", "torch"],

neural_solution/frontend/fastapi/main_server.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
get_cluster_info,
3737
get_cluster_table,
3838
get_res_during_tuning,
39+
is_valid_task,
3940
list_to_string,
4041
serialize,
4142
)
@@ -153,10 +154,14 @@ async def submit_task(task: Task):
153154
Returns:
154155
json: status , id of task and messages.
155156
"""
157+
if not is_valid_task(task.dict()):
158+
raise HTTPException(status_code=422, detail="Invalid task")
159+
156160
msg = "Task submitted successfully"
157161
status = "successfully"
158162
# search the current
159163
db_path = get_db_path(config.workspace)
164+
160165
if os.path.isfile(db_path):
161166
conn = sqlite3.connect(db_path)
162167
cursor = conn.cursor()

neural_solution/frontend/utility.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,3 +295,61 @@ def list_to_string(lst: list):
295295
str: string
296296
"""
297297
return " ".join(str(i) for i in lst)
298+
299+
300+
def is_invalid_str(to_test_str: str):
301+
"""Verify whether the to_test_str is valid.
302+
303+
Args:
304+
to_test_str (str): string to be tested.
305+
306+
Returns:
307+
bool: valid or invalid
308+
"""
309+
return any(char in to_test_str for char in [" ", '"', "'", "&", "|", ";", "`", ">"])
310+
311+
312+
def is_valid_task(task: dict) -> bool:
313+
"""Verify whether the task is valid.
314+
315+
Args:
316+
task (dict): task request
317+
318+
Returns:
319+
bool: valid or invalid
320+
"""
321+
required_fields = ["script_url", "optimized", "arguments", "approach", "requirements", "workers"]
322+
323+
for field in required_fields:
324+
if field not in task:
325+
return False
326+
327+
if not isinstance(task["script_url"], str) or is_invalid_str(task["script_url"]):
328+
return False
329+
330+
if (isinstance(task["optimized"], str) and task["optimized"] not in ["True", "False"]) or (
331+
not isinstance(task["optimized"], str) and not isinstance(task["optimized"], bool)
332+
):
333+
return False
334+
335+
if not isinstance(task["arguments"], list):
336+
return False
337+
else:
338+
for argument in task["arguments"]:
339+
if is_invalid_str(argument):
340+
return False
341+
342+
if not isinstance(task["approach"], str) or task["approach"] not in ["static", "static_ipex", "dynamic", "auto"]:
343+
return False
344+
345+
if not isinstance(task["requirements"], list):
346+
return False
347+
else:
348+
for requirement in task["requirements"]:
349+
if is_invalid_str(requirement):
350+
return False
351+
352+
if not isinstance(task["workers"], int) or task["workers"] < 1:
353+
return False
354+
355+
return True

0 commit comments

Comments
 (0)