Skip to content

Commit

Permalink
MDL-36901: Remove system paths from exceptions
Browse files Browse the repository at this point in the history
Replaces any server paths, like dataroot, with a token
in exception messages and debug info.
  • Loading branch information
polothy authored and stronk7 committed Mar 3, 2013
1 parent 3ecc63e commit 8d220cb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/setuplib.php
Expand Up @@ -526,6 +526,22 @@ function get_exception_info($ex) {
$debuginfo .= PHP_EOL.'$a contents: '.print_r($a, true);
}

// Remove some absolute paths from message and debugging info.
$searches = array();
$replaces = array();
$cfgnames = array('tempdir', 'cachedir', 'themedir',
'langmenucachefile', 'langcacheroot', 'dataroot', 'dirroot');
foreach ($cfgnames as $cfgname) {
if (property_exists($CFG, $cfgname)) {
$searches[] = $CFG->$cfgname;
$replaces[] = "[$cfgname]";
}
}
if (!empty($searches)) {
$message = str_replace($searches, $replaces, $message);
$debuginfo = str_replace($searches, $replaces, $debuginfo);
}

// Be careful, no guarantee weblib.php is loaded.
if (function_exists('clean_text')) {
$message = clean_text($message);
Expand Down
24 changes: 24 additions & 0 deletions lib/tests/setuplib_test.php
Expand Up @@ -118,4 +118,28 @@ public function test_is_web_crawler() {
$this->assertTrue(is_web_crawler(), "$agent should be considered a search engine");
}
}

/**
* Test if get_exception_info() removes file system paths
*/
public function test_exception_info_removes_serverpaths() {
global $CFG;

// This doesn't test them all possible ones, but these are set for unit tests.
$cfgnames = array('dataroot', 'dirroot', 'tempdir', 'cachedir');

$fixture = '';
$expected = '';
foreach ($cfgnames as $cfgname) {
if (!empty($CFG->$cfgname)) {
$fixture .= $CFG->$cfgname.' ';
$expected .= "[$cfgname] ";
}
}
$exception = new moodle_exception('generalexceptionmessage', 'error', '', $fixture, $fixture);
$exceptioninfo = get_exception_info($exception);

$this->assertContains($expected, $exceptioninfo->message, 'Exception message does not contain system paths');
$this->assertContains($expected, $exceptioninfo->debuginfo, 'Exception debug info does not contain system paths');
}
}

0 comments on commit 8d220cb

Please sign in to comment.