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

-- | Native Cargo upload framing rejects ambiguous JSON and retains the exact archive bytes.
module Ecluse.Core.Registry.Cargo.Publish.Body (
    CargoUploadBody,
    decodeUpload,
    readUpload,
    uploadName,
    uploadVersion,
    uploadArchive,
    uploadMetadata,
    encodeUpload,
) where

import Data.Aeson (Value, encode, withObject, (.:), (.:?))
import Data.Aeson.Key qualified as Key
import Data.Aeson.Types (Parser, parseEither)
import Data.Bits (shiftL, (.|.))
import Data.ByteString qualified as BS
import Data.ByteString.Builder (byteString, lazyByteString, toLazyByteString, word32LE)
import Data.ByteString.Lazy qualified as LBS

import Ecluse.Core.Registry (ParseError (..), parseRefusal)
import Ecluse.Core.Registry.Cargo.Manifest (parseRustRelease, unstableDependencyFields, usesUnstableDependencyField)
import Ecluse.Core.Registry.Cargo.Project (projectName)
import Ecluse.Core.Registry.Cargo.Wire (cargoTreeCharge, decodeCargoRecord)
import Ecluse.Core.Security.Limits (Limits (..))
import Ecluse.Core.Text (joinAlternatives)
import Ecluse.Core.Version.Cargo (parseCargo)

-- | The decoded claims and archive remain paired with one fully consumed native body.
data CargoUploadBody = CargoUploadBody Text Text ByteString Value
    deriving stock (CargoUploadBody -> CargoUploadBody -> Bool
(CargoUploadBody -> CargoUploadBody -> Bool)
-> (CargoUploadBody -> CargoUploadBody -> Bool)
-> Eq CargoUploadBody
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CargoUploadBody -> CargoUploadBody -> Bool
== :: CargoUploadBody -> CargoUploadBody -> Bool
$c/= :: CargoUploadBody -> CargoUploadBody -> Bool
/= :: CargoUploadBody -> CargoUploadBody -> Bool
Eq, Int -> CargoUploadBody -> ShowS
[CargoUploadBody] -> ShowS
CargoUploadBody -> String
(Int -> CargoUploadBody -> ShowS)
-> (CargoUploadBody -> String)
-> ([CargoUploadBody] -> ShowS)
-> Show CargoUploadBody
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CargoUploadBody -> ShowS
showsPrec :: Int -> CargoUploadBody -> ShowS
$cshow :: CargoUploadBody -> String
show :: CargoUploadBody -> String
$cshowList :: [CargoUploadBody] -> ShowS
showList :: [CargoUploadBody] -> ShowS
Show)

-- | Preserve the declared published spelling for archive agreement.
uploadName :: CargoUploadBody -> Text
uploadName :: CargoUploadBody -> Text
uploadName (CargoUploadBody Text
name Text
_ ByteString
_ Value
_) = Text
name

-- | Preserve the declared raw native version, including build metadata.
uploadVersion :: CargoUploadBody -> Text
uploadVersion :: CargoUploadBody -> Text
uploadVersion (CargoUploadBody Text
_ Text
version ByteString
_ Value
_) = Text
version

-- | These bytes are a slice of the validated original body.
uploadArchive :: CargoUploadBody -> ByteString
uploadArchive :: CargoUploadBody -> ByteString
uploadArchive (CargoUploadBody Text
_ Text
_ ByteString
bytes Value
_) = ByteString
bytes

-- | Resolver claims retain the reservation acquired before JSON decoding.
uploadMetadata :: CargoUploadBody -> Value
uploadMetadata :: CargoUploadBody -> Value
uploadMetadata (CargoUploadBody Text
_ Text
_ ByteString
_ Value
value) = Value
value

-- | Decode strict native framing and JSON under the configured request and metadata bounds.
decodeUpload :: Limits -> ByteString -> Either ParseError CargoUploadBody
decodeUpload :: Limits -> ByteString -> Either ParseError CargoUploadBody
decodeUpload Limits
limits ByteString
bytes = Limits -> ByteString -> Either ParseError (ByteString, ByteString)
splitUpload Limits
limits ByteString
bytes Either ParseError (ByteString, ByteString)
-> ((ByteString, ByteString) -> Either ParseError CargoUploadBody)
-> Either ParseError CargoUploadBody
forall a b.
Either ParseError a
-> (a -> Either ParseError b) -> Either ParseError b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (ByteString -> ByteString -> Either ParseError CargoUploadBody)
-> (ByteString, ByteString) -> Either ParseError CargoUploadBody
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (Limits
-> ByteString -> ByteString -> Either ParseError CargoUploadBody
decodeSections Limits
limits)

{- | Charge the JSON tree before decoding and return that reservation for archive work.
The caller separately reserves the original body.
-}
readUpload :: Limits -> (Int -> IO ()) -> ByteString -> IO (Either ParseError (CargoUploadBody, Int))
readUpload :: Limits
-> (Int -> IO ())
-> ByteString
-> IO (Either ParseError (CargoUploadBody, Int))
readUpload Limits
limits Int -> IO ()
charge ByteString
bytes = case Limits -> ByteString -> Either ParseError (ByteString, ByteString)
splitUpload Limits
limits ByteString
bytes of
    Left ParseError
fault -> Either ParseError (CargoUploadBody, Int)
-> IO (Either ParseError (CargoUploadBody, Int))
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ParseError -> Either ParseError (CargoUploadBody, Int)
forall a b. a -> Either a b
Left ParseError
fault)
    Right (ByteString
metadata, ByteString
archive) -> do
        let metadataCost :: Int
metadataCost = ByteString -> Int
metadataCharge ByteString
metadata
        Int -> IO ()
charge Int
metadataCost
        Either ParseError (CargoUploadBody, Int)
-> IO (Either ParseError (CargoUploadBody, Int))
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either ParseError (CargoUploadBody, Int)
 -> IO (Either ParseError (CargoUploadBody, Int)))
-> Either ParseError (CargoUploadBody, Int)
-> IO (Either ParseError (CargoUploadBody, Int))
forall a b. (a -> b) -> a -> b
$ do
            upload <- Limits
-> ByteString -> ByteString -> Either ParseError CargoUploadBody
decodeSections Limits
limits ByteString
metadata ByteString
archive
            pure (upload, metadataCost)

metadataCharge :: ByteString -> Int
metadataCharge :: ByteString -> Int
metadataCharge = Integer -> Int
cargoTreeCharge (Integer -> Int) -> (ByteString -> Integer) -> ByteString -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Int -> Integer) -> (ByteString -> Int) -> ByteString -> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Int
BS.length

splitUpload :: Limits -> ByteString -> Either ParseError (ByteString, ByteString)
splitUpload :: Limits -> ByteString -> Either ParseError (ByteString, ByteString)
splitUpload Limits
limits ByteString
bytes = do
    Bool -> Either ParseError () -> Either ParseError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (ByteString -> Int
BS.length ByteString
bytes Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Limits -> Int
maxPublishRequestBytes Limits
limits) (Text -> Either ParseError ()
forall a. Text -> Either ParseError a
parseRefusal Text
"Cargo upload exceeds its request byte bound")
    (metadata, rest) <- Int -> ByteString -> Either ParseError (ByteString, ByteString)
section (Limits -> Int
maxMetadataBytes Limits
limits) ByteString
bytes
    (archive, trailing) <- section (maxMirrorArtifactBytes limits) rest
    unless (BS.null trailing) (parseRefusal "Cargo upload contains trailing bytes")
    when (BS.null archive) (parseRefusal "Cargo upload has no archive")
    pure (metadata, archive)

section :: Int -> ByteString -> Either ParseError (ByteString, ByteString)
section :: Int -> ByteString -> Either ParseError (ByteString, ByteString)
section Int
cap ByteString
bytes = do
    let (ByteString
prefix, ByteString
available) = Int -> ByteString -> (ByteString, ByteString)
BS.splitAt Int
4 ByteString
bytes
        declared :: Integer
declared = (Word8 -> Integer -> Integer) -> Integer -> ByteString -> Integer
forall a. (Word8 -> a -> a) -> a -> ByteString -> a
BS.foldr' (\Word8
byte Integer
total -> Integer
total Integer -> Int -> Integer
forall a. Bits a => a -> Int -> a
`shiftL` Int
8 Integer -> Integer -> Integer
forall a. Bits a => a -> a -> a
.|. Word8 -> Integer
forall a. Integral a => a -> Integer
toInteger Word8
byte) Integer
0 ByteString
prefix
    Bool -> Either ParseError () -> Either ParseError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (ByteString -> Int
BS.length ByteString
prefix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
4) (Text -> Either ParseError ()
forall a. Text -> Either ParseError a
parseRefusal Text
"Cargo upload has a truncated length prefix")
    Bool -> Either ParseError () -> Either ParseError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
cap Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 Bool -> Bool -> Bool
|| Integer
declared Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
cap) (Text -> Either ParseError ()
forall a. Text -> Either ParseError a
parseRefusal Text
"Cargo upload section exceeds its byte bound")
    Bool -> Either ParseError () -> Either ParseError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Integer
declared Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Int -> Integer
forall a. Integral a => a -> Integer
toInteger (ByteString -> Int
BS.length ByteString
available)) (Text -> Either ParseError ()
forall a. Text -> Either ParseError a
parseRefusal Text
"Cargo upload has a truncated section")
    (ByteString, ByteString)
-> Either ParseError (ByteString, ByteString)
forall a. a -> Either ParseError a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int -> ByteString -> (ByteString, ByteString)
BS.splitAt (Integer -> Int
forall a. Num a => Integer -> a
fromInteger Integer
declared) ByteString
available)

decodeSections :: Limits -> ByteString -> ByteString -> Either ParseError CargoUploadBody
decodeSections :: Limits
-> ByteString -> ByteString -> Either ParseError CargoUploadBody
decodeSections Limits
limits ByteString
metadata ByteString
archive = do
    value <- (CargoRecordError -> ParseError)
-> Either CargoRecordError Value -> Either ParseError Value
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 (ParseError -> CargoRecordError -> ParseError
forall a b. a -> b -> a
const (Text -> ParseError
ParseError Text
"Cargo upload metadata is malformed or exceeds its nesting bound")) (Int -> ByteString -> Either CargoRecordError Value
decodeCargoRecord (Limits -> Int
maxNestingDepth Limits
limits) ByteString
metadata)
    (name, version) <- first (ParseError . toText) (parseEither metadataIdentity value)
    pure (CargoUploadBody name version archive value)

metadataIdentity :: Value -> Parser (Text, Text)
metadataIdentity :: Value -> Parser (Text, Text)
metadataIdentity = String
-> (Object -> Parser (Text, Text)) -> Value -> Parser (Text, Text)
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"Cargo upload metadata" ((Object -> Parser (Text, Text)) -> Value -> Parser (Text, Text))
-> (Object -> Parser (Text, Text)) -> Value -> Parser (Text, Text)
forall a b. (a -> b) -> a -> b
$ \Object
fields -> do
    name <- Object
fields Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"name"
    version <- fields .: "vers"
    nativeName name
    unless (isJust (parseCargo version)) (fail "Cargo upload version is invalid")
    dependencies <- fields .: "deps"
    traverse_ uploadDependency (dependencies :: [Value])
    void (fields .: "features" :: Parser (Map Text [Text]))
    for_ ["authors", "keywords", "categories"] $ \Key
key ->
        Parser [Text] -> Parser ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Object
fields Object -> Key -> Parser [Text]
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
key :: Parser [Text])
    for_ ["description", "documentation", "homepage", "readme", "readme_file", "license", "license_file", "repository", "links"] $ \Key
key ->
        Parser (Maybe Text) -> Parser ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Object
fields Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
key :: Parser (Maybe Text))
    void (fields .:? "badges" :: Parser (Maybe (Map Text (Map Text Text))))
    rustVersion <- fields .:? "rust_version"
    traverse_ (either (fail . toString . parseErrorMessage) (const (pure ())) . parseRustRelease) rustVersion
    pure (name, version)

