Skip to content

Commit

Permalink
feat(cleartext): Do not include line ending separator in plaintext (#242
Browse files Browse the repository at this point in the history
)

The cleartext plaintext should exclude the line ending separator defined by the Cleartext Signed Message Structure. The line ending separator change was made to enhance interoperability, as the original implementation was non-deterministic—potentially adding a newline when the input lacked one.

GopenPGP removes the additional line ending separator, but this could be directly handled by go-crypto.
  • Loading branch information
lubux authored Nov 8, 2024
1 parent f8b3f21 commit 2d2c789
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
1 change: 1 addition & 0 deletions openpgp/clearsign/clearsign.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ func Decode(data []byte) (b *Block, rest []byte) {
b.Plaintext = append(b.Plaintext, line...)
b.Plaintext = append(b.Plaintext, lf)
}
b.Plaintext = b.Plaintext[:len(b.Plaintext)-1]

// We want to find the extent of the armored data (including any newlines at
// the end).
Expand Down
30 changes: 15 additions & 15 deletions openpgp/clearsign/clearsign_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ func testParse(t *testing.T, input []byte, expected, expectedPlaintext string) {
}

func TestParse(t *testing.T) {
testParse(t, clearsignInput, "Hello world\r\nline 2", "Hello world\nline 2\n")
testParse(t, clearsignInput2, "\r\n\r\n(This message has a couple of blank lines at the start and end.)\r\n\r\n", "\n\n(This message has a couple of blank lines at the start and end.)\n\n\n")
testParse(t, clearsignInput, "Hello world\r\nline 2", "Hello world\nline 2")
testParse(t, clearsignInput2, "\r\n\r\n(This message has a couple of blank lines at the start and end.)\r\n\r\n", "\n\n(This message has a couple of blank lines at the start and end.)\n\n")
}

func TestParseWithNoNewlineAtEnd(t *testing.T) {
Expand All @@ -77,21 +77,21 @@ func TestParseWithNoNewlineAtEnd(t *testing.T) {
var signingTests = []struct {
in, signed, plaintext string
}{
{"", "", "\n"},
{"a", "a", "a\n"},
{"a\n", "a\r\n", "a\n\n"},
{"-a\n", "-a\r\n", "-a\n\n"},
{"--a\nb", "--a\r\nb", "--a\nb\n"},
{"", "", ""},
{"a", "a", "a"},
{"a\n", "a\r\n", "a\n"},
{"-a\n", "-a\r\n", "-a\n"},
{"--a\nb", "--a\r\nb", "--a\nb"},
// leading whitespace
{" a\n", " a\r\n", " a\n\n"},
{" a\n", " a\r\n", " a\n\n"},
{" a\n", " a\r\n", " a\n"},
{" a\n", " a\r\n", " a\n"},
// trailing whitespace (should be stripped)
{"a \n", "a\r\n", "a\n\n"},
{"a ", "a", "a\n"},
{" \n", "\r\n", "\n\n"},
{" ", "", "\n"},
{"a\n \n \nb\n", "a\r\n\r\n\r\nb\r\n", "a\n\n\nb\n\n"},
{"a\n \n \nb\n", "a\r\n\r\n\r\nb\r\n", "a\n\n\nb\n\n"},
{"a \n", "a\r\n", "a\n"},
{"a ", "a", "a"},
{" \n", "\r\n", "\n"},
{" ", "", ""},
{"a\n \n \nb\n", "a\r\n\r\n\r\nb\r\n", "a\n\n\nb\n"},
{"a\n \n \nb\n", "a\r\n\r\n\r\nb\r\n", "a\n\n\nb\n"},
}

func TestVerifyV6(t *testing.T) {
Expand Down

0 comments on commit 2d2c789

Please sign in to comment.