Skip to content

Commit 2e0f1ef

Browse files
committed
age,cmd/age: detect invalid UTF-8 in identity and recipient files
For #663
1 parent a7586b7 commit 2e0f1ef

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

cmd/age/parse.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"io"
1212
"os"
1313
"strings"
14+
"unicode/utf8"
1415

1516
"filippo.io/age"
1617
"filippo.io/age/agessh"
@@ -77,6 +78,9 @@ func parseRecipientsFile(name string) ([]age.Recipient, error) {
7778
if strings.HasPrefix(line, "#") || line == "" {
7879
continue
7980
}
81+
if !utf8.ValidString(line) {
82+
return nil, fmt.Errorf("%q: recipients file is not valid UTF-8", name)
83+
}
8084
if len(line) > lineLengthLimit {
8185
return nil, fmt.Errorf("%q: line %d is too long", name, n)
8286
}
@@ -226,19 +230,20 @@ func parseIdentities(f io.Reader) ([]age.Identity, error) {
226230
if strings.HasPrefix(line, "#") || line == "" {
227231
continue
228232
}
229-
233+
if !utf8.ValidString(line) {
234+
return nil, fmt.Errorf("identities file is not valid UTF-8")
235+
}
230236
i, err := parseIdentity(line)
231237
if err != nil {
232238
return nil, fmt.Errorf("error at line %d: %v", n, err)
233239
}
234240
ids = append(ids, i)
235-
236241
}
237242
if err := scanner.Err(); err != nil {
238-
return nil, fmt.Errorf("failed to read secret keys file: %v", err)
243+
return nil, fmt.Errorf("failed to read identities file: %v", err)
239244
}
240245
if len(ids) == 0 {
241-
return nil, fmt.Errorf("no secret keys found")
246+
return nil, fmt.Errorf("no identities found")
242247
}
243248
return ids, nil
244249
}

parse.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"fmt"
1010
"io"
1111
"strings"
12+
"unicode/utf8"
1213
)
1314

1415
// ParseIdentities parses a file with one or more private key encodings, one per
@@ -31,17 +32,20 @@ func ParseIdentities(f io.Reader) ([]Identity, error) {
3132
if strings.HasPrefix(line, "#") || line == "" {
3233
continue
3334
}
35+
if !utf8.ValidString(line) {
36+
return nil, fmt.Errorf("identities file is not valid UTF-8")
37+
}
3438
i, err := parseIdentity(line)
3539
if err != nil {
3640
return nil, fmt.Errorf("error at line %d: %v", n, err)
3741
}
3842
ids = append(ids, i)
3943
}
4044
if err := scanner.Err(); err != nil {
41-
return nil, fmt.Errorf("failed to read secret keys file: %v", err)
45+
return nil, fmt.Errorf("failed to read identities file: %v", err)
4246
}
4347
if len(ids) == 0 {
44-
return nil, fmt.Errorf("no secret keys found")
48+
return nil, fmt.Errorf("no identities found")
4549
}
4650
return ids, nil
4751
}
@@ -78,6 +82,9 @@ func ParseRecipients(f io.Reader) ([]Recipient, error) {
7882
if strings.HasPrefix(line, "#") || line == "" {
7983
continue
8084
}
85+
if !utf8.ValidString(line) {
86+
return nil, fmt.Errorf("recipients file is not valid UTF-8")
87+
}
8188
r, err := parseRecipient(line)
8289
if err != nil {
8390
// Hide the error since it might unintentionally leak the contents

0 commit comments

Comments
 (0)