diff options
author | Jaime Perez Crespo <jaime.perez@uninett.no> | 2016-04-20 10:50:50 +0200 |
---|---|---|
committer | Jaime Perez Crespo <jaime.perez@uninett.no> | 2016-04-20 10:50:50 +0200 |
commit | 86a99f91bc0644d804b2503f6dd553ea16568551 (patch) | |
tree | b5f76d91491f0cf11da4cc339fa94f0489474322 /lib/SimpleSAML | |
parent | c3950c6bc896ad8ddddd20edd25322fbb2f8a94a (diff) | |
download | simplesamlphp-86a99f91bc0644d804b2503f6dd553ea16568551.zip simplesamlphp-86a99f91bc0644d804b2503f6dd553ea16568551.tar.gz simplesamlphp-86a99f91bc0644d804b2503f6dd553ea16568551.tar.bz2 |
Start using the new configuration exceptions, handling error situations that before led to blank pages or even worse.
Diffstat (limited to 'lib/SimpleSAML')
-rw-r--r-- | lib/SimpleSAML/Configuration.php | 49 | ||||
-rw-r--r-- | lib/SimpleSAML/Utils/HTTP.php | 14 |
2 files changed, 52 insertions, 11 deletions
diff --git a/lib/SimpleSAML/Configuration.php b/lib/SimpleSAML/Configuration.php index 2a52bf1..62f25bf 100644 --- a/lib/SimpleSAML/Configuration.php +++ b/lib/SimpleSAML/Configuration.php @@ -113,13 +113,32 @@ class SimpleSAML_Configuration // the file initializes a variable named '$config' require($filename); + // check that $config exists + if (!isset($config)) { + throw new \SimpleSAML\Error\ConfigurationError( + '$config is not defined in the configuration file.', + $filename + ); + } + // check that $config is initialized to an array if (!is_array($config)) { - throw new Exception('Invalid configuration file: '.$filename); + throw new \SimpleSAML\Error\ConfigurationError( + '$config is not an array.', + $filename + ); + } + + // check that $config is not empty + if (empty($config)) { + throw new \SimpleSAML\Error\ConfigurationError( + '$config is empty.', + $filename + ); } } elseif ($required) { // file does not exist, but is required - throw new Exception('Missing configuration file: '.$filename); + throw new \SimpleSAML\Error\ConfigurationError('Missing configuration file', $filename); } else { // file does not exist, but is optional, so return an empty configuration object without saving it $cfg = new SimpleSAML_Configuration(array(), $filename); @@ -258,10 +277,17 @@ class SimpleSAML_Configuration } if ($instancename === 'simplesaml') { - return self::getConfig(); + try { + return self::getConfig(); + } catch (SimpleSAML\Error\ConfigurationError $e) { + throw \SimpleSAML\Error\CriticalConfigurationError::fromException($e); + } + } - throw new Exception('Configuration with name '.$instancename.' is not initialized.'); + throw new \SimpleSAML\Error\CriticalConfigurationError( + 'Configuration with name '.$instancename.' is not initialized.' + ); } @@ -407,7 +433,7 @@ class SimpleSAML_Configuration * * @return string The absolute path relative to the root of the website. * - * @throws SimpleSAML_Error_Exception If the format of 'baseurlpath' is incorrect. + * @throws SimpleSAML\Error\CriticalConfigurationError If the format of 'baseurlpath' is incorrect. */ public function getBaseURL() { @@ -428,11 +454,18 @@ class SimpleSAML_Configuration // local path only return $matches[1]; } else { - // invalid format - throw new SimpleSAML_Error_Exception( + /* + * Invalid 'baseurlpath'. We cannot recover from this, so throw a critical exception and try to be graceful + * with the configuration. Use a guessed base path instead of the one provided. + */ + $c = $this->toArray(); + $c['baseurlpath'] = SimpleSAML\Utils\HTTP::guessBasePath(); + throw new SimpleSAML\Error\CriticalConfigurationError( 'Incorrect format for option \'baseurlpath\'. Value is: "'. $this->getString('baseurlpath', 'simplesaml/').'". Valid format is in the form'. - ' [(http|https)://(hostname|fqdn)[:port]]/[path/to/simplesaml/].' + ' [(http|https)://(hostname|fqdn)[:port]]/[path/to/simplesaml/].', + $this->filename, + $c ); } } diff --git a/lib/SimpleSAML/Utils/HTTP.php b/lib/SimpleSAML/Utils/HTTP.php index a6097a5..5ebf776 100644 --- a/lib/SimpleSAML/Utils/HTTP.php +++ b/lib/SimpleSAML/Utils/HTTP.php @@ -539,7 +539,7 @@ class HTTP * https://idp.example.org/simplesaml/ * * @return string The absolute base URL for the SimpleSAMLphp installation. - * @throws \SimpleSAML_Error_Exception If 'baseurlpath' has an invalid format. + * @throws \SimpleSAML\Error\CriticalConfigurationError If 'baseurlpath' has an invalid format. * * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> */ @@ -567,9 +567,17 @@ class HTTP return $protocol.$hostname.$port.$path; } else { - throw new \SimpleSAML_Error_Exception( + /* + * Invalid 'baseurlpath'. We cannot recover from this, so throw a critical exception and try to be graceful + * with the configuration. Use a guessed base path instead of the one provided. + */ + $c = $globalConfig->toArray(); + $c['baseurlpath'] = self::guessBasePath(); + throw new \SimpleSAML\Error\CriticalConfigurationError( 'Invalid value for \'baseurlpath\' in config.php. Valid format is in the form: '. - '[(http|https)://(hostname|fqdn)[:port]]/[path/to/simplesaml/]. It must end with a \'/\'.' + '[(http|https)://(hostname|fqdn)[:port]]/[path/to/simplesaml/]. It must end with a \'/\'.', + null, + $c ); } } |