|
1 | 1 | """Router for /system/register endpoint."""
|
2 | 2 |
|
| 3 | +import re |
3 | 4 | import os
|
4 | 5 | import filetype # type: ignore[import-untyped]
|
5 | 6 | from fastapi import (
|
|
11 | 12 | File,
|
12 | 13 | HTTPException,
|
13 | 14 | )
|
| 15 | +from pathlib import Path |
14 | 16 |
|
15 | 17 | from .models import EnableOEMMode
|
16 | 18 | from ...settings import SystemServerSettings, get_settings, save_settings
|
17 | 19 |
|
18 | 20 |
|
| 21 | +# regex to sanitize the filename |
| 22 | +FILENAME_REGEX = re.compile(r"[^a-zA-Z0-9-.]") |
| 23 | + |
| 24 | + |
19 | 25 | oem_mode_router = APIRouter()
|
20 | 26 |
|
21 | 27 |
|
@@ -78,7 +84,7 @@ async def upload_splash_image(
|
78 | 84 |
|
79 | 85 | # Get the file info
|
80 | 86 | file_info = filetype.guess(file.file)
|
81 |
| - if file_info is None: |
| 87 | + if file_info is None or not file.filename: |
82 | 88 | raise HTTPException(
|
83 | 89 | status_code=status.HTTP_415_UNSUPPORTED_MEDIA_TYPE,
|
84 | 90 | detail="Unable to determine file type",
|
@@ -115,8 +121,12 @@ async def upload_splash_image(
|
115 | 121 | if settings.oem_mode_splash_custom:
|
116 | 122 | os.unlink(settings.oem_mode_splash_custom)
|
117 | 123 |
|
| 124 | + # sanitize the filename |
| 125 | + sanatized_filename = FILENAME_REGEX.sub("_", file.filename) |
| 126 | + filename = f"{Path(sanatized_filename).stem}.{content_type}" |
| 127 | + |
118 | 128 | # file is valid, save to final location
|
119 |
| - filepath = f"{settings.persistence_directory}/{file.filename}" |
| 129 | + filepath = f"{settings.persistence_directory}/{filename}" |
120 | 130 | with open(filepath, "wb+") as f:
|
121 | 131 | f.write(file.file.read())
|
122 | 132 |
|
|
0 commit comments