Skip to content

"RuntimeError: Cannot change server.via on open connection" when using duplicate-modify-replay.py with upstream mode enabled #5241

Closed

Description

@riramar

Problem Description

I was using mitmdump.exe 7.0.4 for windows with the example duplicate-modify-replay.py without any problems.
After migrating to mitmdump.exe 8.0.0 I started to get the error below.

Addon error: Traceback (most recent call last):
  File "duplicate-modify-replay.py", line 9, in request
    flow = flow.copy()
  File "mitmproxy\http.py", line 1254, in copy
  File "mitmproxy\flow.py", line 170, in copy
  File "mitmproxy\coretypes\serializable.py", line 39, in copy
  File "mitmproxy\flow.py", line 165, in from_state
  File "mitmproxy\flow.py", line 160, in set_state
  File "mitmproxy\stateobject.py", line 47, in set_state
  File "mitmproxy\stateobject.py", line 94, in make_object
  File "mitmproxy\stateobject.py", line 56, in _process
  File "mitmproxy\connection.py", line 331, in from_state
  File "mitmproxy\connection.py", line 353, in set_state
  File "mitmproxy\connection.py", line 299, in __setattr__
RuntimeError: Cannot change server.via on open connection.

Steps to reproduce the behavior:

1 - Create a script duplicate-modify-replay.py with the content below.

"""Take incoming HTTP requests and replay them with modified parameters."""
from mitmproxy import ctx


def request(flow):
    # Avoid an infinite loop by not replaying already replayed requests
    if flow.is_replay == "request":
        return
    flow = flow.copy()
    # Only interactive tools have a view. If we have one, add a duplicate entry
    # for our flow.
    if "view" in ctx.master.addons:
        ctx.master.commands.call("view.flows.add", [flow])
    flow.request.path = "/changed"
    ctx.master.commands.call("replay.client", [flow])

2 - Create a proxy (e.g. Burp Suite Proxy) running under port 8082
3 - Run mitmdump.exe 8.0.0 with the command below.
mitmdump.exe --ssl-insecure --set add_upstream_certs_to_client_chain=true -s duplicate-modify-replay.py --mode upstream:http://127.0.0.1:8082
4 - If some request get stucked press Control+C and check the output.

image

System Information

Paste the output of "mitmproxy --version" here.

>mitmdump.exe --version
Mitmproxy: 8.0.0 binary
Python:    3.10.2
OpenSSL:   OpenSSL 1.1.1n  15 Mar 2022
Platform:  Windows-10-10.0.19041-SP0

Activity

  1. added
    kind/triage
    on Apr 4, 2022
  2. added and removed
    kind/triage
    on Apr 4, 2022
  3. riramar commented on Apr 5, 2022

    @riramar
    Author

    Seems to be related to this commit 0f5b8c8

  4. mhils commented on Apr 6, 2022

    @mhils
    Member

    Yeah - raising in __setattr__ is good, we need to fix copy() to not trigger that I think. :)

  5. idb- commented on Aug 23, 2022

    @idb-

    Also having this issue.

  6. kvaidas commented on Oct 11, 2022

    @kvaidas

    Not sure if this is related but this happens when I try to read flows from a file - that has been recorded while using --set mode=upstream:http://
    If I record the same request not using this parameter, everything just works.
    I'm running 8.1.1

  7. idb- commented on Oct 17, 2022

    @idb-

    Not sure if this is related but this happens when I try to read flows from a file - that has been recorded while using --set mode=upstream:http:// If I record the same request not using this parameter, everything just works. I'm running 8.1.1

    In connection.py find the line

    if name in ("address", "via"):
    

    and replace it with

    if name == 'address':
    

    There may be side effects so make note of the edit in source so you can undo if necessary, but this allows you to re-load flows where an upstream proxy was in use.

  8. riramar commented on Nov 3, 2022

    @riramar
    Author

    I'm still having this issue on mitmproxy 9.0.1.

    image

  9. idb- commented on Nov 5, 2022

    @idb-

    I'm still having this issue on mitmproxy 9.0.1.

    image

    Are you certain that you replaced properly, and all instances? I'm not sure if there's more than 1 instance of it. You may be having a completely different issue. Not sure.

  10. riramar commented on Nov 5, 2022

    @riramar
    Author

    It's the same error. I did the same test again from the past (using mitmproxy-9.0.1-windows.zip) with the same file and got the same error.

    image

    The same test using the version 7.0.4 it works properlly.

    image

    This seems to be related to this commit 0f5b8c8

  11. Sim4n6 commented on Mar 16, 2023

    @Sim4n6

    image

  12. cole-nayya commented on May 11, 2023

    @cole-nayya

    @mhils is there a pypi release/publishing schedule? This issue is still occurring in v9.0.1

  13. mhils commented on May 14, 2023

    @mhils
    Member

    This issue is still occurring in v9.0.1

    Probably because it's not fixed yet, PRs welcome. :)

    is there a pypi release/publishing schedule?

    We aim to ship mitmproxy 10 relatively soon to get more HTTP/3 feedback. The HTTP/3 implementation is still very experimental and we intentionally let it sit on main for a bit to iron out the worst bugs. :)

  14. cole-nayya commented on May 16, 2023

    @cole-nayya

    This doesn't happen on the tip/10.0.0.dev0 - at least with the examples I have - otw would be happy to contribute.

  15. mhils commented on May 16, 2023

    @mhils
    Member

    Thanks for the triage, that's a great contribution in itself. 😄 🍰

  16. 3052 commented on May 20, 2024

    @3052
  17. cogitozz commented on Sep 23, 2024

    @cogitozz

    Hi, is there a good solution? I'm still having this issue on mitmproxy 10.4.2

  18. Ad3n1ne commented on May 20, 2025

    @Ad3n1ne

    Not sure if this is related but this happens when I try to read flows from a file - that has been recorded while using --set mode=upstream:http:// If I record the same request not using this parameter, everything just works. I'm running 8.1.1

    In connection.py find the line

    if name in ("address", "via"):
    

    and replace it with

    if name == 'address':
    

    There may be side effects so make note of the edit in source so you can undo if necessary, but this allows you to re-load flows where an upstream proxy was in use.

    我修改了这部分代码也解决了这个问题 想了解具体的原理是什么?

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions