Commit 90145be
Fix quadratic-time PEM scanning in load_ssh_private_key (#15461)
load_ssh_private_key located the OpenSSH PEM block with a regex whose
runtime is quadratic in the length of the input: re.search restarts at
every occurrence of the opening marker and rescans to the end of the
input looking for a closing marker, so input with an opening marker and
no closing marker drives O(n^2) work before any parsing or size check.
An attacker who can supply data to the function can consume unbounded
CPU (~8s for 273 KiB, extrapolating to hours for a few MiB).
Locate the markers directly with bytes.find instead. The first opening
marker and the first closing marker after it delimit exactly what the
lazy regex matched, and finding them is linear. memoryview inputs, which
_check_byteslike permits but bytes.find does not, are converted first.
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>1 parent e448a0c commit 90145be
2 files changed
Lines changed: 37 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
81 | 81 | | |
82 | 82 | | |
83 | 83 | | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | 84 | | |
88 | 85 | | |
89 | 86 | | |
| |||
684 | 681 | | |
685 | 682 | | |
686 | 683 | | |
687 | | - | |
688 | | - | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
| 692 | + | |
| 693 | + | |
| 694 | + | |
| 695 | + | |
689 | 696 | | |
690 | | - | |
691 | | - | |
692 | 697 | | |
693 | 698 | | |
694 | 699 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
485 | 485 | | |
486 | 486 | | |
487 | 487 | | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
488 | 513 | | |
489 | 514 | | |
490 | 515 | | |
| |||
0 commit comments