Example #1
0
def thash(r, r2, setter):
    s = serverplayback.ServerPlayback()
    with taddons.context(s) as tctx:
        s = serverplayback.ServerPlayback()
        tctx.configure(
            s, server_replay_ignore_payload_params=["param1", "param2"])

        setter(r, paramx="x", param1="1")

        setter(r2, paramx="x", param1="1")
        # same parameters
        assert s._hash(r) == s._hash(r2)
        # ignored parameters !=
        setter(r2, paramx="x", param1="2")
        assert s._hash(r) == s._hash(r2)
        # missing parameter
        setter(r2, paramx="x")
        assert s._hash(r) == s._hash(r2)
        # ignorable parameter added
        setter(r2, paramx="x", param1="2")
        assert s._hash(r) == s._hash(r2)
        # not ignorable parameter changed
        setter(r2, paramx="y", param1="1")
        assert not s._hash(r) == s._hash(r2)
        # not ignorable parameter missing
        setter(r2, param1="1")
        r2.request.content = b"param1=1"
        assert not s._hash(r) == s._hash(r2)
Example #2
0
    def test_server_playback_full(self):
        s = serverplayback.ServerPlayback()
        o = options.Options(refresh_server_playback=True, keepserving=False)
        m = mastertest.RecordingMaster(o, proxy.DummyServer())
        m.addons.add(s)

        f = tutils.tflow()
        f.response = mitmproxy.test.tutils.tresp(content=f.request.content)
        s.load([f, f])

        tf = tutils.tflow()
        assert not tf.response
        m.request(tf)
        assert tf.response == f.response

        tf = tutils.tflow()
        tf.request.content = b"gibble"
        assert not tf.response
        m.request(tf)
        assert not tf.response

        assert not s.stop
        s.tick()
        assert not s.stop

        tf = tutils.tflow()
        m.request(tutils.tflow())
        assert s.stop
Example #3
0
    def test_ignore_payload_params(self):
        s = serverplayback.ServerPlayback()
        s.configure(
            options.Options(
                server_replay_ignore_payload_params=["param1", "param2"]), [])

        r = tutils.tflow(resp=True)
        r.request.headers["Content-Type"] = "application/x-www-form-urlencoded"
        r.request.content = b"paramx=x&param1=1"
        r2 = tutils.tflow(resp=True)
        r2.request.headers[
            "Content-Type"] = "application/x-www-form-urlencoded"
        r2.request.content = b"paramx=x&param1=1"
        # same parameters
        assert s._hash(r) == s._hash(r2)
        # ignored parameters !=
        r2.request.content = b"paramx=x&param1=2"
        assert s._hash(r) == s._hash(r2)
        # missing parameter
        r2.request.content = b"paramx=x"
        assert s._hash(r) == s._hash(r2)
        # ignorable parameter added
        r2.request.content = b"paramx=x&param1=2"
        assert s._hash(r) == s._hash(r2)
        # not ignorable parameter changed
        r2.request.content = b"paramx=y&param1=1"
        assert not s._hash(r) == s._hash(r2)
        # not ignorable parameter missing
        r2.request.content = b"param1=1"
        assert not s._hash(r) == s._hash(r2)
Example #4
0
def test_server_playback_full():
    s = serverplayback.ServerPlayback()
    with taddons.context() as tctx:
        tctx.configure(
            s,
            server_replay_refresh=True,
        )

        f = tflow.tflow()
        f.response = mitmproxy.test.tutils.tresp(content=f.request.content)
        s.load_flows([f, f])

        tf = tflow.tflow()
        assert not tf.response
        s.request(tf)
        assert tf.response == f.response

        tf = tflow.tflow()
        tf.request.content = b"gibble"
        assert not tf.response
        s.request(tf)
        assert not tf.response

        assert not s.stop
        s.tick()
        assert not s.stop

        tf = tflow.tflow()
        s.request(tflow.tflow())
        assert s.stop
Example #5
0
def test_load():
    s = serverplayback.ServerPlayback()
    with taddons.context(s) as tctx:
        tctx.configure(s)

        r = tflow.tflow(resp=True)
        r.request.headers["key"] = "one"

        r2 = tflow.tflow(resp=True)
        r2.request.headers["key"] = "two"

        s.load_flows([r, r2])

        assert s.count() == 2

        n = s.next_flow(r)
        assert n.request.headers["key"] == "one"
        assert s.count() == 1

        n = s.next_flow(r)
        assert n.request.headers["key"] == "two"
        assert not s.flowmap
        assert s.count() == 0

        assert not s.next_flow(r)
