8000 Safe authorization · postgres-haskell/postgres-wire@ded4e60 · GitHub
[go: up one dir, main page]

Skip to content

Commit ded4e60

Browse files
Safe authorization
1 parent 1bb7f44 commit ded4e60

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

src/Database/PostgreSQL/Driver/Connection.hs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import Control.Monad
88
import Data.Traversable
99
import Data.Foldable
1010
import Control.Applicative
11+
import Control.Exception
1112
import Data.IORef
1213
import Data.Monoid
1314
import Control.Concurrent (forkIO, killThread, ThreadId, threadDelay)
@@ -88,11 +89,15 @@ connectWith
8889
-> ServerMessageFilter
8990
-> IO (Either Error Connection)
9091
connectWith settings msgFilter =
91-
createRawConnection settings >>=
92-
either throwErrorInIO (\rawConn ->
93-
authorize rawConn settings >>=
94-
either throwErrorInIO (\params ->
95-
Right <$> buildConnection rawConn params msgFilter))
92+
bracketOnError
93+
(createRawConnection settings)
94+
(either throwErrorInIO rClose)
95+
(either throwErrorInIO performAuth)
96+
where
97+
performAuth rawConn = authorize rawConn settings >>= either
98+
-- We should close connection on an authorization failure
99+
(\e -> rClose rawConn >> throwErrorInIO e)
100+
(\params -> Right <$> buildConnection rawConn params msgFilter)
96101

97102
-- | Authorizes on the server and reads connection parameters.
98103
authorize
@@ -108,8 +113,8 @@ authorize rawConn settings = do
108113
readAuthResponse = do
109114
-- 4096 should be enough for the whole response from a server at
110115
-- the startup phase.
111-
r <- rReceive rawConn 4096
112-
case runDecode decodeAuthResponse r of
116+
resp <- rReceive rawConn 4096
117+
case runDecode decodeAuthResponse resp of
113118
Right (rest, r) -> case r of
114119
AuthenticationOk ->
115120
pure $ parseParameters rest

0 commit comments

Comments
 (0)
0