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
12 changes: 9 additions & 3 deletions internal/pkg/auth/user_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,15 @@ func AuthorizeUser(p *print.Printer, isReauthentication bool) error {
var port int
for i := range configuredPortRange {
port = defaultPort + i
portString := fmt.Sprintf(":%s", strconv.Itoa(port))
addr4 := fmt.Sprintf("127.0.0.1:%d", port)
addr6 := 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", addr6)
if ipv6ListenerErr != nil {
continue
}
_ = ipv6Listener.Close()
Copy link
Contributor

Choose a reason for hiding this comment

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

potential race condition:
If after this line another service binds to [::1]:{port} we have the same situation as before.

listener, listenerErr = net.Listen("tcp4", addr4)
if listenerErr == nil {
redirectURL = fmt.Sprintf("http://localhost:%d", port)
p.Debug(print.DebugLevel, "bound port %d for login redirect", port)
Expand All @@ -106,7 +112,7 @@ func AuthorizeUser(p *print.Printer, isReauthentication bool) error {
p.Debug(print.DebugLevel, "unable to bind port %d for login redirect: %s", port, listenerErr)
}
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()

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

conf := &oauth2.Config{
Expand Down
Loading