Skip to content

Commit e036f80

Browse files
committed
Warn users about non-ghcup channels
1 parent c07d28e commit e036f80

File tree

1 file changed

+45
-3
lines changed

1 file changed

+45
-3
lines changed

lib/GHCup/Download.hs

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ import qualified Data.Text as T
8989
import qualified Data.Text.IO as T
9090
import qualified Data.Text.Encoding as E
9191
import qualified Data.Yaml.Aeson as Y
92+
import Data.List (isPrefixOf)
93+
import Control.Monad.IO.Class (liftIO)
9294

9395

9496

@@ -102,6 +104,33 @@ import qualified Data.Yaml.Aeson as Y
102104
------------------
103105

104106

107+
formatURI :: URI -> T.Text
108+
formatURI uri =
109+
let scheme = E.decodeUtf8 $ schemeBS $ uriScheme uri
110+
auth = case uriAuthority uri of
111+
Just a -> "//" <> E.decodeUtf8 (hostBS $ authorityHost a)
112+
Nothing -> ""
113+
path = E.decodeUtf8 $ uriPath uri
114+
in scheme <> ":" <> auth <> path
115+
116+
-- | Logic to check if it is an Official Channel
117+
isOfficialURI :: URI -> Bool
118+
isOfficialURI uri = any (`isURIPrefix` uri) officialURIs
119+
where
120+
officialURIs = [isGitHubMetadata]
121+
isGitHubMetadata uri' =
122+
schemeBS (uriScheme uri') == "https" &&
123+
maybe False (\a -> hostBS (authorityHost a) == "raw.githubusercontent.com") (uriAuthority uri') &&
124+
pathStartsWith "/haskell/ghcup-metadata/" (uriPath uri')
125+
pathStartsWith prefix path = prefix `B.isPrefixOf` path
126+
isURIPrefix predicate uri' = predicate uri'
127+
128+
-- | Special case to check for nightlies URL
129+
isNightliesURI :: URI -> Bool
130+
isNightliesURI uri =
131+
schemeBS (uriScheme uri) == "https" &&
132+
maybe False (\a -> hostBS (authorityHost a) == "ghc.gitlab.haskell.org") (uriAuthority uri) &&
133+
uriPath uri == "/ghcup-metadata/ghcup-nightlies-0.0.7.yaml"
105134

106135
-- | Downloads the download information! But only if we need to ;P
107136
getDownloadsF :: ( FromJSONKey Tool
@@ -124,6 +153,17 @@ getDownloadsF :: ( FromJSONKey Tool
124153
GHCupInfo
125154
getDownloadsF pfreq@(PlatformRequest arch plat _) = do
126155
Settings { urlSource } <- lift getSettings
156+
forM_ urlSource $ \src ->
157+
case src of
158+
NewURI uri -> do
159+
when (not (isOfficialURI uri) || isNightliesURI uri) $
160+
logWarn $ "Warning: Using non-official metadata source: " <> formatURI uri <>
161+
"\nThis source is not maintained or verified by the GHCup team."
162+
NewGHCupInfo _ ->
163+
logWarn "Warning: Using custom GHCupInfo data that is not from an official GHCup metadata source"
164+
NewSetupInfo _ ->
165+
logWarn "Warning: Using custom SetupInfo data that is not from an official GHCup metadata source"
166+
_ -> pure ()
127167
infos <- liftE $ mapM dl' urlSource
128168
keys <- if any isRight infos
129169
then liftE . reThrowAll @_ @_ @'[StackPlatformDetectError] StackPlatformDetectError $ getStackPlatformKey pfreq
@@ -133,7 +173,7 @@ getDownloadsF pfreq@(PlatformRequest arch plat _) = do
133173
Right si -> pure $ fromStackSetupInfo si keys
134174
mergeGhcupInfo ghcupInfos
135175
where
136-
176+
137177
dl' :: ( FromJSONKey Tool
138178
, FromJSONKey Version
139179
, FromJSON VersionInfo
@@ -162,7 +202,9 @@ getDownloadsF pfreq@(PlatformRequest arch plat _) = do
162202
catchE @JSONError (\(JSONDecodeError s) -> do
163203
logDebug $ "Couldn't decode " <> T.pack base <> " as GHCupInfo, trying as SetupInfo: " <> T.pack s
164204
Right <$> decodeMetadata @Stack.SetupInfo base)
165-
$ fmap Left (decodeMetadata @GHCupInfo base >>= \gI -> warnOnMetadataUpdate uri gI >> pure gI)
205+
$ fmap Left (decodeMetadata @GHCupInfo base >>= \gI ->
206+
warnOnMetadataUpdate uri gI >> pure gI)
207+
166208

167209
fromStackSetupInfo :: MonadThrow m
168210
=> Stack.SetupInfo
@@ -890,4 +932,4 @@ applyMirrors (DM ms) uri@(URI { uriAuthority = Just (Authority { authorityHost =
890932
}
891933
Just (DownloadMirror auth Nothing) ->
892934
uri { uriAuthority = Just auth }
893-
applyMirrors _ uri = uri
935+
applyMirrors _ uri = uri

0 commit comments

Comments
 (0)