Skip to content

Commit c3bae7e

Browse files
committed
Warning by introducing officialprefixes in lib/GHCup/Download.hs
1 parent c07d28e commit c3bae7e

File tree

3 files changed

+73
-2
lines changed

3 files changed

+73
-2
lines changed

ghcup.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ common app-common-depends
123123
, utf8-string ^>=1.0
124124
, vector >=0.12 && <0.14
125125
, versions >=6.0.5 && <6.1
126+
, hslogger >= 1.2 && < 1.4
126127

127128
if flag(yaml-streamly)
128129
build-depends:

lib/GHCup.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -677,4 +677,3 @@ rmTmp = do
677677
logDebug $ "rm -rf " <> T.pack (fromGHCupPath f)
678678
rmPathForcibly f
679679

680-

lib/GHCup/Download.hs

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ 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)
94+
import System.Log.Logger (warningM)
9295

9396

9497

@@ -103,7 +106,47 @@ import qualified Data.Yaml.Aeson as Y
103106

104107

105108

109+
officialPrefixes :: [String]
110+
officialPrefixes =
111+
[ "https://raw.githubusercontent.com/haskell/ghcup-metadata/"
112+
, "https://mirror.sjtu.edu.cn/ghcup/yaml/haskell/ghcup-metadata/" -- Add your mirror pattern
113+
]
114+
115+
checkUrlPrefix :: String -> IO ()
116+
checkUrlPrefix url
117+
| any (`isPrefixOf` url) officialPrefixes = return ()
118+
| otherwise = liftIO $ warningM "ghcup" $ "Warning: The URL " ++ url ++ " is not an official GHCup metadata source and may not be maintained or QA'd by GHCup."
119+
106120
-- | Downloads the download information! But only if we need to ;P
121+
isDefaultURL :: NewURLSource -> Bool
122+
isDefaultURL NewGHCupURL = True
123+
isDefaultURL NewStackSetupURL = True
124+
isDefaultURL (NewChannelAlias StackChannel) = True
125+
isDefaultURL (NewChannelAlias _) = True
126+
isDefaultURL (NewGHCupInfo _) = False -- Custom GHCupInfo is not a default source
127+
isDefaultURL (NewSetupInfo _) = False -- Custom SetupInfo is not a default source
128+
isDefaultURL (NewURI uri) = show uri `elem` defaultURLs
129+
130+
-- List of default/known URLs
131+
defaultURLs :: [String]
132+
defaultURLs =
133+
[ "https://raw.githubusercontent.com/haskell/ghcup-metadata/master/ghcup-0.0.9.yaml"
134+
, "https://raw.githubusercontent.com/commercialhaskell/stackage-content/master/stack/stack-setup-2.yaml"
135+
, "https://raw.githubusercontent.com/haskell/ghcup-metadata/master/ghcup-cross-0.0.9.yaml"
136+
, "https://raw.githubusercontent.com/haskell/ghcup-metadata/master/ghcup-prereleases-0.0.9.yaml"
137+
, "https://raw.githubusercontent.com/haskell/ghcup-metadata/master/ghcup-vanilla-0.0.9.yaml"
138+
]
139+
140+
-- Extract URL string from NewURLSource if possible, for checking
141+
getUrlStringFromSource :: NewURLSource -> Maybe String
142+
getUrlStringFromSource NewGHCupURL = Just $ show ghcupURL
143+
getUrlStringFromSource NewStackSetupURL = Just $ show stackSetupURL
144+
getUrlStringFromSource (NewChannelAlias c) = Just $ show $ channelURL c
145+
getUrlStringFromSource (NewGHCupInfo _) = Nothing
146+
getUrlStringFromSource (NewSetupInfo _) = Nothing
147+
getUrlStringFromSource (NewURI uri) = Just $ show uri
148+
149+
-- Modified getDownloadsF to include URL prefix check
107150
getDownloadsF :: ( FromJSONKey Tool
108151
, FromJSONKey Version
109152
, FromJSON VersionInfo
@@ -124,6 +167,23 @@ getDownloadsF :: ( FromJSONKey Tool
124167
GHCupInfo
125168
getDownloadsF pfreq@(PlatformRequest arch plat _) = do
126169
Settings { urlSource } <- lift getSettings
170+
171+
-- Log all URL sources
172+
logDebug $ "URL sources in config: " <> T.pack (show urlSource)
173+
174+
-- Check for custom URL sources that aren't in the default list
175+
forM_ urlSource $ \src ->
176+
case src of
177+
NewURI uri -> do
178+
let url = show uri
179+
logDebug $ "Checking URI: " <> T.pack url
180+
unless (url `elem` defaultURLs) $ liftIO $ checkUrlPrefix url
181+
NewGHCupInfo _ ->
182+
liftIO $ warningM "ghcup" "Warning: Using custom GHCupInfo which is not an official GHCup metadata source"
183+
NewSetupInfo _ ->
184+
liftIO $ warningM "ghcup" "Warning: Using custom SetupInfo which is not an official GHCup metadata source"
185+
_ -> pure () -- Default sources don't need checking
186+
127187
infos <- liftE $ mapM dl' urlSource
128188
keys <- if any isRight infos
129189
then liftE . reThrowAll @_ @_ @'[StackPlatformDetectError] StackPlatformDetectError $ getStackPlatformKey pfreq
@@ -133,7 +193,17 @@ getDownloadsF pfreq@(PlatformRequest arch plat _) = do
133193
Right si -> pure $ fromStackSetupInfo si keys
134194
mergeGhcupInfo ghcupInfos
135195
where
136-
196+
-- Default URLs that are known to be official
197+
defaultURLs :: [String]
198+
defaultURLs =
199+
[ "https://raw.githubusercontent.com/haskell/ghcup-metadata/master/ghcup-0.0.9.yaml"
200+
, "https://raw.githubusercontent.com/commercialhaskell/stackage-content/master/stack/stack-setup-2.yaml"
201+
, "https://raw.githubusercontent.com/haskell/ghcup-metadata/master/ghcup-cross-0.0.9.yaml"
202+
, "https://raw.githubusercontent.com/haskell/ghcup-metadata/master/ghcup-prereleases-0.0.9.yaml"
203+
, "https://raw.githubusercontent.com/haskell/ghcup-metadata/master/ghcup-vanilla-0.0.9.yaml"
204+
, "https://ghc.gitlab.haskell.org/ghcup-metadata/ghcup-nightlies-0.0.7.yaml" -- Nightly channel mentioned in config example
205+
]
206+
137207
dl' :: ( FromJSONKey Tool
138208
, FromJSONKey Version
139209
, FromJSON VersionInfo
@@ -164,6 +234,7 @@ getDownloadsF pfreq@(PlatformRequest arch plat _) = do
164234
Right <$> decodeMetadata @Stack.SetupInfo base)
165235
$ fmap Left (decodeMetadata @GHCupInfo base >>= \gI -> warnOnMetadataUpdate uri gI >> pure gI)
166236

237+
167238
fromStackSetupInfo :: MonadThrow m
168239
=> Stack.SetupInfo
169240
-> [String]

0 commit comments

Comments
 (0)