Skip to content

Simplify Server.sni #4547

Closed
Closed

Description

@Prinzhorn

Problem Description

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.

Activity

  1. added
    kind/feature
    on Apr 6, 2021
  2. mhils commented on Apr 7, 2021

    @mhils
    Member

    server.sni will be set to server.address during the handshake, it will only be set to True before that. Does that help?

  3. Prinzhorn commented on Apr 7, 2021

    @Prinzhorn
    MemberAuthor

    Does that help?

    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?

  4. mhils commented on Apr 7, 2021

    @mhils
    Member

    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:

    if server.sni is True:
    server.sni = client.sni or server.address[0]

    That should still cover all relevant cases. PRs welcome. 😊😇

    Is it correct that both of the current tls_ hooks are too early for that?

    tls_clienthello is too early, SNI is then processed by the TlsConfig addon during tls_start. So it depends on the addon call order here.

    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?

    Both:

    def tls_start(self, tls_start: tls.TlsStartData):
    if tls_start.conn == tls_start.context.client:
    self.create_client_proxy_ssl_conn(tls_start)
    else:
    self.create_proxy_server_ssl_conn(tls_start)

    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! 🍰

  5. Prinzhorn commented on Apr 7, 2021

    @Prinzhorn
    MemberAuthor

    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.

  6. added a commit that references this issue on Jun 24, 2021
    b2a18a9
  7. Prinzhorn commented on Jun 25, 2021

    @Prinzhorn
    MemberAuthor

    @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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions