Skip to content

Commit

Permalink
extract PrefixList from the document
Browse files Browse the repository at this point in the history
  • Loading branch information
fumieval committed Jun 22, 2023
1 parent 60c924f commit b3a778e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/Network/Wai/SAML2/C14N.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ module Network.Wai.SAML2.C14N (
--------------------------------------------------------------------------------

import qualified Data.ByteString as BS
import Data.Text (Text)
import qualified Data.Text.Encoding as T

import Foreign.C.Types

Expand All @@ -22,8 +24,8 @@ import Text.XML.C14N
--------------------------------------------------------------------------------

-- | 'canonicalise' @xml@ produces a canonical representation of @xml@.
canonicalise :: BS.ByteString -> IO BS.ByteString
canonicalise xml = c14n c14nOpts c14n_exclusive_1_0 ["xs"] False Nothing xml
canonicalise :: [Text] -> BS.ByteString -> IO BS.ByteString
canonicalise prefixList xml = c14n c14nOpts c14n_exclusive_1_0 (map T.encodeUtf8 prefixList) False Nothing xml

-- | The options we want to use for canonicalisation of XML documents.
c14nOpts :: [CInt]
Expand Down
10 changes: 10 additions & 0 deletions src/Network/Wai/SAML2/Response.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module Network.Wai.SAML2.Response (
Response(..),
removeSignature,
extractSignedInfo,
extractPrefixList,

-- * Re-exports
module Network.Wai.SAML2.StatusCode,
Expand Down Expand Up @@ -133,6 +134,15 @@ extractSignedInfo cursor = do
) >>= nodes
pure signedInfo

extractPrefixList :: Cursor -> [T.Text]
extractPrefixList cursor = concatMap T.words
$ concatMap (attribute "PrefixList")
$ cursor
$/ element (dsName "Reference")
&/ element (dsName "Transforms")
&/ element (dsName "Transform")
&/ element (ecName "InclusiveNamespaces")

--------------------------------------------------------------------------------

-- Reference [StatusResponseType]
Expand Down
5 changes: 3 additions & 2 deletions src/Network/Wai/SAML2/Validation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,9 @@ validateSAMLResponse cfg responseXmlDoc samlResponse now = do
let signedInfoXml = XML.renderLBS def doc

-- canonicalise the textual representation of the SignedInfo element
let prefixList = extractPrefixList (XML.fromDocument doc)
signedInfoCanonResult <- liftIO $ try $
canonicalise (LBS.toStrict signedInfoXml)
canonicalise prefixList (LBS.toStrict signedInfoXml)

normalisedSignedInfo <- case signedInfoCanonResult of
Left err -> throwError $ CanonicalisationFailure err
Expand Down Expand Up @@ -161,7 +162,7 @@ validateSAMLResponse cfg responseXmlDoc samlResponse now = do

-- then render the resulting document and canonicalise it
let renderedXml = XML.renderLBS def docMinusSignature
refCanonResult <- liftIO $ try $ canonicalise (LBS.toStrict renderedXml)
refCanonResult <- liftIO $ try $ canonicalise prefixList (LBS.toStrict renderedXml)

normalised <- case refCanonResult of
Left err -> throwError $ CanonicalisationFailure err
Expand Down
7 changes: 7 additions & 0 deletions src/Network/Wai/SAML2/XML.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module Network.Wai.SAML2.XML (
xencName,
dsName,
mdName,
ecName,

-- * Utility functions
toMaybeText,
Expand Down Expand Up @@ -67,6 +68,12 @@ mdName name =
Name name (Just "urn:oasis:names:tc:SAML:2.0:metadata") (Just "md")


-- | 'ecName' @name@ constructs a 'Name' for @name@ in the
-- http://www.w3.org/2001/10/xml-exc-c14n# namespace.
ecName :: T.Text -> Name
ecName name =
Name name (Just "http://www.w3.org/2001/10/xml-exc-c14n#") (Just "ec")

-- | 'toMaybeText' @xs@ returns 'Nothing' if @xs@ is the empty list, or
-- the result of concatenating @xs@ wrapped in 'Just' otherwise.
toMaybeText :: [T.Text] -> Maybe T.Text
Expand Down

0 comments on commit b3a778e

Please sign in to comment.