Skip to content

Commit 73c77e2

Browse files
jonsimantova-maurice
authored andcommitted
Include a hash of the process name in the keychain key on Darwin.
PiperOrigin-RevId: 249124178
1 parent 7c9d7ed commit 73c77e2

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

app/src/secure/user_secure_darwin_internal.mm

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
// limitations under the License.
1414

1515
#include "app/src/secure/user_secure_darwin_internal.h"
16+
#include <functional>
17+
#include "app/src/assert.h"
18+
#include "app/src/base64.h"
1619

1720
#import <Foundation/Foundation.h>
1821
#import <Security/Security.h>
@@ -23,23 +26,40 @@
2326

2427
// Prefix and suffix to add to keychain service name.
2528
static const char kServicePrefix[] = "";
29+
static const char kServiceSeparator[] = ".";
2630
static const char kServiceSuffix1[] = ".firebase.";
2731
static const char kServiceSuffix2[] = "";
28-
// For example: com.my_company.my_app.firebase.auth, com.my_company.my_app.firebase.iid
32+
// For example:
33+
// com.my_company.my_app.firebase_project_id.process_name_hash.firebase.auth
34+
// com.my_company.my_app.firebase_project_id.process_hash.firebase.iid
2935

3036
static const int kMaxAllowedKeychainEntries = INT_MAX;
3137

3238
// Prefix and suffix for the key for NSUserDefaults. domain and service are inserted in the middle.
3339
static const char kUserDefaultsPrefix[] = "com.google.firebase.";
40+
static const char kUserDefaultsSeparator[] = ".";
3441
static const char kUserDefaultsSuffix[] = ".has_secure_data";
35-
// For example: com.google.firebase.auth.com.my_company.my_app.has_secure_data
42+
// For example:
43+
// com.google.firebase.com.my_company.my_app.firebase_project_id.process_hash.auth.has_secure_data
44+
45+
static std::string GetProcessId() {
46+
std::string process_name = [[NSProcessInfo processInfo] processName].UTF8String;
47+
size_t hash = std::hash<std::string>()(process_name);
48+
std::string process_name_hash_binary(reinterpret_cast<const char*>(&hash), sizeof(hash));
49+
std::string output;
50+
bool hash_encode_success = internal::Base64Encode(process_name_hash_binary, &output);
51+
FIREBASE_ASSERT(hash_encode_success);
52+
return output;
53+
}
3654

3755
UserSecureDarwinInternal::UserSecureDarwinInternal(const char* domain, const char* service)
3856
: domain_(domain) {
39-
service_ = std::string(kServicePrefix) + service + std::string(kServiceSuffix1) + domain +
40-
std::string(kServiceSuffix2);
41-
user_defaults_key_ =
42-
std::string(kUserDefaultsPrefix) + service + "." + domain + std::string(kUserDefaultsSuffix);
57+
std::string process_id = GetProcessId();
58+
service_ = std::string(kServicePrefix) + service + std::string(kServiceSeparator) + process_id +
59+
std::string(kServiceSuffix1) + domain + std::string(kServiceSuffix2);
60+
user_defaults_key_ = std::string(kUserDefaultsPrefix) + service + kUserDefaultsSeparator +
61+
process_id + kUserDefaultsSeparator + domain +
62+
std::string(kUserDefaultsSuffix);
4363
}
4464

4565
UserSecureDarwinInternal::~UserSecureDarwinInternal() {}

0 commit comments

Comments
 (0)