Browse code

admin/messages.php: Security - CSRF protection for admin message forms; viewpage.php, admin/custpages.php: Security - do not execute PHP in stored pages and CSRF protection, but delete is a confirmation prompt instead - from @ariadoss

Clarissa Walker authored on 2026/10/26 20:56:31
Showing 3 changed files
... ...
@@ -24,12 +24,29 @@
24 24
 
25 25
 if(!defined("_CHARSET")) exit( );
26 26
 
27
+$csrfKey = SITEKEY . '_custpages_csrf';
28
+if(empty($_SESSION[$csrfKey])) $_SESSION[$csrfKey] = bin2hex(random_bytes(32));
29
+$isWrite = isset($_POST['submit']) || isset($_POST['preview']);
30
+if($isWrite && (!isset($_POST['csrf']) || !hash_equals($_SESSION[$csrfKey], (string) $_POST['csrf']))) {
31
+	http_response_code(403);
32
+	exit('Forbidden');
33
+}
34
+$efCsrfField = "<input type='hidden' name='csrf' value='".htmlspecialchars($_SESSION[$csrfKey], ENT_QUOTES, 'UTF-8')."'>";
35
+$confirm = isset($_GET['confirm']) ? $_GET['confirm'] : false;
27 36
 
28 37
 	$output .= "<div id=\"pagetitle\">"._CUSTPAGES."</div>";
29 38
 if(isset($_GET['delete']) && isNumber($_GET['delete'])) {
30
-	$result = dbquery("DELETE FROM ".TABLEPREFIX."fanfiction_messages WHERE message_id = ".$_GET['delete']." LIMIT 1");
31
-	if($result) $output .= write_message(_ACTIONSUCCESSFUL);
32
-	else $output .= write_error(_ERROR);
39
+	if($confirm == "yes") {
40
+		$result = dbquery("DELETE FROM ".TABLEPREFIX."fanfiction_messages WHERE message_id = ".$_GET['delete']." LIMIT 1");
41
+		if($result) $output .= write_message(_ACTIONSUCCESSFUL);
42
+		else $output .= write_error(_ERROR);
43
+	}
44
+	else if($confirm == "no") {
45
+	$output .= write_message(_ACTIONCANCELLED);
46
+	}
47
+	else {
48
+		$output .= write_message("Are you sure you want to remove this custom page? <a href='admin.php?action=custpages&amp;delete=".$_GET['delete']."&amp;confirm=yes'>"._YES."</a> "._OR." <a href='admin.php?action=custpages&amp;delete=".$_GET['delete']."&amp;confirm=no'>"._NO."</a>");
49
+	}
33 50
 }
