Config: make parsing stricter
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
parent
14a7ada6e1
commit
e421b997cd
@ -369,6 +369,11 @@ public class VpnService extends Service
|
|||||||
config.setName(configName);
|
config.setName(configName);
|
||||||
configs.add(config);
|
configs.add(config);
|
||||||
} catch (IllegalArgumentException | IOException e) {
|
} catch (IllegalArgumentException | IOException e) {
|
||||||
|
try {
|
||||||
|
file.delete();
|
||||||
|
} catch (Exception e2) {
|
||||||
|
Log.w(TAG, "Could not remove " + fileName, e2);
|
||||||
|
}
|
||||||
Log.w(TAG, "Failed to load config from " + fileName, e);
|
Log.w(TAG, "Failed to load config from " + fileName, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,7 @@ enum Attribute {
|
|||||||
LISTEN_PORT("ListenPort"),
|
LISTEN_PORT("ListenPort"),
|
||||||
MTU("MTU"),
|
MTU("MTU"),
|
||||||
PERSISTENT_KEEPALIVE("PersistentKeepalive"),
|
PERSISTENT_KEEPALIVE("PersistentKeepalive"),
|
||||||
PRE_SHARED_KEY("PresharedKey"),
|
PRESHARED_KEY("PresharedKey"),
|
||||||
PRIVATE_KEY("PrivateKey"),
|
PRIVATE_KEY("PrivateKey"),
|
||||||
PUBLIC_KEY("PublicKey");
|
PUBLIC_KEY("PublicKey");
|
||||||
|
|
||||||
|
@ -139,19 +139,27 @@ public class Config extends BaseObservable
|
|||||||
new InputStreamReader(stream, StandardCharsets.UTF_8))) {
|
new InputStreamReader(stream, StandardCharsets.UTF_8))) {
|
||||||
Peer currentPeer = null;
|
Peer currentPeer = null;
|
||||||
String line;
|
String line;
|
||||||
|
boolean inInterfaceSection = false;
|
||||||
while ((line = reader.readLine()) != null) {
|
while ((line = reader.readLine()) != null) {
|
||||||
if (line.isEmpty())
|
if (line.isEmpty() || line.startsWith("#"))
|
||||||
continue;
|
continue;
|
||||||
if ("[Interface]".equals(line)) {
|
if ("[Interface]".equals(line)) {
|
||||||
currentPeer = null;
|
currentPeer = null;
|
||||||
|
inInterfaceSection = true;
|
||||||
} else if ("[Peer]".equals(line)) {
|
} else if ("[Peer]".equals(line)) {
|
||||||
currentPeer = addPeer();
|
currentPeer = addPeer();
|
||||||
} else if (currentPeer == null) {
|
inInterfaceSection = false;
|
||||||
|
} else if (inInterfaceSection) {
|
||||||
iface.parse(line);
|
iface.parse(line);
|
||||||
} else {
|
} else if (currentPeer != null) {
|
||||||
currentPeer.parse(line);
|
currentPeer.parse(line);
|
||||||
|
} else {
|
||||||
|
throw new IllegalArgumentException("Invalid configuration line: " + line);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (!inInterfaceSection && currentPeer == null) {
|
||||||
|
throw new IllegalArgumentException("Did not find any config information");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -104,7 +104,7 @@ public class Peer extends BaseObservable implements Copyable<Peer>, Observable,
|
|||||||
setEndpoint(key.parseFrom(line));
|
setEndpoint(key.parseFrom(line));
|
||||||
else if (key == Attribute.PERSISTENT_KEEPALIVE)
|
else if (key == Attribute.PERSISTENT_KEEPALIVE)
|
||||||
setPersistentKeepalive(key.parseFrom(line));
|
setPersistentKeepalive(key.parseFrom(line));
|
||||||
else if (key == Attribute.PRE_SHARED_KEY)
|
else if (key == Attribute.PRESHARED_KEY)
|
||||||
setPreSharedKey(key.parseFrom(line));
|
setPreSharedKey(key.parseFrom(line));
|
||||||
else if (key == Attribute.PUBLIC_KEY)
|
else if (key == Attribute.PUBLIC_KEY)
|
||||||
setPublicKey(key.parseFrom(line));
|
setPublicKey(key.parseFrom(line));
|
||||||
@ -161,7 +161,7 @@ public class Peer extends BaseObservable implements Copyable<Peer>, Observable,
|
|||||||
if (persistentKeepalive != null)
|
if (persistentKeepalive != null)
|
||||||
sb.append(Attribute.PERSISTENT_KEEPALIVE.composeWith(persistentKeepalive));
|
sb.append(Attribute.PERSISTENT_KEEPALIVE.composeWith(persistentKeepalive));
|
||||||
if (preSharedKey != null)
|
if (preSharedKey != null)
|
||||||
sb.append(Attribute.PRE_SHARED_KEY.composeWith(preSharedKey));
|
sb.append(Attribute.PRESHARED_KEY.composeWith(preSharedKey));
|
||||||
if (publicKey != null)
|
if (publicKey != null)
|
||||||
sb.append(Attribute.PUBLIC_KEY.composeWith(publicKey));
|
sb.append(Attribute.PUBLIC_KEY.composeWith(publicKey));
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
|
Loading…
Reference in New Issue
Block a user