From 9254f119f68f7403fe3c766ef6f6eb741d0b82bf Mon Sep 17 00:00:00 2001 From: Wolfgang Walther Date: Sat, 5 Dec 2020 20:25:55 +0100 Subject: [PATCH] fix: implement robust parsing of boolean config values resolves #1572 --- src/PostgREST/Config.hs | 19 ++++++++++----- test/io-tests/configs/boolean-numeric.config | 7 ++++++ test/io-tests/configs/boolean-string.config | 7 ++++++ .../configs/expected/boolean-numeric.config | 24 +++++++++++++++++++ .../configs/expected/boolean-string.config | 24 +++++++++++++++++++ 5 files changed, 75 insertions(+), 6 deletions(-) create mode 100644 test/io-tests/configs/boolean-numeric.config create mode 100644 test/io-tests/configs/boolean-string.config create mode 100644 test/io-tests/configs/expected/boolean-numeric.config create mode 100644 test/io-tests/configs/expected/boolean-string.config diff --git a/src/PostgREST/Config.hs b/src/PostgREST/Config.hs index 066486bc2..47ea6156c 100644 --- a/src/PostgREST/Config.hs +++ b/src/PostgREST/Config.hs @@ -42,9 +42,10 @@ import Data.Aeson (encode, toJSON) import Data.Either.Combinators (fromRight', whenLeft) import Data.List.NonEmpty (fromList, toList) import Data.Scientific (floatingOrInteger) -import Data.Text (dropEnd, dropWhileEnd, intercalate, - pack, replace, splitOn, strip, - stripPrefix, take, toLower, unpack) +import Data.Text (dropEnd, dropWhileEnd, filter, + intercalate, pack, replace, splitOn, + strip, stripPrefix, take, toLower, + toTitle, unpack) import Data.Text.IO (hPutStrLn) import Data.Version (versionBranch) import Development.GitRev (gitHash) @@ -64,9 +65,10 @@ import PostgREST.Parsers (pRoleClaimKey) import PostgREST.Private.ProxyUri (isMalformedProxyUri) import PostgREST.Types (JSPath, JSPathExp (..), LogLevel (..)) -import Protolude hiding (concat, hPutStrLn, +import Protolude hiding (concat, filter, hPutStrLn, intercalate, null, replace, take, - toList, toLower, toS, (<>)) + toList, toLower, toS, toTitle, + (<>)) import Protolude.Conv (toS) -- | Command line interface options @@ -429,7 +431,12 @@ readAppConfig cfgPath = do coerceBool :: C.Value -> Maybe Bool coerceBool (C.Bool b) = Just b - coerceBool (C.String b) = readMaybe $ toS b + coerceBool (C.String s) = + -- parse all kinds of text: True, true, TRUE, "true", ... + case readMaybe . toS $ toTitle $ filter isAlpha $ toS s of + Just b -> Just b + -- numeric instead? + Nothing -> (> 0) <$> (readMaybe $ toS s :: Maybe Integer) coerceBool _ = Nothing parseRoleClaimKey :: C.Value -> Either Text JSPath diff --git a/test/io-tests/configs/boolean-numeric.config b/test/io-tests/configs/boolean-numeric.config new file mode 100644 index 000000000..6dbe6c88e --- /dev/null +++ b/test/io-tests/configs/boolean-numeric.config @@ -0,0 +1,7 @@ +db-uri = "required" +db-schemas = "required" +db-anon-role = "required" + +db-channel-enabled = "1" +db-prepared-statements = "0" +jwt-secret-is-base64 = "2" diff --git a/test/io-tests/configs/boolean-string.config b/test/io-tests/configs/boolean-string.config new file mode 100644 index 000000000..f7967cd4f --- /dev/null +++ b/test/io-tests/configs/boolean-string.config @@ -0,0 +1,7 @@ +db-uri = "required" +db-schemas = "required" +db-anon-role = "required" + +db-channel-enabled = "true" +db-prepared-statements = "FALSE" +jwt-secret-is-base64 = "\"true\"" diff --git a/test/io-tests/configs/expected/boolean-numeric.config b/test/io-tests/configs/expected/boolean-numeric.config new file mode 100644 index 000000000..d5460e7de --- /dev/null +++ b/test/io-tests/configs/expected/boolean-numeric.config @@ -0,0 +1,24 @@ +db-anon-role = "required" +db-channel = "pgrst" +db-channel-enabled = true +db-extra-search-path = "public" +db-max-rows = "" +db-pool = 10 +db-pool-timeout = 10 +db-pre-request = "" +db-prepared-statements = false +db-root-spec = "" +db-schemas = "required" +db-tx-end = "commit" +db-uri = "required" +jwt-aud = "" +jwt-role-claim-key = ".\"role\"" +jwt-secret = "" +jwt-secret-is-base64 = true +log-level = "error" +openapi-server-proxy-uri = "" +raw-media-types = "" +server-host = "!4" +server-port = 3000 +server-unix-socket = "" +server-unix-socket-mode = "660" diff --git a/test/io-tests/configs/expected/boolean-string.config b/test/io-tests/configs/expected/boolean-string.config new file mode 100644 index 000000000..d5460e7de --- /dev/null +++ b/test/io-tests/configs/expected/boolean-string.config @@ -0,0 +1,24 @@ +db-anon-role = "required" +db-channel = "pgrst" +db-channel-enabled = true +db-extra-search-path = "public" +db-max-rows = "" +db-pool = 10 +db-pool-timeout = 10 +db-pre-request = "" +db-prepared-statements = false +db-root-spec = "" +db-schemas = "required" +db-tx-end = "commit" +db-uri = "required" +jwt-aud = "" +jwt-role-claim-key = ".\"role\"" +jwt-secret = "" +jwt-secret-is-base64 = true +log-level = "error" +openapi-server-proxy-uri = "" +raw-media-types = "" +server-host = "!4" +server-port = 3000 +server-unix-socket = "" +server-unix-socket-mode = "660"