hasura / pg-client-hs

A low level Haskell library to connect to postgres
Apache License 2.0
25 stars 15 forks source link

Prefer explicit UTF-8 decoding in TH file parser #35

Closed jkachmar closed 3 years ago

jkachmar commented 3 years ago

Pretty much everything useful is in 0fae3bf's commit message:

The built-in 'readFile' function uses the system locale when determining
how to convert the input bytes to a Haskell 'String', which can be a
source of annoying and difficult to track down issues with respect to
character encoding.

Instead, it's safer to make the assumption that UTF-8 is the most
commonly used encoding scheme and reject all non-UTF-8 input as invalid
at compile-time whenever the 'sqlFromFile' splice is invoked.

It works as follows when run locally with echo 'CREATE DATABASE;' > test.sql and /bin/sh present:

Prelude Database.PG.Query> :set -XTemplateHaskell
Prelude Database.PG.Query>
Prelude Database.PG.Query> $(sqlFromFile "./test.sql")
Query {getQueryText = "CREATE DATABASE;\n"}
Prelude Database.PG.Query>
Prelude Database.PG.Query> $(sqlFromFile "/bin/sh")

<interactive>:3:1: error:
    • Exception when trying to run compile-time code:
        Cannot decode byte '\xdb': Data.Text.Internal.Encoding.decodeUtf8: Invalid UTF-8 stream
      Code: sqlFromFile "/bin/sh"
    • In the untyped splice: $(sqlFromFile "/bin/sh")
jkachmar commented 3 years ago

e7add85, 9fcc82f, and 04aed97 are unrelated code cleanup; let me know if you would prefer that I remove this as out-of-scope.