Skip to content

Commit 11e12c4

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

File tree

1 file changed

+43
-3
lines changed

1 file changed

+43
-3
lines changed

lib/GHCup/Download.hs

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,33 @@ import qualified Data.Yaml.Aeson as Y
102102
------------------
103103

104104

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

106133
-- | Downloads the download information! But only if we need to ;P
107134
getDownloadsF :: ( FromJSONKey Tool
@@ -124,6 +151,17 @@ getDownloadsF :: ( FromJSONKey Tool
124151
GHCupInfo
125152
getDownloadsF pfreq@(PlatformRequest arch plat _) = do
126153
Settings { urlSource } <- lift getSettings
154+
forM_ urlSource $ \src ->
155+
case src of
156+
NewURI uri -> do
157+
when (not (isOfficialURI uri) || isNightliesURI uri) $
158+
logWarn $ "Warning: Using non-official metadata source: " <> formatURI uri <>
159+
"\nThis source is not maintained or verified by the GHCup team."
160+
NewGHCupInfo _ ->
161+
logWarn "Warning: Using custom GHCupInfo data that is not from an official GHCup metadata source"
162+
NewSetupInfo _ ->
163+
logWarn "Warning: Using custom SetupInfo data that is not from an official GHCup metadata source"
164+
_ -> pure ()
127165
infos <- liftE $ mapM dl' urlSource
128166
keys <- if any isRight infos
129167
then liftE . reThrowAll @_ @_ @'[StackPlatformDetectError] StackPlatformDetectError $ getStackPlatformKey pfreq
@@ -133,7 +171,7 @@ getDownloadsF pfreq@(PlatformRequest arch plat _) = do
133171
Right si -> pure $ fromStackSetupInfo si keys
134172
mergeGhcupInfo ghcupInfos
135173
where
136-
174+
137175
dl' :: ( FromJSONKey Tool
138176
, FromJSONKey Version
139177
, FromJSON VersionInfo
@@ -162,9 +200,11 @@ getDownloadsF pfreq@(PlatformRequest arch plat _) = do
162200
catchE @JSONError (\(JSONDecodeError s) -> do
163201
logDebug $ "Couldn't decode " <> T.pack base <> " as GHCupInfo, trying as SetupInfo: " <> T.pack s
164202
Right <$> decodeMetadata @Stack.SetupInfo base)
165-
$ fmap Left (decodeMetadata @GHCupInfo base >>= \gI -> warnOnMetadataUpdate uri gI >> pure gI)
203+
$ fmap Left (decodeMetadata @GHCupInfo base >>= \gI ->
204+
warnOnMetadataUpdate uri gI >> pure gI)
205+
166206

167-
fromStackSetupInfo :: MonadThrow m
207+
fromStackSetupInfo :: MonadThrow m
168208
=> Stack.SetupInfo
169209
-> [String]
170210
-> m GHCupInfo

0 commit comments

Comments
 (0)