Browse Source

Throw IllegalArgumentExceptions when arguments are bad

This will make the two way data binding crash more, but it will improve
the robustness of the config file parser, which deals with exceptions
gracefully, and when we move to one way data binding, it will help with
that too.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
master
Jason A. Donenfeld 6 years ago
parent
commit
9ee976823d
4 changed files with 35 additions and 37 deletions
  1. +1
    -1
      app/src/main/java/com/wireguard/config/Attribute.java
  2. +7
    -3
      app/src/main/java/com/wireguard/config/IPCidr.java
  3. +15
    -18
      app/src/main/java/com/wireguard/config/Interface.java
  4. +12
    -15
      app/src/main/java/com/wireguard/config/Peer.java

+ 1
- 1
app/src/main/java/com/wireguard/config/Attribute.java View File

@@ -51,7 +51,7 @@ enum Attribute {
} }


public static String[] stringToList(final String string) { public static String[] stringToList(final String string) {
return string.trim().split("\\s*,\\s*", -1);
return string.trim().split("\\s*,\\s*");
} }


public String composeWith(final Object value) { public String composeWith(final Object value) {


+ 7
- 3
app/src/main/java/com/wireguard/config/IPCidr.java View File

@@ -25,11 +25,11 @@ public class IPCidr implements Parcelable {
} }
}; };


public IPCidr(String in) throws UnknownHostException {
public IPCidr(String in) {
parse(in); parse(in);
} }


private void parse(String in) throws UnknownHostException {
private void parse(String in) {
cidr = -1; cidr = -1;
int slash = in.lastIndexOf('/'); int slash = in.lastIndexOf('/');
if (slash != -1 && slash < in.length() - 1) { if (slash != -1 && slash < in.length() - 1) {
@@ -39,7 +39,11 @@ public class IPCidr implements Parcelable {
} catch (Exception e) { } catch (Exception e) {
} }
} }
address = InetAddress.getByName(in);
try {
address = InetAddress.getByName(in);
} catch (UnknownHostException e) {
throw new IllegalArgumentException(e);
}
if ((address instanceof Inet6Address) && (cidr > 128 || cidr < 0)) if ((address instanceof Inet6Address) && (cidr > 128 || cidr < 0))
cidr = 128; cidr = 128;
else if ((address instanceof Inet4Address) && (cidr > 32 || cidr < 0)) else if ((address instanceof Inet4Address) && (cidr > 32 || cidr < 0))


+ 15
- 18
app/src/main/java/com/wireguard/config/Interface.java View File

@@ -135,7 +135,7 @@ public class Interface extends BaseObservable implements Parcelable {
return keypair != null ? keypair.getPublicKey() : null; return keypair != null ? keypair.getPublicKey() : null;
} }


