Skip to content

Commit

Permalink
[fix] OpenWrt: sanitize wireguard peer interface name
Browse files Browse the repository at this point in the history
Dashes aren't allowed in UCI, but OpenWISP allows it.
For simplicity we can just automatically convert
dashes to underscores, we do the same in other parts
of the code too.
  • Loading branch information
nemesifier committed Jun 11, 2024
1 parent 089faae commit 176b51b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def to_intermediate_loop(self, block, result, index=None):
return result

def __intermediate_peer(self, peer, index):
interface = peer.pop('interface')
interface = peer.pop('interface').replace('-', '_')
uci_name = f'wgpeer_{interface}'
if index > 1:
uci_name = f'{uci_name}_{index}'
Expand Down
32 changes: 32 additions & 0 deletions tests/openwrt/test_wireguard.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,35 @@ def test_render_multiple_wireguard_peers(self):
def test_parse_multiple_wireguard_peers(self):
o = OpenWrt(native=self._multiple_peers_uci)
self.assertEqual(o.config, self._multiple_peers_netjson)

def test_render_dash(self):
o = OpenWrt(
{
"wireguard_peers": [
{
"interface": "wg-dash",
"public_key": "rn+isMBpyQ4HX6ZzE709bKnZw5IaLZoIS3hIjmfKCkk=",
"allowed_ips": ["10.0.0.1/32"],
"endpoint_host": "192.168.1.42",
"endpoint_port": 40840,
"preshared_key": "oPZmGdHBseaV1TF0julyElNuJyeKs2Eo+o62R/09IB4=",
"persistent_keepalive": 30,
"route_allowed_ips": True,
}
]
}
)
expected = self._tabs(
"""package network
config wireguard_wg_dash 'wgpeer_wg_dash'
list allowed_ips '10.0.0.1/32'
option endpoint_host '192.168.1.42'
option endpoint_port '40840'
option persistent_keepalive '30'
option preshared_key 'oPZmGdHBseaV1TF0julyElNuJyeKs2Eo+o62R/09IB4='
option public_key 'rn+isMBpyQ4HX6ZzE709bKnZw5IaLZoIS3hIjmfKCkk='
option route_allowed_ips '1'
"""
)
self.assertEqual(o.render(), expected)

0 comments on commit 176b51b

Please sign in to comment.