|
501 | 501 | end |
502 | 502 | end |
503 | 503 |
|
| 504 | + describe "top_origin validation" do |
| 505 | + let(:client) { WebAuthn::FakeClient.new(origin, encoding: false, cross_origin:, top_origin: client_top_origin) } |
| 506 | + let(:top_origin) { fake_top_origin } |
| 507 | + |
| 508 | + before do |
| 509 | + WebAuthn.configuration.allowed_top_origins = [top_origin] |
| 510 | + end |
| 511 | + |
| 512 | + context "when cross_origin is true" do |
| 513 | + let(:cross_origin) { "true" } |
| 514 | + |
| 515 | + context "when top_origin is set" do |
| 516 | + context "when top_origin matches client top_origin" do |
| 517 | + let(:client_top_origin) { top_origin } |
| 518 | + |
| 519 | + it "verifies" do |
| 520 | + expect( |
| 521 | + assertion_response.verify(original_challenge, public_key: credential_public_key, sign_count: 0) |
| 522 | + ).to be_truthy |
| 523 | + end |
| 524 | + |
| 525 | + it "is valid" do |
| 526 | + expect( |
| 527 | + assertion_response.valid?(original_challenge, public_key: credential_public_key, sign_count: 0) |
| 528 | + ).to be_truthy |
| 529 | + end |
| 530 | + end |
| 531 | + |
| 532 | + context "when top_origin does not match client top_origin" do |
| 533 | + let(:client_top_origin) { "https://malicious.example.com" } |
| 534 | + |
| 535 | + it "is invalid" do |
| 536 | + expect(assertion_response.valid?(original_challenge, public_key: credential_public_key, sign_count: 0)).to be_falsy |
| 537 | + end |
| 538 | + |
| 539 | + it "doesn't verify" do |
| 540 | + expect { |
| 541 | + assertion_response.verify(original_challenge, public_key: credential_public_key, sign_count: 0) |
| 542 | + }.to raise_exception(WebAuthn::TopOriginVerificationError) |
| 543 | + end |
| 544 | + end |
| 545 | + end |
| 546 | + |
| 547 | + context "when top_origin is not set" do |
| 548 | + let(:client_top_origin) { nil } |
| 549 | + |
| 550 | + it "is invalid" do |
| 551 | + expect(assertion_response.valid?(original_challenge, public_key: credential_public_key, sign_count: 0)).to be_falsy |
| 552 | + end |
| 553 | + |
| 554 | + it "doesn't verify" do |
| 555 | + expect { |
| 556 | + assertion_response.verify(original_challenge, public_key: credential_public_key, sign_count: 0) |
| 557 | + }.to raise_exception(WebAuthn::TopOriginVerificationError) |
| 558 | + end |
| 559 | + end |
| 560 | + end |
| 561 | + |
| 562 | + context "when cross_origin is false" do |
| 563 | + let(:cross_origin) { "false" } |
| 564 | + |
| 565 | + context "when top_origin is set" do |
| 566 | + context "when top_origin matches client top_origin" do |
| 567 | + let(:client_top_origin) { top_origin } |
| 568 | + |
| 569 | + it "is invalid" do |
| 570 | + expect(assertion_response.valid?(original_challenge, public_key: credential_public_key, sign_count: 0)).to be_falsy |
| 571 | + end |
| 572 | + |
| 573 | + it "doesn't verify" do |
| 574 | + expect { |
| 575 | + assertion_response.verify(original_challenge, public_key: credential_public_key, sign_count: 0) |
| 576 | + }.to raise_exception(WebAuthn::TopOriginVerificationError) |
| 577 | + end |
| 578 | + end |
| 579 | + |
| 580 | + context "when top_origin does not match client top_origin" do |
| 581 | + let(:client_top_origin) { "https://malicious.example.com" } |
| 582 | + |
| 583 | + it "is invalid" do |
| 584 | + expect(assertion_response.valid?(original_challenge, public_key: credential_public_key, sign_count: 0)).to be_falsy |
| 585 | + end |
| 586 | + |
| 587 | + it "doesn't verify" do |
| 588 | + expect { |
| 589 | + assertion_response.verify(original_challenge, public_key: credential_public_key, sign_count: 0) |
| 590 | + }.to raise_exception(WebAuthn::TopOriginVerificationError) |
| 591 | + end |
| 592 | + end |
| 593 | + |
| 594 | + context "when top_origin is not set" do |
| 595 | + let(:client_top_origin) { nil } |
| 596 | + |
| 597 | + it "verifies" do |
| 598 | + expect( |
| 599 | + assertion_response.verify(original_challenge, public_key: credential_public_key, sign_count: 0) |
| 600 | + ).to be_truthy |
| 601 | + end |
| 602 | + |
| 603 | + it "is valid" do |
| 604 | + expect( |
| 605 | + assertion_response.valid?(original_challenge, public_key: credential_public_key, sign_count: 0) |
| 606 | + ).to be_truthy |
| 607 | + end |
| 608 | + end |
| 609 | + end |
| 610 | + end |
| 611 | + |
| 612 | + context "when cross_origin is not set" do |
| 613 | + let(:cross_origin) { nil } |
| 614 | + |
| 615 | + context "when top_origin is set" do |
| 616 | + context "when top_origin matches client top_origin" do |
| 617 | + let(:client_top_origin) { top_origin } |
| 618 | + |
| 619 | + it "is invalid" do |
| 620 | + expect(assertion_response.valid?(original_challenge, public_key: credential_public_key, sign_count: 0)).to be_falsy |
| 621 | + end |
| 622 | + |
| 623 | + it "doesn't verify" do |
| 624 | + expect { |
| 625 | + assertion_response.verify(original_challenge, public_key: credential_public_key, sign_count: 0) |
| 626 | + }.to raise_exception(WebAuthn::TopOriginVerificationError) |
| 627 | + end |
| 628 | + end |
| 629 | + |
| 630 | + context "when top_origin does not match client top_origin" do |
| 631 | + let(:client_top_origin) { "https://malicious.example.com" } |
| 632 | + |
| 633 | + it "is invalid" do |
| 634 | + expect(assertion_response.valid?(original_challenge, public_key: credential_public_key, sign_count: 0)).to be_falsy |
| 635 | + end |
| 636 | + |
| 637 | + it "doesn't verify" do |
| 638 | + expect { |
| 639 | + assertion_response.verify(original_challenge, public_key: credential_public_key, sign_count: 0) |
| 640 | + }.to raise_exception(WebAuthn::TopOriginVerificationError) |
| 641 | + end |
| 642 | + end |
| 643 | + |
| 644 | + context "when top_origin is not set" do |
| 645 | + let(:client_top_origin) { nil } |
| 646 | + |
| 647 | + it "verifies" do |
| 648 | + expect( |
| 649 | + assertion_response.verify(original_challenge, public_key: credential_public_key, sign_count: 0) |
| 650 | + ).to be_truthy |
| 651 | + end |
| 652 | + |
| 653 | + it "is valid" do |
| 654 | + expect( |
| 655 | + assertion_response.valid?(original_challenge, public_key: credential_public_key, sign_count: 0) |
| 656 | + ).to be_truthy |
| 657 | + end |
| 658 | + end |
| 659 | + end |
| 660 | + end |
| 661 | + end |
| 662 | + |
504 | 663 | describe "migrated U2F credential" do |
505 | 664 | let(:origin) { "https://example.org" } |
506 | 665 | let(:app_id) { "#{origin}/appid" } |
|
0 commit comments