aeson 2.3.2.0 → 2.3.3.0
raw patch · 10 files changed
+124/−38 lines, 10 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
Files
- aeson.cabal +3/−2
- changelog.md +17/−1
- src/Data/Aeson/Decoding/ByteString.hs +10/−8
- src/Data/Aeson/Decoding/ByteString/Lazy.hs +10/−8
- src/Data/Aeson/Decoding/Text.hs +10/−8
- src/Data/Aeson/Types/FromJSON.hs +14/−6
- tests/JSONTestSuite.hs +0/−1
- tests/JSONTestSuite/results/i_number_huge_exp.tok +1/−2
- tests/Regression/Issue1198.hs +32/−0
- tests/UnitTests.hs +27/−2
aeson.cabal view
@@ -1,6 +1,6 @@ cabal-version: 2.2 name: aeson-version: 2.3.2.0+version: 2.3.3.0 license: BSD-3-Clause license-file: LICENSE category: Text, Web, JSON@@ -127,7 +127,7 @@ , network-uri ^>=2.6.4.1 , OneTuple ^>=0.4.1.1 , primitive ^>=0.8.0.0 || ^>=0.9.0.0- , QuickCheck ^>=2.14.3 || ^>=2.15 || ^>=2.16.0.0 || ^>=2.17.1.0 || ^>=2.18.0.0+ , QuickCheck ^>=2.14.3 || ^>=2.15 || ^>=2.16.0.0 || ^>=2.17.1.0 || ^>=2.18.0.0 || ^>= 2.19.0.0 , scientific ^>=0.3.7.0 , semialign ^>=1.3 || ^>=1.4 , strict ^>=0.5@@ -180,6 +180,7 @@ Regression.Issue687 Regression.Issue967 Regression.Issue1138+ Regression.Issue1198 RFC8785 SerializationFormatSpec Types
changelog.md view
@@ -1,5 +1,14 @@ For the latest version of this document, please see [https://github.com/haskell/aeson/blob/master/changelog.md](https://github.com/haskell/aeson/blob/master/changelog.md). +### 2.3.3.0 - 2026-11-23++* Parse `Version` in linear time. Previously, `parseJSON` and `fromJSONKey`+ for `Version` took quadratic time in the length of the input.++* Reject large exponents when parsing scientific notation. Previously, parsing+ a JSON number into a `Value` would silently let the exponent overflow if it is+ greater than `maxBound :: Int` or smaller than `minBound :: Int`.+ ### 2.3.2.0 - 2026-09-12 * Remove unsound rewrite rule on `FromJSONKey` (`fmap coerce = coerce`)@@ -30,7 +39,14 @@ rejecting more inputs. Error messages for `Ratio` and integral types are also slightly different due to reusing the same bounding logic. * In `text-iso8601-0.1.1.2` (backported from 0.2.0.0):- - (HSEC-2026-0007) Reject years of more than 15 digits.+ - Reject years of more than 15 digits.++ This year parsing issue was previously reported as a DoS vulnerability in [HSEC-2026-0007](https://haskell.github.io/security-advisories/advisory/HSEC-2026-0007.html)+ but [we later reevaluated it as not a DoS vulnerability](https://github.com/haskell/security-advisories/issues/339).+ (The other vulnerability in aeson remains in that advisory.)+ Indeed, years were parsed in time `O(n log n)` which is asymptotically+ no slower than parsing integer literals (which happens on all JSON integers+ regardless of the target type, unlike dates). ### 2.2.5.0
src/Data/Aeson/Decoding/ByteString.hs view
@@ -251,23 +251,25 @@ go_sci_pos :: Integer -> Int -> ByteString -> Int -> ByteString -> r go_sci_pos !coef !exp10 !bs2 !n !bs = case BS.uncons bs of- Nothing -> kont (NumScientific sci) bs Just (w8, bs') | W8.DIGIT_0 <= w8, w8 <= W8.DIGIT_9 -> go_sci_pos coef exp10 bs2 (n + 1) bs'- | otherwise -> kont (NumScientific sci) bs+ -- Technically it would be more lenient to bound `toInteger exp10 + exp10'`+ -- but the extra complexity is not worth it.+ _ | exp10' <= toInteger (maxBound :: Int) -> kont (NumScientific sci) bs+ _ -> err "Scientific notation with exponent too large" where- exp10' = fromInteger (byteStringToInteger (BS.Unsafe.unsafeTake n bs2))- sci = Sci.scientific coef (exp10 + exp10')+ exp10' = byteStringToInteger (BS.Unsafe.unsafeTake n bs2)+ sci = Sci.scientific coef (exp10 + fromInteger exp10') go_sci_neg :: Integer -> Int -> ByteString -> Int -> ByteString -> r go_sci_neg !coef !exp10 !bs2 !n !bs = case BS.uncons bs of- Nothing -> kont (NumScientific sci) bs Just (w8, bs') | W8.DIGIT_0 <= w8, w8 <= W8.DIGIT_9 -> go_sci_neg coef exp10 bs2 (n + 1) bs'- | otherwise -> kont (NumScientific sci) bs+ _ | exp10' <= - toInteger (minBound - exp10) -> kont (NumScientific sci) bs+ _ -> err "Scientific notation with exponent too small" where- exp10' = fromInteger (byteStringToInteger (BS.Unsafe.unsafeTake n bs2))- sci = Sci.scientific coef (exp10 - exp10')+ exp10' = byteStringToInteger (BS.Unsafe.unsafeTake n bs2)+ sci = Sci.scientific coef (exp10 - fromInteger exp10') errEnd = err "Unexpected end-of-input while parsing number literal" errUnx w8 = err $ "Unexpected " ++ show (chr (fromIntegral w8)) ++ " while parsing number literal"
src/Data/Aeson/Decoding/ByteString/Lazy.hs view
@@ -257,23 +257,25 @@ go_sci_pos :: Integer -> Int -> ByteString -> Int -> ByteString -> r go_sci_pos !coef !exp10 !bs2 !n !bs = case LBS.uncons bs of- Nothing -> kont (NumScientific sci) bs Just (w8, bs') | W8.DIGIT_0 <= w8, w8 <= W8.DIGIT_9 -> go_sci_pos coef exp10 bs2 (n + 1) bs'- | otherwise -> kont (NumScientific sci) bs+ -- Technically it would be more lenient to bound `toInteger exp10 + exp10'`+ -- but the extra complexity is not worth it.+ _ | exp10' <= toInteger (maxBound :: Int) -> kont (NumScientific sci) bs+ _ -> err "Scientific notation with exponent too large" where- exp10' = fromInteger (byteStringToInteger (lbsTake n bs2))- sci = Sci.scientific coef (exp10 + exp10')+ exp10' = byteStringToInteger (lbsTake n bs2)+ sci = Sci.scientific coef (exp10 + fromInteger exp10') go_sci_neg :: Integer -> Int -> ByteString -> Int -> ByteString -> r go_sci_neg !coef !exp10 !bs2 !n !bs = case LBS.uncons bs of- Nothing -> kont (NumScientific sci) bs Just (w8, bs') | W8.DIGIT_0 <= w8, w8 <= W8.DIGIT_9 -> go_sci_neg coef exp10 bs2 (n + 1) bs'- | otherwise -> kont (NumScientific sci) bs+ _ | exp10' <= - toInteger (minBound - exp10) -> kont (NumScientific sci) bs+ _ -> err "Scientific notation with exponent too small" where- exp10' = fromInteger (byteStringToInteger (lbsTake n bs2))- sci = Sci.scientific coef (exp10 - exp10')+ exp10' = byteStringToInteger (lbsTake n bs2)+ sci = Sci.scientific coef (exp10 - fromInteger exp10') errEnd = err "Unexpected end-of-input while parsing number literal" errUnx w8 = err $ "Unexpected " ++ show (chr (fromIntegral w8)) ++ " while parsing number literal"
src/Data/Aeson/Decoding/Text.hs view
@@ -261,23 +261,25 @@ go_sci_pos :: Integer -> Int -> Text -> Int -> Text -> r go_sci_pos !coef !exp10 !bs2 !n !bs = case unconsPoint bs of- Nothing -> kont (NumScientific sci) bs Just (w8, bs') | W.DIGIT_0 <= w8, w8 <= W.DIGIT_9 -> go_sci_pos coef exp10 bs2 (n + 1) bs'- | otherwise -> kont (NumScientific sci) bs+ -- Technically it would be more lenient to bound `toInteger exp10 + exp10'`+ -- but the extra complexity is not worth it.+ _ | exp10' <= toInteger (maxBound :: Int) -> kont (NumScientific sci) bs+ _ -> err "Scientific notation with exponent too large" where- exp10' = fromInteger (textToInteger (unsafeTakePoints n bs2))- sci = Sci.scientific coef (exp10 + exp10')+ exp10' = textToInteger (unsafeTakePoints n bs2)+ sci = Sci.scientific coef (exp10 + fromInteger exp10') go_sci_neg :: Integer -> Int -> Text -> Int -> Text -> r go_sci_neg !coef !exp10 !bs2 !n !bs = case unconsPoint bs of- Nothing -> kont (NumScientific sci) bs Just (w8, bs') | W.DIGIT_0 <= w8, w8 <= W.DIGIT_9 -> go_sci_neg coef exp10 bs2 (n + 1) bs'- | otherwise -> kont (NumScientific sci) bs+ _ | exp10' <= - toInteger (minBound - exp10) -> kont (NumScientific sci) bs+ _ -> err "Scientific notation with exponent too small" where- exp10' = fromInteger (textToInteger (unsafeTakePoints n bs2))- sci = Sci.scientific coef (exp10 - exp10')+ exp10' = textToInteger (unsafeTakePoints n bs2)+ sci = Sci.scientific coef (exp10 - fromInteger exp10') errEnd = err "Unexpected end-of-input while parsing number literal" errUnx w8 = err $ "Unexpected " ++ show (chr (fromIntegral w8)) ++ " while parsing number literal"
src/Data/Aeson/Types/FromJSON.hs view
@@ -95,6 +95,7 @@ import Data.Aeson.Types.Generic import Data.Aeson.Types.Internal import Data.Bits (unsafeShiftR)+import Data.Char (isAlphaNum, isDigit) import Data.Complex (Complex(..)) import Data.Fixed (Fixed, HasResolution (resolution), Nano) import Data.Functor.Compose (Compose(..))@@ -121,11 +122,10 @@ import Data.Traversable as Tr (sequence) import Data.Tuple.Solo (Solo (..)) import Data.Type.Coercion (Coercion (..))-import Data.Version (Version, parseVersion)+import Data.Version (Version (..)) import Foreign.Storable (Storable) import Foreign.C.Types (CTime (..)) import GHC.Generics-import Text.ParserCombinators.ReadP (readP_to_S) import Unsafe.Coerce (unsafeCoerce) import qualified Data.Aeson.Parser.Time as Time import qualified Data.Aeson.Key as Key@@ -1863,12 +1863,20 @@ instance FromJSONKey Version where fromJSONKey = FromJSONKeyTextParser parseVersionText +-- | Accepts the same strings as 'Data.Version.parseVersion', which takes+-- quadratic time in the length of the input (#1198). parseVersionText :: Text -> Parser Version-parseVersionText = go . readP_to_S parseVersion . unpack+parseVersionText t = case T.splitOn "-" t of+ branch : tags+ | Just ns <- traverse number (T.splitOn "." branch)+ , all isTag tags+ -> return (Version ns (map unpack tags))+ _ -> fail "parsing Version failed" where- go [(v,[])] = return v- go (_ : xs) = go xs- go _ = fail "parsing Version failed"+ number s+ | not (T.null s) && T.all isDigit s = Just (read (unpack s))+ | otherwise = Nothing+ isTag s = not (T.null s) && T.all isAlphaNum s ------------------------------------------------------------------------------- -- semigroups NonEmpty
tests/JSONTestSuite.hs view
@@ -156,7 +156,6 @@ ignore_accepted :: HashSet.HashSet FilePath ignore_accepted = HashSet.fromList [ "i_number_double_huge_neg_exp.json"- , "i_number_huge_exp.json" , "i_number_neg_int_huge_exp.json" , "i_number_pos_double_huge_exp.json" , "i_number_real_neg_overflow.json"
tests/JSONTestSuite/results/i_number_huge_exp.tok view
@@ -1,4 +1,3 @@ TkArrayOpen TkItem-TkNumber NumScientific 4.0e-30000000995-TkArrayEnd+TkErr "Scientific notation with exponent too large"
+ tests/Regression/Issue1198.hs view
@@ -0,0 +1,32 @@+{-# LANGUAGE OverloadedStrings #-}+module Regression.Issue1198 (issue1198) where++import Data.Version (Version, makeVersion, parseVersion)+import Test.QuickCheck (Gen, elements, forAll, listOf, (===))+import Test.Tasty (TestTree, localOption, mkTimeout, testGroup)+import Test.Tasty.HUnit (assertEqual, testCase)+import Test.Tasty.QuickCheck (testProperty)+import Text.ParserCombinators.ReadP (readP_to_S)+import qualified Data.Text as T++import Data.Aeson+import Data.Aeson.Types (parseMaybe)++-- | The previous implementation.+viaReadP :: String -> Maybe Version+viaReadP s = case [v | (v, "") <- readP_to_S parseVersion s] of+ v : _ -> Just v+ [] -> Nothing++versionLike :: Gen String+versionLike = listOf (elements "0123456789..--abZ_ \x0663\x00e9")++issue1198 :: TestTree+issue1198 = testGroup "Issue #1198"+ [ testProperty "accepts the same strings as parseVersion" $ forAll versionLike $ \s ->+ parseMaybe parseJSON (String (T.pack s)) === viaReadP s+ , localOption (mkTimeout 5000000) $ testCase "long version" $ do+ let n = 100000+ assertEqual "" (Just (makeVersion (replicate n 1)))+ (parseMaybe parseJSON (String (T.intercalate "." (replicate n "1"))))+ ]
tests/UnitTests.hs view
@@ -28,7 +28,7 @@ import Prelude.Compat import Control.Monad (forM, forM_, when)-import Data.Aeson ((.=), (.:), (.:?), (.:!), FromJSON(..), ToJSON1(..), decode, eitherDecode, encode, fromJSON, genericParseJSON, genericToEncoding, genericToJSON, object, withObject, withEmbeddedJSON)+import Data.Aeson ((.=), (.:), (.:?), (.:!), FromJSON(..), ToJSON1(..), decode, eitherDecode, eitherDecodeStrict, encode, fromJSON, genericParseJSON, genericToEncoding, genericToJSON, object, withObject, withEmbeddedJSON, eitherDecodeStrictText) import Data.Aeson.Types (JSONPathElement(..), formatError) import Data.Aeson.QQ.Simple (aesonQQ) import Data.Aeson.TH (deriveJSON, deriveToJSON, deriveToJSON1)@@ -43,6 +43,7 @@ import Data.Kind (Type) import Data.List (isSuffixOf) import Data.Scientific (Scientific, scientific)+import Data.String (IsString, fromString) import GHC.Generics (Generic) #if __GLASGOW_HASKELL__ >= 806 import GHC.Generics.Generically (Generically (..))@@ -66,6 +67,7 @@ import Regression.Issue687 import Regression.Issue967 import Regression.Issue1138+import Regression.Issue1198 import UnitTests.OmitNothingFieldsNote import UnitTests.FromJSONKey import UnitTests.Hashable@@ -504,7 +506,7 @@ bigNaturalKeyDecoding :: Assertion bigNaturalKeyDecoding = assertEqual "Decoding an Integer key with a large exponent should fail"- (Left "Error in $['1e2000']: found a number with exponent 2000, but it must not be greater than 1024")+ (Left "Error in $['1e2000']: parsing Natural failed, found a number with exponent 2000, but it must not be greater than 1024") ((eitherDecode :: L.ByteString -> Either String (HashMap Natural Value)) "{ \"1e2000\": null }") -- A regression test for: https://github.com/bos/aeson/issues/757@@ -513,6 +515,26 @@ newtype Newtype757 a = MkNewtype757 (Fam757 a) deriveToJSON1 defaultOptions ''Newtype757 +veryBigExponent :: Assertion+veryBigExponent = do+ let big, small :: IsString a => a+ big = fromString $ "1e" ++ show (toInteger (maxBound :: Int) + 1)+ small = fromString $ "1.1e" ++ show (toInteger (minBound :: Int))+ bigErr = Left "Scientific notation with exponent too large" :: Either String Value+ smallErr = Left "Scientific notation with exponent too small" :: Either String Value+ assertEqual "Reject very large exponent even in Value (Strict BS)"+ bigErr (eitherDecodeStrict big)+ assertEqual "Reject very negative exponent even in Value (Strict BS)"+ smallErr (eitherDecodeStrict small)+ assertEqual "Reject very large exponent even in Value (Lazy BS)"+ bigErr (eitherDecode big)+ assertEqual "Reject very negative exponent even in Value (Lazy BS)"+ smallErr (eitherDecode small)+ assertEqual "Reject very large exponent even in Value (Strict Text)"+ bigErr (eitherDecodeStrictText big)+ assertEqual "Reject very negative exponent even in Value (Strict Text)"+ smallErr (eitherDecodeStrictText small)+ ------------------------------------------------------------------------------- -- Tests trees -------------------------------------------------------------------------------@@ -567,6 +589,8 @@ , testCase "Big integer decoding" bigIntegerDecoding , testCase "Big natural decoding" bigNaturalDecoding , testCase "Big integer key decoding" bigIntegerKeyDecoding+ , testCase "Big natural key decoding" bigNaturalKeyDecoding+ , testCase "Very big exponent" veryBigExponent , testGroup "QQ.Simple" [ testCase "example" $ assertEqual "" (object ["foo" .= True]) [aesonQQ| {"foo": true } |]@@ -577,6 +601,7 @@ , issue687 , issue967 , issue1138+ , issue1198 , keyMapInsertWithTests , omitNothingFieldsNoteTests , noThunksTests