-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Content-Type header validation (#10526)
Signed-off-by: B-Step62 <yuki.watanabe@databricks.com>
- Loading branch information
Showing
6 changed files
with
79 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| from typing import List | ||
|
|
||
| from mlflow.exceptions import MlflowException | ||
| from mlflow.protos.databricks_pb2 import INVALID_PARAMETER_VALUE | ||
|
|
||
|
|
||
| def _validate_content_type(flask_request, allowed_content_types: List[str]): | ||
| """ | ||
| Validates that the request content type is one of the allowed content types. | ||
| :param flask_request: Flask request object (flask.request) | ||
| :param allowed_content_types: A list of allowed content types | ||
| """ | ||
| if flask_request.method not in ["POST", "PUT"]: | ||
| return | ||
|
|
||
| if flask_request.content_type is None: | ||
| raise MlflowException( | ||
| message="Bad Request. Content-Type header is missing.", | ||
| error_code=INVALID_PARAMETER_VALUE, | ||
| ) | ||
|
|
||
| # Remove any parameters e.g. "application/json; charset=utf-8" -> "application/json" | ||
| content_type = flask_request.content_type.split(";")[0] | ||
| if content_type not in allowed_content_types: | ||
| message = f"Bad Request. Content-Type must be one of {allowed_content_types}." | ||
|
|
||
| raise MlflowException( | ||
| message=message, | ||
| error_code=INVALID_PARAMETER_VALUE, | ||
| ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters