@@ -33,6 +33,7 @@ import Codec.CBOR.Write qualified as CBOR
33
33
import Control.Monad (join , replicateM , (<=<) )
34
34
import Control.Monad.Reader (Reader , runReader )
35
35
import Control.Monad.State.Strict (StateT , runStateT )
36
+ import Data.Bifunctor (second )
36
37
import Data.ByteString (ByteString )
37
38
import Data.ByteString.Base16 qualified as Base16
38
39
import Data.Functor ((<&>) )
@@ -55,7 +56,6 @@ import System.Random.Stateful
55
56
randomM ,
56
57
uniformByteStringM ,
57
58
)
58
- import Data.Bifunctor (second )
59
59
60
60
--------------------------------------------------------------------------------
61
61
-- Generator infrastructure
@@ -268,7 +268,13 @@ genForCTree (CTree.Postlude pt) = S <$> genPostlude pt
268
268
genForCTree (CTree. Map nodes) = do
269
269
items <- pairTermList . flattenWrappedList <$> traverse genForNode nodes
270
270
case items of
271
- Just ts -> pure . S $ TMap ts
271
+ Just ts ->
272
+ let -- De-duplicate keys in the map.
273
+ -- Per RFC7049:
274
+ -- >> A map that has duplicate keys may be well-formed, but it is not
275
+ -- >> valid, and thus it causes indeterminate decoding
276
+ tsNodup = Map. toList $ Map. fromList ts
277
+ in pure . S $ TMap tsNodup
272
278
Nothing -> error " Single terms in map context"
273
279
genForCTree (CTree. Array nodes) = do
274
280
items <- singleTermList . flattenWrappedList <$> traverse genForNode nodes
@@ -308,12 +314,12 @@ genForCTree (CTree.Control op target controller) = do
308
314
tt <- resolveIfRef target
309
315
ct <- resolveIfRef controller
310
316
case (op, ct) of
311
- (CtlOp. Le , CTree. Literal (VUInt n)) -> case tt of
312
- CTree. Postlude PTUInt -> S . TInteger <$> genUniformRM (0 , fromIntegral n)
317
+ (CtlOp. Le , CTree. Literal (VUInt n)) -> case tt of
318
+ CTree. Postlude PTUInt -> S . TInteger <$> genUniformRM (0 , fromIntegral n)
313
319
_ -> error " Cannot apply le operator to target"
314
320
(CtlOp. Le , _) -> error $ " Invalid controller for .le operator: " <> show controller
315
- (CtlOp. Lt , CTree. Literal (VUInt n)) -> case tt of
316
- CTree. Postlude PTUInt -> S . TInteger <$> genUniformRM (0 , fromIntegral n - 1 )
321
+ (CtlOp. Lt , CTree. Literal (VUInt n)) -> case tt of
322
+ CTree. Postlude PTUInt -> S . TInteger <$> genUniformRM (0 , fromIntegral n - 1 )
317
323
_ -> error " Cannot apply lt operator to target"
318
324
(CtlOp. Lt , _) -> error $ " Invalid controller for .lt operator: " <> show controller
319
325
(CtlOp. Size , CTree. Literal (VUInt n)) -> case tt of
0 commit comments