34 51
 if(isset($_POST['submit'])) {
35 52
 	if(!preg_match("!^[a-z0-9_]{3,30}$!i", $_POST['name'])) $output .= write_error(_BADVARNAME);
... ...
@@ -54,12 +71,8 @@ if((isset($_GET['edit']) && isNumber($_GET['edit'])) || isset($_GET["new"]) || i
54 71
 		$title = descript($_POST['title']);
55 72
 		$thistext = descript($_POST['text']);
56 73
 		$edit = isset($_GET['edit']) ? $_GET['edit'] : "";
57
-		if(strpos($thistext, "?>") === false) $output .= "<div id='pagetitle'>$title</div>\n\n$thistext";
58
-		else {
59
-			$text = "";
60
-			eval("?>".$thistext."<?php ");
61
-			$output .= "<div id='pagetitle'>$title</div>\n\n$text";
62
-		}
74
+		if(preg_match('/<\?(?:php|=)|\?>/i', (string) $thistext)) $output .= "<div id='pagetitle'>$title</div>".write_error("This draft contains embedded PHP. This eFiction fork does not execute stored PHP; convert the page to static HTML.");
75
+		else $output .= "<div id='pagetitle'>$title</div>\n\n$thistext";
63 76
 		$text = $thistext;
64 77
 	}
65 78
 	else if(isset($_GET['edit'])) {
... ...
@@ -77,6 +90,7 @@ if((isset($_GET['edit']) && isNumber($_GET['edit'])) || isset($_GET["new"]) || i
77 90
 		$text = "";
78 91
 	}
79 92
 	$output .= "<div id='settingsform'><form method=\"POST\" enctype=\"multipart/form-data\" action=\"admin.php?action=custpages".(isset($edit) ? "&amp;edit=$edit" : "")."\">
93
+		$efCsrfField
80 94
 		<div><label for=\"name\">"._NAME.":</label> <input type=\"text\" class=\"textbox\" name=\"name\" value=\"$name\"></div>
81 95
 		<div><label for=\"title\">"._TITLE.":</label> <input type=\"text\" class=\"textbox\" name=\"title\" value=\"$title\" size=\"30\"></div>
82 96
 		<div><label for=\"text\">"._TEXT.":</label><br />
... ...
@@ -94,7 +108,7 @@ else {
94 108
 		if($msg['message_name'] == "welcome" ||  $msg['message_name'] == "nothankyou" || $msg['message_name'] == "thankyou" ||
95 109
 		$msg['message_name'] == "copyright" || $msg['message_name'] == "printercopyright") continue;
96 110
 		$output .= "<tr><td class=\"tblborder\">$msg[message_id]</td><td class=\"tblborder\">$msg[message_name]</td><td class=\"tblborder\"><a href='viewpage.php?page=$msg[message_name]'>$msg[message_title]</a></td>
97
-		<td class=\"tblborder\"><a href=\"admin.php?action=custpages&amp;edit=$msg[message_id]\">"._EDIT."</a> | 
111
+		<td class=\"tblborder\"><a href=\"admin.php?action=custpages&amp;edit=$msg[message_id]\">"._EDIT."</a> |
98 112
 		<a href=\"admin.php?action=custpages&amp;delete=$msg[message_id]\">"._DELETE."</a></td></tr>";
99 113
 	}
100 114
 	if(!dbnumrows($listquery)) $output .= "<tr><td class=\"tblborder\" colspan=\"3\" align=\"center\">"._NORESULTS."</td></tr>";
... ...
@@ -102,4 +116,4 @@ else {
102 116
 	$output .= "</table>";
103 117
 	$output .= write_message(_CUSTPAGENOTE);
104 118
 }
105
-?>
106 119
\ No newline at end of file
120
+?>
... ...
@@ -22,22 +22,34 @@
22 22
 
23 23
 if(!defined("_CHARSET")) exit( );
24 24
 
25
+$csrfKey = SITEKEY . '_messages_csrf';
26
+if(empty($_SESSION[$csrfKey])) $_SESSION[$csrfKey] = bin2hex(random_bytes(32));
27
+if(isset($_POST['submit']) && (!isset($_POST['csrf']) || !hash_equals($_SESSION[$csrfKey], (string) $_POST['csrf']))) {
28
+	http_response_code(403);
29
+	exit('Forbidden');
30
+}
31
+$messageName = (isset($_GET['message']) && preg_match('/^[a-z0-9_]{3,30}$/i', (string) $_GET['message'])) ? (string) $_GET['message'] : "";
32
+$efCsrfField = "<input type='hidden' name='csrf' value='".htmlspecialchars($_SESSION[$csrfKey], ENT_QUOTES, 'UTF-8')."'>";
33
+
25 34
 	$output .= "<div id=\"pagetitle\">"._MESSAGESETTINGS."</div>";
26 35
 
27
-if(isset($_POST['submit'])) {
28
-	$result = dbquery("UPDATE ".TABLEPREFIX."fanfiction_messages SET message_text = '".escapestring(descript($_POST['text']))."' WHERE message_name = '".$_GET['message']."' LIMIT 1");
36
+if($messageName === "") {
37
+	$output .= write_error(_BADVARNAME);
38
+} else if(isset($_POST['submit'])) {
39
+	$result = dbquery("UPDATE ".TABLEPREFIX."fanfiction_messages SET message_text = '".escapestring(descript($_POST['text']))."' WHERE message_name = '".escapestring($messageName)."' LIMIT 1");
29 40
 	if($result) $output .= write_message(_ACTIONSUCCESSFUL);
30 41
 	else $output .= write_error(_ERROR);
31 42
 }
32 43
 else {
33
-	$pagequery = dbquery("SELECT * FROM ".TABLEPREFIX."fanfiction_messages WHERE message_name = '".$_GET['message']."' LIMIT 1");
44
+	$pagequery = dbquery("SELECT * FROM ".TABLEPREFIX."fanfiction_messages WHERE message_name = '".escapestring($messageName)."' LIMIT 1");
34 45
 	$message =  dbassoc($pagequery);
35 46
 	$text = $message['message_text'];
36
-	if(!$message) dbquery("INSERT INTO `".TABLEPREFIX."fanfiction_messages` (`message_name` , `message_title` , `message_text` ) VALUES ('".$_GET['message']."', '', '')");
37
-	$output .= "<div class='sectionheader'>".preg_replace("@\{sitename\}@", $sitename, $message['message_title'])."</div>
38
-		<div style='width: 100%;'><div style='width: 35%; margin: 0 auto;'><form method=\"POST\" enctype=\"multipart/form-data\" action=\"admin.php?action=messages&message=".$_GET['message']."\">
39
-		<textarea rows=\"10\" cols=\"60\" style=\"width: 100%;\" ".($_GET['message'] == "tinyMCE" ? "class='mceNoEditor'" :"")." name=\"text\">$text</textarea>";
40
-	if($tinyMCE && $_GET['message'] != "tinyMCE") 
47
+	if(!$message) dbquery("INSERT INTO `".TABLEPREFIX."fanfiction_messages` (`message_name` , `message_title` , `message_text` ) VALUES ('".escapestring($messageName)."', '', '')");
48
+	$output .= "<div class='sectionheader'>".htmlspecialchars(preg_replace("@\{sitename\}@", (string) $sitename, (string) $message['message_title']), ENT_QUOTES, 'UTF-8')."</div>
49
+		<div style='width: 100%;'><div  id=\"settingsform\"><form method=\"POST\" enctype=\"multipart/form-data\" action=\"admin.php?action=messages&message=".htmlspecialchars($messageName, ENT_QUOTES, 'UTF-8')."\">
50
+		$efCsrfField
51
+		<textarea rows=\"10\" cols=\"60\" style=\"width: 100%;\" ".($messageName == "tinyMCE" ? "class='mceNoEditor'" :"")." name=\"text\">$text</textarea>";
52
+	if($tinyMCE && $messageName != "tinyMCE")
41 53
 		$output .= "<div class='tinytoggle'><input type='checkbox' name='toggle' onclick=\"toogleEditorMode('text');\" checked><label for='toggle'>"._TINYMCETOGGLE."</label></div>";
42 54
 	$output .= "<div style='clear: both;'>&nbsp; &nbsp;</div><INPUT type='submit' class='button' id='submit' value='"._SUBMIT."' name='submit'>
43 55
 				</form></div><div style='clear: both;'>&nbsp;</div></div>";
... ...
@@ -34,12 +34,13 @@ include("includes/pagesetup.php");
34 34
 
35 35
 $page = dbquery("SELECT message_title, message_text FROM ".TABLEPREFIX."fanfiction_messages WHERE ".($current ? "message_name = '".escapestring($current)."'" : "message_id = '".(isNumber($_GET['id']) ? $_GET[id] : "0")."'")." LIMIT 1");
36 36
 if(dbnumrows($page)) list($title, $text) = dbrow($page);
37
-else $text = write_message(_ERROR);
38
-if(strpos($text, "?>") === false) $tpl->assign("output", "<div id='pagetitle'>$title</div>\n\n$text");
39
-else {
40
-	eval("?>".$text."<?php ");
37
+else { $title = ""; $text = write_message(_ERROR); }
38
+if(preg_match('/<\?(?:php|=)|\?>/i', (string) $text)) {
39
+	http_response_code(503);
40
+	$tpl->assign("output", "This custom page needs administrator review.");
41
+} else {
41 42
 	$tpl->assign("output", "<div id='pagetitle'>$title</div>\n\n$text");
42 43
 }
43 44
 $tpl->printToScreen();
44 45
 dbclose( );
45
-?>
46 46
\ No newline at end of file
47
+?>