Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 023ea406 authored by cketti's avatar cketti Committed by GitHub
Browse files

Merge pull request #8305 from cketti/fix_missing_email_address

Don't allow identities without a syntactically valid email address
parents fcfab277 9cdb889a
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -19,7 +19,7 @@ import timber.log.Timber;


public class K9StoragePersister implements StoragePersister {
    private static final int DB_VERSION = 25;
    private static final int DB_VERSION = 26;
    private static final String DB_NAME = "preferences_storage";

    private final Context context;
+48 −0
Original line number Diff line number Diff line
package com.fsck.k9.preferences.migration

import android.database.sqlite.SQLiteDatabase
import com.fsck.k9.EmailAddressValidator

/**
 * Make sure identities are using a syntactically valid email address.
 *
 * Previously, we didn't validate input in the "Composition defaults" screen, and so there was no guarantee that the
 * `email` values contained a valid email address.
 */
class StorageMigrationTo26(
    private val db: SQLiteDatabase,
    private val migrationsHelper: StorageMigrationHelper,
) {
    private val emailAddressValidator = EmailAddressValidator()

    fun fixIdentities() {
        val accountUuidsListValue = migrationsHelper.readValue(db, "accountUuids")
        if (accountUuidsListValue.isNullOrEmpty()) {
            return
        }

        val accountUuids = accountUuidsListValue.split(",")
        for (accountUuid in accountUuids) {
            fixIdentitiesInAccount(accountUuid)
        }
    }

    private fun fixIdentitiesInAccount(accountUuid: String) {
        var identityIndex = 0

        while (true) {
            val email = migrationsHelper.readValue(db, "$accountUuid.email.$identityIndex") ?: break

            val trimmedEmail = email.trim()
            val newEmail = if (emailAddressValidator.isValidAddressOnly(trimmedEmail)) {
                trimmedEmail
            } else {
                "please.edit@invalid"
            }

            migrationsHelper.writeValue(db, "$accountUuid.email.$identityIndex", newEmail)

            identityIndex++
        }
    }
}
+1 −0
Original line number Diff line number Diff line
@@ -32,5 +32,6 @@ internal object StorageMigrations {
        if (oldVersion < 23) StorageMigrationTo23(db, migrationsHelper).renameSendClientId()
        if (oldVersion < 24) StorageMigrationTo24(db, migrationsHelper).removeLegacyAuthenticationModes()
        if (oldVersion < 25) StorageMigrationTo25(db, migrationsHelper).convertToAuthTypeNone()
        if (oldVersion < 26) StorageMigrationTo26(db, migrationsHelper).fixIdentities()
    }
}
+82 −0
Original line number Diff line number Diff line
package com.fsck.k9.preferences.migration

import assertk.all
import assertk.assertThat
import assertk.assertions.isEqualTo
import assertk.assertions.key
import com.fsck.k9.preferences.createPreferencesDatabase
import java.util.UUID
import kotlin.test.Test
import org.junit.After
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner

@RunWith(RobolectricTestRunner::class)
class StorageMigrationTo26Test {
    private val database = createPreferencesDatabase()
    private val migrationHelper = DefaultStorageMigrationHelper()
    private val migration = StorageMigrationTo26(database, migrationHelper)

    @After
    fun tearDown() {
        database.close()
    }

    @Test
    fun `valid email addresses should not be changed`() {
        val accountOne = createAccount("email.0" to "one@domain.example")
        val accountTwo = createAccount("email.0" to "two@domain.example")
        writeAccountUuids(accountOne, accountTwo)

        migration.fixIdentities()

        assertThat(migrationHelper.readAllValues(database)).all {
            key("$accountOne.email.0").isEqualTo("one@domain.example")
            key("$accountTwo.email.0").isEqualTo("two@domain.example")
        }
    }

    @Test
    fun `valid email addresses with additional spaces should be trimmed`() {
        val account = createAccount(
            "email.0" to " valid@domain.example ",
        )
        writeAccountUuids(account)

        migration.fixIdentities()

        assertThat(migrationHelper.readAllValues(database))
            .key("$account.email.0").isEqualTo("valid@domain.example")
    }

    @Test
    fun `invalid email addresses should be replaced`() {
        val account = createAccount(
            "email.0" to "",
            "email.1" to "invalid",
        )
        writeAccountUuids(account)

        migration.fixIdentities()

        assertThat(migrationHelper.readAllValues(database)).all {
            key("$account.email.0").isEqualTo("please.edit@invalid")
            key("$account.email.1").isEqualTo("please.edit@invalid")
        }
    }

    private fun writeAccountUuids(vararg accounts: String) {
        val accountUuids = accounts.joinToString(separator = ",")
        migrationHelper.insertValue(database, "accountUuids", accountUuids)
    }

