Skip to content

Commit 0928e45

Browse files
committed
Warns users about non-ghcup channels
1 parent c07d28e commit 0928e45

File tree

1 file changed

+66
-2
lines changed

1 file changed

+66
-2
lines changed

lib/GHCup/Download.hs

Lines changed: 66 additions & 2 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

@@ -103,7 +105,42 @@ import qualified Data.Yaml.Aeson as Y
103105

104106

105107

108+
officialPrefixes :: [String]
109+
officialPrefixes =
110+
[ "https://raw.githubusercontent.com/haskell/ghcup-metadata/"
111+
, "https://mirror.sjtu.edu.cn/ghcup/yaml/haskell/ghcup-metadata/"
112+
]
113+
106114
-- | Downloads the download information! But only if we need to ;P
115+
isDefaultURL :: NewURLSource -> Bool
116+
isDefaultURL NewGHCupURL = True
117+
isDefaultURL NewStackSetupURL = True
118+
isDefaultURL (NewChannelAlias StackChannel) = True
119+
isDefaultURL (NewChannelAlias _) = True
120+
isDefaultURL (NewGHCupInfo _) = False -- Custom GHCupInfo is not a default source
121+
isDefaultURL (NewSetupInfo _) = False -- Custom SetupInfo is not a default source
122+
isDefaultURL (NewURI uri) = show uri `elem` defaultURLs
123+
124+
-- List of default/known URLs
125+
defaultURLs :: [String]
126+
defaultURLs =
127+
[ "https://raw.githubusercontent.com/haskell/ghcup-metadata/master/ghcup-0.0.9.yaml"
128+
, "https://raw.githubusercontent.com/commercialhaskell/stackage-content/master/stack/stack-setup-2.yaml"
129+
, "https://raw.githubusercontent.com/haskell/ghcup-metadata/master/ghcup-cross-0.0.9.yaml"
130+
, "https://raw.githubusercontent.com/haskell/ghcup-metadata/master/ghcup-prereleases-0.0.9.yaml"
131+
, "https://raw.githubusercontent.com/haskell/ghcup-metadata/master/ghcup-vanilla-0.0.9.yaml"
132+
]
133+
134+
-- Extract URL string from NewURLSource if possible, for checking
135+
getUrlStringFromSource :: NewURLSource -> Maybe String
136+
getUrlStringFromSource NewGHCupURL = Just $ show ghcupURL
137+
getUrlStringFromSource NewStackSetupURL = Just $ show stackSetupURL
138+
getUrlStringFromSource (NewChannelAlias c) = Just $ show $ channelURL c
139+
getUrlStringFromSource (NewGHCupInfo _) = Nothing
140+
getUrlStringFromSource (NewSetupInfo _) = Nothing
141+
getUrlStringFromSource (NewURI uri) = Just $ show uri
142+
143+
-- Modified getDownloadsF to include URL prefix check
107144
getDownloadsF :: ( FromJSONKey Tool
108145
, FromJSONKey Version
109146
, FromJSON VersionInfo
@@ -124,6 +161,22 @@ getDownloadsF :: ( FromJSONKey Tool
124161
GHCupInfo
125162
getDownloadsF pfreq@(PlatformRequest arch plat _) = do
126163
Settings { urlSource } <- lift getSettings
164+
165+
-- Check for custom URL sources that aren't in the default list
166+
forM_ urlSource $ \src ->
167+
case src of
168+
NewURI uri -> do
169+
let url = show uri
170+
when (not (url `elem` defaultURLs) &&
171+
not (any (`isPrefixOf` url) officialPrefixes) ||
172+
url == "https://ghc.gitlab.haskell.org/ghcup-metadata/ghcup-nightlies-0.0.7.yaml") $
173+
logWarn $ "The URL " <> T.pack url <> " is not an official GHCup metadata source and may not be maintained or QA'd by GHCup."
174+
NewGHCupInfo _ ->
175+
logWarn "Using custom GHCupInfo which is not an official GHCup metadata source"
176+
NewSetupInfo _ ->
177+
logWarn "Using custom SetupInfo which is not an official GHCup metadata source"
178+
_ -> pure ()
179+
127180
infos <- liftE $ mapM dl' urlSource
128181
keys <- if any isRight infos
129182
then liftE . reThrowAll @_ @_ @'[StackPlatformDetectError] StackPlatformDetectError $ getStackPlatformKey pfreq
@@ -133,7 +186,17 @@ getDownloadsF pfreq@(PlatformRequest arch plat _) = do
133186
Right si -> pure $ fromStackSetupInfo si keys
134187
mergeGhcupInfo ghcupInfos
135188
where
136-
189+
-- Default URLs that are known to be official
190+
defaultURLs :: [String]
191+
defaultURLs =
192+
[ "https://raw.githubusercontent.com/haskell/ghcup-metadata/master/ghcup-0.0.9.yaml"
193+
, "https://raw.githubusercontent.com/commercialhaskell/stackage-content/master/stack/stack-setup-2.yaml"
194+
, "https://raw.githubusercontent.com/haskell/ghcup-metadata/master/ghcup-cross-0.0.9.yaml"
195+
, "https://raw.githubusercontent.com/haskell/ghcup-metadata/master/ghcup-prereleases-0.0.9.yaml"
196+
, "https://raw.githubusercontent.com/haskell/ghcup-metadata/master/ghcup-vanilla-0.0.9.yaml"
197+
, "https://ghc.gitlab.haskell.org/ghcup-metadata/ghcup-nightlies-0.0.7.yaml" -- Nightly channel mentioned in config example
198+
]
199+
137200
dl' :: ( FromJSONKey Tool
138201
, FromJSONKey Version
139202
, FromJSON VersionInfo
@@ -164,6 +227,7 @@ getDownloadsF pfreq@(PlatformRequest arch plat _) = do
164227
Right <$> decodeMetadata @Stack.SetupInfo base)
165228
$ fmap Left (decodeMetadata @GHCupInfo base >>= \gI -> warnOnMetadataUpdate uri gI >> pure gI)
166229

230+
167231
fromStackSetupInfo :: MonadThrow m
168232
=> Stack.SetupInfo
169233
-> [String]
@@ -890,4 +954,4 @@ applyMirrors (DM ms) uri@(URI { uriAuthority = Just (Authority { authorityHost =
890954
}
891955
Just (DownloadMirror auth Nothing) ->
892956
uri { uriAuthority = Just auth }
893-
applyMirrors _ uri = uri
957+
applyMirrors _ uri = uri

0 commit comments

Comments
 (0)