Просмотр исходного кода

fix(agent): require HTTPS for self-heal binary download

Require HTTPS for the init self-heal unless NETPULSE_ALLOW_INSECURE_COMMANDS=1 is set. The previous flow downloaded the binary over HTTP and accepted the checksum from the same response, so an active intermediary could replace both.

Apply the guard to the installed init, the install-agent heredoc and the init generated by reinstalls, add a regression test for all three copies and document the opt-in.
Nacho 1 месяц назад
Родитель
Сommit
89585ba0e2

+ 4 - 0
CHANGELOG.md

@@ -11,6 +11,10 @@ y este proyecto se adhiere a [Versionado Semántico](https://semver.org/lang/es/
 
 - **Asuswrt/Merlin pasa a integración externa (#1579)**: el tipo nativo `asuswrt` se migra a `external` conservando el token, las sondas portables siguen compartidas y el nuevo `asuswrt-pusher` vive en `netpulse-integrations`. Desaparecen el alta como `asuswrt` y su actualización desde la UI. La instalación y actualización pasan a ser manuales, como en las demás integraciones externas.
 
+### Fixed
+
+- **Self-heal del agente sin binario por HTTP**: el init solo descarga el binario si el servidor es HTTPS o si `NETPULSE_ALLOW_INSECURE_COMMANDS=1` está activo. Antes aceptaba un checksum que viajaba por el mismo canal HTTP, así que un intermediario podía sustituir binario y digest. La guarda está en el init instalado, en el heredoc del instalador y en el init que genera la reinstalación.
+
 ## [2.47.0] - 2026-10-23
 
 ### Added

+ 9 - 0
agent/deploy/netpulse-agent.init

@@ -29,6 +29,15 @@ selfheal_binary() {
     [ -f "$ENV_FILE" ] || return 1
     . "$ENV_FILE" 2>/dev/null
     [ -n "${NETPULSE_SERVER:-}" ] && [ -n "${NETPULSE_TOKEN:-}" ] && [ -n "${NETPULSE_SLUG:-}" ] || return 1
+    case "${NETPULSE_SERVER%%:*}" in
+        https|HTTPS) ;;
+        *)
+            [ "${NETPULSE_ALLOW_INSECURE_COMMANDS:-}" = "1" ] || {
+                logger -t netpulse-agent "self-heal: HTTP sin NETPULSE_ALLOW_INSECURE_COMMANDS=1, binario no descargado"
+                return 1
+            }
+            ;;
+    esac
     case "$(uname -m)" in
         aarch64|arm64) local ARCH=arm64 ;;
         armv7l|armv7|armhf|arm) local ARCH=arm ;;

+ 2 - 0
docs/manual.en.md

@@ -147,6 +147,8 @@ The agent also refuses privileged SSE events when `NETPULSE_SERVER` is not HTTPS
 
 Use them only while moving the fleet to HTTPS, then remove them. Binary upgrades always require the `X-Checksum-Sha256` header: both the agent upgrade and the self-heal discard the binary when the header is missing or the digest does not match.
 
+The init self-heal follows the same transport policy: it only downloads the binary when `NETPULSE_SERVER` is HTTPS or `NETPULSE_ALLOW_INSECURE_COMMANDS=1` is set. Without that valve, an HTTP agent missing its binary does not self-heal and logs the refusal. That is preferable to running a binary downloaded over an unauthenticated channel.
+
 ---
 
 ## Device detail

+ 2 - 0
docs/manual.es.md

