vpn: multi-peer WireGuard configs are rejected, not silently merged
- Commit
5b4e6b3313524a9073e24313181b2d2a3755bb8b- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-30 21:48 UTC (5 days ago)
- Parents
34d20bf
The config parser collapsed sections by name into one map per name, so a second [Peer] reused the first peer's map: peer 1 vanished entirely and any key peer 2 omitted (AllowedIPs, PresharedKey, PersistentKeepalive) was silently inherited from peer 1. A standard multi-peer wg-quick config 'imported successfully' as a single tunnel to peer-two carrying peer-one's routes — one peer's traffic misrouted or lost, with no error. The same path serves .mavpn bundle servers. The parser now rejects a repeated [Peer] with a clear single-peer message (the tunnel model and native bridge take exactly one peer, so honest rejection beats a lossy import or a pretend multi-peer). Single-peer configs parse exactly as before, and the constraint is documented in MAVPN.md/AGENTS.md. Both new regression tests (.conf and .mavpn paths) failed pre-fix. Connection lifecycle, per-app bypass, DNS and the kill-switch surface were audited and cleared. Verified: :vpn:testDevUnitTest BUILD SUCCESSFUL; lint errors are pre-existing on untouched files.
Diff
vpn/AGENTS.md
diff --git a/vpn/AGENTS.md b/vpn/AGENTS.mdindex 1012799248..055125351d 100644--- a/vpn/AGENTS.md+++ b/vpn/AGENTS.md@@ -22,6 +22,7 @@ _Install: ./install vpn (dev by default)._ - `util/` is NOT a closed package root — don't add more files there - Tunnel state lives in the service + Rust core — UI reflects, never drives; reconnect races come from driving twice - VPN permission + always-on/blocking modes are system-mediated; test grant/revoke, not just in-app toggles+- WireGuard import (`WgConfigParser`, also used for `.mavpn` servers) is single-peer: configs with multiple `[Peer]` sections are rejected rather than merged into one tunnel ## Key files ### Root — entry + navigation
vpn/docs/MAVPN.md
diff --git a/vpn/docs/MAVPN.md b/vpn/docs/MAVPN.mdindex f84bfc9235..2bd7466c05 100644--- a/vpn/docs/MAVPN.md+++ b/vpn/docs/MAVPN.md@@ -63,7 +63,9 @@ servers/<name>.conf - Entry paths must stay inside the bundle: absolute paths, `..`/`.` segments and backslash separators are rejected. - Every referenced `.conf` must parse as a WireGuard config with an- `[Interface]` `PrivateKey` and a `[Peer]` `PublicKey`.+ `[Interface]` `PrivateKey` and a `[Peer]` `PublicKey`. Configs with more+ than one `[Peer]` section are rejected: the tunnel is single-peer, and+ merging peers would connect to one peer with another's routes. - Configs are stored only in app-private storage (`files/mavpn-bundles/<bundle>/`) and in the app's encrypted config database. Private keys are never logged.
vpn/src/main/java/com/vayunmathur/vpn/data/VpnConfig.kt
diff --git a/vpn/src/main/java/com/vayunmathur/vpn/data/VpnConfig.kt b/vpn/src/main/java/com/vayunmathur/vpn/data/VpnConfig.ktindex b85b28e2da..fc8a7bb0ce 100644--- a/vpn/src/main/java/com/vayunmathur/vpn/data/VpnConfig.kt+++ b/vpn/src/main/java/com/vayunmathur/vpn/data/VpnConfig.kt@@ -48,9 +48,10 @@ object WgConfigParser { private fun splitSections(confText: String): Map<String, Map<String, String>> { val sections = mutableMapOf<String, MutableMap<String, String>>()+ val seenSections = mutableSetOf<String>() var cur = "" confText.lineSequence().forEach { raw ->- cur = consumeLine(raw, cur, sections)+ cur = consumeLine(raw, cur, sections, seenSections) } return sections }@@ -59,11 +60,22 @@ object WgConfigParser { raw: String, cur: String, sections: MutableMap<String, MutableMap<String, String>>,+ seenSections: MutableSet<String>, ): String { val line = raw.trim() if (isComment(line)) return cur if (isSectionHeader(line)) { val name = line.removeSurrounding("[", "]").trim()+ // The tunnel model (VpnConfig + VpnNative.newTunnel) is single-peer, but+ // wg-quick allows one [Peer] section per peer. Reusing one map for a+ // second [Peer] would merge both peers into one: keys the second peer+ // omits (AllowedIPs, PresharedKey, …) would be inherited from the first,+ // silently connecting to one peer with the other's routes. Reject+ // multi-peer configs instead of importing a merged, wrong tunnel.+ if (name == "Peer" && !seenSections.add(name)) {+ error("Multiple [Peer] sections are not supported (single-peer tunnel)")+ }+ seenSections.add(name) sections.getOrPut(name) { mutableMapOf() } return name }
vpn/src/test/java/com/vayunmathur/vpn/data/WgConfigParserTest.kt
diff --git a/vpn/src/test/java/com/vayunmathur/vpn/data/WgConfigParserTest.kt b/vpn/src/test/java/com/vayunmathur/vpn/data/WgConfigParserTest.ktindex e891b0a1d6..99f0ca2fa6 100644--- a/vpn/src/test/java/com/vayunmathur/vpn/data/WgConfigParserTest.kt+++ b/vpn/src/test/java/com/vayunmathur/vpn/data/WgConfigParserTest.kt@@ -34,6 +34,27 @@ class WgConfigParserTest { assertEquals(25, imp.peerKeepalive) } + @Test fun multiPeerConfIsRejectedInsteadOfMerged() {+ // Two [Peer] sections are valid wg-quick (one peer each), but this app's+ // tunnel model is single-peer. The sections must not be merged into one+ // peer: below, the second peer would silently inherit the first peer's+ // AllowedIPs and connect to peer-two with peer-one's routes.+ val conf = """+ [Interface]+ PrivateKey = priv=+ [Peer]+ PublicKey = peer-one=+ AllowedIPs = 10.1.0.0/16+ Endpoint = one.example:1111+ [Peer]+ PublicKey = peer-two=+ Endpoint = two.example:2222+ """.trimIndent()+ val result = WgConfigParser.parse(conf)+ assertTrue(result.isFailure, "multi-peer conf must be rejected, got ${result.getOrNull()}")+ assertTrue(result.exceptionOrNull()?.message?.contains("Peer") == true)+ }+ @Test fun defaultsForMissingOptionalFields() { val imp = WgConfigParser.parse("[Interface]\nPrivateKey = abc=\n[Peer]\nPublicKey = def=\n").getOrThrow() assertEquals("", imp.address); assertEquals("", imp.dns)
vpn/src/test/java/com/vayunmathur/vpn/domain/MavpnParserTest.kt
diff --git a/vpn/src/test/java/com/vayunmathur/vpn/domain/MavpnParserTest.kt b/vpn/src/test/java/com/vayunmathur/vpn/domain/MavpnParserTest.ktindex 8a1b46bfcc..9212d302a8 100644--- a/vpn/src/test/java/com/vayunmathur/vpn/domain/MavpnParserTest.kt+++ b/vpn/src/test/java/com/vayunmathur/vpn/domain/MavpnParserTest.kt@@ -128,6 +128,14 @@ class MavpnParserTest { assertTrue("SECRETKEY" !in message, "parser error leaked config contents: $message") } + @Test fun rejectsMultiPeerServerConf() {+ // Bundle servers go through the same single-peer WireGuard parser, so a+ // multi-peer .conf must fail the bundle instead of importing a merged peer.+ val multiPeer = conf + "\n[Peer]\nPublicKey = cGVlcm9uZXR3bzEyMzQ1Njc4OTBhYmNkZWZnaGlqaw==\n" ++ "Endpoint = other.example:51820\n"+ assertTrue(parse(bundle(confText = multiPeer)).isFailure)+ }+ @Test fun rejectsNonZipAndEmptyInput() { assertTrue(parse(ByteArray(0)).isFailure) assertTrue(parse("not a zip".encodeToByteArray()).isFailure)