Skip to content

armgit / peel

Public Imported from GitHub
0

Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.

Ask GitHub for an Electron app's feed only with names

Țugui Dragoș0bf498079330
diff --git a/Packages/PeelCore/Sources/PeelCore/Updates/ElectronUpdater.swift b/Packages/PeelCore/Sources/PeelCore/Updates/ElectronUpdater.swift
index 6e920fd6..42d5a53c 100644
--- a/Packages/PeelCore/Sources/PeelCore/Updates/ElectronUpdater.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Updates/ElectronUpdater.swift
@@ -27,7 +27,9 @@ enum ElectronUpdater {
static func feedURL(fromConfiguration text: String) -> URL? {
let fields = topLevelFields(in: text)
- let file = "\(fields["channel"] ?? "latest")-mac.yml"
+ let channel = fields["channel"] ?? "latest"
+ guard isAName(channel) else { return nil }
+ let file = "\(channel)-mac.yml"
switch fields["provider"] {
case "generic":
@@ -37,8 +39,10 @@ enum ElectronUpdater {
// A `host` other than github.com is a company's own server. The owner and repository were never meant
// to leave that server, so they are not sent to github.com.
guard fields["host"] == nil || fields["host"] == "github.com" else { return nil }
- guard let owner = fields["owner"], let repository = fields["repo"] else { return nil }
- return URL(string: "https://github.com/\(owner)/\(repository)/releases/latest/download/\(file)")
+ guard let owner = fields["owner"], let repository = fields["repo"], isAName(owner), isAName(repository),
+ let github = URL(string: "https://github.com")
+ else { return nil }
+ return github.appending(components: owner, repository, "releases", "latest", "download", file)
case "s3", "r2":
guard let base = storageAddress(in: fields), let url = URL(string: base), url.scheme == "https",
url.host() != nil
@@ -49,6 +53,19 @@ enum ElectronUpdater {
}
}
+ /// True for one name, as GitHub writes an owner's or a repository's and electron-builder a channel's: letters,
+ /// digits, `.`, `_`, and `-`, not beginning with a dot or a hyphen, and no longer than a repository's name may be.
+ /// Anything else, read from the app's own file, could name another page on github.com.
+ private static func isAName(_ value: String) -> Bool {
+ guard let first = value.utf8.first, first != UInt8(ascii: "."), first != UInt8(ascii: "-"),
+ value.utf8.count <= 100
+ else { return false }
+ return value.utf8.allSatisfy { byte in
+ (byte >= 0x30 && byte <= 0x39) || (byte >= 0x41 && byte <= 0x5A) || (byte >= 0x61 && byte <= 0x7A)
+ || byte == 0x2E || byte == 0x2D || byte == 0x5F
+ }
+ }
+
/// The address electron-updater asks a storage service at (`getS3LikeProviderBaseUrl`), only when the app writes it
/// whole: an S3 `endpoint`, or an R2 bucket's `publicUrl`. Any other address is one electron-updater builds itself,
/// and R2's S3 endpoint always asks for credentials, so no app can update from it.
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/UpdateTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/UpdateTests.swift
index 24d6abc3..236845c9 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/UpdateTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/UpdateTests.swift
@@ -291,6 +291,25 @@ struct ElectronUpdaterTests {
#expect(ElectronUpdater.feedURL(fromConfiguration: "provider: github\nhost: github.com\nowner: example\nrepo: tool\n") != nil)
}
+ /// The owner, repository, and channel come from the app's own file, so each has to be one name, as GitHub writes
+ /// an owner's or a repository's and electron-builder a channel's, or they could ask for any page on github.com.
+ @Test func asksGitHubOnlyWithNames() {
+ func feed(_ configuration: String) -> String? {
+ ElectronUpdater.feedURL(fromConfiguration: configuration)?.absoluteString
+ }
+ #expect(feed("provider: github\nowner: example\nrepo: tool.js\n")
+ == "https://github.com/example/tool.js/releases/latest/download/latest-mac.yml")
+ #expect(feed("provider: github\nowner: example-org\nrepo: my_tool\nchannel: beta\n")
+ == "https://github.com/example-org/my_tool/releases/latest/download/beta-mac.yml")
+
+ #expect(feed("provider: github\nowner: vendor?x=1\nrepo: app\n") == nil)
+ #expect(feed("provider: github\nowner: other/project/releases/download/v1/file.yml#\nrepo: app\n") == nil)
+ #expect(feed("provider: github\nowner: vendor\nrepo: app\nchannel: ../../../../orgs/example/repositories?q=\n") == nil)
+ #expect(feed("provider: github\nowner: -vendor\nrepo: app\n") == nil)
+ #expect(feed("provider: github\nowner: vendor\nrepo: .app\n") == nil)
+ #expect(feed("provider: generic\nurl: https://cdn.example.com/release/\nchannel: ../other\n") == nil)
+ }
+
@Test func asksAStorageServiceOnlyAtAnAddressTheAppWritesWhole() {
func feed(_ configuration: String) -> String? {
ElectronUpdater.feedURL(fromConfiguration: configuration)?.absoluteString