Skip to content

Commit 07c60c5

Browse files
authored
When searching for gRPC certificates, search the main bundle as well (#2183)
When the project is manually configured, it's possible that the certificates file gets added to the main bundle, not the Firestore framework bundle; make sure the bundle can be loaded in that case as well.
1 parent 9f6ed13 commit 07c60c5

File tree

1 file changed

+39
-18
lines changed

1 file changed

+39
-18
lines changed

Firestore/core/src/firebase/firestore/remote/grpc_root_certificate_finder_apple.mm

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,28 +34,49 @@
3434
using util::StatusOr;
3535
using util::StringFormat;
3636

37-
std::string LoadGrpcRootCertificate() {
38-
// Try to load certificates bundled by gRPC-C++ if available (depends on
39-
// gRPC-C++ version).
40-
// Note that `mainBundle` may be nil in certain cases (e.g., unit tests).
41-
NSBundle* bundle = [NSBundle bundleWithIdentifier:@"org.cocoapods.grpcpp"];
42-
NSString* path;
43-
if (bundle) {
44-
path =
45-
[bundle pathForResource:@"gRPCCertificates.bundle/roots" ofType:@"pem"];
46-
}
47-
if (path) {
48-
LOG_DEBUG("Using roots.pem file from gRPC-C++ pod");
49-
} else {
37+
NSString* FindPathToCertificatesFile() {
38+
// Certificates file might be present in one of several bundles, based on
39+
// the environment.
40+
NSArray<NSBundle*>* bundles = @[
41+
// First, try to load certificates bundled by gRPC-C++ if available
42+
// (pod versions 0.0.6+).
43+
[NSBundle bundleWithIdentifier:@"org.cocoapods.grpcpp"],
5044
// Fall back to the certificates bundled with Firestore if necessary.
51-
LOG_DEBUG("Using roots.pem file from Firestore pod");
45+
[NSBundle bundleForClass:FSTFirestoreClient.class],
46+
// Finally, users manually adding resources to the project may add the
47+
// certificate to the main application bundle. Note that `mainBundle` is nil
48+
// for unit tests of library projects, so it cannot fully substitute for
49+
// checking framework bundles.
50+
[NSBundle mainBundle],
51+
];
52+
53+
for (NSBundle* bundle in bundles) {
54+
if (!bundle) {
55+
continue;
56+
}
57+
58+
NSString* resource = @"gRPCCertificates.bundle/roots";
59+
NSString* path = [bundle pathForResource:resource ofType:@"pem"];
60+
if (!path) {
61+
resource = @"gRPCCertificates-Firestore.bundle/roots";
62+
path = [bundle pathForResource:resource ofType:@"pem"];
63+
}
5264

53-
bundle = [NSBundle bundleForClass:FSTFirestoreClient.class];
54-
HARD_ASSERT(bundle, "Could not find Firestore bundle");
55-
path = [bundle pathForResource:@"gRPCCertificates-Firestore.bundle/roots"
56-
ofType:@"pem"];
65+
if (path) {
66+
LOG_DEBUG("%s.pem found in bundle %s", resource,
67+
[bundle bundleIdentifier]);
68+
return path;
69+
} else {
70+
LOG_DEBUG("%s.pem not found in bundle %s", resource,
71+
[bundle bundleIdentifier]);
72+
}
5773
}
5874

75+
return nil;
76+
}
77+
78+
std::string LoadGrpcRootCertificate() {
79+
NSString* path = FindPathToCertificatesFile();
5980
HARD_ASSERT(
6081
path,
6182
"Could not load root certificates from the bundle. SSL cannot work.");

0 commit comments

Comments
 (0)