Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/spec/lib/postgres/postgres-pr/connection_spec.rb
Views: 11784
require 'postgres/postgres-pr/connection'12RSpec.describe Msf::Db::PostgresPR::Connection do3describe '#negotiate_sasl' do4subject { described_class.allocate }5let(:user) { 'postgres' }6let(:password) { 'mysecretpassword' }7let(:server_responses) { [] }89before(:each) do10allow(subject).to receive(:write_message)11read_message_mock = allow(Msf::Db::PostgresPR::Message).to receive(:read)12read_message_mock.and_return(*server_responses) if server_responses.any?13allow(SecureRandom).to receive(:bytes).with(32).and_return(("\x01" * 32).b)14end1516context 'when the mechanism contains SCRAM-SHA-256' do17context 'and the negotiation is a success' do18let(:server_responses) do19[20# server-first, containing server nonce, salt, and iteration count21Msf::Db::PostgresPR::AuthenticationSASLContinue.new(22value: 'r=AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE=FUeV3rVpQpa2s8ECj3aXa6vw,s=RwsYP2UCANr95SzCJfmP4A==,i=4096'23),24# server-final, server signature25Msf::Db::PostgresPR::AuthenticationSASLFinal.new(26value: 'v=V4CwoEsGBGMe2jGf5lpKbapnqiooWXnoyuHT3VDl6WY='27)28]29end3031it 'negotaites successfully' do32message = Msf::Db::PostgresPR::AuthenticationSASL.new(33mechanisms: ['SCRAM-SHA-256']34)35subject.negotiate_sasl(message, user, password)36expect(subject).to have_received(:write_message).with(37Msf::Db::PostgresPR::SaslInitialResponseMessage.new(38mechanism: 'SCRAM-SHA-256',39value: 'n,,n=postgres,r=AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE='40)41).ordered42expect(subject).to have_received(:write_message).with(43Msf::Db::PostgresPR::SASLResponseMessage.new(44value: 'c=biws,r=AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE=FUeV3rVpQpa2s8ECj3aXa6vw,p=MN8FiTy5Aqut/H/TOggmlOWXHmpI/+RrnNgQFBk1eBs='45)46).ordered47end48end4950context 'and server-final does not contain the expected calculated server proof' do51let(:server_responses) do52[53# server-first, containing server nonce, salt, and iteration count54Msf::Db::PostgresPR::AuthenticationSASLContinue.new(55value: 'r=AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE=FUeV3rVpQpa2s8ECj3aXa6vw,s=RwsYP2UCANr95SzCJfmP4A==,i=4096'56),57# server-final, server signature58Msf::Db::PostgresPR::AuthenticationSASLFinal.new(59value: 'v=invalid_server_proof'60)61]62end6364it 'raises an error' do65message = Msf::Db::PostgresPR::AuthenticationSASL.new(66mechanisms: ['SCRAM-SHA-256']67)68expect { subject.negotiate_sasl(message, user, password) }.to raise_error 'Server proof failed'69end70end7172context 'and the password is invalid' do73let(:server_responses) do74[75# server-first, containing server nonce, salt, and iteration count76Msf::Db::PostgresPR::AuthenticationSASLContinue.new(77value: 'r=2kRpTcHEFyoG+UgDEpRBdVcJLTWh5WtxARhYOHcG27i7YxAi,s=GNpgixWS5E4INbrMf665Kw==,i=4096'78),79# For auth failure; server-final isn't AuthenticationSASLFinal - but just a generic Postgres ErrorResponse80Msf::Db::PostgresPR::ErrorResponse.new(8183,82["FATAL", "VFATAL", "C28P01", "Mpassword authentication failed for user \"user\"", "Fauth.c", "L326", "Rauth_failed"]83)84]85end8687it 'raises an error' do88message = Msf::Db::PostgresPR::AuthenticationSASL.new(89mechanisms: ['SCRAM-SHA-256']90)91# Runtime error raised for consistency with login scanner expectations, but could be changed to a better exception in the future92expect { subject.negotiate_sasl(message, user, password) }.to raise_error RuntimeError, "FATAL\tVFATAL\tC28P01\tMpassword authentication failed for user \"user\"\tFauth.c\tL326\tRauth_failed"93end94end9596context 'and a AuthenticationSASLContinue is not returned' do97let(:server_responses) do98[99nil100]101end102it 'raises' do103message = Msf::Db::PostgresPR::AuthenticationSASL.new(104mechanisms: ['SCRAM-SHA-256']105)106expect { subject.negotiate_sasl(message, user, password) }.to raise_error Msf::Db::PostgresPR::AuthenticationMethodMismatch, /Did not receive AuthenticationSASLContinue/107end108end109end110111context 'when the mechanism is not supported' do112it 'raises an exception' do113message = Msf::Db::PostgresPR::AuthenticationSASL.new(114mechanisms: ['SCRAM-SHA-256-PLUS']115)116expect { subject.negotiate_sasl(message, user, password) }.to raise_error Msf::Db::PostgresPR::AuthenticationMethodMismatch, /unsupported SASL mechanisms/117end118end119end120end121122123