-- SPDX-FileCopyrightText: 2026 Alexandra de Wit
--
-- SPDX-License-Identifier: MIT

-- | PyPI mirror uploads bind archive metadata to the same verified distribution bytes.
module Ecluse.Core.Registry.PyPI.Publish (
    pypiPublishCodec,
    publishRequest,
    classifyPublish,
) where

import Data.ByteString qualified as BS
import Data.ByteString.Builder (Builder, byteString, toLazyByteString)
import Data.Text qualified as T
import Network.HTTP.Client (Request (method, requestBody, requestHeaders), RequestBody (RequestBodyLBS))
import Network.HTTP.Types.Header (hContentType)

import Ecluse.Core.Credential (ClientCredential, credUsername)
import Ecluse.Core.Package (Artifact (artFilename), hashValue)
import Ecluse.Core.Registry (FetchFault (FetchUrlUnformable), ParseError (parseErrorMessage), PublishError (..), PublishFault (..), isSuccessStatus)
import Ecluse.Core.Registry.CachedDocument (pypiSimpleCached)
import Ecluse.Core.Registry.Metadata (VersionDoc (vdRaw))
import Ecluse.Core.Registry.Metadata.Fetch.Preparation (presentedRead)
import Ecluse.Core.Registry.MirrorUpload
import Ecluse.Core.Registry.MirrorUpload.Internal (checkedMirrorUploadData)
import Ecluse.Core.Registry.Publish (PublishCodec (..))
import Ecluse.Core.Registry.PyPI.Archive (archiveMetadata)
import Ecluse.Core.Registry.PyPI.Credential (pypiCredential)
import Ecluse.Core.Registry.PyPI.Metadata (preparePyPIRead, pypiChargeFactors)
import Ecluse.Core.Registry.PyPI.Request (simpleIndexRequest)
import Ecluse.Core.Registry.PyPI.UploadMetadata (PyPIUploadMetadata, parseUploadMetadata, uploadFields, uploadMetadataMemoryBytes)
import Ecluse.Core.Registry.Request (ServicePresentation, StoreOperation (StoreRead, StoreWrite), attachCredential, parseRequestEither, presentedMapping, presentedRequest, sendsCredentialUsername)
import Ecluse.Core.Security.Limits (Limits (maxMetadataBytes))
import Ecluse.Core.Server.Admission.Types (ChargeFactors (cfFullReadPermille))

-- | Read native target inventories and prepare one checked multipart upload per distribution.
pypiPublishCodec :: ServicePresentation -> PublishCodec
pypiPublishCodec :: ServicePresentation -> PublishCodec
pypiPublishCodec ServicePresentation
presentation =
    PublishCodec
        { pcPrepareSource :: PackageName
-> Version
-> VersionDoc
-> Either PublishFault (MirrorInventory, ())
pcPrepareSource = \PackageName
package Version
version VersionDoc
doc -> do
            source <- PublishFault
-> Maybe SimpleDocument -> Either PublishFault SimpleDocument
forall l r. l -> Maybe r -> Either l r
maybeToRight (Text -> PublishFault
PublishSourceUnavailable Text
"PyPI snapshot has no paired source metadata") (VersionDoc -> Maybe CachedDoc
vdRaw VersionDoc
doc Maybe CachedDoc
-> (CachedDoc -> Maybe SimpleDocument) -> Maybe SimpleDocument
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (SimpleDocument -> CachedDoc, CachedDoc -> Maybe SimpleDocument)
-> CachedDoc -> Maybe SimpleDocument
forall a b. (a, b) -> b
snd (SimpleDocument -> CachedDoc, CachedDoc -> Maybe SimpleDocument)
pypiSimpleCached)
            inventory <- checkedMirrorInventory package version doc (PyPIMirrorRead source)
            pure (inventory, ())
        , pcPrepareUpload :: Deadlines
-> Limits
-> (Int -> IO ())
-> MirrorInventory
-> ()
-> ExpectedMirrorFile
-> VerifiedArtifactBytes
-> IO (Either PublishFault (MirrorUploadData PyPIUploadMetadata))
pcPrepareUpload = \Deadlines
_ Limits
limits Int -> IO ()
charge MirrorInventory
inventory () ExpectedMirrorFile
file VerifiedArtifactBytes
bytes -> do
            extracted <- Limits
-> (Int -> IO ())
-> PackageName
-> Version
-> Text
-> ByteString
-> IO (Either ParseError ByteString)
archiveMetadata Limits
limits Int -> IO ()
charge (MirrorInventory -> PackageName
inventoryPackage MirrorInventory
inventory) (MirrorInventory -> Version
inventoryVersion MirrorInventory
inventory) (Artifact -> Text
artFilename (ExpectedMirrorFile -> Artifact
expectedArtifact ExpectedMirrorFile
file)) (VerifiedArtifactBytes -> ByteString
verifiedBytes VerifiedArtifactBytes
bytes)
            case first (PublishSourceUnavailable . parseErrorMessage) extracted of
                Left PublishFault
fault -> Either PublishFault (MirrorUploadData PyPIUploadMetadata)
-> IO (Either PublishFault (MirrorUploadData PyPIUploadMetadata))
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PublishFault
-> Either PublishFault (MirrorUploadData PyPIUploadMetadata)
forall a b. a -> Either a b
Left PublishFault
fault)
                Right ByteString