Example #6
0
def default_addons():
    return [
        core.Core(),
        core_option_validation.CoreOptionValidation(),
        allowremote.AllowRemote(),
        anticache.AntiCache(),
        anticomp.AntiComp(),
        check_alpn.CheckALPN(),
        check_ca.CheckCA(),
        clientplayback.ClientPlayback(),
        cut.Cut(),
        disable_h2c.DisableH2C(),
        export.Export(),
        onboarding.Onboarding(),
        proxyauth.ProxyAuth(),
        replace.Replace(),
        script.ScriptLoader(),
        serverplayback.ServerPlayback(),
        setheaders.SetHeaders(),
        stickyauth.StickyAuth(),
        stickycookie.StickyCookie(),
        streambodies.StreamBodies(),
        save.Save(),
        upstream_auth.UpstreamAuth(),
    ]
Example #7
0
def test_ignore_content():
    s = serverplayback.ServerPlayback()
    with taddons.context(s) as tctx:
        tctx.configure(s, server_replay_ignore_content=False)

        r = tflow.tflow(resp=True)
        r2 = tflow.tflow(resp=True)

        r.request.content = b"foo"
        r2.request.content = b"foo"
        assert s._hash(r) == s._hash(r2)
        r2.request.content = b"bar"
        assert not s._hash(r) == s._hash(r2)

        tctx.configure(s, server_replay_ignore_content=True)
        r = tflow.tflow(resp=True)
        r2 = tflow.tflow(resp=True)
        r.request.content = b"foo"
        r2.request.content = b"foo"
        assert s._hash(r) == s._hash(r2)
        r2.request.content = b"bar"
        assert s._hash(r) == s._hash(r2)
        r2.request.content = b""
        assert s._hash(r) == s._hash(r2)
        r2.request.content = None
        assert s._hash(r) == s._hash(r2)
Example #8
0
def default_addons():
    return [
        core.Core(),
        browser.Browser(),
        block.Block(),
        blocklist.BlockList(),
        anticache.AntiCache(),
        anticomp.AntiComp(),
        clientplayback.ClientPlayback(),
        command_history.CommandHistory(),
        comment.Comment(),
        cut.Cut(),
        disable_h2c.DisableH2C(),
        export.Export(),
        onboarding.Onboarding(),
        proxyauth.ProxyAuth(),
        proxyserver.Proxyserver(),
        script.ScriptLoader(),
        next_layer.NextLayer(),
        serverplayback.ServerPlayback(),
        mapremote.MapRemote(),
        maplocal.MapLocal(),
        modifybody.ModifyBody(),
        modifyheaders.ModifyHeaders(),
        stickyauth.StickyAuth(),
        stickycookie.StickyCookie(),
        save.Save(),
        tlsconfig.TlsConfig(),
        upstream_auth.UpstreamAuth(),
    ]
Example #9
0
def test_ignore_content():
    s = serverplayback.ServerPlayback()
    s.configure(options.Options(server_replay_ignore_content=False), [])

    r = tflow.tflow(resp=True)
    r2 = tflow.tflow(resp=True)

    r.request.content = b"foo"
    r2.request.content = b"foo"
    assert s._hash(r) == s._hash(r2)
    r2.request.content = b"bar"
    assert not s._hash(r) == s._hash(r2)

    s.configure(options.Options(server_replay_ignore_content=True), [])
    r = tflow.tflow(resp=True)
    r2 = tflow.tflow(resp=True)
    r.request.content = b"foo"
    r2.request.content = b"foo"
    assert s._hash(r) == s._hash(r2)
    r2.request.content = b"bar"
    assert s._hash(r) == s._hash(r2)
    r2.request.content = b""
    assert s._hash(r) == s._hash(r2)
    r2.request.content = None
    assert s._hash(r) == s._hash(r2)
Example #10
0
def default_addons():
    return [
        core.Core(),
        browser.Browser(),
        block.Block(),
        anticache.AntiCache(),
        anticomp.AntiComp(),
        check_ca.CheckCA(),
        clientplayback.ClientPlayback(),
        command_history.CommandHistory(),
        cut.Cut(),
        disable_h2c.DisableH2C(),
        export.Export(),
        onboarding.Onboarding(),
        proxyauth.ProxyAuth(),
        script.ScriptLoader(),
        serverplayback.ServerPlayback(),
        mapremote.MapRemote(),
        modifybody.ModifyBody(),
        modifyheaders.ModifyHeaders(),
        stickyauth.StickyAuth(),
        stickycookie.StickyCookie(),
        streambodies.StreamBodies(),
        save.Save(),
        upstream_auth.UpstreamAuth(),
    ]
def test_config(tmpdir):
    s = serverplayback.ServerPlayback()
    with taddons.context() as tctx:
        fpath = str(tmpdir.join("flows"))
        tdump(fpath, [tflow.tflow(resp=True)])
        tctx.configure(s, server_replay=[fpath])
        with pytest.raises(exceptions.OptionsError):
            tctx.configure(s, server_replay=[str(tmpdir)])