uploadDependency :: Value -> Parser ()
uploadDependency :: Value -> Parser ()
uploadDependency = String -> (Object -> Parser ()) -> Value -> Parser ()
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"Cargo upload dependency" ((Object -> Parser ()) -> Value -> Parser ())
-> (Object -> Parser ()) -> Value -> Parser ()
forall a b. (a -> b) -> a -> b
$ \Object
fields -> do
    name <- Object
fields Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"name"
    nativeName name
    when (usesUnstableDependencyField fields) (fail ("Cargo upload dependency '" <> toString name <> "' carries an unstable " <> toString unstableFieldNames <> " field"))
    void (fields .: "version_req" :: Parser Text)
    void (fields .: "features" :: Parser [Text])
    for_ ["optional", "default_features"] $ \Key
key ->
        Parser Bool -> Parser ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Object
fields Object -> Key -> Parser Bool
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
key :: Parser Bool)
    for_ ["target", "registry"] $ \Key
key ->
        Parser (Maybe Text) -> Parser ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Object
fields Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
key :: Parser (Maybe Text))
    alias <- fields .:? "explicit_name_in_toml"
    traverse_ nativeName alias
    kind <- fields .:? "kind"
    for_ kind $ \Text
value ->
        Bool -> Parser () -> Parser ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Text
value Text -> [Text] -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`elem` [Text
"normal", Text
"dev", Text
"build" :: Text]) (String -> Parser ()
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Cargo upload dependency kind is invalid")

-- The shared field list as the refusal names it: "artifact, bindep_target, lib or public".
unstableFieldNames :: Text
unstableFieldNames :: Text
unstableFieldNames = [Text] -> Text
joinAlternatives ((Key -> Text) -> [Key] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map Key -> Text
Key.toText [Key]
unstableDependencyFields)

nativeName :: Text -> Parser ()
nativeName :: Text -> Parser ()
nativeName = (ParseError -> Parser ())
-> (PackageName -> Parser ())
-> Either ParseError PackageName
-> Parser ()
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Parser () -> ParseError -> Parser ()
forall a b. a -> b -> a
const (String -> Parser ()
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Cargo upload name is invalid")) (Parser () -> PackageName -> Parser ()
forall a b. a -> b -> a
const (() -> Parser ()
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ())) (Either ParseError PackageName -> Parser ())
-> (Text -> Either ParseError PackageName) -> Text -> Parser ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Either ParseError PackageName
projectName

-- | Encode native uint32 framing without changing either the JSON value or verified archive bytes.
encodeUpload :: Value -> ByteString -> Either ParseError LByteString
encodeUpload :: Value -> ByteString -> Either ParseError LByteString
encodeUpload Value
metadata ByteString
archive = do
    let json :: LByteString
json = Value -> LByteString
forall a. ToJSON a => a -> LByteString
encode Value
metadata
    Bool -> Either ParseError () -> Either ParseError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Int64 -> Integer
forall a. Integral a => a -> Integer
toInteger (LByteString -> Int64
LBS.length LByteString
json) Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
<= Word32 -> Integer
forall a. Integral a => a -> Integer
toInteger (Word32
forall a. Bounded a => a
maxBound :: Word32) Bool -> Bool -> Bool
&& Int -> Integer
forall a. Integral a => a -> Integer
toInteger (ByteString -> Int
BS.length ByteString
archive) Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
<= Word32 -> Integer
forall a. Integral a => a -> Integer
toInteger (Word32
forall a. Bounded a => a
maxBound :: Word32)) (Text -> Either ParseError ()
forall a. Text -> Either ParseError a
parseRefusal Text
"Cargo upload cannot fit uint32 framing")
    LByteString -> Either ParseError LByteString
forall a. a -> Either ParseError a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (LByteString -> Either ParseError LByteString)
-> (Builder -> LByteString)
-> Builder
-> Either ParseError LByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> LByteString
toLazyByteString (Builder -> Either ParseError LByteString)
-> Builder -> Either ParseError LByteString
forall a b. (a -> b) -> a -> b
$ Word32 -> Builder
word32LE (Int64 -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (LByteString -> Int64
LBS.length LByteString
json)) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> LByteString -> Builder
lazyByteString LByteString
json Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word32 -> Builder
word32LE (Int -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int
BS.length ByteString
archive)) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
byteString ByteString
archive