Harden LuCI static responses over HTTPS
This commit is contained in:
1 parent
3a120e4e28
commit
44a7f0938a
1 file changed
+37
-14
+37
-14
@@ -1939,16 +1939,23 @@ class Handler(BaseHTTPRequestHandler):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
def send_bytes(self, status, body, content_type="text/plain; charset=utf-8", extra_headers=None):
|
def send_bytes(self, status, body, content_type="text/plain; charset=utf-8", extra_headers=None):
|
||||||
|
self.close_connection = True
|
||||||
self.send_response(status)
|
self.send_response(status)
|
||||||
self.send_header("Content-Type", content_type)
|
self.send_header("Content-Type", content_type)
|
||||||
self.send_header("Cache-Control", "no-store")
|
self.send_header("Cache-Control", "no-store")
|
||||||
|
self.send_header("Connection", "close")
|
||||||
if extra_headers:
|
if extra_headers:
|
||||||
for key, value in extra_headers:
|
for key, value in extra_headers:
|
||||||
|
if key.lower() in {"connection", "content-length", "content-type", "cache-control"}:
|
||||||
|
continue
|
||||||
self.send_header(key, value)
|
self.send_header(key, value)
|
||||||
self.send_header("Content-Length", str(len(body)))
|
self.send_header("Content-Length", str(len(body)))
|
||||||
self.end_headers()
|
self.end_headers()
|
||||||
if self.command != "HEAD":
|
if self.command != "HEAD":
|
||||||
self.wfile.write(body)
|
try:
|
||||||
|
self.wfile.write(body)
|
||||||
|
except (BrokenPipeError, ConnectionResetError, ssl.SSLError):
|
||||||
|
pass
|
||||||
|
|
||||||
def send_text(self, status, text, content_type="text/plain; charset=utf-8"):
|
def send_text(self, status, text, content_type="text/plain; charset=utf-8"):
|
||||||
self.send_bytes(status, text.encode("utf-8"), content_type)
|
self.send_bytes(status, text.encode("utf-8"), content_type)
|
||||||
@@ -2598,7 +2605,6 @@ class Handler(BaseHTTPRequestHandler):
|
|||||||
cached = static_cache_get(cache_key)
|
cached = static_cache_get(cache_key)
|
||||||
if cached:
|
if cached:
|
||||||
status, resp_body, content_type, resp_headers = cached
|
status, resp_body, content_type, resp_headers = cached
|
||||||
resp_headers.append(("Set-Cookie", current_router_cookie(router_id)))
|
|
||||||
resp_headers.append(("X-OWRT-Static-Cache", "hit"))
|
resp_headers.append(("X-OWRT-Static-Cache", "hit"))
|
||||||
self.send_bytes(status, resp_body, content_type, resp_headers)
|
self.send_bytes(status, resp_body, content_type, resp_headers)
|
||||||
return
|
return
|
||||||
@@ -2628,19 +2634,37 @@ class Handler(BaseHTTPRequestHandler):
|
|||||||
|
|
||||||
limiter = None if is_static else router_proxy_limiter(router_id)
|
limiter = None if is_static else router_proxy_limiter(router_id)
|
||||||
acquired = False
|
acquired = False
|
||||||
backend = None
|
|
||||||
try:
|
try:
|
||||||
if limiter is not None:
|
if limiter is not None:
|
||||||
acquired = limiter.acquire(timeout=PROXY_TIMEOUT)
|
acquired = limiter.acquire(timeout=PROXY_TIMEOUT)
|
||||||
if not acquired:
|
if not acquired:
|
||||||
self.send_text(503, "proxy busy: router is handling too many requests")
|
self.send_text(503, "proxy busy: router is handling too many requests")
|
||||||
return
|
return
|
||||||
backend = http.client.HTTPConnection("127.0.0.1", port, timeout=PROXY_TIMEOUT)
|
attempts = 4 if is_static and self.command == "GET" else 1
|
||||||
backend.request(self.command, target, body=body, headers=headers)
|
last_exc = None
|
||||||
resp = backend.getresponse()
|
for attempt in range(attempts):
|
||||||
resp_body = resp.read()
|
backend = None
|
||||||
|
try:
|
||||||
|
backend = http.client.HTTPConnection("127.0.0.1", port, timeout=PROXY_TIMEOUT)
|
||||||
|
backend.request(self.command, target, body=body, headers=headers)
|
||||||
|
resp = backend.getresponse()
|
||||||
|
resp_status = resp.status
|
||||||
|
resp_raw_headers = resp.getheaders()
|
||||||
|
resp_body = resp.read()
|
||||||
|
content_type = resp.getheader("Content-Type", "")
|
||||||
|
last_exc = None
|
||||||
|
break
|
||||||
|
except Exception as exc:
|
||||||
|
last_exc = exc
|
||||||
|
if attempt + 1 < attempts:
|
||||||
|
time.sleep(0.12 * (attempt + 1))
|
||||||
|
continue
|
||||||
|
finally:
|
||||||
|
if backend is not None:
|
||||||
|
backend.close()
|
||||||
|
if last_exc is not None:
|
||||||
|
raise last_exc
|
||||||
resp_headers = []
|
resp_headers = []
|
||||||
content_type = resp.getheader("Content-Type", "")
|
|
||||||
prefix = f"/access/{urllib.parse.quote(router_id)}"
|
prefix = f"/access/{urllib.parse.quote(router_id)}"
|
||||||
public_hosts = normalize_public_hosts(
|
public_hosts = normalize_public_hosts(
|
||||||
self.headers.get("Host", ""),
|
self.headers.get("Host", ""),
|
||||||
@@ -2652,7 +2676,7 @@ class Handler(BaseHTTPRequestHandler):
|
|||||||
row["admin_host"],
|
row["admin_host"],
|
||||||
f"{row['admin_host']}:{row['admin_port']}",
|
f"{row['admin_host']}:{row['admin_port']}",
|
||||||
)
|
)
|
||||||
for key, value in resp.getheaders():
|
for key, value in resp_raw_headers:
|
||||||
low = key.lower()
|
low = key.lower()
|
||||||
if low in {"cache-control", "connection", "content-length", "content-type", "keep-alive", "proxy-authenticate", "proxy-authorization", "te", "trailers", "transfer-encoding", "upgrade"}:
|
if low in {"cache-control", "connection", "content-length", "content-type", "keep-alive", "proxy-authenticate", "proxy-authorization", "te", "trailers", "transfer-encoding", "upgrade"}:
|
||||||
continue
|
continue
|
||||||
@@ -2666,14 +2690,15 @@ class Handler(BaseHTTPRequestHandler):
|
|||||||
if cache_key and self.command == "GET":
|
if cache_key and self.command == "GET":
|
||||||
static_cache_put(
|
static_cache_put(
|
||||||
cache_key,
|
cache_key,
|
||||||
resp.status,
|
resp_status,
|
||||||
resp_body,
|
resp_body,
|
||||||
content_type or "application/octet-stream",
|
content_type or "application/octet-stream",
|
||||||
static_cache_headers(resp_headers),
|
static_cache_headers(resp_headers),
|
||||||
)
|
)
|
||||||
resp_headers.append(("Set-Cookie", current_router_cookie(router_id)))
|
if not is_static:
|
||||||
|
resp_headers.append(("Set-Cookie", current_router_cookie(router_id)))
|
||||||
self.send_bytes(
|
self.send_bytes(
|
||||||
resp.status,
|
resp_status,
|
||||||
resp_body,
|
resp_body,
|
||||||
content_type or "application/octet-stream",
|
content_type or "application/octet-stream",
|
||||||
resp_headers,
|
resp_headers,
|
||||||
@@ -2681,8 +2706,6 @@ class Handler(BaseHTTPRequestHandler):
|
|||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
self.send_text(502, f"proxy error: {exc}")
|
self.send_text(502, f"proxy error: {exc}")
|
||||||
finally:
|
finally:
|
||||||
if backend is not None:
|
|
||||||
backend.close()
|
|
||||||
if acquired:
|
if acquired:
|
||||||
limiter.release()
|
limiter.release()
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user