Skip to content

Commit bb24f66

Browse files
authored
Ensure resource bundles aren't marked as executable (#9354)
This patch reverts a single line from a112ab8#diff-b1845cad8f64e37500b06818ff71629726c14cf5a8d34add6ef3a9d38236cfe5 The executable name should be deliberately omitted from the build settings to avoid having the bundle be marked as executable with the CFBundleExecutable in its Info.plist.
1 parent 6405e73 commit bb24f66

File tree

10 files changed

+165
-0
lines changed

10 files changed

+165
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// swift-tools-version:5.3
2+
import PackageDescription
3+
4+
let package = Package(
5+
name: "TIF",
6+
products: [
7+
.library(name: "TIF", targets: ["TIF"])
8+
],
9+
targets: [
10+
.target(
11+
name: "TIF",
12+
resources: [
13+
.copy("some.txt")
14+
]
15+
),
16+
]
17+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# TIF
2+
3+
A description of this package.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//
2+
// SomeAlert.swift
3+
// MyFwk
4+
//
5+
// Created by ankit on 10/31/19.
6+
// Copyright © 2019 Ankit. All rights reserved.
7+
//
8+
9+
import Cocoa
10+
11+
public class SomeAlert: NSViewController {
12+
13+
@IBOutlet var label: NSTextField!
14+
15+
override public func viewDidLoad() {
16+
super.viewDidLoad()
17+
let bundlePath = Bundle.main.path(forResource: "TIF_TIF", ofType: "bundle")!
18+
let bundle = Bundle(path: bundlePath)!
19+
20+
let txt = bundle.path(forResource: "some", ofType: "txt")!
21+
let c = FileManager.default.contents(atPath: txt)!
22+
23+
label.stringValue = String(data:c, encoding: .utf8)!
24+
// Do view setup here.
25+
}
26+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="14850.00" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
3+
<dependencies>
4+
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="14850.00"/>
5+
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
6+
</dependencies>
7+
<objects>
8+
<customObject id="-2" userLabel="File's Owner" customClass="SomeAlert" customModule="MyFwk" customModuleProvider="target">
9+
<connections>
10+
<outlet property="label" destination="AaI-7f-Hna" id="9Qf-KY-ZKZ"/>
11+
<outlet property="view" destination="Hz6-mo-xeY" id="0bl-1N-x8E"/>
12+
</connections>
13+
</customObject>
14+
<customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
15+
<customObject id="-3" userLabel="Application" customClass="NSObject"/>
16+
<customView id="Hz6-mo-xeY">
17+
<rect key="frame" x="0.0" y="0.0" width="800" height="521"/>
18+
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
19+
<subviews>
20+
<imageView horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="PbM-Nr-mVI">
21+
<rect key="frame" x="0.0" y="0.0" width="800" height="521"/>
22+
<imageCell key="cell" refusesFirstResponder="YES" alignment="left" imageScaling="proportionallyDown" image="this_is_fine" id="oAz-HJ-kbb"/>
23+
</imageView>
24+
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="AaI-7f-Hna">
25+
<rect key="frame" x="373" y="10" width="37" height="16"/>
26+
<textFieldCell key="cell" lineBreakMode="clipping" title="Label" id="8Nf-Ba-Sor">
27+
<font key="font" metaFont="system"/>
28+
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
29+
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
30+
</textFieldCell>
31+
</textField>
32+
</subviews>
33+
<constraints>
34+
<constraint firstItem="PbM-Nr-mVI" firstAttribute="centerY" secondItem="Hz6-mo-xeY" secondAttribute="centerY" id="6qc-mk-w7c"/>
35+
<constraint firstItem="PbM-Nr-mVI" firstAttribute="centerX" secondItem="Hz6-mo-xeY" secondAttribute="centerX" id="7oF-4c-nPi"/>
36+
<constraint firstAttribute="trailing" secondItem="PbM-Nr-mVI" secondAttribute="trailing" id="Ixf-fF-M1X"/>
37+
<constraint firstItem="AaI-7f-Hna" firstAttribute="leading" secondItem="Hz6-mo-xeY" secondAttribute="leading" constant="375" id="cSk-iC-InE"/>
38+
<constraint firstItem="AaI-7f-Hna" firstAttribute="top" secondItem="Hz6-mo-xeY" secondAttribute="top" constant="495" id="ikr-Qp-hYt"/>
39+
<constraint firstItem="PbM-Nr-mVI" firstAttribute="top" secondItem="Hz6-mo-xeY" secondAttribute="top" id="pbz-et-fp8"/>
40+
<constraint firstAttribute="bottom" secondItem="AaI-7f-Hna" secondAttribute="bottom" constant="10" id="pwH-7O-7xL"/>
41+
<constraint firstItem="PbM-Nr-mVI" firstAttribute="leading" secondItem="Hz6-mo-xeY" secondAttribute="leading" id="tzP-BG-gDa"/>
42+
</constraints>
43+
<point key="canvasLocation" x="139" y="189.5"/>
44+
</customView>
45+
</objects>
46+
<resources>
47+
<image name="this_is_fine" width="800" height="450"/>
48+
</resources>
49+
</document>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"info" : {
3+
"author" : "xcode",
4+
"version" : 1
5+
}
6+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"images" : [
3+
{
4+
"filename" : "this_is_fine.jpg",
5+
"idiom" : "universal",
6+
"scale" : "1x"
7+
},
8+
{
9+
"idiom" : "universal",
10+
"scale" : "2x"
11+
},
12+
{
13+
"idiom" : "universal",
14+
"scale" : "3x"
15+
}
16+
],
17+
"info" : {
18+
"author" : "xcode",
19+
"version" : 1
20+
}
21+
}
51.8 KB
Loading
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is being read from some.txt file in the bundle.

Sources/SwiftBuildSupport/PackagePIFProjectBuilder.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,9 @@ struct PackagePIFProjectBuilder {
211211
settings[.PRODUCT_MODULE_NAME] = bundleName
212212
settings[.PRODUCT_BUNDLE_IDENTIFIER] = "\(self.package.identity).\(module.name).resources"
213213
.spm_mangledToBundleIdentifier()
214+
// Resource bundles are not executable. Setting the name to an empty string will
215+
// omit the CFBundleExecutable key from the Info.plist.
216+
settings[.EXECUTABLE_NAME] = ""
214217
settings[.GENERATE_INFOPLIST_FILE] = "YES"
215218
settings[.PACKAGE_RESOURCE_TARGET_KIND] = "resource"
216219

Tests/CommandsTests/BuildCommandTests.swift

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1760,6 +1760,45 @@ struct BuildCommandTestCases {
17601760
data.config == .release
17611761
}
17621762
}
1763+
1764+
@Test(
1765+
.requireHostOS(.macOS),
1766+
.tags(
1767+
.Feature.CommandLineArguments.BuildSystem,
1768+
.Feature.CommandLineArguments.Configuration,
1769+
),
1770+
arguments: getBuildData(for: [.swiftbuild]),
1771+
)
1772+
func trivialPackageResources(
1773+
data: BuildData,
1774+
) async throws {
1775+
let buildSystem = data.buildSystem
1776+
try await fixture(name: "Miscellaneous/TIF") { fixturePath in
1777+
let result = try await build(
1778+
[],
1779+
packagePath: fixturePath,
1780+
configuration: data.config,
1781+
cleanAfterward: false,
1782+
buildSystem: buildSystem,
1783+
)
1784+
try #require(result.binContents.contains("TIF_TIF.bundle"))
1785+
let contentsDir = result.binPath.appending("TIF_TIF.bundle", "Contents")
1786+
let bundleResourceDir = contentsDir.appending("Resources")
1787+
1788+
let bundleResources = try localFileSystem.getDirectoryContents(bundleResourceDir)
1789+
#expect(bundleResources.contains("some.txt"))
1790+
#expect(bundleResources.contains("Assets.car"))
1791+
#expect(bundleResources.contains("SomeAlert.nib"))
1792+
1793+
// Check that the Info.plist of the resource bundle looks reasonable. In particular, it shouldn't have a CFBundleExecutable key, since it's a codeless bundle.
1794+
let infoPlistPath = contentsDir.appending("Info.plist")
1795+
let infoPlistBytes = try localFileSystem.readFileContents(infoPlistPath)
1796+
let infoPlist = try infoPlistBytes.withData({
1797+
try #require(PropertyListSerialization.propertyList(from: $0, options: [], format: nil) as? NSDictionary, "couldn't parse built resource bundle's Info.plist as a property list")
1798+
})
1799+
#expect(infoPlist["CFBundleExecutable"] == nil, "Expected CFBundleExecutable to be omitted from the Info.plist of a codeless resource bundle")
1800+
}
1801+
}
17631802
}
17641803

17651804
extension Triple {

0 commit comments

Comments
 (0)