-
Notifications
You must be signed in to change notification settings - Fork 171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Catch metadata realm decryption. #4285
Changes from 14 commits
1adf3b4
44fbc49
6052a0a
3db16f2
4d17501
9d0ff37
f389d90
cbcad15
36b7512
841d523
91d9ea5
004c523
774f97c
06cad16
32c77bb
bbfe907
ee5dd7f
8317f78
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ | |
#include <realm/object-store/schema.hpp> | ||
|
||
#include <realm/object-store/impl/object_accessor_impl.hpp> | ||
#include <realm/object-store/impl/realm_coordinator.hpp> | ||
#include "util/test_utils.hpp" | ||
|
||
#include <realm/util/file.hpp> | ||
|
@@ -513,8 +514,38 @@ TEST_CASE("sync_metadata: encryption", "[sync]") { | |
|
||
SECTION("prohibits opening the metadata Realm with different keys") { | ||
SECTION("different keys") { | ||
SyncMetadataManager first_manager(metadata_path, true, make_test_encryption_key(10)); | ||
REQUIRE_THROWS(SyncMetadataManager(metadata_path, true, make_test_encryption_key(11))); | ||
const auto identity0 = "identity0"; | ||
const auto auth_url = "https://realm.example.org"; | ||
|
||
// Open metadata realm, make metadata | ||
std::vector<char> key0 = make_test_encryption_key(10); | ||
SyncMetadataManager manager0(metadata_path, true, key0); | ||
|
||
auto user_metadata = manager0.get_or_make_user_metadata(identity0, auth_url); | ||
REQUIRE(bool(user_metadata)); | ||
CHECK(user_metadata->identity() == identity0); | ||
CHECK(user_metadata->provider_type() == auth_url); | ||
CHECK(user_metadata->access_token().empty()); | ||
CHECK(user_metadata->is_valid()); | ||
|
||
// Close realm | ||
_impl::RealmCoordinator::get_coordinator(metadata_path)->clear_cache(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tgoyne Is clearing the cache here bad? I couldn't figure any other way to successfully close the realm. I understand there are dangers for doing this normally, but would it be suitable for a test context? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The above code needs to be wrapped in another scope. The metadata realm will be closed when everything using it goes out of scope (in this case, just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let me know if |
||
|
||
// Open metadata realm with different key | ||
std::vector<char> key1 = make_test_encryption_key(11); | ||
SyncMetadataManager manager1(metadata_path, true, key1); | ||
|
||
auto user_metadata1 = manager1.get_or_make_user_metadata(identity0, auth_url, false); | ||
// Expect previous metadata to no longer be stored | ||
CHECK_FALSE(bool(user_metadata1)); | ||
|
||
// But new metadata can still be created | ||
const auto identity1 = "identity1"; | ||
auto user_metadata2 = manager1.get_or_make_user_metadata(identity1, auth_url); | ||
CHECK(user_metadata2->identity() == identity1); | ||
CHECK(user_metadata2->provider_type() == auth_url); | ||
CHECK(user_metadata2->access_token().empty()); | ||
CHECK(user_metadata2->is_valid()); | ||
} | ||
SECTION("different encryption settings") { | ||
SyncMetadataManager first_manager(metadata_path, true, make_test_encryption_key(10)); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should check the error code from the exception rather than should_encrypt.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Opening with the wrong key and opening when
should_encrypt
configuration doesn't match the original configuration are bothRealmFileException::Kind::AccessError
and have the same error message. What other ways are there to differentiate?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In both of those cases we want to delete the Realm and recreate it. Currently this code will delete the Realm when any error occurs if we want to encrypt the Realm even if it's an unrelated error, and will fail to delete the Realm in a case where we want to (disabling encryption when the existing file is encrypted).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I understand. I previously was trying to retain the response of throwing an error when
should_encrypt == false
was applied to a path that was encrypted.