Skip to content

Commit 04eae0b

Browse files
committed
autocert: validate SNI values more, add tests
Change-Id: I810c8dcc90c056d7fa66bba59c0936f54aabdfc7 Reviewed-on: https://go-review.googlesource.com/42497 Run-TryBot: Alex Vaghin <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Alex Vaghin <[email protected]>
1 parent 2292f58 commit 04eae0b

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

acme/autocert/autocert.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ func (m *Manager) GetCertificate(hello *tls.ClientHelloInfo) (*tls.Certificate,
177177
return nil, errors.New("acme/autocert: missing server name")
178178
}
179179

180+
if strings.ContainsAny(name, `/\`) {
181+
return nil, errors.New("acme/autocert: bogus SNI value")
182+
}
183+
180184
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
181185
defer cancel()
182186

acme/autocert/autocert_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,3 +560,42 @@ func TestValidCert(t *testing.T) {
560560
}
561561
}
562562
}
563+
564+
type cacheGetFunc func(ctx context.Context, key string) ([]byte, error)
565+
566+
func (f cacheGetFunc) Get(ctx context.Context, key string) ([]byte, error) {
567+
return f(ctx, key)
568+
}
569+
570+
func (f cacheGetFunc) Put(ctx context.Context, key string, data []byte) error {
571+
return fmt.Errorf("unsupported Put of %q = %q", key, data)
572+
}
573+
574+
func (f cacheGetFunc) Delete(ctx context.Context, key string) error {
575+
return fmt.Errorf("unsupported Delete of %q", key)
576+
}
577+
578+
func TestManagerGetCertificateBogusSNI(t *testing.T) {
579+
m := Manager{
580+
Prompt: AcceptTOS,
581+
Cache: cacheGetFunc(func(ctx context.Context, key string) ([]byte, error) {
582+
return nil, fmt.Errorf("cache.Get of %s", key)
583+
}),
584+
}
585+
tests := []struct {
586+
name string
587+
wantErr string
588+
}{
589+
{"foo.com", "cache.Get of foo.com"},
590+
{"foo.com.", "cache.Get of foo.com"},
591+
{`a\b`, "acme/autocert: bogus SNI value"},
592+
{"", "acme/autocert: missing server name"},
593+
}
594+
for _, tt := range tests {
595+
_, err := m.GetCertificate(&tls.ClientHelloInfo{ServerName: tt.name})
596+
got := fmt.Sprint(err)
597+
if got != tt.wantErr {
598+
t.Errorf("GetCertificate(SNI = %q) = %q; want %q", tt.name, got, tt.wantErr)
599+
}
600+
}
601+
}

0 commit comments

Comments
 (0)