public void parse(final String line) throws UnknownHostException {
public void parse(final String line) {
final Attribute key = Attribute.match(line); final Attribute key = Attribute.match(line);
if (key == Attribute.ADDRESS) if (key == Attribute.ADDRESS)
addAddresses(key.parseList(line)); addAddresses(key.parseList(line));
@@ -151,11 +151,11 @@ public class Interface extends BaseObservable implements Parcelable {
throw new IllegalArgumentException(line); throw new IllegalArgumentException(line);
} }


public void addAddresses(String[] addresses) throws UnknownHostException {
public void addAddresses(String[] addresses) {
if (addresses != null && addresses.length > 0) { if (addresses != null && addresses.length > 0) {
for (final String addr : addresses) { for (final String addr : addresses) {
if (addr.isEmpty()) if (addr.isEmpty())
throw new UnknownHostException("{empty}");
throw new IllegalArgumentException("Address is empty");
this.addressList.add(new IPCidr(addr)); this.addressList.add(new IPCidr(addr));
} }
} }
@@ -166,19 +166,19 @@ public class Interface extends BaseObservable implements Parcelable {


public void setAddressString(final String addressString) { public void setAddressString(final String addressString) {
this.addressList.clear(); this.addressList.clear();
try {
addAddresses(Attribute.stringToList(addressString));
} catch (Exception e) {
this.addressList.clear();
}
addAddresses(Attribute.stringToList(addressString));
} }


public void addDnses(String[] dnses) throws UnknownHostException {
public void addDnses(String[] dnses) {
if (dnses != null && dnses.length > 0) { if (dnses != null && dnses.length > 0) {
for (final String dns : dnses) { for (final String dns : dnses) {
if (dns.isEmpty()) if (dns.isEmpty())
throw new UnknownHostException("{empty}");
this.dnsList.add(InetAddress.getByName(dns));
throw new IllegalArgumentException("DNS is empty");
try {
this.dnsList.add(InetAddress.getByName(dns));
} catch (UnknownHostException e) {
throw new IllegalArgumentException(e);
}
} }
} }
notifyPropertyChanged(BR.dnses); notifyPropertyChanged(BR.dnses);
@@ -187,12 +187,8 @@ public class Interface extends BaseObservable implements Parcelable {
} }


public void setDnsString(final String dnsString) { public void setDnsString(final String dnsString) {
try {
this.dnsList.clear();
addDnses(Attribute.stringToList(dnsString));
} catch (Exception e) {
this.dnsList.clear();
}
this.dnsList.clear();
addDnses(Attribute.stringToList(dnsString));
} }


public void setListenPort(int listenPort) { public void setListenPort(int listenPort) {
@@ -228,8 +224,9 @@ public class Interface extends BaseObservable implements Parcelable {
if (privateKey != null && privateKey.length() == KeyEncoding.KEY_LENGTH_BASE64) { if (privateKey != null && privateKey.length() == KeyEncoding.KEY_LENGTH_BASE64) {
try { try {
keypair = new Keypair(privateKey); keypair = new Keypair(privateKey);
} catch (final IllegalArgumentException ignored) {
} catch (final IllegalArgumentException e) {
keypair = null; keypair = null;
throw e;
} }
} else { } else {
keypair = null; keypair = null;


+ 12
- 15
app/src/main/java/com/wireguard/config/Peer.java View File

@@ -125,7 +125,7 @@ public class Peer extends BaseObservable implements Parcelable {
return publicKey; return publicKey;
} }


public void parse(final String line) throws UnknownHostException {
public void parse(final String line) {
final Attribute key = Attribute.match(line); final Attribute key = Attribute.match(line);
if (key == Attribute.ALLOWED_IPS) if (key == Attribute.ALLOWED_IPS)
addAllowedIPs(key.parseList(line)); addAllowedIPs(key.parseList(line));
@@ -141,11 +141,11 @@ public class Peer extends BaseObservable implements Parcelable {
throw new IllegalArgumentException(line); throw new IllegalArgumentException(line);
} }


public void addAllowedIPs(String[] allowedIPs) throws UnknownHostException {
public void addAllowedIPs(String[] allowedIPs) {
if (allowedIPs != null && allowedIPs.length > 0) { if (allowedIPs != null && allowedIPs.length > 0) {
for (final String allowedIP : allowedIPs) { for (final String allowedIP : allowedIPs) {
if (allowedIP.isEmpty()) if (allowedIP.isEmpty())
throw new UnknownHostException("{empty}");
throw new IllegalArgumentException("AllowedIP is empty");
this.allowedIPsList.add(new IPCidr(allowedIP)); this.allowedIPsList.add(new IPCidr(allowedIP));
} }
} }
@@ -154,12 +154,8 @@ public class Peer extends BaseObservable implements Parcelable {
} }


public void setAllowedIPsString(final String allowedIPsString) { public void setAllowedIPsString(final String allowedIPsString) {
try {
this.allowedIPsList.clear();
addAllowedIPs(Attribute.stringToList(allowedIPsString));
} catch (Exception e) {
this.allowedIPsList.clear();
}
this.allowedIPsList.clear();
addAllowedIPs(Attribute.stringToList(allowedIPsString));
} }


public void setEndpoint(InetSocketAddress endpoint) { public void setEndpoint(InetSocketAddress endpoint) {
@@ -171,14 +167,15 @@ public class Peer extends BaseObservable implements Parcelable {
public void setEndpointString(final String endpoint) { public void setEndpointString(final String endpoint) {
if (endpoint != null && !endpoint.isEmpty()) { if (endpoint != null && !endpoint.isEmpty()) {
InetSocketAddress constructedEndpoint; InetSocketAddress constructedEndpoint;
if (endpoint.indexOf('/') != -1 || endpoint.indexOf('?') != -1 || endpoint.indexOf('#') != -1)
throw new IllegalArgumentException("Forbidden characters in endpoint");
URI uri;
try { try {
if (endpoint.indexOf('/') != -1 || endpoint.indexOf('?') != -1 || endpoint.indexOf('#') != -1)
throw new Exception();
URI uri = new URI("wg://" + endpoint);
constructedEndpoint = InetSocketAddress.createUnresolved(uri.getHost(), uri.getPort());
} catch (Exception e) {
return; /* XXX: Uh oh. */
uri = new URI("wg://" + endpoint);
} catch (URISyntaxException e) {
throw new IllegalArgumentException(e);
} }
constructedEndpoint = InetSocketAddress.createUnresolved(uri.getHost(), uri.getPort());
setEndpoint(constructedEndpoint); setEndpoint(constructedEndpoint);
} else } else
setEndpoint(null); setEndpoint(null);


Loading…
Cancel
Save