raw -> case (ParseError -> PublishFault)
-> Either ParseError Int -> Either PublishFault Int
forall a b c. (a -> b) -> Either a c -> Either b c
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first (Text -> PublishFault
PublishSourceUnavailable (Text -> PublishFault)
-> (ParseError -> Text) -> ParseError -> PublishFault
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ParseError -> Text
parseErrorMessage) (ByteString -> Either ParseError Int
uploadMetadataMemoryBytes ByteString
raw) of
                    Left PublishFault
fault -> Either PublishFault (MirrorUploadData PyPIUploadMetadata)
-> IO (Either PublishFault (MirrorUploadData PyPIUploadMetadata))
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PublishFault
-> Either PublishFault (MirrorUploadData PyPIUploadMetadata)
forall a b. a -> Either a b
Left PublishFault
fault)
                    Right Int
weight -> do
                        Int -> IO ()
charge Int
weight
                        Either PublishFault (MirrorUploadData PyPIUploadMetadata)
-> IO (Either PublishFault (MirrorUploadData PyPIUploadMetadata))
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either PublishFault (MirrorUploadData PyPIUploadMetadata)
 -> IO (Either PublishFault (MirrorUploadData PyPIUploadMetadata)))
-> Either PublishFault (MirrorUploadData PyPIUploadMetadata)
-> IO (Either PublishFault (MirrorUploadData PyPIUploadMetadata))
forall a b. (a -> b) -> a -> b
$ do
                            metadata <- (ParseError -> PublishFault)
-> Either ParseError PyPIUploadMetadata
-> Either PublishFault PyPIUploadMetadata
forall a b c. (a -> b) -> Either a c -> Either b c
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first (Text -> PublishFault
PublishSourceUnavailable (Text -> PublishFault)
-> (ParseError -> Text) -> ParseError -> PublishFault
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ParseError -> Text
parseErrorMessage) (Int
-> PackageName
-> Version
-> ByteString
-> Either ParseError PyPIUploadMetadata
parseUploadMetadata (Limits -> Int
maxMetadataBytes Limits
limits) (MirrorInventory -> PackageName
inventoryPackage MirrorInventory
inventory) (MirrorInventory -> Version
inventoryVersion MirrorInventory
inventory) ByteString
raw)
                            checkedMirrorUploadData inventory file bytes metadata
        , pcMirrorCompanion :: Maybe MirrorCompanion
pcMirrorCompanion = Maybe MirrorCompanion
forall a. Maybe a
Nothing
        , pcNativeComplete :: PackageName -> Version -> PackageInfo -> Bool
pcNativeComplete = \PackageName
_ Version
_ PackageInfo
_ -> Bool
False
        , pcTargetRead :: PrepareRead
pcTargetRead = CredentialMapping -> PrepareRead -> PrepareRead
presentedRead CredentialMapping
readAs PrepareRead
preparePyPIRead
        , pcTargetReadPermille :: Int
pcTargetReadPermille = ChargeFactors -> Int
cfFullReadPermille ChargeFactors
pypiChargeFactors
        , pcProbeRequest :: Text
-> Maybe ClientCredential
-> PackageName
-> Either UrlFormationError Request
pcProbeRequest = \Text
target Maybe ClientCredential
credential PackageName
name -> CredentialMapping
-> Maybe ClientCredential
-> (Maybe ClientCredential -> Either UrlFormationError Request)
-> Either UrlFormationError Request
forall e.
CredentialMapping
-> Maybe ClientCredential
-> (Maybe ClientCredential -> Either e Request)
-> Either e Request
presentedRequest CredentialMapping
readAs Maybe ClientCredential
credential (\Maybe ClientCredential
anonymous -> Text
-> Maybe ClientCredential
-> PackageName
-> Either UrlFormationError Request
simpleIndexRequest Text
target Maybe ClientCredential
anonymous PackageName
name)
        , pcVersionListWalk :: Maybe (Limits -> Cursor -> Step VersionListState)
pcVersionListWalk = Maybe (Limits -> Cursor -> Step VersionListState)
forall a. Maybe a
Nothing
        , pcPrepareTarget :: OriginClient
-> RegistryUrl -> IO (Either MetadataError RegistryUrl)
pcPrepareTarget = \OriginClient
_ RegistryUrl
target -> Either MetadataError RegistryUrl
-> IO (Either MetadataError RegistryUrl)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (RegistryUrl -> Either MetadataError RegistryUrl
forall a b. b -> Either a b
Right RegistryUrl
target)
        , pcPublishRequest :: Text
-> Maybe ClientCredential
-> PublishPlan
-> MirrorUploadData PyPIUploadMetadata
-> Either PublishFault Request
pcPublishRequest = \Text
target Maybe ClientCredential
credential PublishPlan
_ -> ServicePresentation
-> Text
-> Maybe ClientCredential
-> MirrorUploadData PyPIUploadMetadata
-> Either PublishFault Request
publishRequest ServicePresentation
presentation Text
target Maybe ClientCredential
credential
        , pcPublishOutcome :: Int -> ByteString -> Either PublishFault ()
pcPublishOutcome = Int -> ByteString -> Either PublishFault ()
classifyPublish
        }
  where
    readAs :: CredentialMapping
readAs = ServicePresentation
-> StoreOperation -> CredentialMapping -> CredentialMapping
presentedMapping ServicePresentation
presentation StoreOperation
StoreRead CredentialMapping
pypiCredential

{- | Post verified bytes to the exact configured endpoint, presenting the credential as the store
declares for writes. A credential needs a username only where that presentation sends it.
-}
publishRequest :: ServicePresentation -> Text -> Maybe ClientCredential -> MirrorUploadData PyPIUploadMetadata -> Either PublishFault Request
publishRequest :: ServicePresentation
-> Text
-> Maybe ClientCredential
-> MirrorUploadData PyPIUploadMetadata
-> Either PublishFault Request
publishRequest ServicePresentation
presentation Text
target Maybe ClientCredential
credential MirrorUploadData PyPIUploadMetadata
upload = do
    Maybe ClientCredential
-> (ClientCredential -> Either PublishFault ())
-> Either PublishFault ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ Maybe ClientCredential
credential ((ClientCredential -> Either PublishFault ())
 -> Either PublishFault ())
