Skip to content

Commit 3328e19

Browse files
committed
platform check
1 parent 3f48bb8 commit 3328e19

File tree

5 files changed

+195
-28
lines changed

5 files changed

+195
-28
lines changed

Sources/PackageCollections/API.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,6 @@ public enum PackageCollectionError: Equatable, Error {
182182
case invalidSignature
183183

184184
case missingSignature
185+
186+
case unsupportedPlatform
185187
}

Sources/PackageCollections/PackageCollections.swift

Lines changed: 68 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ import TSCBasic
1414

1515
// TODO: is there a better name? this conflicts with the module name which is okay in this case but not ideal in Swift
1616
public struct PackageCollections: PackageCollectionsProtocol {
17+
// Check JSONPackageCollectionProvider.isSignatureCheckSupported before updating or removing this
18+
#if os(macOS) || os(Linux) || os(Windows)
19+
static let isSupportedPlatform = true
20+
#else
21+
static let isSupportedPlatform = false
22+
#endif
23+
1724
private let configuration: Configuration
1825
private let diagnosticsEngine: DiagnosticsEngine?
1926
private let storageContainer: (storage: Storage, owned: Bool)
@@ -64,6 +71,10 @@ public struct PackageCollections: PackageCollectionsProtocol {
6471

6572
public func listCollections(identifiers: Set<PackageCollectionsModel.CollectionIdentifier>? = nil,
6673
callback: @escaping (Result<[PackageCollectionsModel.Collection], Error>) -> Void) {
74+
guard Self.isSupportedPlatform else {
75+
return callback(.failure(PackageCollectionError.unsupportedPlatform))
76+
}
77+
6778
self.storage.sources.list { result in
6879
switch result {
6980
case .failure(let error):
@@ -115,6 +126,10 @@ public struct PackageCollections: PackageCollectionsProtocol {
115126
}
116127

117128
public func refreshCollections(callback: @escaping (Result<[PackageCollectionsModel.CollectionSource], Error>) -> Void) {
129+
guard Self.isSupportedPlatform else {
130+
return callback(.failure(PackageCollectionError.unsupportedPlatform))
131+
}
132+
118133
self.storage.sources.list { result in
119134
switch result {
120135
case .failure(let error):
@@ -137,17 +152,23 @@ public struct PackageCollections: PackageCollectionsProtocol {
137152
}
138153
}
139154

140-
public func refreshCollection(
141-
_ source: PackageCollectionsModel.CollectionSource,
142-
callback: @escaping (Result<PackageCollectionsModel.Collection, Error>) -> Void
143-
) {
155+
public func refreshCollection(_ source: PackageCollectionsModel.CollectionSource,
156+
callback: @escaping (Result<PackageCollectionsModel.Collection, Error>) -> Void) {
157+
guard Self.isSupportedPlatform else {
158+
return callback(.failure(PackageCollectionError.unsupportedPlatform))
159+
}
160+
144161
self.refreshCollectionFromSource(source: source, trustConfirmationProvider: nil, callback: callback)
145162
}
146163

147164
public func addCollection(_ source: PackageCollectionsModel.CollectionSource,
148165
order: Int? = nil,
149166
trustConfirmationProvider: ((PackageCollectionsModel.Collection, @escaping (Bool) -> Void) -> Void)? = nil,
150167
callback: @escaping (Result<PackageCollectionsModel.Collection, Error>) -> Void) {
168+
guard Self.isSupportedPlatform else {
169+
return callback(.failure(PackageCollectionError.unsupportedPlatform))
170+
}
171+
151172
if let errors = source.validate()?.errors() {
152173
return callback(.failure(MultipleErrors(errors)))
153174
}
@@ -182,6 +203,10 @@ public struct PackageCollections: PackageCollectionsProtocol {
182203

183204
public func removeCollection(_ source: PackageCollectionsModel.CollectionSource,
184205
callback: @escaping (Result<Void, Error>) -> Void) {
206+
guard Self.isSupportedPlatform else {
207+
return callback(.failure(PackageCollectionError.unsupportedPlatform))
208+
}
209+
185210
self.storage.sources.remove(source: source) { result in
186211
switch result {
187212
case .failure(let error):
@@ -195,11 +220,19 @@ public struct PackageCollections: PackageCollectionsProtocol {
195220
public func moveCollection(_ source: PackageCollectionsModel.CollectionSource,
196221
to order: Int,
197222
callback: @escaping (Result<Void, Error>) -> Void) {
223+
guard Self.isSupportedPlatform else {
224+
return callback(.failure(PackageCollectionError.unsupportedPlatform))
225+
}
226+
198227
self.storage.sources.move(source: source, to: order, callback: callback)
199228
}
200229

201230
public func updateCollection(_ source: PackageCollectionsModel.CollectionSource,
202231
callback: @escaping (Result<PackageCollectionsModel.Collection, Error>) -> Void) {
232+
guard Self.isSupportedPlatform else {
233+
return callback(.failure(PackageCollectionError.unsupportedPlatform))
234+
}
235+
203236
self.storage.sources.update(source: source) { result in
204237
switch result {
205238
case .failure(let error):
@@ -215,6 +248,10 @@ public struct PackageCollections: PackageCollectionsProtocol {
215248
// If not found locally (storage), the collection will be fetched from the source.
216249
public func getCollection(_ source: PackageCollectionsModel.CollectionSource,
217250
callback: @escaping (Result<PackageCollectionsModel.Collection, Error>) -> Void) {
251+
guard Self.isSupportedPlatform else {
252+
return callback(.failure(PackageCollectionError.unsupportedPlatform))
253+
}
254+
218255
if let errors = source.validate()?.errors() {
219256
return callback(.failure(MultipleErrors(errors)))
220257
}
@@ -234,11 +271,13 @@ public struct PackageCollections: PackageCollectionsProtocol {
234271

235272
// MARK: - Packages
236273

237-
public func findPackages(
238-
_ query: String,
239-
collections: Set<PackageCollectionsModel.CollectionIdentifier>? = nil,
240-
callback: @escaping (Result<PackageCollectionsModel.PackageSearchResult, Error>) -> Void
241-
) {
274+
public func findPackages(_ query: String,
275+
collections: Set<PackageCollectionsModel.CollectionIdentifier>? = nil,
276+
callback: @escaping (Result<PackageCollectionsModel.PackageSearchResult, Error>) -> Void) {
277+
guard Self.isSupportedPlatform else {
278+
return callback(.failure(PackageCollectionError.unsupportedPlatform))
279+
}
280+
242281
self.storage.sources.list { result in
243282
switch result {
244283
case .failure(let error):
@@ -257,6 +296,10 @@ public struct PackageCollections: PackageCollectionsProtocol {
257296

258297
public func getPackageMetadata(_ reference: PackageReference,
259298
callback: @escaping (Result<PackageCollectionsModel.PackageMetadata, Error>) -> Void) {
299+
guard Self.isSupportedPlatform else {
300+
return callback(.failure(PackageCollectionError.unsupportedPlatform))
301+
}
302+
260303
// first find in storage
261304
self.findPackage(identifier: reference.identity) { result in
262305
switch result {
@@ -288,10 +331,12 @@ public struct PackageCollections: PackageCollectionsProtocol {
288331

289332
// MARK: - Targets
290333

291-
public func listTargets(
292-
collections: Set<PackageCollectionsModel.CollectionIdentifier>? = nil,
293-
callback: @escaping (Result<PackageCollectionsModel.TargetListResult, Error>) -> Void
294-
) {
334+
public func listTargets(collections: Set<PackageCollectionsModel.CollectionIdentifier>? = nil,
335+
callback: @escaping (Result<PackageCollectionsModel.TargetListResult, Error>) -> Void) {
336+
guard Self.isSupportedPlatform else {
337+
return callback(.failure(PackageCollectionError.unsupportedPlatform))
338+
}
339+
295340
self.listCollections(identifiers: collections) { result in
296341
switch result {
297342
case .failure(let error):
@@ -303,12 +348,14 @@ public struct PackageCollections: PackageCollectionsProtocol {
303348
}
304349
}
305350

306-
public func findTargets(
307-
_ query: String,
308-
searchType: PackageCollectionsModel.TargetSearchType? = nil,
309-
collections: Set<PackageCollectionsModel.CollectionIdentifier>? = nil,
310-
callback: @escaping (Result<PackageCollectionsModel.TargetSearchResult, Error>) -> Void
311-
) {
351+
public func findTargets(_ query: String,
352+
searchType: PackageCollectionsModel.TargetSearchType? = nil,
353+
collections: Set<PackageCollectionsModel.CollectionIdentifier>? = nil,
354+
callback: @escaping (Result<PackageCollectionsModel.TargetSearchResult, Error>) -> Void) {
355+
guard Self.isSupportedPlatform else {
356+
return callback(.failure(PackageCollectionError.unsupportedPlatform))
357+
}
358+
312359
let searchType = searchType ?? .exactMatch
313360

314361
self.storage.sources.list { result in
@@ -393,10 +440,8 @@ public struct PackageCollections: PackageCollectionsProtocol {
393440
}
394441
}
395442

396-
func findPackage(
397-
identifier: PackageIdentity,
398-
callback: @escaping (Result<PackageCollectionsModel.PackageSearchResult.Item, Error>) -> Void
399-
) {
443+
func findPackage(identifier: PackageIdentity,
444+
callback: @escaping (Result<PackageCollectionsModel.PackageSearchResult.Item, Error>) -> Void) {
400445
self.storage.sources.list { result in
401446
switch result {
402447
case .failure(let error):

Sources/PackageCollections/Providers/JSONPackageCollectionProvider.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ struct JSONPackageCollectionProvider: PackageCollectionProvider {
2828
// FIXME: remove
2929
static let enableSignatureCheck = ProcessInfo.processInfo.environment["ENABLE_COLLECTION_SIGNATURE_CHECK"] != nil
3030

31+
// TODO: This can be removed when the `Security` framework APIs that the `PackageCollectionsSigning`
32+
// module depends on are available on all Apple platforms.
33+
#if os(macOS) || os(Linux) || os(Windows)
34+
static let isSignatureCheckSupported = true
35+
#else
36+
static let isSignatureCheckSupported = false
37+
#endif
38+
3139
private let configuration: Configuration
3240
private let diagnosticsEngine: DiagnosticsEngine
3341
private let httpClient: HTTPClient
@@ -136,6 +144,10 @@ struct JSONPackageCollectionProvider: PackageCollectionProvider {
136144
// Don't validate signature; set isVerified=false
137145
callback(self.makeCollection(from: signedCollection.collection, source: source, signature: Model.SignatureData(from: signedCollection.signature, isVerified: false)))
138146
} else {
147+
if !Self.isSignatureCheckSupported {
148+
fatalError("Unsupported platform")
149+
}
150+
139151
// Check the signature
140152
self.signatureValidator.validate(signedCollection: signedCollection, certPolicyKey: certPolicyKey) { result in
141153
switch result {

Tests/PackageCollectionsTests/JSONPackageCollectionProviderTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ class JSONPackageCollectionProviderTests: XCTestCase {
335335
}
336336

337337
func testSignedGood() throws {
338-
if !enableSignatureCheck {
338+
if !enableSignatureCheck || !JSONPackageCollectionProvider.isSignatureCheckSupported {
339339
try XCTSkipIf(true)
340340
}
341341

@@ -471,7 +471,7 @@ class JSONPackageCollectionProviderTests: XCTestCase {
471471
}
472472

473473
func testSigned_noTrustedRootCertsConfigured() throws {
474-
if !enableSignatureCheck {
474+
if !enableSignatureCheck || !JSONPackageCollectionProvider.isSignatureCheckSupported {
475475
try XCTSkipIf(true)
476476
}
477477

@@ -515,7 +515,7 @@ class JSONPackageCollectionProviderTests: XCTestCase {
515515
}
516516

517517
func testSignedBad() throws {
518-
if !enableSignatureCheck {
518+
if !enableSignatureCheck || !JSONPackageCollectionProvider.isSignatureCheckSupported {
519519
try XCTSkipIf(true)
520520
}
521521

@@ -560,7 +560,7 @@ class JSONPackageCollectionProviderTests: XCTestCase {
560560
}
561561

562562
func testSignedLocalFile() throws {
563-
if !enableSignatureCheck {
563+
if !enableSignatureCheck || !JSONPackageCollectionProvider.isSignatureCheckSupported {
564564
try XCTSkipIf(true)
565565
}
566566

0 commit comments

Comments
 (0)