Skip to content

Commit

Permalink
fix: send proper close connection on local webserver redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
yeisonvargasf committed Feb 20, 2025
1 parent 3de59d8 commit d05b420
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions safety/auth/server.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import http.server
import json
import logging
import random
import socket
import sys
import time
Expand Down Expand Up @@ -28,9 +29,11 @@ def find_available_port() -> Optional[int]:
Optional[int]: An available port number, or None if no ports are available.
"""
# Dynamic ports IANA
port_range = range(49152, 65536)
port_range = list(range(49152, 65536))

for port in port_range:
while port_range:
port = random.choice(port_range)
port_range.remove(port)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
try:
result = s.connect(('localhost', port))
Expand Down Expand Up @@ -136,6 +139,8 @@ def do_GET(self) -> None:
if isinstance(c_type, list) and len(c_type) == 1 and isinstance(c_type[0], str):
callback_type = c_type[0]
except Exception:
msg = "Unable to process the callback, try again."
self.send_error(400, msg)
click.secho("Unable to process the callback, try again.")
return

Expand All @@ -158,8 +163,10 @@ def do_redirect(self, location: str, params: Dict) -> None:
location (str): The URL to redirect to.
params (dict): Additional parameters for the redirection.
"""
self.send_response(301)
self.send_response(302)
self.send_header('Location', location)
self.send_header('Connection', 'close')
self.send_header('Cache-Control', 'no-store, no-cache, must-revalidate')
self.end_headers()

def log_message(self, format: str, *args: Any) -> None:
Expand Down

0 comments on commit d05b420

Please sign in to comment.