Commit fd8eebb
committed
Bugfix: Send the TLS alert to the peer upon a
This commit (partially) addresses python-trio/trio-websocket#199, which I
traced to a bug in Trio itself. A corresponding bugfix relying on the present
bugfix will be submitted to the Trio Websocket repo.
From the user's perspective, the issue was that a TLS client hanged after
submitting an invalid (e.g. expired) client certificate.
Before this fix, when `SSLStream._retry` caught a `ssl.CertificateError`, the
error was immediately re-raised (wrapped in a `trio.BrokenResourceError`). The
TLS alert prepared by the `ssl` library and waiting in MemoryBIO was therefore
never sent to the peer.
Now, upon catching a `ssl.CertificateError`, we first check whether we have any
pending outgoing data (`self._outgoing.pending`). If so, the exception is
stashed away and only raised (again, wrapped in a `trio.BrokenResourceError`)
after sending out the pending data (which should contain the alert).
I had first tried an alternative implementation, where the `CertificateError`
was not stashed away. Rather, the error was raised immediately, but only if
there was no pending outgoing data. The idea was to rely on the loop in
`SSLStream._retry`. Upon seeing `CertificateError` for the first time, there
would be pending data, so the exception would not be reraised and the pending
data would be sent out, while upon seeing the `CertificateError` for the second
time, there would be no pending data, so the exception would be re-raised.
However, it turned out that the loop did not always continue (I'm not sure
why), so there was no second time in some situations.
TESTS in `test_ssl.py`
`test_ssl_client_basics` (modified)
Here we test whether the TLS alert sent out by the client reaches
the (blocking) server.
The second (no CA file) and the third (wrong host name) subtest of this test
were modified to check that the server encounters the correct SSL error. In
the old code, the server encountered `UNEXPECTED_EOF_WHILE_READING` (protocol
error) in both subtests. After the fix, it correctly receives
`TLSV1_ALERT_UNKNOWN_CA` and `SSLV3_ALERT_BAD_CERTIFICATE`, respectively.
To facilitate the modified test, function `ssl_echo_serve_sync` (called by
`ssl_echo_server_raw` called by this test) now allows for a special value of
keyword argument `expect_fail`: `"raise"`. When given this value, the error is
expected but raised nevertheless, the idea being that it should be caught and
inspected in the test code.
`test_client_certificate` (new)
Here we test whether the TLS alert sent out by the server reaches
the (blocking) client. The test is modeled on `test_ssl_server_basics`.
The server is configured to require client authentication (`SERVER_CTX_REQ`,
defined at the top of the file). In the first subtest, the client submits a
valid certificate; in the second subtest, an expired one.
There is a complication with the second subtest. If the client does not send
out the TLS alert (like before the bugfix), the server hangs, but we don't want
the test to hang. I could think of no other way to test whether the server
hangs than imposing an arbitrary (small) timeout, and there is a (very) small
chance that even the correct code will not finish within the allotted time,
resulting in a false negative.ssl.CertificateError.1 parent 82af3ab commit fd8eebb
File tree
3 files changed
+159
-15
lines changed- newsfragments
- src/trio
- _tests
3 files changed
+159
-15
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
394 | 394 | | |
395 | 395 | | |
396 | 396 | | |
| 397 | + | |
| 398 | + | |
397 | 399 | | |
398 | 400 | | |
399 | 401 | | |
| |||
499 | 501 | | |
500 | 502 | | |
501 | 503 | | |
502 | | - | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
503 | 517 | | |
504 | 518 | | |
505 | 519 | | |
| |||
633 | 647 | | |
634 | 648 | | |
635 | 649 | | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
636 | 657 | | |
637 | 658 | | |
638 | 659 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
| |||
77 | 78 | | |
78 | 79 | | |
79 | 80 | | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
80 | 88 | | |
81 | 89 | | |
82 | 90 | | |
| |||
105 | 113 | | |
106 | 114 | | |
107 | 115 | | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
108 | 119 | | |
109 | 120 | | |
110 | 121 | | |
| |||
142 | 153 | | |
143 | 154 | | |
144 | 155 | | |
145 | | - | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
146 | 159 | | |
147 | 160 | | |
148 | 161 | | |
| |||
453 | 466 | | |
454 | 467 | | |
455 | 468 | | |
456 | | - | |
457 | | - | |
458 | | - | |
459 | | - | |
460 | | - | |
461 | | - | |
462 | | - | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
463 | 483 | | |
464 | 484 | | |
465 | | - | |
466 | | - | |
467 | | - | |
468 | | - | |
469 | | - | |
470 | | - | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
471 | 496 | | |
472 | 497 | | |
473 | 498 | | |
| |||
503 | 528 | | |
504 | 529 | | |
505 | 530 | | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
| 620 | + | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
| 625 | + | |
506 | 626 | | |
507 | 627 | | |
508 | 628 | | |
| |||
0 commit comments