]> Pileus Git - ~andy/freeotp/commitdiff
Improve URI validation
authorAndy Spencer <andy753421@gmail.com>
Wed, 5 Feb 2014 02:11:11 +0000 (02:11 +0000)
committerAndy Spencer <andy753421@gmail.com>
Wed, 5 Feb 2014 02:11:11 +0000 (02:11 +0000)
Invalid URIs currently result in a null pointer exception if scheme is
null. Instead throw the appropriate exception.

src/org/fedorahosted/freeotp/Token.java

index 9acecb4e8a86b2b6c09fb211051db76c218efbf9..43342b1b566f46303282e3cb6376738446b60615 100644 (file)
@@ -54,20 +54,23 @@ public class Token {
        private long mLastCode;
 
        private Token(Uri uri) throws TokenUriInvalidException {
-               if (!uri.getScheme().equals("otpauth"))
+               String scheme = uri.getScheme();
+               String authority = uri.getAuthority();
+               String path = uri.getPath();
+
+               if (scheme == null || authority == null || path == null)
+                       throw new TokenUriInvalidException();
+
+               if (!scheme.equals("otpauth"))
                        throw new TokenUriInvalidException();
 
-               if (uri.getAuthority().equals("totp"))
+               if (authority.equals("totp"))
                        mType = TokenType.TOTP;
-               else if (uri.getAuthority().equals("hotp"))
+               else if (authority.equals("hotp"))
                        mType = TokenType.HOTP;
                else
                        throw new TokenUriInvalidException();
 
-               String path = uri.getPath();
-               if (path == null)
-                       throw new TokenUriInvalidException();
-
                // Strip the path of its leading '/'
                for (int i = 0; path.charAt(i) == '/'; i++)
                        path = path.substring(1);