Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions internal/pkg/auth/user_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ func AuthorizeUser(p *print.Printer, authConfig UserAuthConfig) error {
var redirectURL string
var listener net.Listener
var listenerErr error
var ipv6Listener net.Listener
var ipv6ListenerErr error
var port int
startingPort := defaultPort
portRange := configuredPortRange
Expand All @@ -94,18 +96,27 @@ func AuthorizeUser(p *print.Printer, authConfig UserAuthConfig) error {
}
for i := range portRange {
port = startingPort + i
portString := fmt.Sprintf(":%s", strconv.Itoa(port))
ipv4addr := fmt.Sprintf("127.0.0.1:%d", port)
ipv6addr := fmt.Sprintf("[::1]:%d", port)
p.Debug(print.DebugLevel, "trying to bind port %d for login redirect", port)
listener, listenerErr = net.Listen("tcp", portString)
ipv6Listener, ipv6ListenerErr = net.Listen("tcp6", ipv6addr)
if ipv6ListenerErr != nil {
continue
}
listener, listenerErr = net.Listen("tcp4", ipv4addr)
if listenerErr == nil {
_ = ipv6Listener.Close()
redirectURL = fmt.Sprintf("http://localhost:%d", port)
p.Debug(print.DebugLevel, "bound port %d for login redirect", port)
break
}
p.Debug(print.DebugLevel, "unable to bind port %d for login redirect: %s", port, listenerErr)
}
if ipv6ListenerErr != nil {
return fmt.Errorf("unable to bind port for login redirect, tried from port %d to %d: %w", startingPort, port, ipv6ListenerErr)
}
if listenerErr != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be extended with a check if ipv6ListenerErr is not nil. It's probably unlikely, but if someone uses already all the ports from 8000-8020, it will continue here without an error and try the authentication flow without an redirect url and the CLI will stop with a panic.

I extended your python code, to simulate this case

#!/usr/bin/env python3
import threading
from http.server import HTTPServer, SimpleHTTPRequestHandler
import socket

HOST = "::1"
PORT = 8000


class IPv6HTTPServer(HTTPServer):
   address_family = socket.AF_INET6

for i in range(0, 21):
   server = IPv6HTTPServer((HOST, PORT + i), SimpleHTTPRequestHandler)
   print(f"Serving on http://[{HOST}]:{PORT+i}")
   t = threading.Thread(target=server.serve_forever)
   t.start()

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a check for ipv6ListenerErr with the same message for binding ports error, couldn't think of a more appropriate error message.

return fmt.Errorf("unable to bind port for login redirect, tried from port %d to %d: %w", defaultPort, port, listenerErr)
return fmt.Errorf("unable to bind port for login redirect, tried from port %d to %d: %w", startingPort, port, listenerErr)
}

conf := &oauth2.Config{
Expand Down