@@ -1451,6 +1451,42 @@ - (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView
14511451 }
14521452}
14531453
1454+ /* *
1455+ * Determines if a MIME type should be blocked from rendering.
1456+ */
1457+ - (BOOL )shouldBlockMIMETypeFromRendering : (NSString *)mimeType {
1458+ if (mimeType == nil ) {
1459+ return NO ;
1460+ }
1461+
1462+ NSString *normalizedType = [[mimeType componentsSeparatedByString: @" ;" ].firstObject
1463+ stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet ]];
1464+ normalizedType = [normalizedType lowercaseString ];
1465+
1466+ static NSSet *blockedTypes = nil ;
1467+ static dispatch_once_t onceToken;
1468+ dispatch_once (&onceToken, ^{
1469+ blockedTypes = [NSSet setWithObjects:
1470+ @" application/octet-stream" ,
1471+ @" application/x-msdownload" ,
1472+ @" application/x-executable" ,
1473+ @" application/x-dosexec" ,
1474+ @" application/zip" ,
1475+ @" application/x-zip-compressed" ,
1476+ @" application/x-rar-compressed" ,
1477+ @" application/x-7z-compressed" ,
1478+ @" application/x-tar" ,
1479+ @" application/gzip" ,
1480+ @" application/x-gzip" ,
1481+ @" application/x-bzip2" ,
1482+ @" application/x-msi" ,
1483+ nil
1484+ ];
1485+ });
1486+
1487+ return [blockedTypes containsObject: normalizedType];
1488+ }
1489+
14541490/* *
14551491 * Decides whether to allow or cancel a navigation after its response is known.
14561492 * @see https://developer.apple.com/documentation/webkit/wknavigationdelegate/1455643-webview?language=objc
@@ -1479,16 +1515,18 @@ - (void) webView:(WKWebView *)webView
14791515 disposition = [response valueForHTTPHeaderField: @" Content-Disposition" ];
14801516 }
14811517 BOOL isAttachment = disposition != nil && [disposition hasPrefix: @" attachment" ];
1482- if (isAttachment || !navigationResponse.canShowMIMEType ) {
1483- if (_onFileDownload) {
1484- policy = WKNavigationResponsePolicyCancel ;
1518+ NSString *contentType = [response valueForHTTPHeaderField: @" Content-Type" ];
1519+ BOOL isUnsafeMIMEType = [self shouldBlockMIMETypeFromRendering: contentType];
14851520
1521+ if (isAttachment || !navigationResponse.canShowMIMEType || isUnsafeMIMEType) {
1522+ if (_onFileDownload) {
14861523 NSMutableDictionary <NSString *, id > *downloadEvent = [self baseEvent ];
14871524 [downloadEvent addEntriesFromDictionary: @{
14881525 @" downloadUrl" : (response.URL ).absoluteString ,
14891526 }];
14901527 _onFileDownload (downloadEvent);
14911528 }
1529+ policy = WKNavigationResponsePolicyCancel ;
14921530 }
14931531 }
14941532
0 commit comments