Skip to content

Commit

Permalink
Add file path verification
Browse files Browse the repository at this point in the history
  • Loading branch information
MathieuMoalic committed Mar 27, 2024
1 parent b4484cb commit 9384b23
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
13 changes: 13 additions & 0 deletions manager/manager/components/check_mx3_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import logging
from pathlib import Path

log = logging.getLogger("rich")


def validate_mx3_file(path_str: str) -> bool:
path = Path(path_str)
if not path.exists():
log.error(f"File does not exist: {path}")
return False
log.debug(f"Checking file: {path} {path.exists()}")
return path.exists()
2 changes: 1 addition & 1 deletion manager/manager/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 5.0.2 on 2024-03-27 11:26
# Generated by Django 5.0.2 on 2024-03-27 13:19

import django.db.models.deletion
import django.utils.timezone
Expand Down
8 changes: 8 additions & 0 deletions manager/manager/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from rest_framework.exceptions import ValidationError
from rest_framework.response import Response

from manager.components.check_mx3_file import validate_mx3_file
from manager.components.ws_messages import WebsocketMessage, send_message

from .components.run_job import run_job
Expand Down Expand Up @@ -71,6 +72,13 @@ def create(self, request, *_args, **_kwargs):
serializer = self.get_serializer(data=modified_data)
try:
serializer.is_valid(raise_exception=True)
if not validate_mx3_file(serializer.validated_data["path"]):
log.error("Job not created: Invalid file path")
return Response(
{"detail": "Invalid file path"},
status=status.HTTP_400_BAD_REQUEST,
)

self.perform_create(serializer)
headers = self.get_success_headers(serializer.data)
log.debug(f"Job created: {serializer.data}")
Expand Down
7 changes: 7 additions & 0 deletions manager/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,9 @@ components:
id:
type: integer
readOnly: true
username:
type: string
readOnly: true
node_name:
type: string
readOnly: true
Expand Down Expand Up @@ -1040,6 +1043,7 @@ components:
- node_name
- path
- user
- username
JobStatusEnum:
enum:
- PENDING
Expand Down Expand Up @@ -1196,6 +1200,9 @@ components:
id:
type: integer
readOnly: true
username:
type: string
readOnly: true
node_name:
type: string
readOnly: true
Expand Down

0 comments on commit 9384b23

Please sign in to comment.