Skip to content

Commit e4d125e

Browse files
fix: send proper close connection on local webserver redirect (#681)
1 parent 3de59d8 commit e4d125e

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

safety/auth/server.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import http.server
22
import json
33
import logging
4+
import random
45
import socket
56
import sys
67
import time
@@ -28,7 +29,8 @@ def find_available_port() -> Optional[int]:
2829
Optional[int]: An available port number, or None if no ports are available.
2930
"""
3031
# Dynamic ports IANA
31-
port_range = range(49152, 65536)
32+
port_range = list(range(49152, 65536))
33+
random.shuffle(port_range)
3234

3335
for port in port_range:
3436
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
@@ -136,6 +138,8 @@ def do_GET(self) -> None:
136138
if isinstance(c_type, list) and len(c_type) == 1 and isinstance(c_type[0], str):
137139
callback_type = c_type[0]
138140
except Exception:
141+
msg = "Unable to process the callback, try again."
142+
self.send_error(400, msg)
139143
click.secho("Unable to process the callback, try again.")
140144
return
141145

@@ -158,8 +162,10 @@ def do_redirect(self, location: str, params: Dict) -> None:
158162
location (str): The URL to redirect to.
159163
params (dict): Additional parameters for the redirection.
160164
"""
161-
self.send_response(301)
165+
self.send_response(302)
162166
self.send_header('Location', location)
167+
self.send_header('Connection', 'close')
168+
self.send_header('Cache-Control', 'no-store, no-cache, must-revalidate')
163169
self.end_headers()
164170

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

0 commit comments

Comments
 (0)