Skip to content

Commit 1e2695c

Browse files
committed
Replace invalid characters in PGP input strings
1 parent b1318e2 commit 1e2695c

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

crypto/pgp.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package crypto
33
import (
44
"bufio"
55
"bytes"
6+
"regexp"
67

78
"github.com/pkg/errors"
89

@@ -16,6 +17,10 @@ type PGPKeyPair struct {
1617
}
1718

1819
func GeneratePGPKeyPair(name, comment, email string) (*PGPKeyPair, error) {
20+
name = makeSafe(name)
21+
comment = makeSafe(comment)
22+
email = makeSafe(email)
23+
1924
// ent type is *openpgp.Entity
2025
ent, err := openpgp.NewEntity(name, comment, email, nil)
2126
if err != nil {
@@ -68,3 +73,12 @@ func GeneratePGPKeyPair(name, comment, email string) (*PGPKeyPair, error) {
6873

6974
return keyPair, nil
7075
}
76+
77+
// From the openpgp package source:
78+
// NewUserId returns a UserId or nil if any of the arguments contain invalid
79+
// characters. The invalid characters are '\x00', '(', ')', '<' and '>'
80+
var safeRe = regexp.MustCompile("[\x00()<>]")
81+
82+
func makeSafe(s string) string {
83+
return safeRe.ReplaceAllString(s, "-")
84+
}

crypto/pgp_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package crypto
2+
3+
import "testing"
4+
5+
func TestMakeSafe(t *testing.T) {
6+
if s := makeSafe("safestring"); s != "safestring" {
7+
t.Errorf("%q != %q", s, "safestring")
8+
}
9+
10+
if s := makeSafe("not-safe<>()\x00"); s != "not-safe-----" {
11+
t.Errorf("%q != %q", s, "not-safe-----")
12+
}
13+
}

0 commit comments

Comments
 (0)