-> (ClientCredential -> Either PublishFault ())
-> Either PublishFault ()
forall a b. (a -> b) -> a -> b
$ \ClientCredential
presented ->
        Bool -> Either PublishFault () -> Either PublishFault ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (ServicePresentation -> StoreOperation -> Bool
sendsCredentialUsername ServicePresentation
presentation StoreOperation
StoreWrite Bool -> Bool -> Bool
&& Maybe Text -> Bool
forall a. Maybe a -> Bool
isNothing (ClientCredential -> Maybe Text
credUsername ClientCredential
presented)) (Either PublishFault () -> Either PublishFault ())
-> Either PublishFault () -> Either PublishFault ()
forall a b. (a -> b) -> a -> b
$
            PublishFault -> Either PublishFault ()
forall a b. a -> Either a b
Left (Text -> PublishFault
PublishSourceUnavailable Text
"PyPI upload requires an explicit credential username")
    base <- (UrlFormationError -> PublishFault)
-> Either UrlFormationError Request -> Either PublishFault Request
forall a b c. (a -> b) -> Either a c -> Either b c
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first (FetchFault -> PublishFault
PublishFetch (FetchFault -> PublishFault)
-> (UrlFormationError -> FetchFault)
-> UrlFormationError
-> PublishFault
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UrlFormationError -> FetchFault
FetchUrlUnformable) (Text -> Either UrlFormationError Request
parseRequestEither Text
target)
    let filename = Artifact -> Text
artFilename (ExpectedMirrorFile -> Artifact
expectedArtifact (MirrorUploadData PyPIUploadMetadata -> ExpectedMirrorFile
forall metadata. MirrorUploadData metadata -> ExpectedMirrorFile
uploadFile MirrorUploadData PyPIUploadMetadata
upload))
        bytes = MirrorUploadData PyPIUploadMetadata -> ByteString
forall metadata. MirrorUploadData metadata -> ByteString
uploadBytes MirrorUploadData PyPIUploadMetadata
upload
        digest = Hash -> Text
hashValue (MirrorUploadData PyPIUploadMetadata -> Hash
forall metadata. MirrorUploadData metadata -> Hash
uploadTargetHash MirrorUploadData PyPIUploadMetadata
upload)
        boundary = ByteString
"ecluse-" ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Text -> ByteString
forall a b. ConvertUtf8 a b => a -> b
encodeUtf8 (Int -> Text -> Text
T.take Int
48 Text
digest)
    python <- pythonTag filename
    let fields = [(Text
":action", Text
"file_upload"), (Text
"protocol_version", Text
"1"), (Text
"filetype", if Text
".whl" Text -> Text -> Bool
`T.isSuffixOf` Text
filename then Text
"bdist_wheel" else Text
"sdist"), (Text
"pyversion", Text
python), (Text
"sha256_digest", Text
digest)] [(Text, Text)] -> [(Text, Text)] -> [(Text, Text)]
forall a. Semigroup a => a -> a -> a
<> PyPIUploadMetadata -> [(Text, Text)]
uploadFields (MirrorUploadData PyPIUploadMetadata -> PyPIUploadMetadata
forall metadata. MirrorUploadData metadata -> metadata
uploadMetadata MirrorUploadData PyPIUploadMetadata
upload)
    when (T.any (`elem` ['\r', '\n', '"', '\\']) filename) (Left (PublishSourceUnavailable "unsafe multipart filename"))
    when (BS.isInfixOf boundary bytes || any (BS.isInfixOf boundary . encodeUtf8 . snd) fields) (Left (PublishSourceUnavailable "multipart boundary collides with upload content"))
    let body = ((Text, Text) -> Builder) -> [(Text, Text)] -> Builder
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (ByteString -> (Text, Text) -> Builder
fieldPart ByteString
boundary) [(Text, Text)]
fields Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> ByteString -> Text -> ByteString -> Builder
filePart ByteString
boundary Text
filename ByteString
bytes Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString (ByteString
"--" ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
boundary ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"--\r\n")
    pure . attachCredential (presentedMapping presentation StoreWrite pypiCredential) credential $
        base
            { method = "POST"
            , requestHeaders = (hContentType, "multipart/form-data; boundary=" <> boundary) : requestHeaders base
            , requestBody = RequestBodyLBS (toLazyByteString body)
            }

