google / google/apitools

Bad Assumption in AcceptableMimeType, Leading to "Invalid MIME type"

Open
#86 4 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
157
Forks
131
PR merge metrics
No merged PRs in 30d

Description

The function `AcceptableMimeType` in `base/py/util.py` incorrectly assumes that all mimetypes will have a forward slash within them:

```
def AcceptableMimeType(accept_patterns, mime_type):
"""Return True iff mime_type is acceptable for one of accept_patterns.

Note that this function assumes that all patterns in accept_patterns
will be simple types of the form "type/subtype", where one or both
of these can be "*". We do not support parameters (i.e. "; q=") in
patterns.

Args:
accept_patterns: list of acceptable MIME types.
mime_type: the mime type we would like to match.

Returns:
Whether or not mime_type matches (at least) one of these patterns.
"""
if '/' not in mime_type:
raise exceptions.InvalidUserInputError(
'Invalid MIME type: "%s"' % mime_type)
```

This assumption fails for `.p12` files, which are used for server-to-server authentication. The function `mimetypes.guess_type` returns `x-pkcs12` for `.p12` files instead of `application/x-pkcs12`. This may be a bug with `mimetypes`, but it should nevertheless be handled properly by the `apitools`.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.