Skip to content
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

🐞Bug with encryption #58

Open
Fi5t opened this issue Oct 24, 2019 · 3 comments
Open

🐞Bug with encryption #58

Fi5t opened this issue Oct 24, 2019 · 3 comments

Comments

@Fi5t
Copy link
Contributor

Fi5t commented Oct 24, 2019

I faced a strange bug when tried to work with two instances of this shared preferences. I use Tink as an encryption engine with your library and want to show you some unexcepted behavior when they work together.

P.S. Sorry for the Kotlin :D

        try {
            TinkConfig.register()
        } catch (e: GeneralSecurityException) {
            throw RuntimeException(e)
        }

        val keysetHandle = AndroidKeysetManager.Builder()
            .withSharedPref(this, KEYSET_NAME, PREFERENCE_FILE)
            .withKeyTemplate(AeadKeyTemplates.AES256_GCM)
            .withMasterKeyUri(MASTER_KEY_URI)
            .build()
            .keysetHandle

        val aead = AeadFactory.getPrimitive(keysetHandle)

        val bpInstance1 = BinaryPreferencesBuilder(this)
            .name("main_file")
            .valueEncryption(TinkValueEncryption(aead, "good_data".toByteArray()))
            .build()

        val bpInstance2 = BinaryPreferencesBuilder(this)
            .name("main_file")
            .valueEncryption(TinkValueEncryption(aead, "bad_data".toByteArray()))
            .build()

        bpInstance1.edit {
            putString("my_key", "bug")
        }

        val result = bpInstance2.getString("my_key", "no bug")

I expect to see "GeneralSecurityException" or "no_bug" in the result variable, but I see "bug" there. It's wrong behavior, because I set the different associated data for each instance. Any ideas?

I've prepared a repository to reproduce this bug. Please, check it out and tell me where I was wrong =)

@iamironz
Copy link
Contributor

Hey. Thank you for submitting the bug.
This behavior is expected.If you look at the code listing here https://github.com/yandextaxitech/binaryprefs/blob/master/library/src/main/java/com/ironz/binaryprefs/ParametersProvider.java#L17 then you'll see static caching. It's more efficient than using separate cache instances per preference instance.
Why do you think this is bug?

@Fi5t
Copy link
Contributor Author

Fi5t commented Oct 29, 2019

When the second instance can decrypt data of the first instance despite the fact that both of these instances have different additional data is a bug for me.

@iamironz
Copy link
Contributor

Got it. Lets discuss about in DM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants