From b13a43efaf6ca905a9be04252df31480178848c2 Mon Sep 17 00:00:00 2001 From: Andreas Auernhammer Date: Thu, 8 Apr 2021 14:34:13 +0200 Subject: [PATCH] trim carriage-return at the end of signature comments This commit fixes a bug when parsing minisign signatures. Before, any carriage return ('\r') at the end of a trusted / untrusted comment was not removed. This lead to signature verification failures. In particular, the verification of the comment signature fails when e.g. the OS uses `\r\n` for newlines instead of `\n`. Byte representation of a trusted comment - with and without the trailing carriage return. ``` [116 105 109 101 115 116 97 109 112 58 49 54 49 55 55 50 49 48 50 51 9 102 105 108 101 58 114 111 98 116 101 115 116 46 112 115 49 13] [116 105 109 101 115 116 97 109 112 58 49 54 49 55 55 50 49 48 50 51 9 102 105 108 101 58 114 111 98 116 101 115 116 46 112 115 49] ``` Fixes #7 --- internal/testdata/robtest.ps1.minisig | 4 ++++ signature.go | 4 ++-- signature_test.go | 18 +++++++++++++++++- 3 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 internal/testdata/robtest.ps1.minisig diff --git a/internal/testdata/robtest.ps1.minisig b/internal/testdata/robtest.ps1.minisig new file mode 100644 index 0000000..7d09aa4 --- /dev/null +++ b/internal/testdata/robtest.ps1.minisig @@ -0,0 +1,4 @@ +untrusted comment: signature from minisign secret key +RWQ3ly9IPenQ6XE4gvV0tpJPSRdw/Si+Q4r97LbpLj0Hb3sV+XFydynJg3iFT2PjIlE3xViNOmFT9XrIoidedDr41+Ly0AYbUQg= +trusted comment: timestamp:1617721023 file:robtest.ps1 +HkxuqHSvipJo/unNKgDS+JGDB0+Q5d8nOeoJ0NGOnKBNsNdvAj8FWf7fhaPV7mzRJ1ooLvYpI0yUsD7lpaDwBQ== diff --git a/signature.go b/signature.go index 3e24b37..e49900d 100644 --- a/signature.go +++ b/signature.go @@ -141,9 +141,9 @@ func (s *Signature) UnmarshalText(text []byte) error { } var ( - untrustedComment = segments[0] + untrustedComment = strings.TrimRight(segments[0], "\r") encodedSignature = segments[1] - trustedComment = segments[2] + trustedComment = strings.TrimRight(segments[2], "\r") encodedCommentSignature = segments[3] ) if !strings.HasPrefix(untrustedComment, "untrusted comment: ") { diff --git a/signature_test.go b/signature_test.go index 30a0322..aa1f3e3 100644 --- a/signature_test.go +++ b/signature_test.go @@ -4,7 +4,10 @@ package minisign -import "testing" +import ( + "strings" + "testing" +) func TestEqualSignature(t *testing.T) { for i, test := range equalSignatureTests { @@ -64,6 +67,19 @@ func TestUnmarshalSignature(t *testing.T) { } } +func TestSignatureCarriageReturn(t *testing.T) { + signature, err := SignatureFromFile("./internal/testdata/robtest.ps1.minisig") + if err != nil { + t.Fatalf("Failed to read signature from file: %v", err) + } + if strings.HasSuffix(signature.UntrustedComment, "\r") { + t.Fatal("Untrusted comment ends with a carriage return") + } + if strings.HasSuffix(signature.TrustedComment, "\r") { + t.Fatal("Trusted comment ends with a carriage return") + } +} + var equalSignatureTests = []struct { A, B Signature Equal bool