@@ -825,12 +825,14 @@ def start_element(name, _):
825825
826826class AttackProtectionTest (unittest .TestCase ):
827827
828- def billion_laughs (self , ncols , nrows , text = '.' , indent = ' ' ):
829- """Create a billion laugh payload.
828+ def exponential_expansion_payload (self , ncols , nrows , text = '.' ):
829+ """Create a billion laughs attack payload.
830830
831831 Be careful: the number of total items is pow(n, k), thereby
832832 requiring at least pow(ncols, nrows) * sizeof(text) memory!
833833 """
834+ # 'indent' affects the peak amplification factor and allocation
835+ indent = ' ' * 2
834836 body = textwrap .indent ('\n ' .join (
835837 f'<!ENTITY row{ i + 1 } "{ f"&row{ i } ;" * ncols } ">'
836838 for i in range (nrows )
@@ -847,9 +849,9 @@ def billion_laughs(self, ncols, nrows, text='.', indent=' '):
847849
848850 def test_set_alloc_tracker_maximum_amplification (self ):
849851 # On WASI, the maximum amplification factor of the payload may differ,
850- # so we craft a payload that is likely to yield an allocation factor
852+ # so we craft a payload that is likely to yield an amplification factor
851853 # way larger than 1.0 and way smaller than 10^5.
852- payload = self .billion_laughs (1 , 2 )
854+ payload = self .exponential_expansion_payload (1 , 2 )
853855
854856 p = expat .ParserCreate ()
855857 # Unconditionally enable maximum amplification factor.
@@ -859,7 +861,7 @@ def test_set_alloc_tracker_maximum_amplification(self):
859861 msg = r"out of memory: line \d+, column \d+"
860862 self .assertRaisesRegex (expat .ExpatError , msg , p .Parse , payload )
861863
862- # # Re-create a parser as the current parser is now in an error state.
864+ # Re-create a parser as the current parser is now in an error state.
863865 p = expat .ParserCreate ()
864866 # Unconditionally enable maximum amplification factor.
865867 p .SetAllocTrackerActivationThreshold (0 )
@@ -880,11 +882,11 @@ def test_set_alloc_tracker_maximum_amplification_invalid_args(self):
880882 self .assertRaisesRegex (expat .ExpatError , msg , fsub , 1.0 )
881883
882884 def test_set_alloc_tracker_activation_threshold (self ):
883- # Run the test with EXPAT_MALLOC_DEBUG=2 to detect those constants.
885+ # Run the test with EXPAT_MALLOC_DEBUG=2 to find those constants.
884886 MAX_ALLOC = 17333
885887 MIN_ALLOC = 1096
886888
887- payload = self .billion_laughs (10 , 4 )
889+ payload = self .exponential_expansion_payload (10 , 4 )
888890
889891 p = expat .ParserCreate ()
890892 p .SetAllocTrackerActivationThreshold (MAX_ALLOC + 1 )
0 commit comments