Browse Source

Fix unexpected permission error on an IPv6 configured machine (#176)

when visiting the webui.

When net.SplitHostPort is called, it will return the string "::1",
but the code was only checking for "[::1]".
Sanjay Ghemawat 8 years ago
parent
commit
47fb2f570c
2 changed files with 15 additions and 1 deletions
  1. 1
    1
      internal/driver/webui.go
  2. 14
    0
      internal/driver/webui_test.go

+ 1
- 1
internal/driver/webui.go View File

105
 }
105
 }
106
 
106
 
107
 func isLocalhost(host string) bool {
107
 func isLocalhost(host string) bool {
108
-	for _, v := range []string{"localhost", "127.0.0.1", "[::1]"} {
108
+	for _, v := range []string{"localhost", "127.0.0.1", "[::1]", "::1"} {
109
 		if host == v {
109
 		if host == v {
110
 			return true
110
 			return true
111
 		}
111
 		}

+ 14
- 0
internal/driver/webui_test.go View File

17
 import (
17
 import (
18
 	"fmt"
18
 	"fmt"
19
 	"io/ioutil"
19
 	"io/ioutil"
20
+	"net"
20
 	"net/http"
21
 	"net/http"
21
 	"net/http/httptest"
22
 	"net/http/httptest"
22
 	"net/url"
23
 	"net/url"
235
 		})
236
 		})
236
 	}
237
 	}
237
 }
238
 }
239
+
240
+func TestIsLocalHost(t *testing.T) {
241
+	for _, s := range []string{"localhost:10000", "[::1]:10000", "127.0.0.1:10000"} {
242
+		host, _, err := net.SplitHostPort(s)
243
+		if err != nil {
244
+			t.Error("unexpected error when splitting", s)
245
+			continue
246
+		}
247
+		if !isLocalhost(host) {
248
+			t.Errorf("host %s from %s not considered local", host, s)
249
+		}
250
+	}
251
+}