Bug 171678 - Cleanup: Extract CachedScript::mimeTypeAllowedByNosniff() into a common function
Summary: Cleanup: Extract CachedScript::mimeTypeAllowedByNosniff() into a common function
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: WebCore Misc. (show other bugs)
Version: WebKit Local Build
Hardware: All All
: P2 Normal
Assignee: Daniel Bates
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-05-04 11:33 PDT by Daniel Bates
Modified: 2017-05-04 12:49 PDT (History)
6 users (show)

See Also:


Attachments
Patch (7.81 KB, patch)
2017-05-04 11:59 PDT, Daniel Bates
aestes: review+
Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Daniel Bates 2017-05-04 11:33:22 PDT
CachedScript::mimeType() is only used by CachedScript::mimeTypeAllowedByNosniff(). Moreover, it is unnecessary to lowercase the MIME type before querying MIMETypeRegistry as MIMETypeRegistry performs lookup case-insensitively. We should remove the lowercase conversion and inline CachedScript::mimeType() into CachedScript::mimeTypeAllowedByNosniff().
Comment 1 Daniel Bates 2017-05-04 11:51:17 PDT
Even better, we should extract CachedScript::mimeTypeAllowedByNosniff() into a common function that can be shared by LoadableClassicScript and WorkerScriptLoader.
Comment 2 Daniel Bates 2017-05-04 11:59:26 PDT
Created attachment 309075 [details]
Patch
Comment 3 Daniel Bates 2017-05-04 12:02:46 PDT
I am open to suggestions on the name and placement of isScriptAllowedByNosniff(). When I wrote the patch (attachment #309075 [details]) I put this function in ResourceResponseBase.{cpp, h} because it operates on a ResourceResponse.
Comment 4 Andy Estes 2017-05-04 12:06:20 PDT
Comment on attachment 309075 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=309075&action=review

> Source/WebCore/platform/network/ResourceResponseBase.cpp:47
> +    String mimeType = extractMIMETypeFromMediaType(response.httpHeaderField(HTTPHeaderName::ContentType));
> +    return parseContentTypeOptionsHeader(response.httpHeaderField(HTTPHeaderName::XContentTypeOptions)) != ContentTypeOptionsNosniff || MIMETypeRegistry::isSupportedJavaScriptMIMEType(mimeType);

Seems like this could be broken up a bit. For instance, if parseContentTypeOptionsHeader(response.httpHeaderField(HTTPHeaderName::XContentTypeOptions)) is true then you don't need to compute the mimeType from the ContentType header.
Comment 5 Andy Estes 2017-05-04 12:07:05 PDT
(In reply to Andy Estes from comment #4)
> Comment on attachment 309075 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=309075&action=review
> 
> > Source/WebCore/platform/network/ResourceResponseBase.cpp:47
> > +    String mimeType = extractMIMETypeFromMediaType(response.httpHeaderField(HTTPHeaderName::ContentType));
> > +    return parseContentTypeOptionsHeader(response.httpHeaderField(HTTPHeaderName::XContentTypeOptions)) != ContentTypeOptionsNosniff || MIMETypeRegistry::isSupportedJavaScriptMIMEType(mimeType);
> 
> Seems like this could be broken up a bit. For instance, if
> parseContentTypeOptionsHeader(response.httpHeaderField(HTTPHeaderName::
> XContentTypeOptions)) is true

... is not ContentTypeOptionsNosniff, that is.
Comment 6 Daniel Bates 2017-05-04 12:48:05 PDT
(In reply to Andy Estes from comment #4)
> Comment on attachment 309075 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=309075&action=review
> 
> > Source/WebCore/platform/network/ResourceResponseBase.cpp:47
> > +    String mimeType = extractMIMETypeFromMediaType(response.httpHeaderField(HTTPHeaderName::ContentType));
> > +    return parseContentTypeOptionsHeader(response.httpHeaderField(HTTPHeaderName::XContentTypeOptions)) != ContentTypeOptionsNosniff || MIMETypeRegistry::isSupportedJavaScriptMIMEType(mimeType);
> 
> Seems like this could be broken up a bit. For instance, if
> parseContentTypeOptionsHeader(response.httpHeaderField(HTTPHeaderName::
> XContentTypeOptions)) is true then you don't need to compute the mimeType
> from the ContentType header.

Wow, I need more sleep.

Will update code before landing to implement isScriptAllowedByNosniff() as follows:

bool isScriptAllowedByNosniff(const ResourceResponse& response)
{
    if (parseContentTypeOptionsHeader(response.httpHeaderField(HTTPHeaderName::XContentTypeOptions)) != ContentTypeOptionsNosniff)
        return true;
    String mimeType = extractMIMETypeFromMediaType(response.httpHeaderField(HTTPHeaderName::ContentType));
    return MIMETypeRegistry::isSupportedJavaScriptMIMEType(mimeType);
}
Comment 7 Daniel Bates 2017-05-04 12:49:55 PDT
Committed r216199: <http://trac.webkit.org/changeset/216199>