Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: send proper close connection on local webserver redirect #681

Merged
merged 1 commit into from
Feb 20, 2025
Merged
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
10 changes: 8 additions & 2 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,7 +29,8 @@ 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))
random.shuffle(port_range)

for port in port_range:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
Expand Down Expand Up @@ -136,6 +138,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 +162,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
Loading