From 73215457d5e7cab7e1e92479c57f736f9e369ca6 Mon Sep 17 00:00:00 2001 From: DreamyLynn <59802880+DreamyLynn@users.noreply.github.com> Date: Tue, 25 Jul 2023 22:10:20 +0200 Subject: [PATCH] Update Test Case for empty label Remove null-saftey, as test cases do not like it. Match empty issuer and organization with URL parsing RegEx, add proper error message when manually adding token with ":" in label/organization name. Allow empty issuer and label when manually adding token. --- .../fedorahosted/freeotp/TokenUriInvalidTest.java | 2 +- .../java/org/fedorahosted/freeotp/ManualAdd.java | 8 ++++++++ .../main/java/org/fedorahosted/freeotp/Token.java | 12 +++++++----- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/mobile/src/androidTest/java/org/fedorahosted/freeotp/TokenUriInvalidTest.java b/mobile/src/androidTest/java/org/fedorahosted/freeotp/TokenUriInvalidTest.java index 3ba3b35d..f2a44c1f 100644 --- a/mobile/src/androidTest/java/org/fedorahosted/freeotp/TokenUriInvalidTest.java +++ b/mobile/src/androidTest/java/org/fedorahosted/freeotp/TokenUriInvalidTest.java @@ -24,7 +24,7 @@ public static Collection data() { { "otpauth://totp", Token.InvalidLabelException.class }, { "otpauth://totp/", - Token.InvalidLabelException.class }, + Token.InvalidSecretException.class }, { "otpauth://totp/foo:bar:baz", Token.InvalidLabelException.class }, { "otpauth://totp/bar", diff --git a/mobile/src/main/java/org/fedorahosted/freeotp/ManualAdd.java b/mobile/src/main/java/org/fedorahosted/freeotp/ManualAdd.java index a6bd4d29..88f7e22a 100644 --- a/mobile/src/main/java/org/fedorahosted/freeotp/ManualAdd.java +++ b/mobile/src/main/java/org/fedorahosted/freeotp/ManualAdd.java @@ -17,6 +17,7 @@ import android.widget.TextView; import android.widget.Toast; +import org.apache.commons.codec.binary.StringUtils; import org.fedorahosted.freeotp.main.Activity; public class ManualAdd extends AppCompatActivity { @@ -105,6 +106,8 @@ private Uri makeUri() { private boolean inputValid() { String secret = mSecret.getText().toString(); + String issuer = mIssuer.getText().toString(); + String account = mAccount.getText().toString(); Boolean valid = true; String msg = ""; @@ -113,6 +116,11 @@ private boolean inputValid() { valid = false; } + if(issuer.contains(":") || account.contains(":")) { + msg = "Issuer and account may not contain \":\""; + valid = false; + } + if (!valid) { Toast.makeText(getApplicationContext(), msg,Toast.LENGTH_SHORT).show(); return false; diff --git a/mobile/src/main/java/org/fedorahosted/freeotp/Token.java b/mobile/src/main/java/org/fedorahosted/freeotp/Token.java index bc2e3683..4710d1fe 100644 --- a/mobile/src/main/java/org/fedorahosted/freeotp/Token.java +++ b/mobile/src/main/java/org/fedorahosted/freeotp/Token.java @@ -66,7 +66,7 @@ public static class InvalidColorException extends InvalidUriException {} public enum Type { HOTP, TOTP } private static final String[] SAFE_ALGOS = { "SHA1", "SHA224", "SHA256", "SHA384", "SHA512" }; - private static final Pattern PATTERN = Pattern.compile("^/(?:([^:]+):)?([^:]+)$"); + private static final Pattern PATTERN = Pattern.compile("^/(?:([^:]*):)?([^:]*)$"); @SerializedName("algo") private final String mAlgorithm; @@ -227,11 +227,13 @@ private Token(Uri uri) throws InvalidUriException { try { Matcher matcher = PATTERN.matcher(uri.getPath()); - if (!matcher.find()) + if (!matcher.find()) { throw new InvalidLabelException(); - - mIssuer = matcher.group(1); - mLabel = matcher.group(2); + } + else { + mIssuer = matcher.group(1); + mLabel = matcher.group(2); + } } catch (NullPointerException e) { throw new InvalidLabelException(); }