-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBrowserStackAccessibilityLint.swift
More file actions
766 lines (660 loc) · 29.9 KB
/
Copy pathBrowserStackAccessibilityLint.swift
File metadata and controls
766 lines (660 loc) · 29.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
import Foundation
import PackagePlugin
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
#if canImport(Darwin)
import Darwin
#elseif canImport(Glibc)
import Glibc
#elseif canImport(ucrt)
import ucrt
#endif
@main
struct BrowserStackAccessibilityLintPlugin: CommandPlugin {
func performCommand(context: PluginContext, arguments: [String]) async throws {
var extractor = ArgumentExtractor(arguments)
let overrideDownloadURLString = extractor.extractOption(named: "download-url").last
let forceDownloadFlag = extractor.extractFlag(named: "force-download") > 0
let passthrough = extractor.remainingArguments
let environment = ProcessInfo.processInfo.environment
let forceDownload = forceDownloadFlag || isTruthy(environment["BROWSERSTACK_A11Y_CLI_FORCE_DOWNLOAD"])
let overrideDownloadURL = try parseOverride(urlString: overrideDownloadURLString ?? environment["BROWSERSTACK_A11Y_CLI_DOWNLOAD_URL"])
let cacheRoot = packageCacheRoot()
let artifact = try await BrowserStackCLIArtifact.ensureLatestBinary(
overrideURL: overrideDownloadURL,
forceDownload: forceDownload,
cacheRoot: cacheRoot
)
Diagnostics.remark("BrowserStackAccessibilityLint: Using CLI \(artifact.version) at \(artifact.executableURL.path)")
let sanitizedArguments = sanitizeArguments(passthrough)
let finalArguments = ["a11y"] + sanitizedArguments
try await runCLI(
executableURL: artifact.executableURL,
arguments: finalArguments,
workingDirectory: context.package.directory
)
}
}
private func isTruthy(_ value: String?) -> Bool {
guard let value = value?.trimmingCharacters(in: .whitespacesAndNewlines).lowercased(), !value.isEmpty else {
return false
}
return ["1", "true", "yes"].contains(value)
}
private func packageCacheRoot() -> URL {
// NOTE: Ignoring the package directory for caching; using a global user cache folder.
// Order of precedence:
// 1. XDG_CACHE_HOME if set
// 2. HOME/.cache
// 3. NSHomeDirectory()/.cache (fallback)
let env = ProcessInfo.processInfo.environment
let baseCache: URL = {
if let xdg = env["XDG_CACHE_HOME"], !xdg.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
return URL(fileURLWithPath: xdg, isDirectory: true)
}
if let home = env["HOME"], !home.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
return URL(fileURLWithPath: home, isDirectory: true).appendingPathComponent(".cache", isDirectory: true)
}
return URL(fileURLWithPath: NSHomeDirectory(), isDirectory: true).appendingPathComponent(".cache", isDirectory: true)
}()
let target = baseCache
.appendingPathComponent("browserstack", isDirectory: true)
.appendingPathComponent("devtools", isDirectory: true)
.appendingPathComponent("spm-plugin", isDirectory: true)
// Verify write access to cache directory (exit code 2 if not writable)
let fm = FileManager.default
do {
try fm.createDirectory(at: target, withIntermediateDirectories: true)
if !fm.isWritableFile(atPath: target.path) {
forwardExit(code: 2, message: "Unable to access cache directory. Please add \"--allow-writing-to-directory ~/.cache/\" directive in the linter’s build phase command.")
}
let probe = target.appendingPathComponent(".write-probe-\(UUID().uuidString)")
do {
try "probe".data(using: .utf8)?.write(to: probe, options: [.atomic, .completeFileProtection])
try? fm.removeItem(at: probe)
} catch {
forwardExit(code: 2, message: "Unable to access cache directory. Please include directive \"--allow-writing-to-directory ~/.cache/\" where you are invoking the Swift package")
}
} catch {
forwardExit(code: 2, message: "Unable to access cache directory. Please include directive \"--allow-writing-to-directory ~/.cache/\" where you are invoking the Swift package")
}
return target
}
// MARK: - URL / Argument helpers
private func parseOverride(urlString: String?) throws -> URL? {
guard let urlString = urlString, !urlString.isEmpty else {
return nil
}
guard let url = URL(string: urlString), let scheme = url.scheme else {
throw PluginError("Invalid download URL: \(urlString). Only HTTPS URLs are supported.")
}
guard scheme.lowercased() == "https" else {
throw PluginError("Unsupported URL scheme '\(scheme)' in download URL. Only HTTPS is allowed.")
}
return url
}
private func sanitizeArguments(_ arguments: [String]) -> [String] {
var result: [String] = []
var skipNext = false
var passthroughMode = false
for argument in arguments {
if passthroughMode {
result.append(argument)
continue
}
if skipNext {
skipNext = false
continue
}
if argument == "--" {
passthroughMode = true
result.append(argument)
continue
}
if argument == "--output-format" || argument == "-o" {
skipNext = true
continue
}
if argument.hasPrefix("--output-format=") {
continue
}
if argument.count > 2, argument.hasPrefix("-o"), argument != "-o" {
// Handle short-form like "-oxcode".
continue
}
result.append(argument)
}
return result
}
// MARK: - CLI artifact management
private struct BrowserStackCLIArtifact {
let version: String
let executableURL: URL
static func ensureLatestBinary(overrideURL: URL?, forceDownload: Bool, cacheRoot: URL) async throws -> BrowserStackCLIArtifact {
let downloader = BrowserStackCLIDownloader(overrideURL: overrideURL, forceDownload: forceDownload, cacheRoot: cacheRoot)
return try await downloader.ensureArtifact()
}
}
private struct BrowserStackCLIDownloader {
let overrideURL: URL?
let forceDownload: Bool
let cacheRoot: URL
private var fileManager: FileManager { .default }
// Decompression-bomb guards (DEVA11Y-484). The CLI binary is a few tens of MB; these
// ceilings leave generous headroom while bounding a malicious archive's footprint.
private static let maxCompressedBytes = 100 * 1024 * 1024 // 100 MB on the wire
private static let maxDecompressedBytes: Int64 = 200 * 1024 * 1024 // 200 MB on disk
private static let maxArchiveEntries = 10_000
func ensureArtifact() async throws -> BrowserStackCLIArtifact {
if let overrideURL {
let info = try await resolveOverrideArtifact(from: overrideURL)
return try await prepareArtifact(using: info)
}
let defaultURL = try defaultDownloadURL()
let info = try await resolveRemoteArtifact(from: defaultURL)
return try await prepareArtifact(using: info)
}
private func ensureCacheRootExists() throws -> URL {
do {
try fileManager.createDirectory(at: cacheRoot, withIntermediateDirectories: true)
} catch let error as NSError where error.domain == NSCocoaErrorDomain && error.code == CocoaError.fileWriteNoPermission.rawValue {
throw PluginError("Permission denied writing to cache directory at \(cacheRoot.path). Rerun the plugin with --allow-writing-to-package-directory.")
} catch {
throw error
}
return cacheRoot
}
private func prepareArtifact(using info: ArtifactInfo) async throws -> BrowserStackCLIArtifact {
let cacheRoot = try ensureCacheRootExists()
let versionDirectory = cacheRoot.appendingPathComponent(info.version, isDirectory: true)
let executableName = info.executableName
let expectedExecutableURL = versionDirectory.appendingPathComponent(executableName, isDirectory: false)
if !forceDownload, fileManager.isExecutableFile(atPath: expectedExecutableURL.path) {
return BrowserStackCLIArtifact(version: info.version, executableURL: expectedExecutableURL)
}
if fileManager.fileExists(atPath: versionDirectory.path) {
try fileManager.removeItem(at: versionDirectory)
}
try fileManager.createDirectory(at: versionDirectory, withIntermediateDirectories: true)
Diagnostics.remark("BrowserStackAccessibilityLint: Downloading CLI \(info.version)...")
#if os(Windows)
let archiveURL = versionDirectory.appendingPathComponent("browserstack-cli.zip")
try await download(from: info.resolvedURL, to: archiveURL)
Diagnostics.remark("BrowserStackAccessibilityLint: Extracting CLI \(info.version)...")
try unzip(archive: archiveURL, into: versionDirectory)
try? fileManager.removeItem(at: archiveURL)
#else
try extractWithBsdtar(from: info.resolvedURL, into: versionDirectory)
#endif
// Platform-agnostic backstop (DEVA11Y-484). The bsdtar paths already abort mid-stream
// via the watchdog; this also covers the Windows Expand-Archive path, which has no
// streaming guard — it can't bound peak disk during extraction, but it rejects and
// cleans up a bomb before the binary is ever used.
if let reason = footprintExceeded(at: versionDirectory, maxBytes: Self.maxDecompressedBytes, maxEntries: Self.maxArchiveEntries) {
try? fileManager.removeItem(at: versionDirectory)
forwardExit(code: 1, message: "BrowserStack CLI archive rejected: \(reason). Aborting to prevent disk exhaustion.")
}
let locatedBinary = try locateExecutable(in: versionDirectory, preferredName: executableName)
let finalBinaryURL: URL
if locatedBinary.lastPathComponent == executableName {
finalBinaryURL = locatedBinary
} else {
finalBinaryURL = expectedExecutableURL
if fileManager.fileExists(atPath: finalBinaryURL.path) {
try fileManager.removeItem(at: finalBinaryURL)
}
try fileManager.moveItem(at: locatedBinary, to: finalBinaryURL)
}
try ensureExecutablePermissions(at: finalBinaryURL)
return BrowserStackCLIArtifact(version: info.version, executableURL: finalBinaryURL)
}
#if !os(Windows)
private func extractWithBsdtar(from url: URL, into directory: URL) throws {
if url.isFileURL {
try extractLocalArchive(at: url, into: directory)
} else {
try extractRemoteArchive(from: url, into: directory)
}
}
private func extractRemoteArchive(from url: URL, into directory: URL) throws {
let pipe = Pipe()
let curl = Process()
curl.executableURL = URL(fileURLWithPath: "/usr/bin/env")
// --max-filesize caps the *compressed* download as a coarse first line of defense
// against a malicious endpoint streaming an unbounded body.
curl.arguments = ["curl", "-fsSL", "--max-filesize", String(Self.maxCompressedBytes), url.absoluteString]
curl.standardOutput = pipe
let curlError = Pipe()
curl.standardError = curlError
let bsdtar = Process()
bsdtar.executableURL = URL(fileURLWithPath: "/usr/bin/env")
bsdtar.arguments = ["bsdtar", "-xpf", "-", "-C", directory.path]
bsdtar.standardInput = pipe
let tarError = Pipe()
bsdtar.standardError = tarError
do {
try bsdtar.run()
} catch {
throw PluginError("Unable to launch bsdtar: \(error.localizedDescription)")
}
// See the DEVA11Y-484 EXTRACTION GUARD block below for the rationale.
let limitState = startExtractionWatchdog(on: bsdtar, directory: directory, maxBytes: Self.maxDecompressedBytes, maxEntries: Self.maxArchiveEntries)
do {
try curl.run()
} catch {
bsdtar.terminate()
bsdtar.waitUntilExit()
throw PluginError("Unable to launch curl: \(error.localizedDescription)")
}
curl.waitUntilExit()
pipe.fileHandleForWriting.closeFile()
bsdtar.waitUntilExit()
// Catch a bomb that completed within a single watchdog poll interval (fast disk).
if !limitState.exceeded, let reason = footprintExceeded(at: directory, maxBytes: Self.maxDecompressedBytes, maxEntries: Self.maxArchiveEntries) {
limitState.markExceeded(reason)
}
if limitState.exceeded {
try? fileManager.removeItem(at: directory)
forwardExit(code: 1, message: "BrowserStack CLI archive rejected: \(limitState.reason). Aborting to prevent disk exhaustion.")
}
if curl.terminationStatus != 0 {
let message = String(data: curlError.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8)?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
forwardExit(code: curl.terminationStatus, message: message)
}
guard bsdtar.terminationReason == .exit, bsdtar.terminationStatus == 0 else {
let message = String(data: tarError.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8)?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
forwardExit(code: bsdtar.terminationStatus, message: message.isEmpty ? "bsdtar failed to extract BrowserStack CLI." : message)
}
}
private func extractLocalArchive(at archiveURL: URL, into directory: URL) throws {
let process = Process()
process.executableURL = URL(fileURLWithPath: "/usr/bin/env")
process.arguments = ["bsdtar", "-xpf", archiveURL.path, "-C", directory.path]
let errorPipe = Pipe()
process.standardError = errorPipe
let limitState: ExtractionLimitState
do {
try process.run()
// Decompressed-size/entry guard (DEVA11Y-484): same rationale as the remote path.
limitState = startExtractionWatchdog(on: process, directory: directory, maxBytes: Self.maxDecompressedBytes, maxEntries: Self.maxArchiveEntries)
process.waitUntilExit()
} catch {
throw PluginError("Failed to launch bsdtar: \(error.localizedDescription)")
}
// Catch a bomb that completed within a single watchdog poll interval (fast disk).
if !limitState.exceeded, let reason = footprintExceeded(at: directory, maxBytes: Self.maxDecompressedBytes, maxEntries: Self.maxArchiveEntries) {
limitState.markExceeded(reason)
}
if limitState.exceeded {
try? fileManager.removeItem(at: directory)
forwardExit(code: 1, message: "BrowserStack CLI archive rejected: \(limitState.reason). Aborting to prevent disk exhaustion.")
}
if process.terminationReason != .exit || process.terminationStatus != 0 {
// Fall back to copying the file directly if it's already an executable.
let message = String(data: errorPipe.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8)?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
if fileManager.isExecutableFile(atPath: archiveURL.path) {
let destination = directory.appendingPathComponent(archiveURL.lastPathComponent)
if fileManager.fileExists(atPath: destination.path) {
try fileManager.removeItem(at: destination)
}
try fileManager.copyItem(at: archiveURL, to: destination)
} else {
forwardExit(code: process.terminationStatus, message: message.isEmpty ? "bsdtar failed to extract BrowserStack CLI." : message)
}
}
}
#endif
private func resolveOverrideArtifact(from url: URL) async throws -> ArtifactInfo {
let resolvedURL: URL
if url.isFileURL {
resolvedURL = url
} else {
resolvedURL = try await followRedirects(for: url)
}
let version = extractVersion(from: resolvedURL) ?? "override"
return ArtifactInfo(version: version, resolvedURL: resolvedURL, executableName: executableFileName())
}
private func resolveRemoteArtifact(from url: URL) async throws -> ArtifactInfo {
let resolvedURL = try await followRedirects(for: url)
guard let version = extractVersion(from: resolvedURL) else {
throw PluginError("Unable to determine BrowserStack CLI version from \(resolvedURL.absoluteString)")
}
return ArtifactInfo(version: version, resolvedURL: resolvedURL, executableName: executableFileName())
}
private func defaultDownloadURL() throws -> URL {
let os = try currentOSName()
let arch = try currentArchName()
guard let url = URL(string: "https://api.browserstack.com/sdk/v1/download_cli?os=\(os)&os_arch=\(arch)") else {
throw PluginError("Failed to create download URL for \(os) \(arch).")
}
return url
}
private func currentOSName() throws -> String {
#if os(macOS)
return "macos"
#elseif os(Linux)
return isAlpineLinux() ? "alpine" : "linux"
#elseif os(Windows)
return "windows"
#else
throw PluginError("Unsupported operating system for BrowserStack CLI.")
#endif
}
private func currentArchName() throws -> String {
let machine = try hardwareIdentifier()
switch machine.lowercased() {
case "arm64", "aarch64":
return "arm64"
case "x86_64", "amd64":
return "x64"
default:
throw PluginError("Unsupported architecture '\(machine)' for BrowserStack CLI.")
}
}
private func followRedirects(for url: URL) async throws -> URL {
var request = URLRequest(url: url)
request.httpMethod = "HEAD"
request.timeoutInterval = 30
do {
let (_, response) = try await URLSession.shared.data(for: request)
if let http = response as? HTTPURLResponse {
if http.statusCode == 405 || http.statusCode == 501 {
return try await followWithGet(for: url)
}
if let location = http.value(forHTTPHeaderField: "Location"),
let redirectURL = URL(string: location, relativeTo: url)?.absoluteURL {
return redirectURL
}
}
if let finalURL = response.url {
return finalURL
}
} catch let error as URLError where error.code == .badServerResponse || error.code == .unsupportedURL {
return try await followWithGet(for: url)
} catch let error as URLError where error.code == .cannotConnectToHost {
throw PluginError("Network connection failed for \(url.absoluteString): \(error.localizedDescription)")
} catch {
throw error
}
throw PluginError("Failed to resolve redirect for \(url.absoluteString).")
}
private func followWithGet(for url: URL) async throws -> URL {
var request = URLRequest(url: url)
request.httpMethod = "GET"
request.httpShouldHandleCookies = false
request.timeoutInterval = 60
let (_, response) = try await URLSession.shared.data(for: request)
if let http = response as? HTTPURLResponse,
let location = http.value(forHTTPHeaderField: "Location"),
let redirectURL = URL(string: location, relativeTo: url)?.absoluteURL {
return redirectURL
}
guard let finalURL = response.url else {
throw PluginError("Failed to resolve redirect for \(url.absoluteString).")
}
return finalURL
}
#if os(Windows)
private func download(from url: URL, to destination: URL) async throws {
if url.isFileURL {
if fileManager.fileExists(atPath: destination.path) {
try fileManager.removeItem(at: destination)
}
try fileManager.copyItem(at: url, to: destination)
return
}
let (tempURL, response) = try await URLSession.shared.download(from: url)
if let httpResponse = response as? HTTPURLResponse, !(200..<300).contains(httpResponse.statusCode) {
throw PluginError("Failed to download BrowserStack CLI (HTTP \(httpResponse.statusCode)).")
}
if fileManager.fileExists(atPath: destination.path) {
try fileManager.removeItem(at: destination)
}
try fileManager.moveItem(at: tempURL, to: destination)
}
private func unzip(archive: URL, into destination: URL) throws {
let powershell = Process()
powershell.executableURL = URL(fileURLWithPath: "powershell")
powershell.arguments = [
"-NoProfile",
"-Command",
"Expand-Archive -LiteralPath \"\(archive.path)\" -DestinationPath \"\(destination.path)\" -Force"
]
try run(process: powershell, errorDescription: "Unable to extract BrowserStack CLI archive.")
}
#endif
private func locateExecutable(in directory: URL, preferredName: String) throws -> URL {
let preferredURL = directory.appendingPathComponent(preferredName, isDirectory: false)
if fileManager.isExecutableFile(atPath: preferredURL.path) {
return preferredURL
}
let enumerator = fileManager.enumerator(
at: directory,
includingPropertiesForKeys: [.isRegularFileKey, .isExecutableKey],
options: [.skipsHiddenFiles]
)
var fallback: URL?
var scanned = 0
while let element = enumerator?.nextObject() as? URL {
scanned += 1
if scanned > Self.maxArchiveEntries {
// Bound enumeration so an archive packed with millions of entries can't turn
// locateExecutable into a CPU/IO drain (DEVA11Y-484).
throw PluginError("Extracted archive contains more than \(Self.maxArchiveEntries) entries; refusing to continue.")
}
var isDirectory: ObjCBool = false
guard fileManager.fileExists(atPath: element.path, isDirectory: &isDirectory), !isDirectory.boolValue else {
continue
}
if element.lastPathComponent == preferredName {
return element
}
if fileManager.isExecutableFile(atPath: element.path) {
return element
}
if fallback == nil {
fallback = element
}
}
if let fallback {
return fallback
}
throw PluginError("Extracted archive does not contain a binary payload.")
}
private func ensureExecutablePermissions(at url: URL) throws {
#if os(Windows)
_ = url
#else
var attributes = [FileAttributeKey: Any]()
attributes[.posixPermissions] = 0o755
try fileManager.setAttributes(attributes, ofItemAtPath: url.path)
#endif
}
private func run(process: Process, errorDescription: String) throws {
process.standardOutput = FileHandle.standardOutput
process.standardError = FileHandle.standardError
process.standardInput = FileHandle.standardInput
try process.run()
process.waitUntilExit()
guard process.terminationReason == .exit else {
forwardExit(code: 1, message: errorDescription)
}
let status = process.terminationStatus
guard status == 0 else {
forwardExit(code: status, message: errorDescription)
}
}
private func executableFileName() -> String {
#if os(Windows)
return "browserstack-cli.exe"
#else
return "browserstack-cli"
#endif
}
}
private struct ArtifactInfo {
let version: String
let resolvedURL: URL
let executableName: String
}
// MARK: - System helpers
private func hardwareIdentifier() throws -> String {
#if os(Windows)
if let arch = ProcessInfo.processInfo.environment["PROCESSOR_ARCHITECTURE"]?.lowercased() {
return arch
}
throw PluginError("Unable to detect CPU architecture.")
#else
var systemInfo = utsname()
guard uname(&systemInfo) == 0 else {
throw PluginError("uname() failed to determine CPU architecture.")
}
let capacity = MemoryLayout.size(ofValue: systemInfo.machine)
let identifier = withUnsafePointer(to: &systemInfo.machine) { ptr -> String in
return ptr.withMemoryRebound(to: CChar.self, capacity: capacity) {
String(cString: $0)
}
}.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines.union(.controlCharacters))
return identifier
#endif
}
private func extractVersion(from url: URL) -> String? {
let filename = url.deletingPathExtension().lastPathComponent
if let range = filename.range(of: "-", options: .backwards) {
let version = filename[range.upperBound...]
return version.isEmpty ? nil : String(version)
}
return nil
}
#if os(Linux)
private func isAlpineLinux() -> Bool {
guard let contents = try? String(contentsOfFile: "/etc/os-release") else {
return false
}
return contents.contains("ID=alpine")
}
#else
private func isAlpineLinux() -> Bool { false }
#endif
// MARK: - CLI invocation
private func runCLI(executableURL: URL, arguments: [String], workingDirectory: PackagePlugin.Path) async throws {
let process = Process()
process.executableURL = executableURL
process.arguments = arguments
process.currentDirectoryURL = URL(fileURLWithPath: workingDirectory.string, isDirectory: true)
process.standardInput = FileHandle.standardInput
process.standardOutput = FileHandle.standardOutput
process.standardError = FileHandle.standardError
try process.run()
process.waitUntilExit()
guard process.terminationReason == .exit else {
forwardExit(code: 1, message: "browserstack-cli terminated abnormally.")
}
let status = process.terminationStatus
guard status == 0 else {
forwardExit(code: status, message: "")
}
}
// MARK: - Error
// === DEVA11Y-484 EXTRACTION GUARD: shared block ===
// This block is mirrored verbatim in
// scripts/test/swift-harness/Sources/ExtractionHarness/Guard.swift
// so the integration harness exercises the real logic. scripts/test/check_drift.sh
// fails CI if the two copies diverge. Edit both, or neither.
//
// Rationale: bsdtar writes decompressed bytes straight to disk, so a cap on the
// curl→bsdtar pipe would only bound the *compressed* size — useless against a
// decompression bomb. Instead we poll the destination directory while bsdtar runs
// and terminate it if the decompressed footprint crosses a byte OR entry ceiling
// (the entry ceiling stops a "millions of tiny files" bomb that stays small on disk).
/// Thread-safe flag shared between the extraction watchdog and the main flow.
final class ExtractionLimitState {
private let lock = NSLock()
private var didExceed = false
private var why = ""
func markExceeded(_ reason: String) {
lock.lock()
if !didExceed {
didExceed = true
why = reason
}
lock.unlock()
}
var exceeded: Bool {
lock.lock()
defer { lock.unlock() }
return didExceed
}
var reason: String {
lock.lock()
defer { lock.unlock() }
return why
}
}
/// Total bytes and entry count of all regular files under `url`.
func extractionFootprint(at url: URL) -> (bytes: Int64, entries: Int) {
let fm = FileManager.default
guard let enumerator = fm.enumerator(at: url, includingPropertiesForKeys: [.isRegularFileKey, .fileSizeKey]) else {
return (0, 0)
}
var total: Int64 = 0
var count = 0
for case let element as URL in enumerator {
count += 1
let values = try? element.resourceValues(forKeys: [.isRegularFileKey, .fileSizeKey])
if values?.isRegularFile == true, let size = values?.fileSize {
total += Int64(size)
}
}
return (total, count)
}
/// Returns a rejection reason if the footprint under `directory` exceeds either ceiling.
func footprintExceeded(at directory: URL, maxBytes: Int64, maxEntries: Int) -> String? {
let footprint = extractionFootprint(at: directory)
if footprint.bytes > maxBytes {
return "decompressed size exceeds \(maxBytes / (1024 * 1024)) MB"
}
if footprint.entries > maxEntries {
return "archive contains more than \(maxEntries) entries"
}
return nil
}
/// Starts a background watchdog that terminates `process` (bsdtar) if the decompressed
/// footprint in `directory` exceeds the byte or entry ceiling.
///
/// This is a SOFT ceiling: bsdtar can write up to one poll interval's worth of data past
/// the limit before it is killed, so peak disk use is roughly `maxBytes + (pollInterval ×
/// disk write rate)`. The goal is to prevent disk *exhaustion* by a multi-GB/TB bomb, not
/// to enforce an exact byte count. The interval is kept short to bound the overshoot.
/// Callers MUST also run `footprintExceeded` once the process exits, to catch a fast bomb
/// that finished within a single poll interval.
func startExtractionWatchdog(on process: Process, directory: URL, maxBytes: Int64, maxEntries: Int) -> ExtractionLimitState {
let state = ExtractionLimitState()
let watchdog = Thread {
while process.isRunning {
if let reason = footprintExceeded(at: directory, maxBytes: maxBytes, maxEntries: maxEntries) {
state.markExceeded(reason)
process.terminate()
break
}
Thread.sleep(forTimeInterval: 0.05)
}
}
watchdog.start()
return state
}
// === END DEVA11Y-484 EXTRACTION GUARD ===
private struct PluginError: Error, CustomStringConvertible {
let message: String
init(_ message: String) {
self.message = message
}
var description: String { message }
}
private func forwardExit(code: Int32, message: String) -> Never {
if !message.isEmpty, let data = (message + "\n").data(using: .utf8) {
FileHandle.standardError.write(data)
}
exit(code)
}