|
1 | 1 | import sys
|
| 2 | +import time |
2 | 3 |
|
3 | 4 | import unittest
|
4 | 5 | from test import support
|
@@ -571,6 +572,87 @@ def test_max_str_digits(self):
|
571 | 572 | with self.assertRaises(ValueError):
|
572 | 573 | str(i)
|
573 | 574 |
|
| 575 | + def test_denial_of_service_prevented_int_to_str(self): |
| 576 | + """Regression test: ensure we fail before performing O(N**2) work.""" |
| 577 | + maxdigits = sys.get_int_max_str_digits() |
| 578 | + assert maxdigits < 50_000, maxdigits # A test prerequisite. |
| 579 | + get_time = time.process_time |
| 580 | + if get_time() <= 0: # some platforms like WASM lack process_time() |
| 581 | + get_time = time.monotonic |
| 582 | + |
| 583 | + huge_int = int(f'0x{"c"*65_000}', base=16) # 78268 decimal digits. |
| 584 | + digits = 78_268 |
| 585 | + with support.adjust_int_max_str_digits(digits): |
| 586 | + start = get_time() |
| 587 | + huge_decimal = str(huge_int) |
| 588 | + seconds_to_convert = get_time() - start |
| 589 | + self.assertEqual(len(huge_decimal), digits) |
| 590 | + # Ensuring that we chose a slow enough conversion to measure. |
| 591 | + # It takes 0.1 seconds on a Zen based cloud VM in an opt build. |
| 592 | + if seconds_to_convert < 0.005: |
| 593 | + raise unittest.SkipTest('"slow" conversion took only ' |
| 594 | + f'{seconds_to_convert} seconds.') |
| 595 | + |
| 596 | + # We test with the limit almost at the size needed to check performance. |
| 597 | + # The performant limit check is slightly fuzzy, give it a some room. |
| 598 | + with support.adjust_int_max_str_digits(int(.995 * digits)): |
| 599 | + with self.assertRaises(ValueError) as err: |
| 600 | + start = get_time() |
| 601 | + str(huge_int) |
| 602 | + seconds_to_fail_huge = get_time() - start |
| 603 | + self.assertIn('conversion', str(err.exception)) |
| 604 | + self.assertLess(seconds_to_fail_huge, seconds_to_convert/8) |
| 605 | + |
| 606 | + # Now we test that a conversion that would take 30x as long also fails |
| 607 | + # in a similarly fast fashion. |
| 608 | + extra_huge_int = int(f'0x{"c"*500_000}', base=16) # 602060 digits. |
| 609 | + with self.assertRaises(ValueError) as err: |
| 610 | + start = get_time() |
| 611 | + # If not limited, 8 seconds said Zen based cloud VM. |
| 612 | + str(extra_huge_int) |
| 613 | + seconds_to_fail_extra_huge = get_time() - start |
| 614 | + self.assertIn('conversion', str(err.exception)) |
| 615 | + self.assertLess(seconds_to_fail_extra_huge, seconds_to_convert/8) |
| 616 | + |
| 617 | + def test_denial_of_service_prevented_str_to_int(self): |
| 618 | + """Regression test: ensure we fail before performing O(N**2) work.""" |
| 619 | + maxdigits = sys.get_int_max_str_digits() |
| 620 | + assert maxdigits < 100_000, maxdigits # A test prerequisite. |
| 621 | + get_time = time.process_time |
| 622 | + if get_time() <= 0: # some platforms like WASM lack process_time() |
| 623 | + get_time = time.monotonic |
| 624 | + |
| 625 | + digits = 133700 |
| 626 | + huge = '8'*digits |
| 627 | + with support.adjust_int_max_str_digits(digits): |
| 628 | + start = get_time() |
| 629 | + int(huge) |
| 630 | + seconds_to_convert = get_time() - start |
| 631 | + # Ensuring that we chose a slow enough conversion to measure. |
| 632 | + # It takes 0.1 seconds on a Zen based cloud VM in an opt build. |
| 633 | + if seconds_to_convert < 0.005: |
| 634 | + raise unittest.SkipTest('"slow" conversion took only ' |
| 635 | + f'{seconds_to_convert} seconds.') |
| 636 | + |
| 637 | + with support.adjust_int_max_str_digits(digits - 1): |
| 638 | + with self.assertRaises(ValueError) as err: |
| 639 | + start = get_time() |
| 640 | + int(huge) |
| 641 | + seconds_to_fail_huge = get_time() - start |
| 642 | + self.assertIn('conversion', str(err.exception)) |
| 643 | + self.assertLess(seconds_to_fail_huge, seconds_to_convert/8) |
| 644 | + |
| 645 | + # Now we test that a conversion that would take 30x as long also fails |
| 646 | + # in a similarly fast fashion. |
| 647 | + extra_huge = '7'*1_200_000 |
| 648 | + with self.assertRaises(ValueError) as err: |
| 649 | + start = get_time() |
| 650 | + # If not limited, 8 seconds in the Zen based cloud VM. |
| 651 | + int(extra_huge) |
| 652 | + seconds_to_fail_extra_huge = get_time() - start |
| 653 | + self.assertIn('conversion', str(err.exception)) |
| 654 | + self.assertLess(seconds_to_fail_extra_huge, seconds_to_convert/8) |
| 655 | + |
574 | 656 | def test_power_of_two_bases_unlimited(self):
|
575 | 657 | """The limit does not apply to power of 2 bases."""
|
576 | 658 | maxdigits = sys.get_int_max_str_digits()
|
|
0 commit comments