Repository navigation
HTTP/2 PING and server side reset #5044
Replies: 5 comments 13 replies
|
Thanks for the detailed bug description! 🍰
Ah, such great server behavior. 😄
After increasing My test page is https://www.heise.de/Datenschutzerklaerung-der-Heise-Medien-GmbH-Co-KG-4860.html, clicking the "Datenschutz" link at the bottom of the page after 70s is a no-op with the increased ping timeout. The page only (re)loads after clicking it again.
AFAIK there is nothing formally standardized here, there definitely is no negotiation as part of HTTP/2. Firefox just pings every 58 seconds by default. I presume Chrome does something similar.
Forwarding HTTP/2 pings would be the obvious thing to do. But we deliberately don't do that as that would create a side channel through which an adversary could get data without it being logged. As you already suggested, another way forward here is to ping all remotes every N seconds (irrespective of what the client is doing). Unfortunately this is relatively nontrivial as we need to keep a timer somewhere. Doing that is in the proxy core would directly violate what we try to achieve with sans-io, so we either need to add some timer/alarm/wakeup functionality to our I/O handler, or have an addon that keeps track of open connections an injects a ping every N seconds somehow. I'm leaning towards the timer as we may need that anyways for some protocols (and, thinking long term, UDP). |
|
Thanks for the proof-of-concept at #5051! 😃 This makes more sense now. mitmproxy/mitmproxy/addons/proxyserver.py Lines 108 to 114 in 1e97073 which then is just used in the proxy logic to conditionally update the host header: mitmproxy/mitmproxy/proxy/layers/http/__init__.py Lines 190 to 196 in ef8c88d You can set that manually ( from mitmproxy.ctx import options
options.keep_host_header = TrueIs there something that you are missing from this? |
|
I gave it a shot to implement the ping forwarding. I came up with this (can do a PR if this helps): diff --git a/mitmproxy/proxy/layers/http/__init__.py b/mitmproxy/proxy/layers/http/__init__.py
index 8e4e5b764..10d3c75c4 100644
--- a/mitmproxy/proxy/layers/http/__init__.py
+++ b/mitmproxy/proxy/layers/http/__init__.py
@@ -16,7 +16,7 @@ from mitmproxy.proxy.layers.http import _upstream_proxy
from mitmproxy.proxy.utils import expect
from mitmproxy.utils import human
from mitmproxy.websocket import WebSocketData
-from ._base import HttpCommand, HttpConnection, ReceiveHttp, StreamId
+from ._base import HttpCommand, HttpConnection, ReceiveHttp, ReceivePing, StreamId
from ._events import HttpEvent, RequestData, RequestEndOfMessage, RequestHeaders, RequestProtocolError, RequestTrailers, \
ResponseData, ResponseEndOfMessage, ResponseHeaders, ResponseProtocolError, ResponseTrailers
from ._hooks import HttpConnectHook, HttpErrorHook, HttpRequestHeadersHook, HttpRequestHook, HttpResponseHeadersHook, \
@@ -765,6 +765,9 @@ class HttpLayer(layer.Layer):
yield from self.make_stream(command.event.stream_id)
stream = self.streams[command.event.stream_id]
yield from self.event_to_child(stream, command.event)
+ elif isinstance(command, ReceivePing):
+ conn = self.connections[command.connection]
+ yield from self.event_to_child(conn, command.event)
elif isinstance(command, SendHttp):
conn = self.connections[command.connection]
yield from self.event_to_child(conn, command.event)
diff --git a/mitmproxy/proxy/layers/http/_base.py b/mitmproxy/proxy/layers/http/_base.py
index 5e1946b8b..f95aec772 100644
--- a/mitmproxy/proxy/layers/http/_base.py
+++ b/mitmproxy/proxy/layers/http/_base.py
@@ -28,6 +28,18 @@ class HttpCommand(commands.Command):
pass
+class ReceivePing(HttpCommand):
+ event: events.Event
+ connection: Connection
+
+ def __init__(self, event: events.Event, conn: Connection):
+ self.event = event
+ self.connection = conn
+
+ def __repr__(self) -> str:
+ return f"ReceivePing({self.event})"
+
+
class ReceiveHttp(HttpCommand):
event: HttpEvent
diff --git a/mitmproxy/proxy/layers/http/_events.py b/mitmproxy/proxy/layers/http/_events.py
index edddf5b6e..1d4205934 100644
--- a/mitmproxy/proxy/layers/http/_events.py
+++ b/mitmproxy/proxy/layers/http/_events.py
@@ -1,6 +1,7 @@
from dataclasses import dataclass
from typing import Optional
+from mitmproxy.proxy import events
from mitmproxy import http
from mitmproxy.http import HTTPFlow
from ._base import HttpEvent
@@ -97,6 +98,14 @@ class ResponseProtocolError(HttpEvent):
self.code = code
+@dataclass
+class ForwardPing(events.Event):
+ ping_data: str
+
+ def __init__(self, ping_data: str):
+ self.ping_data = ping_data
+
+
__all__ = [
"HttpEvent",
"RequestHeaders",
@@ -109,4 +118,5 @@ __all__ = [
"ResponseEndOfMessage",
"RequestProtocolError",
"ResponseProtocolError",
+ "ForwardPing",
]
diff --git a/mitmproxy/proxy/layers/http/_http2.py b/mitmproxy/proxy/layers/http/_http2.py
index 8e477c34f..75a0da6c5 100644
--- a/mitmproxy/proxy/layers/http/_http2.py
+++ b/mitmproxy/proxy/layers/http/_http2.py
@@ -16,9 +16,9 @@ from mitmproxy import http, version
from mitmproxy.connection import Connection
from mitmproxy.net.http import status_codes, url
from mitmproxy.utils import human
-from . import RequestData, RequestEndOfMessage, RequestHeaders, RequestProtocolError, ResponseData, \
- ResponseEndOfMessage, ResponseHeaders, RequestTrailers, ResponseTrailers, ResponseProtocolError
-from ._base import HttpConnection, HttpEvent, ReceiveHttp, format_error
+from ._events import RequestData, RequestEndOfMessage, RequestHeaders, RequestProtocolError, ResponseData, \
+ ResponseEndOfMessage, ResponseHeaders, RequestTrailers, ResponseTrailers, ResponseProtocolError, ForwardPing
+from ._base import HttpConnection, HttpEvent, ReceiveHttp, ReceivePing, format_error
from ._http_h2 import BufferedH2Connection, H2ConnectionLogger
from ...commands import CloseConnection, Log, SendData
from ...context import Context
@@ -164,6 +164,8 @@ class Http2Connection(HttpConnection):
elif isinstance(event, ConnectionClosed):
yield from self.close_connection("peer closed connection")
+ elif isinstance(event, ForwardPing):
+ yield Log("PPP Ping forwarded to the wrong connection: " + repr(event.ping_data) + " -> " + repr(self.conn.address))
else:
raise AssertionError(f"Unexpected event: {event!r}")
@@ -326,6 +328,10 @@ class Http2Server(Http2Connection):
self.streams[event.stream_id] = StreamState.HEADERS_RECEIVED
yield ReceiveHttp(RequestHeaders(event.stream_id, request, end_stream=bool(event.stream_ended)))
return False
+ elif isinstance(event, h2.events.PingReceived):
+ yield Log("PPP Ping received from client: " + repr(event.ping_data))
+ yield ReceivePing(ForwardPing(event.ping_data), self.conn)
+ return False
else:
return (yield from super().handle_h2_event(event))
@@ -396,7 +402,13 @@ class Http2Client(Http2Connection):
yield from self._handle_event(event)
def _handle_event2(self, event: Event) -> CommandGenerator[None]:
- if isinstance(event, RequestHeaders):
+ if isinstance(event, ForwardPing):
+ yield Log("PPP Ping forwarded: " + repr(event.ping_data) + " -> " + repr(self.conn.address))
+ self.h2_conn.ping(event.ping_data);
+ data_to_send = self.h2_conn.data_to_send()
+ if data_to_send:
+ yield SendData(self.conn, data_to_send)
+ elif isinstance(event, RequestHeaders):
pseudo_headers = [
(b':method', event.request.data.method),
(b':scheme', event.request.data.scheme),
@@ -452,6 +464,9 @@ class Http2Client(Http2Connection):
elif isinstance(event, h2.events.RequestReceived):
yield from self.protocol_error(f"HTTP/2 protocol error: received request from server")
return True
+ elif isinstance(event, h2.events.PingAckReceived):
+ yield Log("PPP Ping Ack received from server: " + repr(event.ping_data))
+ return False
elif isinstance(event, h2.events.RemoteSettingsChanged):
# We have received at least one settings from now,
# which means we can rely on the max concurrency in remote_settingsThis does not work as expected, when a ping is received it is sent back to the client instead of the server. I get log entries like this: Can you give me a pointer how I can manage to forward the command/event to the right instance of |
|
BTW, from my iPad I do not receive any PINGs but the behavior is the same, when proxied I get a 502 after 60 seconds of idle time. I guess Safari uses another HTTP/2 management primitive to keep a connection alive. I'll try to figure out which one it is. |
|
Building web tools requires careful handling of requests, performance, and user experience. Testing different inputs is especially important for applications that process conversions or structured data. |


Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
I have noticed that for instance https://heise.de, when proxied, returns a '502' HTTP error to the client when I click on a link after not doing anything (=reading the page) for over 60 seconds. What I see on the outgoing interface is that the remote server resets the connection the moment the connection is used again (TCP RST).
When not proxied I observe that the client sends a HTTP/2 PING after 60 seconds which seems to keep the connection alive. Varying the idle time from 60 to 120 seconds, I never manage to reset the connection and a link always loads.
I wonder if there is a way to remedy the proxied behavior. I am not familiar enough with HTTP/2 to see if this PING interval is something that client and server agree to. Or if this is 'best practice' for browsers. Any ideas where to start? Would it make sense that the proxy sends regular PINGs for idle HTTP/2 connections?
All reactions