From 6a1986449f277314700e59665987dd22740225ed Mon Sep 17 00:00:00 2001 From: Hydriz Date: Sun, 11 Nov 2012 19:19:54 +0800 Subject: [PATCH] (issue #1) Add a whitelist of commands that a person can run This commit attempts to do a check on whether the command is safe to excute before really running the command. Not a fix, but an initial checkin for development testing purposes. --- SpecialHostStats.php | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/SpecialHostStats.php b/SpecialHostStats.php index 6e6e1ae..2187293 100644 --- a/SpecialHostStats.php +++ b/SpecialHostStats.php @@ -7,6 +7,8 @@ */ class SpecialHostStats extends SpecialPage { + protected $cmdwhitelist; + public function __construct() { parent::__construct( 'HostStats' ); } @@ -14,10 +16,23 @@ public function __construct() { public function execute( $par ) { global $wgHostStatsCommands; $this->setHeaders(); + $this->whitelistedcmds(); + $commands = array(); + foreach ( $wgHostStatsCommands as $cmd ) { + if ( in_array( $cmd, $this->cmdwhitelist ) ) { + array_push( $cmd, $commands ); + } else { + # Reject those unsafe commands and log it to hoststats + wfDebugLog( "hoststats", "Rejected running command '" . + $cmd . "' as it is unsafe, please remove it from " . + "\$wgHostStatsCommands!" ); + continue; + } + } $this->getOutput->setPageTitle( wfMessage( 'hoststats-title' )->escaped() ); $outpage = wfMessage( 'hoststats-intro' )->escaped(); $outpage .= "\n"; - foreach ( $wgHostStatsCommands as $cmd ) { + foreach ( $commands as $cmd ) { $outpage .= '

' . $cmd . '

'; $outpage .= "\n
\n" . $this->query( $cmd ) . "
"; } @@ -28,4 +43,12 @@ protected function query( $query ) { $output = wfShellExec( $query ); return $output; } + + protected function whitelistedcmds() { + $this->cmdwhitelist = array( + 'df', + 'whoami', + 'hostname', + ); + } }