Skip to content

Commit e103952

Browse files
authored
Merge pull request #1238 from roberth/redact-authorization-header
servant-client-core: Redact Authorization header
2 parents 78cf24a + 13b21cb commit e103952

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

changelog.d/pull1238

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
synopsis: Redact the authorization header in Show and exceptions
2+
prs: #1238

servant-client-core/servant-client-core.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ test-suite spec
9696
main-is: Spec.hs
9797
other-modules:
9898
Servant.Client.Core.Internal.BaseUrlSpec
99+
Servant.Client.Core.RequestSpec
99100

100101
-- Dependencies inherited from the library. No need to specify bounds.
101102
build-depends:

servant-client-core/src/Servant/Client/Core/Request.hs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,32 @@ data RequestF body path = Request
6464
, requestHeaders :: Seq.Seq Header
6565
, requestHttpVersion :: HttpVersion
6666
, requestMethod :: Method
67-
} deriving (Generic, Typeable, Eq, Show, Functor, Foldable, Traversable)
67+
} deriving (Generic, Typeable, Eq, Functor, Foldable, Traversable)
6868

69+
instance (Show a, Show b) =>
70+
Show (Servant.Client.Core.Request.RequestF a b) where
71+
showsPrec p req
72+
= showParen
73+
(p >= 11)
74+
( showString "Request {requestPath = "
75+
. showsPrec 0 (requestPath req)
76+
. showString ", requestQueryString = "
77+
. showsPrec 0 (requestQueryString req)
78+
. showString ", requestBody = "
79+
. showsPrec 0 (requestBody req)
80+
. showString ", requestAccept = "
81+
. showsPrec 0 (requestAccept req)
82+
. showString ", requestHeaders = "
83+
. showsPrec 0 (redactSensitiveHeader <$> requestHeaders req))
84+
. showString ", requestHttpVersion = "
85+
. showsPrec 0 (requestHttpVersion req)
86+
. showString ", requestMethod = "
87+
. showsPrec 0 (requestMethod req)
88+
. showString "}"
89+
where
90+
redactSensitiveHeader :: Header -> Header
91+
redactSensitiveHeader ("Authorization", _) = ("Authorization", "<REDACTED>")
92+
redactSensitiveHeader h = h
6993
instance Bifunctor RequestF where bimap = bimapDefault
7094
instance Bifoldable RequestF where bifoldMap = bifoldMapDefault
7195
instance Bitraversable RequestF where
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{-# OPTIONS_GHC -fno-warn-orphans #-}
2+
{-# LANGUAGE OverloadedStrings #-}
3+
module Servant.Client.Core.RequestSpec (spec) where
4+
5+
6+
import Prelude ()
7+
import Prelude.Compat
8+
import Control.Monad
9+
import Data.List (isInfixOf)
10+
import Servant.Client.Core.Request
11+
import Test.Hspec
12+
13+
spec :: Spec
14+
spec = do
15+
describe "Request" $ do
16+
describe "show" $ do
17+
it "redacts the authorization header" $ do
18+
let request = void $ defaultRequest { requestHeaders = pure ("authorization", "secret") }
19+
isInfixOf "secret" (show request) `shouldBe` False

0 commit comments

Comments
 (0)