1e39ccd63SJonny Dee<?php 2e39ccd63SJonny Dee/** 3994617c9SJohann Duscher * Delete Page Guard for DokuWiki 4e39ccd63SJonny Dee * 5e39ccd63SJonny Dee * This action plugin prevents the deletion of pages by blocking "empty save" 6e39ccd63SJonny Dee * operations on pages whose IDs or file paths match a set of user‑defined 7e39ccd63SJonny Dee * regular expressions. Administrators (superusers) and optionally configured 8e39ccd63SJonny Dee * exempt groups are allowed to delete pages regardless of these patterns. 9e39ccd63SJonny Dee * 10*0da69785SJohann Duscher * @license GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html) - see LICENSE.md 11994617c9SJohann Duscher * @author Johann Duscher <jonny.dee@posteo.net> 12*0da69785SJohann Duscher * @copyright 2025 Johann Duscher 13e39ccd63SJonny Dee */ 14e39ccd63SJonny Dee 15e39ccd63SJonny Deeuse dokuwiki\Extension\ActionPlugin; 16e39ccd63SJonny Deeuse dokuwiki\Extension\Event; 17e39ccd63SJonny Deeuse dokuwiki\Extension\EventHandler; 18e39ccd63SJonny Dee 19e39ccd63SJonny Dee// Protect against direct call 20e39ccd63SJonny Deeif (!defined('DOKU_INC')) die(); 21e39ccd63SJonny Dee 22e39ccd63SJonny Dee/** 23994617c9SJohann Duscher * Class action_plugin_deletepageguard 24e39ccd63SJonny Dee * 25e39ccd63SJonny Dee * Registers a handler on COMMON_WIKIPAGE_SAVE to intercept page save 26e39ccd63SJonny Dee * operations. When a deletion (empty save) is attempted on a protected page 27e39ccd63SJonny Dee * by a non‑admin user, the save is prevented and an error message is shown. 28e39ccd63SJonny Dee */ 29994617c9SJohann Duscherclass action_plugin_deletepageguard extends ActionPlugin { 30e39ccd63SJonny Dee 31e39ccd63SJonny Dee /** 32e39ccd63SJonny Dee * Register the plugin events 33e39ccd63SJonny Dee * 34e39ccd63SJonny Dee * @param EventHandler $controller 35e39ccd63SJonny Dee * @return void 36e39ccd63SJonny Dee */ 37e39ccd63SJonny Dee public function register(EventHandler $controller) { 38e39ccd63SJonny Dee // Run before the page is saved so we can abort the delete 39e39ccd63SJonny Dee $controller->register_hook('COMMON_WIKIPAGE_SAVE', 'BEFORE', $this, 'handle_common_wikipage_save'); 40e39ccd63SJonny Dee } 41e39ccd63SJonny Dee 42e39ccd63SJonny Dee /** 43e39ccd63SJonny Dee * Handler for the COMMON_WIKIPAGE_SAVE event 44e39ccd63SJonny Dee * 45e39ccd63SJonny Dee * This method checks whether the save operation represents a deletion 46e39ccd63SJonny Dee * (i.e. the new content is empty) and whether the page matches one of 47e39ccd63SJonny Dee * the configured regular expressions. If so, and the current user is 48e39ccd63SJonny Dee * neither an administrator nor in one of the exempt groups, the 49e39ccd63SJonny Dee * deletion is prevented. 50e39ccd63SJonny Dee * 51e39ccd63SJonny Dee * @param Event $event The event object 52e39ccd63SJonny Dee * @param mixed $param Additional parameters (unused) 53e39ccd63SJonny Dee * @return void 54e39ccd63SJonny Dee */ 55e39ccd63SJonny Dee public function handle_common_wikipage_save(Event $event, $param) { 56e39ccd63SJonny Dee global $USERINFO, $conf; 57e39ccd63SJonny Dee 58e39ccd63SJonny Dee // Only take action when the event is preventable 59e39ccd63SJonny Dee if (!$event->canPreventDefault) { 60e39ccd63SJonny Dee return; 61e39ccd63SJonny Dee } 62e39ccd63SJonny Dee 63e39ccd63SJonny Dee // Allow administrators to delete pages 64e39ccd63SJonny Dee if (function_exists('auth_isadmin') && auth_isadmin()) { 65e39ccd63SJonny Dee return; 66e39ccd63SJonny Dee } 67e39ccd63SJonny Dee 68e39ccd63SJonny Dee // Check for exempt groups configuration 69e39ccd63SJonny Dee $exemptSetting = (string)$this->getConf('exempt_groups'); 70e39ccd63SJonny Dee $exemptGroups = array_filter(array_map('trim', explode(',', $exemptSetting))); 71e39ccd63SJonny Dee 72e39ccd63SJonny Dee if (!empty($exemptGroups) && isset($USERINFO['grps']) && is_array($USERINFO['grps'])) { 73e39ccd63SJonny Dee foreach ($USERINFO['grps'] as $group) { 74e39ccd63SJonny Dee if (in_array($group, $exemptGroups, true)) { 75e39ccd63SJonny Dee // User is in an exempt group, allow deletion 76e39ccd63SJonny Dee return; 77e39ccd63SJonny Dee } 78e39ccd63SJonny Dee } 79e39ccd63SJonny Dee } 80e39ccd63SJonny Dee 81e39ccd63SJonny Dee // Determine if the save represents a deletion 82e39ccd63SJonny Dee $newContent = isset($event->data['newContent']) ? $event->data['newContent'] : ''; 83e39ccd63SJonny Dee $trimMode = (bool)$this->getConf('trim_mode'); 84e39ccd63SJonny Dee $isEmpty = $trimMode ? trim($newContent) === '' : $newContent === ''; 85e39ccd63SJonny Dee 86e39ccd63SJonny Dee if (!$isEmpty) { 87e39ccd63SJonny Dee // Not empty – normal edit, allow saving 88e39ccd63SJonny Dee return; 89e39ccd63SJonny Dee } 90e39ccd63SJonny Dee 91e39ccd63SJonny Dee // Determine the matching target: page ID or relative file path 92e39ccd63SJonny Dee $matchTarget = $this->getConf('match_target') === 'filepath' ? 93e39ccd63SJonny Dee $this->getRelativeFilePath($event->data['file'], $conf['datadir']) : 94e39ccd63SJonny Dee $event->data['id']; 95e39ccd63SJonny Dee 96e39ccd63SJonny Dee // Retrieve regex patterns from configuration 97e39ccd63SJonny Dee $patternsSetting = (string)$this->getConf('patterns'); 98e39ccd63SJonny Dee $patternLines = preg_split('/\R+/', $patternsSetting, -1, PREG_SPLIT_NO_EMPTY); 99e39ccd63SJonny Dee 100e39ccd63SJonny Dee foreach ($patternLines as $rawPattern) { 101e39ccd63SJonny Dee $pattern = trim($rawPattern); 102e39ccd63SJonny Dee if ($pattern === '') { 103e39ccd63SJonny Dee continue; 104e39ccd63SJonny Dee } 105*0da69785SJohann Duscher 106*0da69785SJohann Duscher // Validate and secure the regex pattern 107*0da69785SJohann Duscher if (!$this->validateRegexPattern($pattern)) { 108e39ccd63SJonny Dee continue; 109e39ccd63SJonny Dee } 110*0da69785SJohann Duscher 111*0da69785SJohann Duscher // Apply the regex with timeout protection 112*0da69785SJohann Duscher if ($this->matchesPattern($pattern, $matchTarget)) { 113e39ccd63SJonny Dee // Match found – prevent deletion 114e39ccd63SJonny Dee $event->preventDefault(); 115e39ccd63SJonny Dee $event->stopPropagation(); 116e39ccd63SJonny Dee msg($this->getLang('deny_msg'), -1); 117e39ccd63SJonny Dee return; 118e39ccd63SJonny Dee } 119e39ccd63SJonny Dee } 120e39ccd63SJonny Dee } 121e39ccd63SJonny Dee 122e39ccd63SJonny Dee /** 123e39ccd63SJonny Dee * Convert an absolute file path into a relative one below the data directory 124e39ccd63SJonny Dee * 125e39ccd63SJonny Dee * The COMMON_WIKIPAGE_SAVE event provides the absolute file path. When 126e39ccd63SJonny Dee * matching against the file path, we want a path relative to the base 127e39ccd63SJonny Dee * pages directory so users can write concise regular expressions. 128e39ccd63SJonny Dee * 129e39ccd63SJonny Dee * @param string $fullPath Absolute path to the file 130e39ccd63SJonny Dee * @param string $dataDir Base data directory (usually $conf['datadir']) 131e39ccd63SJonny Dee * @return string Relative file path 132e39ccd63SJonny Dee */ 133e39ccd63SJonny Dee protected function getRelativeFilePath($fullPath, $dataDir) { 134e39ccd63SJonny Dee $base = rtrim($dataDir, '/'); 135e39ccd63SJonny Dee // DokuWiki stores pages in $datadir/pages 136e39ccd63SJonny Dee $pagesDir = $base . '/pages/'; 137e39ccd63SJonny Dee if (strpos($fullPath, $pagesDir) === 0) { 138e39ccd63SJonny Dee return substr($fullPath, strlen($pagesDir)); 139e39ccd63SJonny Dee } 140e39ccd63SJonny Dee // Fallback: attempt to strip base datadir 141e39ccd63SJonny Dee if (strpos($fullPath, $base . '/') === 0) { 142e39ccd63SJonny Dee return substr($fullPath, strlen($base) + 1); 143e39ccd63SJonny Dee } 144e39ccd63SJonny Dee return $fullPath; 145e39ccd63SJonny Dee } 146*0da69785SJohann Duscher 147*0da69785SJohann Duscher /** 148*0da69785SJohann Duscher * Validate a regular expression pattern for security and correctness 149*0da69785SJohann Duscher * 150*0da69785SJohann Duscher * Performs basic validation to prevent ReDoS attacks and ensure the 151*0da69785SJohann Duscher * pattern is syntactically correct. Logs warnings for invalid patterns. 152*0da69785SJohann Duscher * 153*0da69785SJohann Duscher * @param string $pattern The regex pattern to validate 154*0da69785SJohann Duscher * @return bool True if the pattern is valid and safe, false otherwise 155*0da69785SJohann Duscher */ 156*0da69785SJohann Duscher protected function validateRegexPattern($pattern) { 157*0da69785SJohann Duscher // Check for obviously malicious patterns (basic ReDoS protection) 158*0da69785SJohann Duscher if (preg_match('/(\(.*\).*\+.*\(.*\).*\+)|(\(.*\).*\*.*\(.*\).*\*)/', $pattern)) { 159*0da69785SJohann Duscher // Pattern looks like it could cause catastrophic backtracking 160*0da69785SJohann Duscher return false; 161*0da69785SJohann Duscher } 162*0da69785SJohann Duscher 163*0da69785SJohann Duscher // Limit pattern length to prevent extremely complex patterns 164*0da69785SJohann Duscher if (strlen($pattern) > 1000) { 165*0da69785SJohann Duscher return false; 166*0da69785SJohann Duscher } 167*0da69785SJohann Duscher 168*0da69785SJohann Duscher // Test if the pattern is syntactically valid 169*0da69785SJohann Duscher $test = @preg_match('/' . str_replace('/', '\/', $pattern) . '/u', ''); 170*0da69785SJohann Duscher if ($test === false) { 171*0da69785SJohann Duscher return false; 172*0da69785SJohann Duscher } 173*0da69785SJohann Duscher 174*0da69785SJohann Duscher return true; 175*0da69785SJohann Duscher } 176*0da69785SJohann Duscher 177*0da69785SJohann Duscher /** 178*0da69785SJohann Duscher * Safely match a pattern against a target string with timeout protection 179*0da69785SJohann Duscher * 180*0da69785SJohann Duscher * Applies the regex pattern with error handling and basic timeout protection 181*0da69785SJohann Duscher * to prevent ReDoS attacks. 182*0da69785SJohann Duscher * 183*0da69785SJohann Duscher * @param string $pattern The validated regex pattern 184*0da69785SJohann Duscher * @param string $target The string to match against 185*0da69785SJohann Duscher * @return bool True if the pattern matches, false otherwise 186*0da69785SJohann Duscher */ 187*0da69785SJohann Duscher protected function matchesPattern($pattern, $target) { 188*0da69785SJohann Duscher // Escape forward slashes in pattern to use with / delimiters 189*0da69785SJohann Duscher $escapedPattern = '/' . str_replace('/', '\/', $pattern) . '/u'; 190*0da69785SJohann Duscher 191*0da69785SJohann Duscher // Set a reasonable time limit for regex execution (basic ReDoS protection) 192*0da69785SJohann Duscher $oldTimeLimit = ini_get('max_execution_time'); 193*0da69785SJohann Duscher if ($oldTimeLimit > 5) { 194*0da69785SJohann Duscher @set_time_limit(5); 195*0da69785SJohann Duscher } 196*0da69785SJohann Duscher 197*0da69785SJohann Duscher $result = @preg_match($escapedPattern, $target); 198*0da69785SJohann Duscher 199*0da69785SJohann Duscher // Restore original time limit 200*0da69785SJohann Duscher if ($oldTimeLimit > 5) { 201*0da69785SJohann Duscher @set_time_limit($oldTimeLimit); 202*0da69785SJohann Duscher } 203*0da69785SJohann Duscher 204*0da69785SJohann Duscher return $result === 1; 205*0da69785SJohann Duscher } 206e39ccd63SJonny Dee} 207