Skip to content

Commit ee84750

Browse files
authored
auth(login): explicit check for ipv6 port bindings before creating listener for redirectUrl (#1251)
1 parent ef5172c commit ee84750

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

internal/pkg/auth/user_login.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ func AuthorizeUser(p *print.Printer, authConfig UserAuthConfig) error {
8585
var redirectURL string
8686
var listener net.Listener
8787
var listenerErr error
88+
var ipv6Listener net.Listener
89+
var ipv6ListenerErr error
8890
var port int
8991
startingPort := defaultPort
9092
portRange := configuredPortRange
@@ -94,18 +96,27 @@ func AuthorizeUser(p *print.Printer, authConfig UserAuthConfig) error {
9496
}
9597
for i := range portRange {
9698
port = startingPort + i
97-
portString := fmt.Sprintf(":%s", strconv.Itoa(port))
99+
ipv4addr := fmt.Sprintf("127.0.0.1:%d", port)
100+
ipv6addr := fmt.Sprintf("[::1]:%d", port)
98101
p.Debug(print.DebugLevel, "trying to bind port %d for login redirect", port)
99-
listener, listenerErr = net.Listen("tcp", portString)
102+
ipv6Listener, ipv6ListenerErr = net.Listen("tcp6", ipv6addr)
103+
if ipv6ListenerErr != nil {
104+
continue
105+
}
106+
listener, listenerErr = net.Listen("tcp4", ipv4addr)
100107
if listenerErr == nil {
108+
_ = ipv6Listener.Close()
101109
redirectURL = fmt.Sprintf("http://localhost:%d", port)
102110
p.Debug(print.DebugLevel, "bound port %d for login redirect", port)
103111
break
104112
}
105113
p.Debug(print.DebugLevel, "unable to bind port %d for login redirect: %s", port, listenerErr)
106114
}
115+
if ipv6ListenerErr != nil {
116+
return fmt.Errorf("unable to bind port for login redirect, tried from port %d to %d: %w", startingPort, port, ipv6ListenerErr)
117+
}
107118
if listenerErr != nil {
108-
return fmt.Errorf("unable to bind port for login redirect, tried from port %d to %d: %w", defaultPort, port, listenerErr)
119+
return fmt.Errorf("unable to bind port for login redirect, tried from port %d to %d: %w", startingPort, port, listenerErr)
109120
}
110121

111122
conf := &oauth2.Config{

0 commit comments

Comments
 (0)