pythonTag :: Text -> Either PublishFault Text
pythonTag :: Text -> Either PublishFault Text
pythonTag Text
filename = case Text -> Text -> Maybe Text
T.stripSuffix Text
".whl" Text
filename of
    Maybe Text
Nothing -> Text -> Either PublishFault Text
forall a b. b -> Either a b
Right Text
"source"
    Just Text
stem -> case [Text] -> [Text]
forall a. [a] -> [a]
reverse (HasCallStack => Text -> Text -> [Text]
Text -> Text -> [Text]
T.splitOn Text
"-" Text
stem) of
        Text
_platform : Text
_abi : Text
python : [Text]
_ | Bool -> Bool
not (Text -> Bool
T.null Text
python) -> Text -> Either PublishFault Text
forall a b. b -> Either a b
Right Text
python
        [Text]
_ -> PublishFault -> Either PublishFault Text
forall a b. a -> Either a b
Left (Text -> PublishFault
PublishSourceUnavailable Text
"wheel filename has no Python tag")

fieldPart :: ByteString -> (Text, Text) -> Builder
fieldPart :: ByteString -> (Text, Text) -> Builder
fieldPart ByteString
boundary (Text
name, Text
value) = ByteString -> Builder
byteString (ByteString
"--" ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
boundary ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"\r\nContent-Disposition: form-data; name=\"" ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Text -> ByteString
forall a b. ConvertUtf8 a b => a -> b
encodeUtf8 Text
name ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"\"\r\n\r\n" ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Text -> ByteString
forall a b. ConvertUtf8 a b => a -> b
encodeUtf8 Text
value ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"\r\n")

filePart :: ByteString -> Text -> ByteString -> Builder
filePart :: ByteString -> Text -> ByteString -> Builder
filePart ByteString
boundary Text
filename ByteString
bytes =
    ByteString -> Builder
byteString (ByteString
"--" ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
boundary ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"\r\nContent-Disposition: form-data; name=\"content\"; filename=\"" ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Text -> ByteString
forall a b. ConvertUtf8 a b => a -> b
encodeUtf8 Text
filename ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"\"\r\nContent-Type: application/octet-stream\r\n\r\n")
        Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
bytes
        Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
"\r\n"

-- | A duplicate-file response still requires the worker's independent target digest proof.
classifyPublish :: Int -> ByteString -> Either PublishFault ()
classifyPublish :: Int -> ByteString -> Either PublishFault ()
classifyPublish Int
status ByteString
body
    | Int -> Bool
isSuccessStatus Int
status = () -> Either PublishFault ()
forall a b. b -> Either a b
Right ()
    | Int
status Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
400 Bool -> Bool -> Bool
&& ByteString -> Bool
duplicateFile ByteString
body = () -> Either PublishFault ()
forall a b. b -> Either a b
Right ()
    | Bool
otherwise = PublishFault -> Either PublishFault ()
forall a b. a -> Either a b
Left (PublishError -> PublishFault
PublishRejected (Text -> PublishError
PublishError (Text
"PyPI upload failed with HTTP status " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Int -> Text
forall b a. (Show a, IsString b) => a -> b
show Int
status)))

duplicateFile :: ByteString -> Bool
duplicateFile :: ByteString -> Bool
duplicateFile ByteString
body =
    ByteString
"File already exists." ByteString -> ByteString -> Bool
`BS.isInfixOf` ByteString
body
        Bool -> Bool -> Bool
|| (ByteString
"File already exists (" ByteString -> ByteString -> Bool
`BS.isInfixOf` ByteString
body Bool -> Bool -> Bool
&& ByteString
"with blake2_256 hash" ByteString -> ByteString -> Bool
`BS.isInfixOf` ByteString
body)
        Bool -> Bool -> Bool
|| ByteString
"A file with the same name already exists" ByteString -> ByteString -> Bool
`BS.isInfixOf` ByteString
body