diff --git a/src/dk.nita.saml20/dk.nita.saml20/Config/SAML20FederationConfig.cs b/src/dk.nita.saml20/dk.nita.saml20/Config/SAML20FederationConfig.cs index 59797ac..dc9a025 100644 --- a/src/dk.nita.saml20/dk.nita.saml20/Config/SAML20FederationConfig.cs +++ b/src/dk.nita.saml20/dk.nita.saml20/Config/SAML20FederationConfig.cs @@ -479,6 +479,8 @@ private void HandleDeleteIdp(string filename) private void HandleUpdateIdp(string filename) { var metadataDoc = ParseFile(filename); + if (metadataDoc == null) + return; // Not a SAML 2.0 metadata file - ignore it instead of failing. // First we try and find the endpoint based on the entity id in the metadata var endp = FindEndPoint(metadataDoc.EntityId); if (endp == null && _fileToEntity.ContainsKey(filename)) @@ -501,6 +503,8 @@ private void HandleCreateIdp(string filename) if (!_fileToEntity.ContainsKey(filename)) { var metadataDoc = ParseFile(filename); + if (metadataDoc == null) + return; // Not a SAML 2.0 metadata file (e.g. a README placed in the metadata directory) - ignore it instead of failing. var endp = FindEndPoint(metadataDoc.EntityId); if (endp == null) // If the endpoint does not exist, create it. { diff --git a/src/dk.nita.saml20/dk.nita.saml20/Properties/Resources.Designer.cs b/src/dk.nita.saml20/dk.nita.saml20/Properties/Resources.Designer.cs index 773e8db..3d26388 100644 --- a/src/dk.nita.saml20/dk.nita.saml20/Properties/Resources.Designer.cs +++ b/src/dk.nita.saml20/dk.nita.saml20/Properties/Resources.Designer.cs @@ -755,7 +755,7 @@ internal static string MetadataLocationNotFound { } /// - /// Looks up a localized string similar to Metadata not found for idp endpoint with id {0}. + /// Looks up a localized string similar to No metadata was found for the configured IDPEndpoint with id "{0}". Verify that the id matches the entityID of a metadata file in the configured metadata directory (a misspelled id is the most common cause). /// internal static string MetadataNotFound { get { diff --git a/src/dk.nita.saml20/dk.nita.saml20/Properties/Resources.resx b/src/dk.nita.saml20/dk.nita.saml20/Properties/Resources.resx index 6a2b119..a84c466 100644 --- a/src/dk.nita.saml20/dk.nita.saml20/Properties/Resources.resx +++ b/src/dk.nita.saml20/dk.nita.saml20/Properties/Resources.resx @@ -334,7 +334,7 @@ AppID={1} The specified metadata directory "{0}" could not be located. - Metadata not found for idp endpoint with id {0} + No metadata was found for the configured IDPEndpoint with id "{0}". Verify that the id matches the entityID of a metadata file in the configured metadata directory (a misspelled id is the most common cause). Xml error: attribute {0} of element {1}: {2} must have a value. diff --git a/src/dk.nita.saml20/dk.nita.saml20/Protocol/Saml20AbstractEndpointHandler.cs b/src/dk.nita.saml20/dk.nita.saml20/Protocol/Saml20AbstractEndpointHandler.cs index 9e65c3c..7e16f56 100644 --- a/src/dk.nita.saml20/dk.nita.saml20/Protocol/Saml20AbstractEndpointHandler.cs +++ b/src/dk.nita.saml20/dk.nita.saml20/Protocol/Saml20AbstractEndpointHandler.cs @@ -9,6 +9,7 @@ using dk.nita.saml20.Properties; using dk.nita.saml20.Schema.Protocol; using dk.nita.saml20.Utils; +using Saml2.Properties; using Trace = dk.nita.saml20.Utils.Trace; namespace dk.nita.saml20.protocol @@ -142,6 +143,11 @@ public IDPEndPoint RetrieveIDP(HttpContext context) var defaultIdp = config.Endpoints.IDPEndPoints.Find(idp => idp.Default); if (defaultIdp != null) { + // A configured endpoint has no metadata when no metadata file matches its id (typically a misspelled + // id). Fail with a clear message instead of the opaque NullReferenceException that follows downstream. + if (defaultIdp.metadata == null) + throw new Saml20Exception(string.Format(Resources.MetadataNotFound, defaultIdp.Id)); + if (Trace.ShouldTrace(TraceEventType.Information)) Trace.TraceData(TraceEventType.Information, "Using IdP marked as default: " + defaultIdp.Id);