summary refs log tree commit diff
path: root/nixos/modules/services/networking/syncthing.nix
diff options
context:
space:
mode:
authorBjørn Forsman <bjorn.forsman@gmail.com>2022-07-23 13:57:25 +0200
committerBjørn Forsman <bjorn.forsman@gmail.com>2022-07-23 13:59:11 +0200
commit65399c47424c7b33d90c6a3b581b38ed2f66b859 (patch)
treebbbed7092ecf5da3c45c61614ea7925c912af387 /nixos/modules/services/networking/syncthing.nix
parent16108ff74a5949ab43851be17860a177766feab0 (diff)
downloadnixpkgs-65399c47424c7b33d90c6a3b581b38ed2f66b859.tar
nixpkgs-65399c47424c7b33d90c6a3b581b38ed2f66b859.tar.gz
nixpkgs-65399c47424c7b33d90c6a3b581b38ed2f66b859.tar.bz2
nixpkgs-65399c47424c7b33d90c6a3b581b38ed2f66b859.tar.lz
nixpkgs-65399c47424c7b33d90c6a3b581b38ed2f66b859.tar.xz
nixpkgs-65399c47424c7b33d90c6a3b581b38ed2f66b859.tar.zst
nixpkgs-65399c47424c7b33d90c6a3b581b38ed2f66b859.zip
nixos/syncthing: don't leak the secret API key in process listings
Diffstat (limited to 'nixos/modules/services/networking/syncthing.nix')
-rw-r--r--nixos/modules/services/networking/syncthing.nix14
1 files changed, 11 insertions, 3 deletions
diff --git a/nixos/modules/services/networking/syncthing.nix b/nixos/modules/services/networking/syncthing.nix
index 0f697c0cc25..66b85cd9d8a 100644
--- a/nixos/modules/services/networking/syncthing.nix
+++ b/nixos/modules/services/networking/syncthing.nix
@@ -30,15 +30,22 @@ let
   updateConfig = pkgs.writers.writeDash "merge-syncthing-config" ''
     set -efu
 
+    # be careful not to leak secrets in the filesystem or in process listings
+
+    umask 0077
+
     # get the api key by parsing the config.xml
     while
-        ! api_key=$(${pkgs.libxml2}/bin/xmllint \
+        ! ${pkgs.libxml2}/bin/xmllint \
             --xpath 'string(configuration/gui/apikey)' \
-            ${cfg.configDir}/config.xml)
+            ${cfg.configDir}/config.xml \
+            >"$RUNTIME_DIRECTORY/api_key"
     do sleep 1; done
 
+    (printf "X-API-Key: "; cat "$RUNTIME_DIRECTORY/api_key") >"$RUNTIME_DIRECTORY/headers"
+
     curl() {
-        ${pkgs.curl}/bin/curl -sSLk -H "X-API-Key: $api_key" \
+        ${pkgs.curl}/bin/curl -sSLk -H "@$RUNTIME_DIRECTORY/headers" \
             --retry 1000 --retry-delay 1 --retry-all-errors \
             "$@"
     }
@@ -576,6 +583,7 @@ in {
         serviceConfig = {
           User = cfg.user;
           RemainAfterExit = true;
+          RuntimeDirectory = "syncthing-init";
           Type = "oneshot";
           ExecStart = updateConfig;
         };