Skip to content

Commit 636acdc

Browse files
committed
Warning by introducing officialprefixes in lib/GHCup/Download.hs
1 parent e1f60cc commit 636acdc

File tree

3 files changed

+74
-26
lines changed

3 files changed

+74
-26
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: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,6 @@ import System.FilePath
7676
import System.IO.Error
7777
import System.IO.Temp
7878
import Text.Regex.Posix
79-
import qualified URI.ByteString as URI
80-
import qualified Data.ByteString.Lazy.Char8 as BL8
81-
import qualified Data.Text.Encoding as TE
82-
import Data.ByteString.Builder (toLazyByteString)
8379

8480
import qualified Data.Text as T
8581

@@ -254,7 +250,6 @@ getDebugInfo :: ( Alternative m
254250
, MonadFail m
255251
, MonadReader env m
256252
, HasDirs env
257-
, HasSettings env
258253
, HasLog env
259254
, MonadCatch m
260255
, MonadIO m
@@ -265,28 +260,10 @@ getDebugInfo :: ( Alternative m
265260
DebugInfo
266261
getDebugInfo = do
267262
diDirs <- lift getDirs
268-
settings <- lift getSettings
269-
let diChannels = fmap (\c -> (c, GHCup.Version.channelURL c)) [minBound..maxBound]
263+
let diChannels = fmap (\c -> (c, channelURL c)) [minBound..maxBound]
270264
let diShimGenURL = shimGenURL
271265
diArch <- lE getArchitecture
272266
diPlatform <- liftE getPlatform
273-
274-
let officialPrefix = "https://raw.githubusercontent.com/haskell/ghcup-metadata/"
275-
let isOfficial url = officialPrefix `T.isPrefixOf` T.pack url
276-
let maybeMetadataURL = case settings of
277-
_ -> Nothing
278-
279-
case maybeMetadataURL of
280-
Just url | not (isOfficial url) ->
281-
logWarn $ "Warning: Using non-official metadata URL: " <> T.pack url
282-
_ -> pure ()
283-
284-
forM_ diChannels $ \(channel, url) -> do
285-
let urlText = TE.decodeUtf8 $ BL8.toStrict $ toLazyByteString $ URI.serializeURI url
286-
unless (isOfficial (T.unpack urlText)) $
287-
logWarn $ "Warning: The channel " <> T.pack (show channel)
288-
<> " uses a non-official URL: " <> urlText
289-
290267
pure $ DebugInfo { .. }
291268

292269

@@ -700,4 +677,3 @@ rmTmp = do
700677
logDebug $ "rm -rf " <> T.pack (fromGHCupPath f)
701678
rmPathForcibly f
702679

703-

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)