Skip to content

Commit 78ac8fc

Browse files
authored
fix(system-server): sanitize the filename in the upload_splash endpoint for OEM Mode. (#15063)
1 parent 1cea210 commit 78ac8fc

File tree

1 file changed

+12
-2
lines changed
  • system-server/system_server/system/oem_mode

1 file changed

+12
-2
lines changed

system-server/system_server/system/oem_mode/router.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Router for /system/register endpoint."""
22

3+
import re
34
import os
45
import filetype # type: ignore[import-untyped]
56
from fastapi import (
@@ -11,11 +12,16 @@
1112
File,
1213
HTTPException,
1314
)
15+
from pathlib import Path
1416

1517
from .models import EnableOEMMode
1618
from ...settings import SystemServerSettings, get_settings, save_settings
1719

1820

21+
# regex to sanitize the filename
22+
FILENAME_REGEX = re.compile(r"[^a-zA-Z0-9-.]")
23+
24+
1925
oem_mode_router = APIRouter()
2026

2127

@@ -78,7 +84,7 @@ async def upload_splash_image(
7884

7985
# Get the file info
8086
file_info = filetype.guess(file.file)
81-
if file_info is None:
87+
if file_info is None or not file.filename:
8288
raise HTTPException(
8389
status_code=status.HTTP_415_UNSUPPORTED_MEDIA_TYPE,
8490
detail="Unable to determine file type",
@@ -115,8 +121,12 @@ async def upload_splash_image(
115121
if settings.oem_mode_splash_custom:
116122
os.unlink(settings.oem_mode_splash_custom)
117123

124+
# sanitize the filename
125+
sanatized_filename = FILENAME_REGEX.sub("_", file.filename)
126+
filename = f"{Path(sanatized_filename).stem}.{content_type}"
127+
118128
# file is valid, save to final location
119-
filepath = f"{settings.persistence_directory}/{file.filename}"
129+
filepath = f"{settings.persistence_directory}/{filename}"
120130
with open(filepath, "wb+") as f:
121131
f.write(file.file.read())
122132

0 commit comments

Comments
 (0)