Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Enhanced Error Handling for Disallowed MIME Types #58

Merged
merged 4 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@
_trial_temp
__pycache__
/dist
config.yaml
config.yaml
/build
/.vscode
mcs_pickle.txt
pickle
1 change: 1 addition & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ status code of the response for each scenario:
| 404 | `M_NOT_FOUND` | No route could be found at the given path. |
| 404 | `M_NOT_FOUND` | The requested media was not present in the media repo. |
| 403 | `MCS_MEDIA_NOT_CLEAN` | The server scanned the downloaded media but the antivirus script returned a non-zero exit code. |
| 403 | `MCS_MIME_TYPE_FORBIDDEN` | The Mime type is not in the allowed list of Mime types. |
| 403 | `MCS_BAD_DECRYPTION` | The provided `encrypted_body` could not be decrypted, or the encrypted file could not be decrypted. The client should request the public key of the server and then retry (once). |
| 500 | `M_UNKNOWN` | The server experienced an unexpected error. |
| 502 | `MCS_MEDIA_REQUEST_FAILED` | The server failed to request media from the media repo. |
Expand Down
10 changes: 8 additions & 2 deletions src/matrix_content_scanner/scanner/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@
from multidict import MultiMapping

from matrix_content_scanner.utils.constants import ErrCode
from matrix_content_scanner.utils.errors import ContentScannerRestError, FileDirtyError
from matrix_content_scanner.utils.errors import (
ContentScannerRestError,
FileDirtyError,
FileMimeTypeForbiddenError,
)
from matrix_content_scanner.utils.types import JsonDict, MediaDescription

if TYPE_CHECKING:
Expand Down Expand Up @@ -544,4 +548,6 @@ def _check_mimetype(
"MIME type for file is forbidden: %s",
detected_mimetype,
)
raise FileDirtyError("File type not supported")
raise FileMimeTypeForbiddenError(
f"File type: {detected_mimetype} not allowed"
)
2 changes: 2 additions & 0 deletions src/matrix_content_scanner/utils/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ class ErrCode(str, Enum):
FAILED_TO_DECRYPT = "MCS_MEDIA_FAILED_TO_DECRYPT"
# The request body isn't valid JSON, or is missing a required parameter.
MALFORMED_JSON = "MCS_MALFORMED_JSON"
# The Mime type is not in the allowed list of Mime types.
MIME_TYPE_FORBIDDEN = "MCS_MIME_TYPE_FORBIDDEN"
11 changes: 11 additions & 0 deletions src/matrix_content_scanner/utils/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ def __init__(
self.cacheable = cacheable


class FileMimeTypeForbiddenError(ContentScannerRestError):
"""An error indicating that the file's MIME type is forbidden."""

def __init__(self, info: Optional[str]) -> None:
super(FileMimeTypeForbiddenError, self).__init__(
http_status=403,
reason=ErrCode.MIME_TYPE_FORBIDDEN,
info=info,
)


class ConfigError(Exception):
"""An error indicating an issue with the configuration file."""

Expand Down
10 changes: 7 additions & 3 deletions tests/scanner/test_scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@

from matrix_content_scanner.scanner.scanner import CacheEntry
from matrix_content_scanner.utils.constants import ErrCode
from matrix_content_scanner.utils.errors import ContentScannerRestError, FileDirtyError
from matrix_content_scanner.utils.errors import (
ContentScannerRestError,
FileDirtyError,
FileMimeTypeForbiddenError,
)
from matrix_content_scanner.utils.types import MediaDescription
from tests.testutils import (
ENCRYPTED_FILE_METADATA,
Expand Down Expand Up @@ -231,7 +235,7 @@ async def test_mimetype(self) -> None:
self.scanner._allowed_mimetypes = ["image/jpeg"]

# Check that the scan fails since the file is a PNG.
with self.assertRaises(FileDirtyError):
with self.assertRaises(FileMimeTypeForbiddenError):
await self.scanner.scan_file(MEDIA_PATH)

async def test_mimetype_encrypted(self) -> None:
Expand All @@ -244,7 +248,7 @@ async def test_mimetype_encrypted(self) -> None:
self.scanner._allowed_mimetypes = ["image/jpeg"]

# Check that the scan fails since the file is a PNG.
with self.assertRaises(FileDirtyError):
with self.assertRaises(FileMimeTypeForbiddenError):
await self.scanner.scan_file(MEDIA_PATH, ENCRYPTED_FILE_METADATA)

async def test_mimetype_content_type_mismatch(self) -> None:
Expand Down