Example #12
0
def test_tick():
    s = serverplayback.ServerPlayback()
    with taddons.context() as tctx:
        s.stop = True
        s.final_flow = tflow.tflow()
        s.final_flow.live = False
        s.tick()
        assert tctx.master.should_exit.is_set()
Example #13
0
def test_tick():
    s = serverplayback.ServerPlayback()
    with taddons.context() as tctx:
        s.stop = True
        s.final_flow = tflow.tflow()
        s.final_flow.live = False
        s.tick()
        assert tctx.master.has_event("processing_complete")
def test_config():
    s = serverplayback.ServerPlayback()
    with tutils.tmpdir() as p:
        with taddons.context() as tctx:
            fpath = os.path.join(p, "flows")
            tdump(fpath, [tflow.tflow(resp=True)])
            tctx.configure(s, server_replay = [fpath])
            tutils.raises(exceptions.OptionsError, tctx.configure, s, server_replay = [p])
Example #15
0
def test_load_file(tmpdir):
    s = serverplayback.ServerPlayback()
    with taddons.context(s):
        fpath = str(tmpdir.join("flows"))
        tdump(fpath, [tflow.tflow(resp=True)])
        s.load_file(fpath)
        assert s.flowmap
        with pytest.raises(exceptions.CommandError):
            s.load_file("/nonexistent")
Example #16
0
    def test_server_playback(self):
        sp = serverplayback.ServerPlayback()
        sp.configure(options.Options(), [])
        f = tutils.tflow(resp=True)

        assert not sp.flowmap

        sp.load([f])
        assert sp.flowmap
        assert sp.next_flow(f)
        assert not sp.flowmap
Example #17
0
def test_ignore_host():
    sp = serverplayback.ServerPlayback()
    sp.configure(options.Options(server_replay_ignore_host=True), [])

    r = tflow.tflow(resp=True)
    r2 = tflow.tflow(resp=True)

    r.request.host = "address"
    r2.request.host = "address"
    assert sp._hash(r) == sp._hash(r2)
    r2.request.host = "wrong_address"
    assert sp._hash(r) == sp._hash(r2)
Example #18
0
def test_ignore_host():
    sp = serverplayback.ServerPlayback()
    with taddons.context(sp) as tctx:
        tctx.configure(sp, server_replay_ignore_host=True)

        r = tflow.tflow(resp=True)
        r2 = tflow.tflow(resp=True)

        r.request.host = "address"
        r2.request.host = "address"
        assert sp._hash(r) == sp._hash(r2)
        r2.request.host = "wrong_address"
        assert sp._hash(r) == sp._hash(r2)
def test_server_playback_kill():
    s = serverplayback.ServerPlayback()
    with taddons.context() as tctx:
        tctx.configure(s, refresh_server_playback=True, replay_kill_extra=True)

        f = tflow.tflow()
        f.response = mitmproxy.test.tutils.tresp(content=f.request.content)
        s.load_flows([f])

        f = tflow.tflow()
        f.request.host = "nonexistent"
        tctx.cycle(s, f)
        assert f.reply.value == exceptions.Kill
Example #20
0
    def test_server_playback_kill(self):
        s = serverplayback.ServerPlayback()
        o = options.Options(refresh_server_playback=True,
                            replay_kill_extra=True)
        m = mastertest.RecordingMaster(o, proxy.DummyServer())
        m.addons.add(s)

        f = tutils.tflow()
        f.response = mitmproxy.test.tutils.tresp(content=f.request.content)
        s.load([f])

        f = tutils.tflow()
        f.request.host = "nonexistent"
        m.request(f)
        assert f.reply.value == exceptions.Kill
Example #21
0
def test_load_with_server_replay_nopop():
    s = serverplayback.ServerPlayback()
    s.configure(options.Options(server_replay_nopop=True), [])

    r = tflow.tflow(resp=True)
    r.request.headers["key"] = "one"

    r2 = tflow.tflow(resp=True)
    r2.request.headers["key"] = "two"

    s.load([r, r2])

    assert s.count() == 2
    s.next_flow(r)
    assert s.count() == 2
Example #22
0
async def test_server_playback_kill():
    s = serverplayback.ServerPlayback()
    with taddons.context(s) as tctx:
        tctx.configure(s,
                       server_replay_refresh=True,
                       server_replay_kill_extra=True)

        f = tflow.tflow()
        f.response = mitmproxy.test.tutils.tresp(content=f.request.content)
        s.load_flows([f])

        f = tflow.tflow()
        f.request.host = "nonexistent"
        await tctx.cycle(s, f)
        assert f.error
