MDL-36901: Remove system paths from exceptions

Replaces any server paths, like dataroot, with a token
in exception messages and debug info.

Conflicts:
	lib/tests/setuplib_test.php
This commit is contained in:
Mark Nielsen 2013-03-03 23:52:09 +01:00 committed by Eloy Lafuente (stronk7)
parent c7eea4e585
commit 2c7cdbb3b0
2 changed files with 40 additions and 0 deletions

View file

@ -526,6 +526,22 @@ function get_exception_info($ex) {
$debuginfo .= PHP_EOL.'$a contents: '.print_r($a, true); $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. // Be careful, no guarantee weblib.php is loaded.
if (function_exists('clean_text')) { if (function_exists('clean_text')) {
$message = clean_text($message); $message = clean_text($message);

View file

@ -71,4 +71,28 @@ class core_setuplib_testcase extends basic_testcase {
$this->assertEquals($CFG->wwwroot . '/lib/tests/setuplib_test.php', $this->assertEquals($CFG->wwwroot . '/lib/tests/setuplib_test.php',
get_docs_url('%%WWWROOT%%/lib/tests/setuplib_test.php')); get_docs_url('%%WWWROOT%%/lib/tests/setuplib_test.php'));
} }
/**
* 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');
}
} }