From d48464c10201d3eee83559fead0f367a0332b358 Mon Sep 17 00:00:00 2001 From: Jezen Thomas Date: Fri, 3 May 2024 18:44:07 +0200 Subject: [PATCH 1/2] Sanitise fake email domain In order to more closely conform to RFC 1035 and actually generate valid fake emails, we only allow alphanumeric characters or hyphens in the text output. See: https://datatracker.ietf.org/doc/html/rfc1035#section-2.3.1 Resolves https://github.com/fakedata-haskell/fakedata/issues/52 --- ChangeLog.md | 4 ++++ fakedata.cabal | 2 +- package.yaml | 2 +- src/Faker/Company.hs | 37 +++++++++++++++++++++---------------- 4 files changed, 27 insertions(+), 18 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index a872916..6c981a4 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -1,5 +1,9 @@ # Changelog for fakedata +## 1.0.4 + +* Only generate fake email domains with alphanumeric characters and hyphens. [#53](https://github.com/fakedata-haskell/fakedata/pull/53) + ## 1.0.3 * [Make the `Fake` type synonym partially applied](https://github.com/fakedata-haskell/fakedata/pull/45) diff --git a/fakedata.cabal b/fakedata.cabal index ea68989..459d97c 100644 --- a/fakedata.cabal +++ b/fakedata.cabal @@ -5,7 +5,7 @@ cabal-version: 1.12 -- see: https://github.com/sol/hpack name: fakedata -version: 1.0.3 +version: 1.0.4 synopsis: Library for producing fake data description: Please see the README on GitHub at category: Random, Fake, FakeData diff --git a/package.yaml b/package.yaml index db1f00f..98cf846 100644 --- a/package.yaml +++ b/package.yaml @@ -1,5 +1,5 @@ name: fakedata -version: 1.0.3 +version: 1.0.4 github: "psibi/fakedata" license: BSD3 author: "Sibi Prabakaran" diff --git a/src/Faker/Company.hs b/src/Faker/Company.hs index f0a7a9c..66fb233 100644 --- a/src/Faker/Company.hs +++ b/src/Faker/Company.hs @@ -17,7 +17,8 @@ module Faker.Company ) where import Data.Monoid ((<>)) -import Data.Text +import Data.Text (Text) +import qualified Data.Text as T import qualified Data.Vector as V import Faker import Faker.Internal @@ -44,7 +45,7 @@ bs = Fake (\settings -> do vec :: V.Vector (V.Vector Text) <- companyBsProvider settings - let item :: V.Vector (IO Text) = V.map (\v -> rvec settings v) vec + let item :: V.Vector (IO Text) = V.map (rvec settings) vec item' :: IO (V.Vector Text) = sequence item items <- item' let txt = V.foldl1' (\a b -> a <> " " <> b) items @@ -72,27 +73,31 @@ domain :: Fake Text domain = do suffix <- F.domainSuffix companyName <- name - pure $ fixupName companyName <> "." <> suffix + pure $ sanitise companyName <> "." <> suffix + where + -- Replaces spaces with hyphens and filters out anything that isn't an + -- alphanumeric character or a hyphen, so the domain has a better chance of + -- conforming to RFC 1035. + -- + -- See: https://datatracker.ietf.org/doc/html/rfc1035#section-2.3.1 + sanitise :: Text -> Text + sanitise = T.filter (\c -> isAlphaNum c || c == '-') . T.replace " " "-" -- | Generates an email like "jappie_klooster@crazychairauction.com" -- -- @since 0.8.1 --- email :: Fake Text email = do humanName <- F.name number <- F.fromRange @Int (0, 999999999) -- reasonable uniqueness domainName <- domain let numText :: Text - numText = pack $ show number - pure $ fixupName humanName <> "-" <> numText <> "@" <> domainName - --- | Ensures the spaces are replaced by "_", --- and no special characters are in the name. --- So "Elizabeth Warder!" becomes "Elizabeth_Warder". --- Any fancy symbols such as "!@#$" etc are filtered out. --- --- @since 0.8.1 --- -fixupName :: Text -> Text -fixupName = Data.Text.filter (\c -> isAlphaNum c || c == '_') . replace " " "_" + numText = T.pack $ show number + pure $ sanitise humanName <> "-" <> numText <> "@" <> domainName + where + -- Ensures the spaces are replaced by "_", + -- and no special characters are in the name. + -- So "Elizabeth Warder!" becomes "Elizabeth_Warder". + -- Any fancy symbols such as "!@#$" etc are filtered out. + sanitise :: Text -> Text + sanitise = T.filter (\c -> isAlphaNum c || c == '_') . T.replace " " "_" From 2c7798a008a4ce1e4c04c324957c7b651c0e0283 Mon Sep 17 00:00:00 2001 From: Jezen Thomas Date: Mon, 5 Aug 2024 11:16:00 +0200 Subject: [PATCH 2/2] Trigger CI