diff --git a/scripts/pi-hole/js/debug.js b/scripts/pi-hole/js/debug.js index c0db2ce0..0a6b3ab1 100644 --- a/scripts/pi-hole/js/debug.js +++ b/scripts/pi-hole/js/debug.js @@ -29,20 +29,21 @@ function eventsource() { var ta = $("#output"); var upload = $( "#upload" ); var checked = ""; + var token = encodeURIComponent($("#token").html()); if(upload.prop("checked")) { - checked = "upload"; + checked = "upload"; } // IE does not support EventSource - load whole content at once if (typeof EventSource !== "function") { - httpGet(ta,"/admin/scripts/pi-hole/php/debug.php?IE&"+checked); + httpGet(ta,"/admin/scripts/pi-hole/php/debug.php?IE&token="+token+"&"+checked); return; } var host = window.location.host; - var source = new EventSource("/admin/scripts/pi-hole/php/debug.php?"+checked); + var source = new EventSource("/admin/scripts/pi-hole/php/debug.php?&token="+token+"&"+checked); // Reset and show field ta.empty(); diff --git a/scripts/pi-hole/php/debug.php b/scripts/pi-hole/php/debug.php index 93ae3166..39d5c8ea 100644 --- a/scripts/pi-hole/php/debug.php +++ b/scripts/pi-hole/php/debug.php @@ -5,6 +5,18 @@ ob_implicit_flush(true); header('Content-Type: text/event-stream'); header('Cache-Control: no-cache'); +require "password.php"; +require "auth.php"; + +if(!$auth) { + die("Unauthorized"); +} + +check_cors(); + +$token = isset($_GET["token"]) ? $_GET["token"] : ""; +check_csrf($token); + function echoEvent($datatext) { if(!isset($_GET["IE"])) echo "data: ".implode("\ndata: ", explode("\n", $datatext))."\n\n"; diff --git a/scripts/pi-hole/php/header.php b/scripts/pi-hole/php/header.php index a6a1c79d..a858c328 100644 --- a/scripts/pi-hole/php/header.php +++ b/scripts/pi-hole/php/header.php @@ -12,6 +12,12 @@ check_cors(); + // Generate CSRF token + if(empty($_SESSION['token'])) { + $_SESSION['token'] = base64_encode(openssl_random_pseudo_bytes(32)); + } + $token = $_SESSION['token']; + // Try to get temperature value from different places (OS dependent) if(file_exists("/sys/class/thermal/thermal_zone0/temp")) { @@ -201,6 +207,11 @@
To enable Javascript click here
+$token"; +} +?> diff --git a/scripts/pi-hole/php/savesettings.php b/scripts/pi-hole/php/savesettings.php index 8a5f9ef5..da339526 100644 --- a/scripts/pi-hole/php/savesettings.php +++ b/scripts/pi-hole/php/savesettings.php @@ -159,6 +159,9 @@ function readAdlists() if(isset($_POST["field"])) { + // Handle CSRF + check_csrf(isset($_POST["token"]) ? $_POST["token"] : ""); + // Process request switch ($_POST["field"]) { // Set DNS server diff --git a/scripts/pi-hole/php/teleporter.php b/scripts/pi-hole/php/teleporter.php index 43092806..0965d109 100644 --- a/scripts/pi-hole/php/teleporter.php +++ b/scripts/pi-hole/php/teleporter.php @@ -7,11 +7,13 @@ * Please see LICENSE file for your rights under this license. */ require "password.php"; +require "auth.php"; // Also imports func.php + if (php_sapi_name() !== "cli") { if(!$auth) die("Not authorized"); + check_csrf(isset($_POST["token"]) ? $_POST["token"] : ""); } -require('func.php'); function process_zip($name) { global $zip; diff --git a/settings.php b/settings.php index 96f3a78d..d74069b4 100644 --- a/settings.php +++ b/settings.php @@ -1,4 +1,4 @@ - @@ -596,6 +597,7 @@ if(isset($_POST["submit"])) { @@ -637,6 +639,7 @@ if(isset($_POST["submit"])) { @@ -839,12 +844,15 @@ if(isset($_POST["submit"])) { @@ -883,6 +891,7 @@ if($FTL)The PHP extension zip is not loaded. Please ensure it is installed and loaded if you want to use the Pi-hole teleporter.
+The PHP extension zip is not loaded. Please ensure it is installed and loaded if you want to use the Pi-hole teleporter.