You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When a property has a mix of unrelated types it takes me more time than it should take to wrap my head around. To me this always smells like something could be simplified, but I guess that's something I have to get used to with Python? I'm talking about Server.sni: Union[str, Literal[True], NoneType] = True vs Client.sni: Optional[str] = None
The Server Name Indication (SNI) sent in the ClientHello. For server connections, this value may also be set to True, which means "use Server.address".
Is this something that could be hidden under the hood? As a user I don't care. I just want the string that was used for sni. I don't want to replicate internal logic inside my addon.
Proposal
When SNI is used just duplicate address[0] into sni.
Somewhat I guess. So that means in later hooks (e.g. server_disconnected) it is guaranteed to be either a string or None? I've seen this pattern more than once in mitmproxy, where a property is used to either hold a value or a sort of flag that like has a side effect on the meaning later in some internal parts of the code. I find it confusing. Could there be a (private?) boolean flag that does that job and sni is kept clean?
Is it correct that both of the current tls_ hooks are too early for that? So sni could still be True? Or does SNI happen between tls_start and tls_clienthello?
Wait a second, isn't tls_start ambiguous? Is it the TLS start between client<->mitmproxy or mitmproxy<->server? Or does it fire for both? Since TlsStartData holds a generic connection.Connection I assume both? Maybe there needs to be tls_clientstart and tls_serverstart?
Somewhat I guess. So that means in later hooks (e.g. server_disconnected) it is guaranteed to be either a string or None?
Yes, as long as you don't intentionally disable the TlsConfig addon somehow.
Could there be a (private?) boolean flag that does that job and sni is kept clean?
I think you somewhat convinced me here. Let's make sni an Optional[str]. In tlsconfig.py, we just change the if statement here to if server.sni is None:
The fact that the only place where we are using this has a check and you are requesting essentially the same, I'd be happy to change it and split it into two separate events. Again, PRs welcome. 😊😇
Overall this is excellent feedback on unpolished APIs, please keep it coming! 🍰
@mhils thanks! I've had this tab open for a couple of days now but I'm having a hard time getting anything done...but I do want to look into separating tls_start into two event. So bear with me.
Activity
mhils commentedon Apr 7, 2021
server.sni will be set to server.address during the handshake, it will only be set to True before that. Does that help?
Prinzhorn commentedon Apr 7, 2021
Somewhat I guess. So that means in later hooks (e.g.
server_disconnected) it is guaranteed to be either a string orNone? I've seen this pattern more than once in mitmproxy, where a property is used to either hold a value or a sort of flag that like has a side effect on the meaning later in some internal parts of the code. I find it confusing. Could there be a (private?) boolean flag that does that job andsniis kept clean?Is it correct that both of the current
tls_hooks are too early for that? Sosnicould still beTrue? Or does SNI happen betweentls_startandtls_clienthello?Wait a second, isn't
tls_startambiguous? Is it the TLS start between client<->mitmproxy or mitmproxy<->server? Or does it fire for both? SinceTlsStartDataholds a genericconnection.ConnectionI assume both? Maybe there needs to betls_clientstartandtls_serverstart?mhils commentedon Apr 7, 2021
Yes, as long as you don't intentionally disable the TlsConfig addon somehow.
I think you somewhat convinced me here. Let's make
snianOptional[str]. Intlsconfig.py, we just change theifstatement here toif server.sni is None:mitmproxy/mitmproxy/addons/tlsconfig.py
Lines 166 to 167 in 438567f
That should still cover all relevant cases. PRs welcome. 😊😇
tls_clienthellois too early, SNI is then processed by the TlsConfig addon duringtls_start. So it depends on the addon call order here.Both:
mitmproxy/mitmproxy/addons/tlsconfig.py
Lines 114 to 118 in 438567f
The fact that the only place where we are using this has a check and you are requesting essentially the same, I'd be happy to change it and split it into two separate events. Again, PRs welcome. 😊😇
Overall this is excellent feedback on unpolished APIs, please keep it coming! 🍰
Prinzhorn commentedon Apr 7, 2021
Nice, thanks for the details. I'm interested in making the PRs, but not right now. I'll self-assign the issue once I do to avoid conflicts.
simplify Server.sni, fix mitmproxy#4547
Prinzhorn commentedon Jun 25, 2021
@mhils thanks! I've had this tab open for a couple of days now but I'm having a hard time getting anything done...but I do want to look into separating tls_start into two event. So bear with me.
separate tls_start hooks for client and server, fixes mitmproxy#4665,…
separate tls_start hooks for client and server, fixes #4665, refs #4547…