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

Fonts: check file signature before emitting warning in sandboxed env (plugin) #1080

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

kenji21
Copy link

@kenji21 kenji21 commented Sep 27, 2023

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

Screenshot 5

@kenji21 kenji21 changed the title Add one more check before emitting warning in sandboxed env (plugin) Fonts: check file signature before emitting warning in sandboxed env (plugin) Sep 27, 2023
@kenji21 kenji21 force-pushed the develop branch 2 times, most recently from 12ab7c4 to a5b18cf Compare September 27, 2023 07:41
Copy link
Contributor

@mrackwitz mrackwitz left a 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.

Copy link
Member

@djbe djbe left a 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
Copy link
Member

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),
Copy link
Member

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
Copy link
Member

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.

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

Successfully merging this pull request may close these issues.

None yet

6 participants