From: Andy Spencer Date: Wed, 5 Feb 2014 02:11:11 +0000 (+0000) Subject: Improve URI validation X-Git-Url: http://pileus.org/git/?p=~andy%2Ffreeotp;a=commitdiff_plain;h=995d219e668d27056965b8904cda8c743190b203 Improve URI validation Invalid URIs currently result in a null pointer exception if scheme is null. Instead throw the appropriate exception. --- diff --git a/src/org/fedorahosted/freeotp/Token.java b/src/org/fedorahosted/freeotp/Token.java index 9acecb4..43342b1 100644 --- a/src/org/fedorahosted/freeotp/Token.java +++ b/src/org/fedorahosted/freeotp/Token.java @@ -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);