Example #23
0
def default_addons():
    return [
        onboarding.Onboarding(),
        anticache.AntiCache(),
        anticomp.AntiComp(),
        stickyauth.StickyAuth(),
        stickycookie.StickyCookie(),
        script.ScriptLoader(),
        filestreamer.FileStreamer(),
        streambodies.StreamBodies(),
        replace.Replace(),
        setheaders.SetHeaders(),
        serverplayback.ServerPlayback(),
        clientplayback.ClientPlayback(),
    ]
def test_ignore_params():
    s = serverplayback.ServerPlayback()
    s.configure(
        options.Options(server_replay_ignore_params=["param1", "param2"]), [])

    r = tflow.tflow(resp=True)
    r.request.path = "/test?param1=1"
    r2 = tflow.tflow(resp=True)
    r2.request.path = "/test"
    assert s._hash(r) == s._hash(r2)
    r2.request.path = "/test?param1=2"
    assert s._hash(r) == s._hash(r2)
    r2.request.path = "/test?param2=1"
    assert s._hash(r) == s._hash(r2)
    r2.request.path = "/test?param3=2"
    assert not s._hash(r) == s._hash(r2)
Example #25
0
def test_ignore_params():
    s = serverplayback.ServerPlayback()
    with taddons.context(s) as tctx:
        tctx.configure(s, server_replay_ignore_params=["param1", "param2"])

        r = tflow.tflow(resp=True)
        r.request.path = "/test?param1=1"
        r2 = tflow.tflow(resp=True)
        r2.request.path = "/test"
        assert s._hash(r) == s._hash(r2)
        r2.request.path = "/test?param1=2"
        assert s._hash(r) == s._hash(r2)
        r2.request.path = "/test?param2=1"
        assert s._hash(r) == s._hash(r2)
        r2.request.path = "/test?param3=2"
        assert not s._hash(r) == s._hash(r2)
Example #26
0
def test_load_with_server_replay_nopop():
    s = serverplayback.ServerPlayback()
    with taddons.context(s) as tctx:
        tctx.configure(s, server_replay_nopop=True)

        r = tflow.tflow(resp=True)
        r.request.headers["key"] = "one"

        r2 = tflow.tflow(resp=True)
        r2.request.headers["key"] = "two"

        s.load_flows([r, r2])

        assert s.count() == 2
        s.next_flow(r)
        assert s.count() == 2
Example #27
0
def test_headers():
    s = serverplayback.ServerPlayback()
    s.configure(options.Options(server_replay_use_headers=["foo"]), [])

    r = tflow.tflow(resp=True)
    r.request.headers["foo"] = "bar"
    r2 = tflow.tflow(resp=True)
    assert not s._hash(r) == s._hash(r2)
    r2.request.headers["foo"] = "bar"
    assert s._hash(r) == s._hash(r2)
    r2.request.headers["oink"] = "bar"
    assert s._hash(r) == s._hash(r2)

    r = tflow.tflow(resp=True)
    r2 = tflow.tflow(resp=True)
    assert s._hash(r) == s._hash(r2)
Example #28
0
def test_hash():
    s = serverplayback.ServerPlayback()
    s.configure(options.Options(), [])

    r = tflow.tflow()
    r2 = tflow.tflow()

    assert s._hash(r)
    assert s._hash(r) == s._hash(r2)
    r.request.headers["foo"] = "bar"
    assert s._hash(r) == s._hash(r2)
    r.request.path = "voing"
    assert s._hash(r) != s._hash(r2)

    r.request.path = "path?blank_value"
    r2.request.path = "path?"
    assert s._hash(r) != s._hash(r2)
Example #29
0
def test_headers():
    s = serverplayback.ServerPlayback()
    with taddons.context(s) as tctx:
        tctx.configure(s, server_replay_use_headers=["foo"])

        r = tflow.tflow(resp=True)
        r.request.headers["foo"] = "bar"
        r2 = tflow.tflow(resp=True)
        assert not s._hash(r) == s._hash(r2)
        r2.request.headers["foo"] = "bar"
        assert s._hash(r) == s._hash(r2)
        r2.request.headers["oink"] = "bar"
        assert s._hash(r) == s._hash(r2)

        r = tflow.tflow(resp=True)
        r2 = tflow.tflow(resp=True)
        assert s._hash(r) == s._hash(r2)
Example #30
0
def test_server_playback():
    sp = serverplayback.ServerPlayback()
    with taddons.context(sp) as tctx:
        tctx.configure(sp)
        f = tflow.tflow(resp=True)

        assert not sp.flowmap

        sp.load_flows([f])
        assert sp.flowmap
        assert sp.next_flow(f)
        assert not sp.flowmap

        sp.load_flows([f])
        assert sp.flowmap
        sp.clear()
        assert not sp.flowmap