Unverified Commit 468dfc97 authored by Bruno Bigras's avatar Bruno Bigras Committed by GitHub
Browse files

pihole-ftl: fix build (#495363)

parents a4b744be cd869b7f
Loading
Loading
Loading
Loading
+0 −45
Original line number Diff line number Diff line
From 14311f18ae9427a0baa1e0cb67198580d9d2dc69 Mon Sep 17 00:00:00 2001
From: averyvigolo <avery@averyv.me>
Date: Sun, 24 Aug 2025 18:35:37 +0100
Subject: [PATCH] Only use redirect_root_handler if webhome is set (fixes
 #2518)

This fixes an infinite redirect on the home page when authentication is enabled.
The redirects are caused by XHR calls to the API, which receive a 401 response, and
the error handlers simply reload the page.

If webhome is not set, the default request handler should be used to properly
handle authentication. So, conditionally enable redirect_root_handler, if
webhome is not empty or `/`. This fixes the problem, as there's an immediate
redirect to /login, before any XHR calls.

Remove the initial fix in https://github.com/pi-hole/FTL/pull/2521, as it is no
longer necessary. That fix involved checking in redirect_root_handler, if the
redirect destination is the same as the request URI.

Signed-off-by: averyvigolo <avery@averyv.me>
---
 src/webserver/webserver.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/src/webserver/webserver.c b/src/webserver/webserver.c
index e0999d230..c3e32a35c 100644
--- a/src/webserver/webserver.c
+++ b/src/webserver/webserver.c
@@ -720,7 +714,14 @@ void http_init(void)
 	// prefix should be stripped away by the reverse proxy
 	mg_set_request_handler(ctx, "/api", api_handler, NULL);
 
-	mg_set_request_handler(ctx, "/$", redirect_root_handler, NULL);
+	if(strcmp(prefix_webhome, "/") == 0 || strlen(prefix_webhome) == 0)
+	{
+		log_debug(DEBUG_API, "Not redirecting root since webhome is '%s'",
+			  prefix_webhome);
+	} else {
+		// Redirect requests to / to the webhome path.
+		mg_set_request_handler(ctx, "/$", redirect_root_handler, NULL);
+	}
 
 	if(strcmp(config.webserver.paths.webhome.v.s, "/") == 0 &&
 	   config.dns.blocking.mode.v.blocking_mode == MODE_IP)
+19 −23
Original line number Diff line number Diff line
{
  lib,
  stdenv,
  nixosTests,
  fetchFromGitHub,
  cmake,
  fetchFromGitHub,
  gmp,
  lib,
  libidn2,
  libunistring,
  mbedtls,
  ncurses,
  nettle,
  nix-update-script,
  nixosTests,
  readline,
  stdenv,
  versionCheckHook,
  xxd,
  iproute2,
}:

stdenv.mkDerivation (finalAttrs: {
@@ -26,11 +27,6 @@ stdenv.mkDerivation (finalAttrs: {
    hash = "sha256-OpbBd+HS/gwcWNe/6VB3glout1sifJ8o5EnKuXfyZ/o=";
  };

  patches = [
    # https://github.com/pi-hole/FTL/pull/2610: Fix authentication redirect when webhome is /
    ./disable-redirect-root.patch
  ];

  nativeBuildInputs = [
    cmake
    xxd
@@ -52,37 +48,37 @@ stdenv.mkDerivation (finalAttrs: {

  postPatch = ''
    substituteInPlace src/version.c.in \
      --replace-quiet "@GIT_VERSION@" "v${finalAttrs.version}" \
      --replace-quiet "@GIT_DATE@" "1970-01-01" \
      --replace-quiet "@GIT_BRANCH@" "master" \
      --replace-quiet "@GIT_TAG@" "v${finalAttrs.version}" \
      --replace-quiet "@GIT_HASH@" "builtfromreleasetarball"
      --replace-fail "@GIT_VERSION@" "v${finalAttrs.version}" \
      --replace-fail "@GIT_DATE@" "1970-01-01" \
      --replace-fail "@GIT_BRANCH@" "master" \
      --replace-fail "@GIT_TAG@" "v${finalAttrs.version}" \
      --replace-fail "@GIT_HASH@" "builtfromreleasetarball"

    # Remove hard-coded absolute path to the pihole script, rely on it being provided by $PATH
    # Use execvp instead of execv so PATH is followed
    # Remove hard-coded absolute path to the pihole script, rely on it
    # being provided by $PATH.  Use execvp instead of execv so PATH is
    # followed.
    substituteInPlace src/api/action.c \
      --replace-fail "/usr/local/bin/pihole" "pihole" \
      --replace-fail "execv" "execvp"

    substituteInPlace src/database/network-table.c \
      --replace-fail "ip neigh show" "${lib.getExe' iproute2 "ip"} neigh show" \
      --replace-fail "ip address show" "${lib.getExe' iproute2 "ip"} address show"
  '';

  installPhase = ''
    runHook preInstall

    install -Dm 555 -t $out/bin pihole-FTL
    install -D pihole-FTL $out/bin/${finalAttrs.meta.mainProgram}

    runHook postInstall
  '';

  passthru = {
    settingsTemplate = ./pihole.toml;

    tests = nixosTests.pihole-ftl;
    updateScript = nix-update-script { };
  };

  doInstallCheck = true;
  nativeInstallCheckInputs = [ versionCheckHook ];

  meta = {
    description = "Pi-hole FTL engine";
    homepage = "https://github.com/pi-hole/FTL";