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

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

Open
speedplane opened this issue Feb 9, 2016 · 4 comments
Open

Comments

@speedplane
Copy link

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.

speedplane referenced this issue Feb 9, 2016
Currently, we'll allow any string as the MIME type -- but valid MIME types
must contain a `/`. The code implicitly assumes this, so we might as well
check it and raise a comprehensible error.
@craigcitro
Copy link
Contributor

Ah, yes, that's entirely believable. Would you be willing to send along a PR with the fix?

@speedplane
Copy link
Author

I'd like to, but I'm just using this function to get Google's Managed VM system to work and this is only one of several other issues I'm facing. I have a workaround for this (e.g., not upload any .p12 files), so I'm not sure I'll be able to get to it.

@craigcitro
Copy link
Contributor

So I just took a quick skim through the stdlib -- it looks like mimetypes should return the expected application/x-pkcs12 here.

Do you have a small failing sample?

@elharo
Copy link

elharo commented Apr 5, 2018

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

No branches or pull requests

3 participants