Added better logging
This commit is contained in:
@@ -1,13 +1,15 @@
|
||||
import os
|
||||
from mimetypes import guess_type
|
||||
from pathlib import Path
|
||||
|
||||
import logging
|
||||
from fastapi import APIRouter, Response
|
||||
|
||||
from ..assets import asset_path
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@router.get("/{path_name:path}")
|
||||
async def root(path_name: str) -> Response:
|
||||
@@ -16,6 +18,7 @@ async def root(path_name: str) -> Response:
|
||||
filename = Path(os.path.normpath(asset_path() / path_name))
|
||||
|
||||
if not filename.is_relative_to(asset_path()):
|
||||
log.error("Prevented directory traversal: %s", filename)
|
||||
# prevent directory traversal
|
||||
return Response(status_code=403)
|
||||
|
||||
@@ -23,6 +26,7 @@ async def root(path_name: str) -> Response:
|
||||
if filename.suffix == "":
|
||||
filename = filename.with_suffix(".html")
|
||||
if not filename.is_file():
|
||||
log.error("File not found: %s", filename)
|
||||
return Response(status_code=404)
|
||||
|
||||
content_type, _ = guess_type(filename)
|
||||
|
||||
Reference in New Issue
Block a user