    private fun createAccount(vararg pairs: Pair<String, String>): String {
        val accountUuid = UUID.randomUUID().toString()

        for ((key, value) in pairs) {
            migrationHelper.insertValue(database, "$accountUuid.$key", value)
        }

        return accountUuid
    }
}
+0 −149
Original line number Diff line number Diff line
package com.fsck.k9.activity.setup;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import app.k9mail.legacy.account.Account;
import com.fsck.k9.Preferences;
import com.fsck.k9.ui.R;
import com.fsck.k9.ui.base.K9Activity;
import com.google.android.material.checkbox.MaterialCheckBox;
import com.google.android.material.radiobutton.MaterialRadioButton;


public class AccountSetupComposition extends K9Activity {

    private static final String EXTRA_ACCOUNT = "account";

    private Account mAccount;

    private EditText mAccountSignature;
    private EditText mAccountEmail;
    private EditText mAccountAlwaysBcc;
    private EditText mAccountName;
    private MaterialCheckBox mAccountSignatureUse;
    private MaterialRadioButton mAccountSignatureBeforeLocation;
    private MaterialRadioButton mAccountSignatureAfterLocation;
    private LinearLayout mAccountSignatureLayout;


    public static void actionEditCompositionSettings(Activity context, String accountUuid) {
        Intent intent = new Intent(context, AccountSetupComposition.class);
        intent.setAction(Intent.ACTION_EDIT);
        intent.putExtra(EXTRA_ACCOUNT, accountUuid);
        context.startActivity(intent);
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String accountUuid = getIntent().getStringExtra(EXTRA_ACCOUNT);
        mAccount = Preferences.getPreferences().getAccount(accountUuid);

        setLayout(R.layout.account_setup_composition);
        setTitle(R.string.account_settings_composition_title);

        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }

        /*
         * If we're being reloaded we override the original account with the one
         * we saved
         */
        if (savedInstanceState != null && savedInstanceState.containsKey(EXTRA_ACCOUNT)) {
            accountUuid = savedInstanceState.getString(EXTRA_ACCOUNT);
            mAccount = Preferences.getPreferences().getAccount(accountUuid);
        }

        mAccountName = findViewById(R.id.account_name);
        mAccountName.setText(mAccount.getSenderName());

        mAccountEmail = findViewById(R.id.account_email);
        mAccountEmail.setText(mAccount.getEmail());

        mAccountAlwaysBcc = findViewById(R.id.account_always_bcc);
        mAccountAlwaysBcc.setText(mAccount.getAlwaysBcc());

        mAccountSignatureLayout = findViewById(R.id.account_signature_layout);

        mAccountSignatureUse = findViewById(R.id.account_signature_use);
        boolean useSignature = mAccount.getSignatureUse();
        mAccountSignatureUse.setChecked(useSignature);
        mAccountSignatureUse.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    mAccountSignatureLayout.setVisibility(View.VISIBLE);
                    mAccountSignature.setText(mAccount.getSignature());
                    boolean isSignatureBeforeQuotedText = mAccount.isSignatureBeforeQuotedText();
                    mAccountSignatureBeforeLocation.setChecked(isSignatureBeforeQuotedText);
                    mAccountSignatureAfterLocation.setChecked(!isSignatureBeforeQuotedText);
                } else {
                    mAccountSignatureLayout.setVisibility(View.GONE);
                }
            }
        });

        mAccountSignature = findViewById(R.id.account_signature);

        mAccountSignatureBeforeLocation = findViewById(R.id.account_signature_location_before_quoted_text);
        mAccountSignatureAfterLocation = findViewById(R.id.account_signature_location_after_quoted_text);

        if (useSignature) {
            mAccountSignature.setText(mAccount.getSignature());

            boolean isSignatureBeforeQuotedText = mAccount.isSignatureBeforeQuotedText();
            mAccountSignatureBeforeLocation.setChecked(isSignatureBeforeQuotedText);
            mAccountSignatureAfterLocation.setChecked(!isSignatureBeforeQuotedText);
        } else {
            mAccountSignatureLayout.setVisibility(View.GONE);
        }
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private void saveSettings() {
        mAccount.setEmail(mAccountEmail.getText().toString());
        mAccount.setAlwaysBcc(mAccountAlwaysBcc.getText().toString());
        mAccount.setSenderName(mAccountName.getText().toString());
        mAccount.setSignatureUse(mAccountSignatureUse.isChecked());
        if (mAccountSignatureUse.isChecked()) {
            mAccount.setSignature(mAccountSignature.getText().toString());
            boolean isSignatureBeforeQuotedText = mAccountSignatureBeforeLocation.isChecked();
            mAccount.setSignatureBeforeQuotedText(isSignatureBeforeQuotedText);
        }

        Preferences.getPreferences().saveAccount(mAccount);
    }

    @Override
    public void onStop() {
        // TODO: Instead of saving the changes when the activity is stopped, add buttons to explicitly save or discard
        //  changes.
        saveSettings();
        super.onStop();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putSerializable(EXTRA_ACCOUNT, mAccount.getUuid());
    }
}
Loading