Skip to content

Commit f26e94f

Browse files
committed
Add tests for failing to decode a signature
1 parent 5f3cbdf commit f26e94f

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

spec/encoding_spec.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,5 +834,39 @@
834834
data, total_read = SSHData::Encoding.decode_openssh_signature(signature_appended, offset = prefix.bytesize)
835835
expect(total_read).to eq(signature.bytesize)
836836
end
837+
838+
it "fails if the signature does not begin with wrong magic prefix" do
839+
wrong_magic = signature.gsub(/^SSHSIG/, "NOGOOD")
840+
expect { SSHData::Encoding.decode_openssh_signature(wrong_magic) }.to raise_error(SSHData::DecodeError)
841+
end
842+
843+
it "fails if the signature is shorter than magic prefix" do
844+
short_sig = "OHHI"
845+
expect { SSHData::Encoding.decode_openssh_signature(short_sig) }.to raise_error(SSHData::DecodeError)
846+
end
847+
848+
it "fails if the signature has magic but no decodable signature" do
849+
bad_sig = "SSHSIGOHNO"
850+
expect { SSHData::Encoding.decode_openssh_signature(bad_sig) }.to raise_error(SSHData::DecodeError)
851+
end
852+
853+
it "fails if the signature is corrupt" do
854+
# 6 for the magic prefix
855+
# 4 for the signature version (int32)
856+
# The next 4 will be the length-prefixed the public key.
857+
# Corrupt the key by setting the length to something bogus and maintain the rest of the key.
858+
start = 6 + 4
859+
finish = start + 4
860+
bad_sig = signature.dup
861+
bad_sig[start...finish] = "\xFF\xFF\xFF\xFF".force_encoding('BINARY')
862+
expect(bad_sig.length).to eq(signature.length)
863+
expect { SSHData::Encoding.decode_openssh_signature(bad_sig) }.to raise_error(SSHData::DecodeError)
864+
end
865+
866+
it "fails if the signature is too short" do
867+
# Hack a byte off at the end.
868+
bad_sig = signature.byteslice((..-2))
869+
expect { SSHData::Encoding.decode_openssh_signature(bad_sig) }.to raise_error(SSHData::DecodeError)
870+
end
837871
end
838872
end

0 commit comments

Comments
 (0)