@@ -147,6 +147,8 @@ El agente también rechaza los eventos SSE privilegiados cuando `NETPULSE_SERVER
 
 Úsalas solo mientras mueves la flota a HTTPS y retíralas después. Las actualizaciones de binario exigen siempre la cabecera `X-Checksum-Sha256`: tanto la actualización del agente como el self-heal descartan el binario si falta la cabecera o el digest no coincide.
 
+El self-heal del init aplica la misma política de transporte: solo descarga el binario si `NETPULSE_SERVER` es HTTPS o si `NETPULSE_ALLOW_INSECURE_COMMANDS=1`. Sin esa válvula, un agente HTTP al que le falta el binario no se auto-repara y registra el rechazo. Es preferible a ejecutar un binario descargado por un canal no autenticado.
+
 ---
 
 ## Detalle del dispositivo

+ 9 - 0
install-agent.sh

@@ -435,6 +435,15 @@ selfheal_binary() {
     [ -f "$ENV_FILE" ] || return 1
     . "$ENV_FILE" 2>/dev/null
     [ -n "${NETPULSE_SERVER:-}" ] && [ -n "${NETPULSE_TOKEN:-}" ] && [ -n "${NETPULSE_SLUG:-}" ] || return 1
+    case "${NETPULSE_SERVER%%:*}" in
+        https|HTTPS) ;;
+        *)
+            [ "${NETPULSE_ALLOW_INSECURE_COMMANDS:-}" = "1" ] || {
+                logger -t netpulse-agent "self-heal: HTTP sin NETPULSE_ALLOW_INSECURE_COMMANDS=1, binario no descargado"
+                return 1
+            }
+            ;;
+    esac
     case "$(uname -m)" in
         aarch64|arm64) local ARCH=arm64 ;;
         armv7l|armv7|armhf|arm) local ARCH=arm ;;

+ 9 - 0
server-go/internal/reinstall/reinstall.go

@@ -297,6 +297,15 @@ selfheal_binary() {
 	[ -f "$ENV_FILE" ] || return 1
 	. "$ENV_FILE" 2>/dev/null
 	[ -n "${NETPULSE_SERVER:-}" ] && [ -n "${NETPULSE_TOKEN:-}" ] && [ -n "${NETPULSE_SLUG:-}" ] || return 1
+	case "${NETPULSE_SERVER%%:*}" in
+		https|HTTPS) ;;
+		*)
+			[ "${NETPULSE_ALLOW_INSECURE_COMMANDS:-}" = "1" ] || {
+				logger -t netpulse-agent "self-heal: HTTP sin NETPULSE_ALLOW_INSECURE_COMMANDS=1, binario no descargado"
+				return 1
+			}
+			;;
+	esac
 	case "$(uname -m)" in
 		aarch64|arm64) local ARCH=arm64 ;;
 		armv7l|armv7|armhf|arm) local ARCH=arm ;;

+ 29 - 0
server-go/internal/reinstall/script_test.go

@@ -3,7 +3,9 @@
 package reinstall_test
 
 import (
+	"os"
 	"os/exec"
+	"path/filepath"
 	"strings"
 	"testing"
 
@@ -272,6 +274,33 @@ func TestScriptSelfHealVerifiesDigest(t *testing.T) {
 	}
 }
 
+func TestSelfHealRequiresHTTPSOrExplicitOptIn(t *testing.T) {
+	want := []string{
+		`[ "${NETPULSE_ALLOW_INSECURE_COMMANDS:-}" = "1" ]`,
+		"self-heal: HTTP sin NETPULSE_ALLOW_INSECURE_COMMANDS=1, binario no descargado",
+	}
+	generated := scriptForTest()
+	for _, w := range want {
+		if !strings.Contains(generated, w) {
+			t.Errorf("init generado sin guarda de transporte: falta %q", w)
+		}
+	}
+	for _, path := range []string{
+		"../../../agent/deploy/netpulse-agent.init",
+		"../../../install-agent.sh",
+	} {
+		b, err := os.ReadFile(filepath.Clean(path))
+		if err != nil {
+			t.Fatalf("leyendo %s: %v", path, err)
+		}
+		for _, w := range want {
+			if !strings.Contains(string(b), w) {
+				t.Errorf("%s sin guarda de transporte: falta %q", path, w)
+			}
+		}
+	}
+}
+
 // #851: con server https y FP derivado, el .env generado debe llevar
 // NETPULSE_SERVER_FP o el agente entra en bucle fatal al arrancar.
 func TestScriptIncludesServerFP(t *testing.T) {