Skip to content

Commit

Permalink
Merge pull request #399 from catalyst/improve-xml-errors
Browse files Browse the repository at this point in the history
Improved xml parsing error handling #398
  • Loading branch information
brendanheywood authored Mar 3, 2020
2 parents 717e915 + ecf4a14 commit 49262f6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
16 changes: 14 additions & 2 deletions classes/admin/setting_idpmetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,20 @@ public function get_idps_data($value) {
*/
private function get_idp_xml_path(idp_data $idp) {
$xml = new DOMDocument();
if (!$xml->loadXML($idp->rawxml)) {
throw new setting_idpmetadata_exception(get_string('idpmetadata_invalid', 'auth_saml2'));

libxml_use_internal_errors(true);

$rawxml = $idp->rawxml;

if (!$xml->loadXML($rawxml)) {
$errors = libxml_get_errors();
$lines = explode("\n", $rawxml);
$msg = '';
foreach ($errors as $error) {
$msg .= "<br>Error ({$error->code}) line $error->line char $error->column: $error->message";
}

throw new setting_idpmetadata_exception(get_string('idpmetadata_invalid', 'auth_saml2') . $msg);
}

$xpath = new DOMXPath($xml);
Expand Down
11 changes: 11 additions & 0 deletions classes/idp_parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,18 @@ public function parse($data) {
return $this->idps;
}

/**
* Does the field *look* like xml, mostly?
*/
public function check_xml($xml) {

$declaration = '<?xml';

$xml = trim($xml);
if (substr($xml, 0, strlen($declaration)) === $declaration) {
return true;
}

libxml_use_internal_errors(true);
if (simplexml_load_string($xml)) {
return true;
Expand Down

0 comments on commit 49262f6

Please sign in to comment.