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
Fonts: check file signature before emitting warning in sandboxed env (plugin) #1080
base: develop
Are you sure you want to change the base?
Conversation
12ab7c4
to
a5b18cf
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seeing the same issue, I was happy to see this being addressed by a PR.
This looks good to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice idea, I like it! Some small changes to improve the code & perf.
do { | ||
let values = try path.url.resourceValues(forKeys: [.typeIdentifierKey]) | ||
guard let uti = values.typeIdentifier else { | ||
return true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is incorrect. If you can't get the typeIdentifier
, it should fall back to reading the header bytes like you've done below.
I'd rework this do-catch
into a simple if-else
.
} | ||
return UTTypeConformsTo(uti as CFString, kUTTypeFont) | ||
} catch { | ||
if let data = try? Data(contentsOf: path.url), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will load the whole file into memory, which is (or can be) slow. Instead, we should use FileHandle.read(upToCount:), with a maximum of N header bytes.
Note: best to create a constant for the amount of header bytes, just to make it clear.
var isTrueTypeFont: Bool { | ||
var values = [UInt8](repeating: 0, count:5) | ||
self.copyBytes(to: &values, count: 5) | ||
let firstBytesOfTTF: [UInt8] = [0x00, 0x01, 0x00, 0x00, 0x00] // https://en.wikipedia.org/wiki/List_of_file_signatures |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These could be constants, like trueTypeFontHeaderBytes
and such. And then a simple function on Path
to check a file type given some header bytes.
Also avoids having a fixed "count" in the line above, as you can get the .count
from the constant.
Fix for #1079 and #952
To prevent warning being displayed in Xcode when using the plugin, I've added a check for the signature of font files (the first bytes) before emitting the warning