Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions mac/Config/Config/PackageContentWebView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import WebKit
import KeymanSettings

public struct PackageContentWebView: NSViewRepresentable {
private static let allowedLocalExtensions = ["pdf", "txt", "png", "jpg", "jpeg"]
private static let allowedMimeTypes = ["application/pdf", "text/plain", "image/jpeg", "image/png"]
Comment on lines +16 to +17

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I am uncomfortable with this. There may be many other file types included, e.g. webp, gif, videos, css, fonts. This is hard to whitelist correctly.

Is this for navigation operations only?


let packageFileUrl: URL

// create the AppKit view instance
Expand Down Expand Up @@ -65,10 +68,21 @@ public struct PackageContentWebView: NSViewRepresentable {
return
}

// local files load in webview
// load local files in webview, force-loading some common file types
if url.isFileURL {
decisionHandler(.allow)
let fileExtension = url.pathExtension.lowercased()

if PackageContentWebView.allowedLocalExtensions.contains(fileExtension) {
// cancel the automatic navigation (which fails silently)
decisionHandler(.cancel)

// force-load the file into the web view frame
webView.loadFileURL(url, allowingReadAccessTo: url.deletingLastPathComponent())
return
}

decisionHandler(.allow)
return
}

// handle external links by opening in web browser
Expand All @@ -89,4 +103,20 @@ public struct PackageContentWebView: NSViewRepresentable {
NSWorkspace.shared.open(externalUrl)
}
}

/**
* Allow display of some common file types that may be linked in the package help
*/
public func webView(_ webView: WKWebView,
decidePolicyFor navigationResponse: WKNavigationResponse,
decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {

if let mimeType = navigationResponse.response.mimeType {
if PackageContentWebView.allowedMimeTypes.contains(mimeType) {
decisionHandler(.allow)
return
}
}
decisionHandler(.allow)
Comment on lines +114 to +120

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This seems to be a nothing test? both code paths give decisionHandler(.allow)

}
}