Skip to content

Commit a1d5f92

Browse files
committed
Fix(docat): Mime Type not checked properly
I added python-magic to check the actual bytes of the upload. I also had to adapt the tests because of that. Is also necessary for compatabilty with the new docatl upload-icon feature. fixes: #289
1 parent dedc0b7 commit a1d5f92

File tree

4 files changed

+85
-63
lines changed

4 files changed

+85
-63
lines changed

docat/docat/app.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from pathlib import Path
1515
from typing import Optional
1616

17+
import magic
1718
from fastapi import Depends, FastAPI, File, Header, Response, UploadFile, status
1819
from fastapi.staticfiles import StaticFiles
1920
from pydantic import BaseModel
@@ -129,7 +130,10 @@ def upload_icon(
129130
response.status_code = status.HTTP_404_NOT_FOUND
130131
return ApiResponse(message=f"Project {project} not found")
131132

132-
if not file.content_type.startswith("image/"):
133+
mime_type_checker = magic.Magic(mime=True)
134+
mime_type = mime_type_checker.from_buffer(file.file.read())
135+
136+
if not mime_type.startswith("image/"):
133137
response.status_code = status.HTTP_400_BAD_REQUEST
134138
return ApiResponse(message="Icon must be an image")
135139

docat/poetry.lock

+67-55
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docat/pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ tinydb = "^4.7.0"
1111
fastapi = "^0.85.0"
1212
python-multipart = "^0.0.5"
1313
uvicorn = "^0.18.3"
14+
python-magic = "^0.4.27"
1415

1516
[tool.poetry.dev-dependencies]
1617
flake8 = "^5.0.4"

0 commit comments

Comments
 (0)