git.iko.soy/typeable/nixpkgs
on master help sign in
wireguard module: Quote all command line arguments correctly.

Standard best-practice shell quoting, which can prevent the most horrible production accidents. Note that we cannot use `+ optionalString someBool '' someString''` because Nix's multi-line ''double-quoted'' strings remove leading whitespace.

commitaaffc6447d4cd66be202fb34b31bacf947f0f709 authorNiklas Hambüchen <mail@nh2.me> dateFri 30 Apr 2021, 22:56 (6 years ago) parentc8dff32
1 file changed+20−16
nixos/modules/services/networking/wireguard.nix+20−16
nixos/modules/services/networking/wireguard.nix+20−16
@@ -286,16 +286,18 @@ let
286286 };
287287
288288 script = let
289− wg_setup = "${wg} set ${interfaceName} peer ${peer.publicKey}" +
290− optionalString (psk != null) " preshared-key ${psk}" +
291− optionalString (peer.endpoint != null) " endpoint ${peer.endpoint}" +
292− optionalString (peer.persistentKeepalive != null) " persistent-keepalive ${toString peer.persistentKeepalive}" +
293− optionalString (peer.allowedIPs != []) " allowed-ips ${concatStringsSep "," peer.allowedIPs}";
289+ wg_setup = concatStringsSep " " (
290+ [ ''${wg} set ${interfaceName} peer "${peer.publicKey}"'' ]
291+ ++ optional (psk != null) ''preshared-key "${psk}"''
292+ ++ optional (peer.endpoint != null) ''endpoint "${peer.endpoint}"''
293+ ++ optional (peer.persistentKeepalive != null) ''persistent-keepalive "${toString peer.persistentKeepalive}"''
294+ ++ optional (peer.allowedIPs != []) ''allowed-ips "${concatStringsSep "," peer.allowedIPs}"''
295+ );
294296 route_setup =
295297 optionalString interfaceCfg.allowedIPsAsRoutes
296298 (concatMapStringsSep "\n"
297299 (allowedIP:
298− "${ip} route replace ${allowedIP} dev ${interfaceName} table ${interfaceCfg.table}"
300+ ''${ip} route replace "${allowedIP}" dev "${interfaceName}" table "${interfaceCfg.table}"''
299301 ) peer.allowedIPs);
300302 in ''
301303 ${wg_setup}
@@ -306,10 +308,10 @@ let
306308 route_destroy = optionalString interfaceCfg.allowedIPsAsRoutes
307309 (concatMapStringsSep "\n"
308310 (allowedIP:
309− "${ip} route delete ${allowedIP} dev ${interfaceName} table ${interfaceCfg.table}"
311+ ''${ip} route delete "${allowedIP}" dev "${interfaceName}" table "${interfaceCfg.table}"''
310312 ) peer.allowedIPs);
311313 in ''
312− ${wg} set ${interfaceName} peer ${peer.publicKey} remove
314+ ${wg} set "${interfaceName}" peer "${peer.publicKey}" remove
313315 ${route_destroy}
314316 '';
315317 };
@@ -345,23 +347,25 @@ let
345347
346348 ${values.preSetup}
347349
348− ${ipPreMove} link add dev ${name} type wireguard
349− ${optionalString (values.interfaceNamespace != null && values.interfaceNamespace != values.socketNamespace) "${ipPreMove} link set ${name} netns ${ns}"}
350+ ${ipPreMove} link add dev "${name}" type wireguard
351+ ${optionalString (values.interfaceNamespace != null && values.interfaceNamespace != values.socketNamespace) ''${ipPreMove} link set "${name}" netns "${ns}"''}
350352
351353 ${concatMapStringsSep "\n" (ip:
352− "${ipPostMove} address add ${ip} dev ${name}"
354+ ''${ipPostMove} address add "${ip}" dev "${name}"''
353355 ) values.ips}
354356
355− ${wg} set ${name} private-key ${privKey} ${
356− optionalString (values.listenPort != null) " listen-port ${toString values.listenPort}"}
357+ ${concatStringsSep " " (
358+ [ ''${wg} set "${name}" private-key "${privKey}"'' ]
359+ ++ optional (values.listenPort != null) ''listen-port "${toString values.listenPort}"''
360+ )}
357361
358− ${ipPostMove} link set up dev ${name}
362+ ${ipPostMove} link set up dev "${name}"
359363
360364 ${values.postSetup}
361365 '';
362366
363367 postStop = ''
364− ${ipPostMove} link del dev ${name}
368+ ${ipPostMove} link del dev "${name}"
365369 ${values.postShutdown}
366370 '';
367371 };
@@ -371,7 +375,7 @@ let
371375 nsList = filter (ns: ns != null) [ src dst ];
372376 ns = last nsList;
373377 in
374− if (length nsList > 0 && ns != "init") then "ip netns exec ${ns} ${cmd}" else cmd;
378+ if (length nsList > 0 && ns != "init") then ''ip netns exec "${ns}" "${cmd}"'' else cmd;
375379in
376380
377381{