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

feat(io/image): allow restricting mime types #2999

Merged
merged 3 commits into from
Sep 18, 2022
Merged

Conversation

sauyon
Copy link
Contributor

@sauyon sauyon commented Sep 14, 2022

This PR enables support for passing a list of mime types to the Image IO descriptor, which, alongside passing accept_all_images=False, will allow users to whitelist allowed image formats.

@sauyon sauyon requested a review from a team as a code owner September 14, 2022 22:58
@sauyon sauyon requested review from bojiang and removed request for a team September 14, 2022 22:58
bentoml/_internal/io_descriptors/image.py Outdated Show resolved Hide resolved
bentoml/_internal/io_descriptors/image.py Outdated Show resolved Hide resolved
@codecov
Copy link

codecov bot commented Sep 14, 2022

Codecov Report

Merging #2999 (d8825e8) into main (9d86cef) will decrease coverage by 10.79%.
The diff coverage is 30.43%.

Impacted file tree graph

@@             Coverage Diff             @@
##             main    #2999       +/-   ##
===========================================
- Coverage   62.89%   52.10%   -10.80%     
===========================================
  Files         146      114       -32     
  Lines       11822    11052      -770     
===========================================
- Hits         7436     5759     -1677     
- Misses       4386     5293      +907     
Impacted Files Coverage Δ
bentoml/_internal/io_descriptors/image.py 37.66% <30.43%> (-20.46%) ⬇️
bentoml/_internal/server/runner_app.py 0.00% <0.00%> (-91.37%) ⬇️
bentoml/_internal/runner/utils.py 0.00% <0.00%> (-88.89%) ⬇️
bentoml/_internal/runner/container.py 0.00% <0.00%> (-87.45%) ⬇️
bentoml/_internal/marshal/dispatcher.py 0.00% <0.00%> (-83.55%) ⬇️
bentoml/_internal/runner/runner_handle/remote.py 0.00% <0.00%> (-83.02%) ⬇️
bentoml/_internal/server/http/instruments.py 25.23% <0.00%> (-68.23%) ⬇️
bentoml/_internal/server/http/access.py 28.16% <0.00%> (-67.61%) ⬇️
bentoml/_internal/runner/runner_handle/local.py 0.00% <0.00%> (-66.67%) ⬇️
bentoml/_internal/utils/docker.py 0.00% <0.00%> (-65.63%) ⬇️
... and 62 more

bentoml/_internal/io_descriptors/image.py Outdated Show resolved Hide resolved
bentoml/_internal/io_descriptors/image.py Outdated Show resolved Hide resolved
@aarnphm
Copy link
Member

aarnphm commented Sep 15, 2022

tests are failing. Probably you have also to add unit test and fix the e2e once the gRPC PR got merged.

@sauyon sauyon force-pushed the image-mime branch 2 times, most recently from c25e3ee to 18ba195 Compare September 16, 2022 02:06
Comment on lines 183 to 198
for mtype in chain(self._allowed_mimes, [self._mime_type]):
if mtype not in MIME_EXT_MAPPING: # pragma: no cover
raise InvalidArgument(
f"Invalid Image mime_type '{mtype}'; supported mime types are {', '.join(PIL.Image.MIME.values())} "
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we have separate messages to indicate where the invalid mime type came from, mime_type or allowed_mime_types.

Comment on lines +233 to +249
if (
val_content_type in MIME_EXT_MAPPING
or val_content_type.startswith("image/")
):
bytes_ = await val.read()
break
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be a reject case with BadInput?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is our old behavior if allowed_mime_types is not specified.

Comment on lines 261 to 279
if self._allowed_mimes is None:
raise BadInput(
f"no multipart image file (with mime type in {MIME_EXT_MAPPING.keys()} or 'image/*'), got request with content type {mime_type}"
)
else:
raise BadInput(
f"no multipart image file (with mime type in {self._allowed_mimes}), got a request with content type {mime_type}"
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are they still "multipart" image files under the else condition?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, I failed to update this copy-paste.

def __init__(
self,
pilmode: _Mode | None = DEFAULT_PIL_MODE,
mime_type: str = "image/jpeg",
*,
allowed_mime_types: t.Iterable[str] | None = None,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aarnphm changed this to have a separate parameter for allowed mimes.


if isinstance(bytes_, str):
bytes_ = bytes(bytes_, "UTF-8")

try:
return PIL.Image.open(io.BytesIO(bytes_))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

after we open the image, should we also run verify ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should leave this lazy and with as little overhead as we can; users can do that if they want. At some point maybe we can turn on some sort of option to enable automatic verification.

@sauyon
Copy link
Contributor Author

sauyon commented Sep 17, 2022

@aarnphm @ssheng I think this should be good to go in now.

@aarnphm
Copy link
Member

aarnphm commented Sep 17, 2022

maybe we need test for this :)

@aarnphm
Copy link
Member

aarnphm commented Sep 18, 2022

I will fix the broken CI on #2984 since we have a lot of changes wrt e2e tests there.

@aarnphm aarnphm merged commit fd39950 into bentoml:main Sep 18, 2022
@aarnphm aarnphm deleted the image-mime branch September 18, 2022 09:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants