Skip to content

Commit 927e685

Browse files
authored
Merge pull request #4961 from parkera/package
Merge main into package
2 parents 6f4c134 + 2c5b4e2 commit 927e685

File tree

6 files changed

+74
-20
lines changed

6 files changed

+74
-20
lines changed

Docs/GettingStarted.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ swift-corelibs-foundation swift-corelibs-xctest
2424

2525
Build and test steps:
2626

27-
0. Run Xcode with the latest toolchain. Follow [the instructions here](https://swift.org/download/#installation) to start Xcode with the correct toolchain.
27+
0. Run Xcode with the latest toolchain. Follow [the instructions here](https://www.swift.org/install/macos/#installation-via-swiftorg-package-installer) to start Xcode with the correct toolchain.
2828
0. Open `Foundation.xcworkspace`.
2929
0. Build the _SwiftFoundation_ target. This builds CoreFoundation and Foundation.
3030
0. Run (Cmd-R) the _TestFoundation_ target. This builds CoreFoundation, Foundation, XCTest, and TestFoundation, then runs the tests.

Sources/CoreFoundation/CFXMLPreferencesDomain.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -273,18 +273,17 @@ static Boolean __CFWriteBytesToFileWithAtomicity(CFURLRef url, const void *bytes
273273
close(fd);
274274

275275
if (atomic) {
276+
// If the file was renamed successfully and we wrote it as root we need to reset the owner & group as they were.
277+
if (writingFileAsRoot) {
278+
chown(auxPath, owner, group);
279+
}
276280
// preserve the mode as passed in originally
277281
chmod(auxPath, mode);
278282

279283
if (0 != rename(auxPath, cpath)) {
280284
unlink(auxPath);
281285
return false;
282286
}
283-
284-
// If the file was renamed successfully and we wrote it as root we need to reset the owner & group as they were.
285-
if (writingFileAsRoot) {
286-
chown(cpath, owner, group);
287-
}
288287
}
289288
return true;
290289
}

Sources/Foundation/NSLock.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ private typealias _RecursiveMutexPointer = UnsafeMutablePointer<pthread_mutex_t>
4949
private typealias _ConditionVariablePointer = UnsafeMutablePointer<pthread_cond_t>
5050
#endif
5151

52-
open class NSLock: NSObject, NSLocking {
52+
open class NSLock: NSObject, NSLocking, @unchecked Sendable {
5353
internal var mutex = _MutexPointer.allocate(capacity: 1)
5454
#if os(macOS) || os(iOS) || os(Windows)
5555
private var timeoutCond = _ConditionVariablePointer.allocate(capacity: 1)
@@ -165,7 +165,7 @@ extension NSLock {
165165
}
166166

167167
#if SWIFT_CORELIBS_FOUNDATION_HAS_THREADS
168-
open class NSConditionLock : NSObject, NSLocking {
168+
open class NSConditionLock : NSObject, NSLocking, @unchecked Sendable {
169169
internal var _cond = NSCondition()
170170
internal var _value: Int
171171
internal var _thread: _swift_CFThreadRef?
@@ -259,7 +259,7 @@ open class NSConditionLock : NSObject, NSLocking {
259259
}
260260
#endif
261261

262-
open class NSRecursiveLock: NSObject, NSLocking {
262+
open class NSRecursiveLock: NSObject, NSLocking, @unchecked Sendable {
263263
internal var mutex = _RecursiveMutexPointer.allocate(capacity: 1)
264264
#if os(macOS) || os(iOS) || os(Windows)
265265
private var timeoutCond = _ConditionVariablePointer.allocate(capacity: 1)
@@ -381,7 +381,7 @@ open class NSRecursiveLock: NSObject, NSLocking {
381381
open var name: String?
382382
}
383383

384-
open class NSCondition: NSObject, NSLocking {
384+
open class NSCondition: NSObject, NSLocking, @unchecked Sendable {
385385
internal var mutex = _MutexPointer.allocate(capacity: 1)
386386
internal var cond = _ConditionVariablePointer.allocate(capacity: 1)
387387

Sources/FoundationNetworking/URLSession/libcurl/EasyHandle.swift

Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -182,17 +182,17 @@ extension _EasyHandle {
182182
_config = config
183183
}
184184

185-
/// Set allowed protocols
185+
/// Set the CA bundle path automatically if it isn't set
186186
///
187-
/// - Note: This has security implications. Not limiting this, someone could
188-
/// redirect a HTTP request into one of the many other protocols that libcurl
189-
/// supports.
190-
/// - SeeAlso: https://curl.haxx.se/libcurl/c/CURLOPT_PROTOCOLS.html
191-
/// - SeeAlso: https://curl.haxx.se/libcurl/c/CURLOPT_REDIR_PROTOCOLS.html
192-
func setAllowedProtocolsToHTTPAndHTTPS() {
193-
let protocols = (CFURLSessionProtocolHTTP | CFURLSessionProtocolHTTPS)
194-
try! CFURLSession_easy_setopt_long(rawHandle, CFURLSessionOptionPROTOCOLS, protocols).asError()
195-
try! CFURLSession_easy_setopt_long(rawHandle, CFURLSessionOptionREDIR_PROTOCOLS, protocols).asError()
187+
/// Curl does not necessarily know where to find the CA root bundle,
188+
/// and in that case we need to specify where it is. There was a hack
189+
/// to do this automatically for Android but allowing an environment
190+
/// variable to control the location of the CA root bundle seems like
191+
/// a security issue in general.
192+
///
193+
/// Rather than doing that, we have a list of places we might expect
194+
/// to find it, and search those until we locate a suitable file.
195+
func setCARootBundlePath() {
196196
#if os(Android)
197197
// See https://curl.haxx.se/docs/sslcerts.html
198198
// For SSL on Android you need a "cacert.pem" to be
@@ -205,8 +205,58 @@ extension _EasyHandle {
205205
else {
206206
try! CFURLSession_easy_setopt_ptr(rawHandle, CFURLSessionOptionCAINFO, caInfo).asError()
207207
}
208+
return
208209
}
209210
#endif
211+
212+
#if !NS_CURL_MISSING_CURLINFO_CAINFO
213+
#if !os(Windows) && !os(macOS) && !os(iOS) && !os(watchOS) && !os(tvOS)
214+
// Check if there is a default path; if there is, it will already
215+
// be set, so leave things alone
216+
var p: UnsafeMutablePointer<Int8>? = nil
217+
218+
try! CFURLSession_easy_getinfo_charp(rawHandle, CFURLSessionInfoCAINFO, &p).asError()
219+
220+
if p != nil {
221+
return
222+
}
223+
224+
// Otherwise, search a list of known paths
225+
let paths = [
226+
"/etc/ssl/certs/ca-certificates.crt",
227+
"/etc/pki/tls/certs/ca-bundle.crt",
228+
"/usr/share/ssl/certs/ca-bundle.crt",
229+
"/usr/local/share/certs/ca-root-nss.crt",
230+
"/etc/ssl/cert.pem"
231+
]
232+
233+
for path in paths {
234+
var isDirectory: ObjCBool = false
235+
if FileManager.default.fileExists(atPath: path,
236+
isDirectory: &isDirectory)
237+
&& !isDirectory.boolValue {
238+
path.withCString { pathPtr in
239+
try! CFURLSession_easy_setopt_ptr(rawHandle, CFURLSessionOptionCAINFO, UnsafeMutablePointer(mutating: pathPtr)).asError()
240+
}
241+
return
242+
}
243+
}
244+
#endif // !os(Windows) && !os(macOS) && !os(iOS) && !os(watchOS) && !os(tvOS)
245+
#endif // !NS_CURL_MISSING_CURLINFO_CAINFO
246+
}
247+
248+
/// Set allowed protocols
249+
///
250+
/// - Note: This has security implications. Not limiting this, someone could
251+
/// redirect a HTTP request into one of the many other protocols that libcurl
252+
/// supports.
253+
/// - SeeAlso: https://curl.haxx.se/libcurl/c/CURLOPT_PROTOCOLS.html
254+
/// - SeeAlso: https://curl.haxx.se/libcurl/c/CURLOPT_REDIR_PROTOCOLS.html
255+
func setAllowedProtocolsToHTTPAndHTTPS() {
256+
let protocols = (CFURLSessionProtocolHTTP | CFURLSessionProtocolHTTPS)
257+
try! CFURLSession_easy_setopt_long(rawHandle, CFURLSessionOptionPROTOCOLS, protocols).asError()
258+
try! CFURLSession_easy_setopt_long(rawHandle, CFURLSessionOptionREDIR_PROTOCOLS, protocols).asError()
259+
setCARootBundlePath()
210260
//TODO: Added in libcurl 7.45.0
211261
//TODO: Set default protocol for schemeless URLs
212262
//CURLOPT_DEFAULT_PROTOCOL available only in libcurl 7.45.0
@@ -217,6 +267,7 @@ extension _EasyHandle {
217267
let redirectProtocols = (CFURLSessionProtocolHTTP | CFURLSessionProtocolHTTPS)
218268
try! CFURLSession_easy_setopt_long(rawHandle, CFURLSessionOptionPROTOCOLS, protocols).asError()
219269
try! CFURLSession_easy_setopt_long(rawHandle, CFURLSessionOptionREDIR_PROTOCOLS, redirectProtocols).asError()
270+
setCARootBundlePath()
220271
}
221272

222273
//TODO: Proxy setting, namely CFURLSessionOptionPROXY, CFURLSessionOptionPROXYPORT,

Sources/_CFURLSessionInterface/CFURLSessionInterface.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,9 @@ CFURLSessionInfo const CFURLSessionInfoFTP_ENTRY_PATH = { CURLINFO_FTP_ENTRY_PAT
586586
CFURLSessionInfo const CFURLSessionInfoREDIRECT_URL = { CURLINFO_REDIRECT_URL };
587587
CFURLSessionInfo const CFURLSessionInfoPRIMARY_IP = { CURLINFO_PRIMARY_IP };
588588
CFURLSessionInfo const CFURLSessionInfoAPPCONNECT_TIME = { CURLINFO_APPCONNECT_TIME };
589+
#if !NS_CURL_MISSING_CURLINFO_CAINFO
590+
CFURLSessionInfo const CFURLSessionInfoCAINFO = { CURLINFO_CAINFO };
591+
#endif
589592
CFURLSessionInfo const CFURLSessionInfoCERTINFO = { CURLINFO_CERTINFO };
590593
CFURLSessionInfo const CFURLSessionInfoCONDITION_UNMET = { CURLINFO_CONDITION_UNMET };
591594
CFURLSessionInfo const CFURLSessionInfoRTSP_SESSION_ID = { CURLINFO_RTSP_SESSION_ID };

Sources/_CFURLSessionInterface/include/CFURLSessionInterface.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,7 @@ CF_EXPORT CFURLSessionInfo const CFURLSessionInfoFTP_ENTRY_PATH; // CURLINFO_FTP
446446
CF_EXPORT CFURLSessionInfo const CFURLSessionInfoREDIRECT_URL; // CURLINFO_REDIRECT_URL
447447
CF_EXPORT CFURLSessionInfo const CFURLSessionInfoPRIMARY_IP; // CURLINFO_PRIMARY_IP
448448
CF_EXPORT CFURLSessionInfo const CFURLSessionInfoAPPCONNECT_TIME; // CURLINFO_APPCONNECT_TIME
449+
CF_EXPORT CFURLSessionInfo const CFURLSessionInfoCAINFO; // CURLINFO_CAINFO
449450
CF_EXPORT CFURLSessionInfo const CFURLSessionInfoCERTINFO; // CURLINFO_CERTINFO
450451
CF_EXPORT CFURLSessionInfo const CFURLSessionInfoCONDITION_UNMET; // CURLINFO_CONDITION_UNMET
451452
CF_EXPORT CFURLSessionInfo const CFURLSessionInfoRTSP_SESSION_ID; // CURLINFO_RTSP_SESSION_ID

0 commit comments

Comments
 (0)