Skip to content

Commit 37ace5e

Browse files
authored
Merge pull request #815 from smallstep/herman/fix-empty-dns-init
Add check for empty DNS value in ca init
2 parents 7babb90 + 226d80d commit 37ace5e

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

command/ca/init.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"crypto/rand"
66
"crypto/x509"
7+
stderrors "errors"
78
"fmt"
89
"io"
910
"net"
@@ -823,8 +824,11 @@ func processDNSValue(dnsValue string) ([]string, error) {
823824
)
824825
dnsValue = strings.ReplaceAll(dnsValue, " ", ",")
825826
parts := strings.Split(dnsValue, ",")
827+
if allEmpty(parts) {
828+
return nil, stderrors.New("dns must not be empty")
829+
}
826830
for _, name := range parts {
827-
if name == "" {
831+
if name == "" { // skip empty name
828832
continue
829833
}
830834
if err := dnsValidator(name); err != nil {
@@ -845,3 +849,14 @@ func normalize(name string) string {
845849
}
846850
return name
847851
}
852+
853+
// allEmpty loops through all strings in the slice and returns if
854+
// all are empty (length 0).
855+
func allEmpty(parts []string) bool {
856+
for _, p := range parts {
857+
if p != "" {
858+
return false
859+
}
860+
}
861+
return true
862+
}

command/ca/init_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,19 @@ func Test_processDNSValue(t *testing.T) {
1414
want []string
1515
wantErr bool
1616
}{
17+
18+
{
19+
name: "fail/empty",
20+
dnsValue: "",
21+
want: nil,
22+
wantErr: true,
23+
},
24+
{
25+
name: "fail/empty-multiple",
26+
dnsValue: ",,",
27+
want: nil,
28+
wantErr: true,
29+
},
1730
{
1831
name: "fail/dns",
1932
dnsValue: "ca.smallstep.com:8443",
@@ -44,6 +57,12 @@ func Test_processDNSValue(t *testing.T) {
4457
want: []string{"ca.smallstep.com", "ca.localhost"},
4558
wantErr: false,
4659
},
60+
{
61+
name: "ok/multi-dns-with-skip",
62+
dnsValue: "ca.smallstep.com,ca.localhost,,test.localhost",
63+
want: []string{"ca.smallstep.com", "ca.localhost", "test.localhost"},
64+
wantErr: false,
65+
},
4766
{
4867
name: "ok/multi-space-dns",
4968
dnsValue: "ca.smallstep.com ca.localhost",

0 commit comments

Comments
 (0)