aboutsummaryrefslogtreecommitdiffstats
path: root/test/mitmproxy/proxy/test_server.py
blob: affdf221f063a3df681fe809c408dcd3d02ae3db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
import os
import socket
import time
from unittest import mock

import pytest

import mitmproxy.net.http
from mitmproxy import certs
from mitmproxy import exceptions
from mitmproxy import http
from mitmproxy import options
from mitmproxy.addons import proxyauth
from mitmproxy.addons import script
from mitmproxy.net import socks
from mitmproxy.net import tcp
from mitmproxy.net.http import http1
from mitmproxy.proxy.config import HostMatcher
from mitmproxy.test import tutils
from pathod import pathoc
from pathod import pathod
from .. import tservers
from ...conftest import skip_appveyor

"""
    Note that the choice of response code in these tests matters more than you
    might think. libcurl treats a 304 response code differently from, say, a
    200 response code - it will correctly terminate a 304 response with no
    content-length header, whereas it will block forever waiting for content
    for a 200 response.
"""


class CommonMixin:

    def test_large(self):
        assert len(self.pathod("200:b@50k").content) == 1024 * 50

    @staticmethod
    def wait_until_not_live(flow):
        """
        Race condition: We don't want to replay the flow while it is still live.
        """
        s = time.time()
        while flow.live:
            time.sleep(0.001)
            if time.time() - s > 5:
                raise RuntimeError("Flow is live for too long.")

    def test_replay(self):
        assert self.pathod("304").status_code == 304
        assert len(self.master.state.flows) == 1
        l = self.master.state.flows[-1]
        assert l.response.status_code == 304
        l.request.path = "/p/305"
        self.wait_until_not_live(l)
        rt = self.master.replay_request(l, block=True)
        assert l.response.status_code == 305

        # Disconnect error
        l.request.path = "/p/305:d0"
        rt = self.master.replay_request(l, block=True)
        assert rt
        if isinstance(self, tservers.HTTPUpstreamProxyTest):
            assert l.response.status_code == 502
        else:
            assert l.error

        # Port error
        l.request.port = 1
        # In upstream mode, we get a 502 response from the upstream proxy server.
        # In upstream mode with ssl, the replay will fail as we cannot establish
        # SSL with the upstream proxy.
        rt = self.master.replay_request(l, block=True)
        assert rt
        if isinstance(self, tservers.HTTPUpstreamProxyTest):
            assert l.response.status_code == 502
        else:
            assert l.error

    def test_http(self):
        f = self.pathod("304")
        assert f.status_code == 304

        # In Upstream mode with SSL, we may already have a previous CONNECT
        # request.
        l = self.master.state.flows[-1]
        assert l.client_conn.address
        assert "host" in l.request.headers
        assert l.response.status_code == 304

    def test_invalid_http(self):
        t = tcp.TCPClient(("127.0.0.1", self.proxy.port))
        with t.connect():
            t.wfile.write(b"invalid\r\n\r\n")
            t.wfile.flush()
            line = t.rfile.readline()
            assert (b"Bad Request" in line) or (b"Bad Gateway" in line)

    def test_sni(self):
        if not self.ssl:
            return

        if getattr(self, 'reverse', False):
            # In reverse proxy mode, we expect to use the upstream host as our SNI value
            expected_sni = "127.0.0.1"
        else:
            expected_sni = "testserver.com"

        f = self.pathod("304", sni="testserver.com")
        assert f.status_code == 304
        log = self.server.last_log()
        assert log["request"]["sni"] == expected_sni


class TcpMixin:

    def _ignore_on(self):
        assert not hasattr(self, "_ignore_backup")
        self._ignore_backup = self.options.ignore_hosts
        self.options.ignore_hosts = [".+:%s" % self.server.port] + self.options.ignore_hosts

    def _ignore_off(self):
        assert hasattr(self, "_ignore_backup")
        self.options.ignore_hosts = self._ignore_backup
        del self._ignore_backup

    def test_ignore(self):
        n = self.pathod("304")
        self._ignore_on()
        i = self.pathod("305")
        i2 = self.pathod("306")
        self._ignore_off()

        self.master.event_queue.join()

        assert n.status_code == 304
        assert i.status_code == 305
        assert i2.status_code == 306
        assert any(f.response.status_code == 304 for f in self.master.state.flows)
        assert not any(f.response.status_code == 305 for f in self.master.state.flows)
        assert not any(f.response.status_code == 306 for f in self.master.state.flows)

        # Test that we get the original SSL cert
        if self.ssl:
            i_cert = certs.SSLCert(i.sslinfo.certchain[0])
            i2_cert = certs.SSLCert(i2.sslinfo.certchain[0])
            n_cert = certs.SSLCert(n.sslinfo.certchain[0])

            assert i_cert == i2_cert
            assert i_cert != n_cert

        # Test Non-HTTP traffic
        spec = "200:i0,@100:d0"  # this results in just 100 random bytes
        # mitmproxy responds with bad gateway
        assert self.pathod(spec).status_code == 502
        self._ignore_on()
        with pytest.raises(exceptions.HttpException):
            self.pathod(spec)  # pathoc tries to parse answer as HTTP

        self._ignore_off()

    def _tcpproxy_on(self):
        assert not hasattr(self, "_tcpproxy_backup")
        self._tcpproxy_backup = self.options.tcp_hosts
        self.options.tcp_hosts = [".+:%s" % self.server.port] + self.options.tcp_hosts

    def _tcpproxy_off(self):
        assert hasattr(self, "_tcpproxy_backup")
        self.options.tcp_hosts = self._tcpproxy_backup
        del self._tcpproxy_backup

    def test_tcp(self):
        n = self.pathod("304")
        self._tcpproxy_on()
        i = self.pathod("305")
        i2 = self.pathod("306")
        self._tcpproxy_off()

        self.master.event_queue.join()

        assert n.status_code == 304
        assert i.status_code == 305
        assert i2.status_code == 306
        assert any(f.response.status_code == 304 for f in self.master.state.flows if isinstance(f, http.HTTPFlow))
        assert not any(f.response.status_code == 305 for f in self.master.state.flows if isinstance(f, http.HTTPFlow))
        assert not any(f.response.status_code == 306 for f in self.master.state.flows if isinstance(f, http.HTTPFlow))

        # Test that we get the original SSL cert
        if self.ssl:
            i_cert = certs.SSLCert(i.sslinfo.certchain[0])
            i2_cert = certs.SSLCert(i2.sslinfo.certchain[0])
            n_cert = certs.SSLCert(n.sslinfo.certchain[0])

            assert i_cert == i2_cert
            assert i_cert != n_cert

        # Make sure that TCP messages are in the event log.
        # Re-enable and fix this when we start keeping TCPFlows in the state.
        # assert any("305" in m for m in self.master.tlog)
        # assert any("306" in m for m in self.master.tlog)


class TestHTTP(tservers.HTTPProxyTest, CommonMixin):
    def test_invalid_connect(self):
        t = tcp.TCPClient(("127.0.0.1", self.proxy.port))
        with t.connect():
            t.wfile.write(b"CONNECT invalid\n\n")
            t.wfile.flush()
            assert b"Bad Request" in t.rfile.readline()

    def test_upstream_ssl_error(self):
        p = self.pathoc()
        with p.connect():
            ret = p.request("get:'https://localhost:%s/'" % self.server.port)
        assert ret.status_code == 400

    def test_connection_close(self):
        # Add a body, so we have a content-length header, which combined with
        # HTTP1.1 means the connection is kept alive.
        response = '%s/p/200:b@1' % self.server.urlbase

        # Lets sanity check that the connection does indeed stay open by
        # issuing two requests over the same connection
        p = self.pathoc()
        with p.connect():
            assert p.request("get:'%s'" % response)
            assert p.request("get:'%s'" % response)

        # Now check that the connection is closed as the client specifies
        p = self.pathoc()
        with p.connect():
            assert p.request("get:'%s':h'Connection'='close'" % response)
            # There's a race here, which means we can get any of a number of errors.
            # Rather than introduce yet another sleep into the test suite, we just
            # relax the Exception specification.
            with pytest.raises(Exception):
                p.request("get:'%s'" % response)

    def test_reconnect(self):
        req = "get:'%s/p/200:b@1'" % self.server.urlbase
        p = self.pathoc()

        class MockOnce:
            call = 0

            def mock_once(self, http1obj, req):
                self.call += 1
                if self.call == 1:
                    raise exceptions.TcpDisconnect
                else:
                    headers = http1.assemble_request_head(req)
                    http1obj.server_conn.wfile.write(headers)
                    http1obj.server_conn.wfile.flush()

        with p.connect():
            with mock.patch("mitmproxy.proxy.protocol.http1.Http1Layer.send_request_headers",
                            side_effect=MockOnce().mock_once, autospec=True):
                # Server disconnects while sending headers but mitmproxy reconnects
                resp = p.request(req)
                assert resp
                assert resp.status_code == 200

    def test_get_connection_switching(self):
        req = "get:'%s/p/200:b@1'"
        p = self.pathoc()
        with p.connect():
            assert p.request(req % self.server.urlbase)
            assert p.request(req % self.server2.urlbase)
        assert self.proxy.tmaster.has_log("serverdisconnect")

    def test_blank_leading_line(self):
        p = self.pathoc()
        with p.connect():
            req = "get:'%s/p/201':i0,'\r\n'"
            assert p.request(req % self.server.urlbase).status_code == 201

    def test_invalid_headers(self):
        p = self.pathoc()
        with p.connect():
            resp = p.request("get:'http://foo':h':foo'='bar'")
        assert resp.status_code == 400

    def test_stream_modify(self):
        s = script.Script(
            tutils.test_data.path("mitmproxy/data/addonscripts/stream_modify.py")
        )
        self.master.addons.add(s)
        d = self.pathod('200:b"foo"')
        assert d.content == b"bar"
        self.master.addons.remove(s)

    def test_first_line_rewrite(self):
        """
        If mitmproxy is a regular HTTP proxy, it must rewrite an absolute-form request like
            GET http://example.com/foo HTTP/1.0
        to
            GET /foo HTTP/1.0
        when sending the request upstream. While any server should technically accept
        the absolute form, this is not the case in practice.
        """
        req = "get:'%s/p/200'" % self.server.urlbase
        p = self.pathoc()
        with p.connect():
            assert p.request(req).status_code == 200
            assert self.server.last_log()["request"]["first_line_format"] == "relative"


class TestHTTPAuth(tservers.HTTPProxyTest):
    def test_auth(self):
        self.master.addons.add(proxyauth.ProxyAuth())
        self.master.addons.trigger(
            "configure", self.master.options.keys()
        )
        self.master.options.proxyauth = "test:test"
        assert self.pathod("202").status_code == 407
        p = self.pathoc()
        with p.connect():
            ret = p.request("""
                get
                'http://localhost:%s/p/202'
                h'%s'='%s'
            """ % (
                self.server.port,
                "Proxy-Authorization",
                proxyauth.mkauth("test", "test")
            ))
        assert ret.status_code == 202


class TestHTTPReverseAuth(tservers.ReverseProxyTest):
    def test_auth(self):
        self.master.addons.add(proxyauth.ProxyAuth())
        self.master.options.proxyauth = "test:test"
        assert self.pathod("202").status_code == 401
        p = self.pathoc()
        with p.connect():
            ret = p.request("""
                get
                '/p/202'
                h'%s'='%s'
            """ % (
                "Authorization",
                proxyauth.mkauth("test", "test")
            ))
        assert ret.status_code == 202


class TestHTTPS(tservers.HTTPProxyTest, CommonMixin, TcpMixin):
    ssl = True
    ssloptions = pathod.SSLOptions(request_client_cert=True)

    def test_clientcert_file(self):
        try:
            self.options.client_certs = os.path.join(
                tutils.test_data.path("mitmproxy/data/clientcert"), "client.pem")
            f = self.pathod("304")
            assert f.status_code == 304
            assert self.server.last_log()["request"]["clientcert"]["keyinfo"]
        finally:
            self.options.client_certs = None

    def test_clientcert_dir(self):
        try:
            self.options.client_certs = tutils.test_data.path("mitmproxy/data/clientcert")
            f = self.pathod("304")
            assert f.status_code == 304
            assert self.server.last_log()["request"]["clientcert"]["keyinfo"]
        finally:
            self.options.client_certs = None

    def test_error_post_connect(self):
        p = self.pathoc()
        with p.connect():
            assert p.request("get:/:i0,'invalid\r\n\r\n'").status_code == 400


class TestHTTPSCertfile(tservers.HTTPProxyTest, CommonMixin):
    ssl = True
    certfile = True

    def test_certfile(self):
        assert self.pathod("304")


class TestHTTPSSecureByDefault:
    def test_secure_by_default(self):
        """
        Certificate verification should be turned on by default.
        """
        default_opts = options.Options()
        assert not default_opts.ssl_insecure


class TestHTTPSUpstreamServerVerificationWTrustedCert(tservers.HTTPProxyTest):

    """
    Test upstream server certificate verification with a trusted server cert.
    """
    ssl = True
    ssloptions = pathod.SSLOptions(
        cn=b"example.mitmproxy.org",
        certs=[
            ("example.mitmproxy.org", tutils.test_data.path("mitmproxy/data/servercert/trusted-leaf.pem"))
        ]
    )

    def _request(self):
        p = self.pathoc(sni="example.mitmproxy.org")
        with p.connect():
            return p.request("get:/p/242")

    def test_verification_w_cadir(self):
        self.options.update(
            ssl_insecure=False,
            ssl_verify_upstream_trusted_cadir=tutils.test_data.path(
                "mitmproxy/data/servercert/"
            ),
            ssl_verify_upstream_trusted_ca=None,
        )
        assert self._request().status_code == 242

    def test_verification_w_pemfile(self):
        self.options.update(
            ssl_insecure=False,
            ssl_verify_upstream_trusted_cadir=None,
            ssl_verify_upstream_trusted_ca=tutils.test_data.path(
                "mitmproxy/data/servercert/trusted-root.pem"
            ),
        )
        assert self._request().status_code == 242


class TestHTTPSUpstreamServerVerificationWBadCert(tservers.HTTPProxyTest):

    """
    Test upstream server certificate verification with an untrusted server cert.
    """
    ssl = True
    ssloptions = pathod.SSLOptions(
        cn=b"example.mitmproxy.org",
        certs=[
            ("example.mitmproxy.org", tutils.test_data.path("mitmproxy/data/servercert/self-signed.pem"))
        ])

    def _request(self):
        p = self.pathoc(sni="example.mitmproxy.org")
        with p.connect():
            return p.request("get:/p/242")

    @classmethod
    def get_options(cls):
        opts = super().get_options()
        opts.ssl_verify_upstream_trusted_ca = tutils.test_data.path(
            "mitmproxy/data/servercert/trusted-root.pem"
        )
        return opts

    def test_no_verification_w_bad_cert(self):
        self.options.ssl_insecure = True
        r = self._request()
        assert r.status_code == 242

    def test_verification_w_bad_cert(self):
        # We only test for a single invalid cert here.
        # Actual testing of different root-causes (invalid hostname, expired, ...)
        # is done in mitmproxy.net.
        self.options.ssl_insecure = False
        r = self._request()
        assert r.status_code == 502
        assert b"Certificate verification error" in r.raw_content


class TestHTTPSNoCommonName(tservers.HTTPProxyTest):

    """
    Test what happens if we get a cert without common name back.
    """
    ssl = True
    ssloptions = pathod.SSLOptions(
        certs=[
            (b"*", tutils.test_data.path("mitmproxy/data/no_common_name.pem"))
        ]
    )

    def test_http(self):
        f = self.pathod("202")
        assert f.sslinfo.certchain[0].get_subject().CN == "127.0.0.1"


class TestReverse(tservers.ReverseProxyTest, CommonMixin, TcpMixin):
    reverse = True

    def test_host_header(self):
        self.options.keep_host_header = True
        p = self.pathoc()
        with p.connect():
            resp = p.request("get:/p/200:h'Host'='example.com'")
        assert resp.status_code == 200

        req = self.master.state.flows[0].request
        assert req.host_header == "example.com"

    def test_overridden_host_header(self):
        self.options.keep_host_header = False  # default value
        p = self.pathoc()
        with p.connect():
            resp = p.request("get:/p/200:h'Host'='example.com'")
        assert resp.status_code == 200

        req = self.master.state.flows[0].request
        assert req.host_header == "127.0.0.1"


class TestReverseSSL(tservers.ReverseProxyTest, CommonMixin, TcpMixin):
    reverse = True
    ssl = True


class TestSocks5(tservers.SocksModeTest):

    def test_simple(self):
        p = self.pathoc()
        with p.connect():
            p.socks_connect(("localhost", self.server.port))
            f = p.request("get:/p/200")
        assert f.status_code == 200

    def test_with_authentication_only(self):
        p = self.pathoc()
        with p.connect():
            f = p.request("get:/p/200")
        assert f.status_code == 502
        assert b"SOCKS5 mode failure" in f.content
        assert b"Invalid SOCKS version. Expected 0x05, got 0x47" in f.content

    def test_no_connect(self):
        """
        mitmproxy doesn't support UDP or BIND SOCKS CMDs
        """
        p = self.pathoc()
        with p.connect():
            socks.ClientGreeting(
                socks.VERSION.SOCKS5,
                [socks.METHOD.NO_AUTHENTICATION_REQUIRED]
            ).to_file(p.wfile)
            socks.Message(
                socks.VERSION.SOCKS5,
                socks.CMD.BIND,
                socks.ATYP.DOMAINNAME,
                ("example.com", 8080)
            ).to_file(p.wfile)

            p.wfile.flush()
            p.rfile.read(2)  # read server greeting
            f = p.request("get:/p/200")  # the request doesn't matter, error response from handshake will be read anyway.
        assert f.status_code == 502
        assert b"SOCKS5 mode failure" in f.content
        assert b"mitmproxy only supports SOCKS5 CONNECT" in f.content

    def test_with_authentication(self):
        p = self.pathoc()
        with p.connect():
            socks.ClientGreeting(
                socks.VERSION.SOCKS5,
                [socks.METHOD.USERNAME_PASSWORD]
            ).to_file(p.wfile)
            p.wfile.flush()
            f = p.request("get:/p/200")  # the request doesn't matter, error response from handshake will be read anyway.
        assert f.status_code == 502
        assert b"SOCKS5 mode failure" in f.content
        assert b"mitmproxy only supports SOCKS without authentication" in f.content


class TestSocks5SSL(tservers.SocksModeTest):
    ssl = True

    def test_simple(self):
        p = self.pathoc_raw()
        with p.connect():
            p.socks_connect(("localhost", self.server.port))
            p.convert_to_ssl()
            f = p.request("get:/p/200")
        assert f.status_code == 200


class TestHttps2Http(tservers.ReverseProxyTest):

    @classmethod
    def get_options(cls):
        opts = super().get_options()
        return opts

    def pathoc(self, ssl, sni=None):
        """
            Returns a connected Pathoc instance.
        """
        p = pathoc.Pathoc(
            ("localhost", self.proxy.port), ssl=True, sni=sni, fp=None
        )
        return p

    def test_all(self):
        p = self.pathoc(ssl=True)
        with p.connect():
            assert p.request("get:'/p/200'").status_code == 200

    def test_sni(self):
        p = self.pathoc(ssl=True, sni="example.com")
        with p.connect():
            assert p.request("get:'/p/200'").status_code == 200
            assert not self.proxy.tmaster.has_log("error in handle_sni")

    def test_http(self):
        p = self.pathoc(ssl=False)
        with p.connect():
            assert p.request("get:'/p/200'").status_code == 200


class TestTransparent(tservers.TransparentProxyTest, CommonMixin, TcpMixin):
    ssl = False

    def test_tcp_stream_modify(self):
        s = script.Script(
            tutils.test_data.path("mitmproxy/data/addonscripts/tcp_stream_modify.py")
        )
        self.master.addons.add(s)
        self._tcpproxy_on()
        d = self.pathod('200:b"foo"')
        self._tcpproxy_off()
        assert d.content == b"bar"
        self.master.addons.remove(s)


class TestTransparentSSL(tservers.TransparentProxyTest, CommonMixin, TcpMixin):
    ssl = True

    def test_sslerr(self):
        p = pathoc.Pathoc(("localhost", self.proxy.port), fp=None)
        p.connect()
        r = p.request("get:/")
        assert r.status_code == 502


class TestProxy(tservers.HTTPProxyTest):

    def test_http(self):
        f = self.pathod("304")
        assert f.status_code == 304

        f = self.master.state.flows[0]
        assert f.client_conn.address
        assert "host" in f.request.headers
        assert f.response.status_code == 304

    @skip_appveyor
    def test_response_timestamps(self):
        # test that we notice at least 1 sec delay between timestamps
        # in response object
        f = self.pathod("304:b@1k:p50,1")
        assert f.status_code == 304

        response = self.master.state.flows[0].response
        # timestamp_start might fire a bit late, so we play safe and only require 300ms.
        assert 0.3 <= response.timestamp_end - response.timestamp_start

    @skip_appveyor
    def test_request_timestamps(self):
        # test that we notice a delay between timestamps in request object
        connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        connection.connect(("127.0.0.1", self.proxy.port))

        # call pathod server, wait a second to complete the request
        connection.send(
            b"GET http://localhost:%d/p/304:b@1k HTTP/1.1\r\n" %
            self.server.port)
        time.sleep(1)
        connection.send(b"\r\n")
        connection.recv(50000)
        connection.close()

        request, response = self.master.state.flows[
            0].request, self.master.state.flows[0].response
        assert response.status_code == 304  # sanity test for our low level request
        # timestamp_start might fire a bit late, so we play safe and only require 300ms.
        assert 0.3 <= request.timestamp_end - request.timestamp_start

    def test_request_tcp_setup_timestamp_presence(self):
        # tests that the client_conn a tcp connection has a tcp_setup_timestamp
        connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        connection.connect(("localhost", self.proxy.port))
        connection.send(
            b"GET http://localhost:%d/p/200:b@1k HTTP/1.1\r\n" %
            self.server.port)
        connection.send(b"\r\n")
        # a bit hacky: make sure that we don't just read the headers only.
        recvd = 0
        while recvd < 1024:
            recvd += len(connection.recv(5000))
        connection.send(
            b"GET http://localhost:%d/p/200:b@1k HTTP/1.1\r\n" %
            self.server.port)
        connection.send(b"\r\nb")
        recvd = 0
        while recvd < 1024:
            recvd += len(connection.recv(5000))
        connection.close()

        first_flow = self.master.state.flows[0]
        second_flow = self.master.state.flows[1]
        assert first_flow.server_conn.timestamp_tcp_setup
        assert first_flow.server_conn.timestamp_ssl_setup is None
        assert second_flow.server_conn.timestamp_tcp_setup
        assert first_flow.server_conn.timestamp_tcp_setup == second_flow.server_conn.timestamp_tcp_setup

    def test_request_ip(self):
        f = self.pathod("200:b@100")
        assert f.status_code == 200
        f = self.master.state.flows[0]
        assert f.server_conn.address == ("127.0.0.1", self.server.port)


class TestProxySSL(tservers.HTTPProxyTest):
    ssl = True

    def test_request_ssl_setup_timestamp_presence(self):
        # tests that the ssl timestamp is present when ssl is used
        f = self.pathod("304:b@10k")
        assert f.status_code == 304
        first_flow = self.master.state.flows[0]
        assert first_flow.server_conn.timestamp_ssl_setup

    def test_via(self):
        # tests that the ssl timestamp is present when ssl is used
        f = self.pathod("200:b@10")
        assert f.status_code == 200
        first_flow = self.master.state.flows[0]
        assert not first_flow.server_conn.via


class ARedirectRequest:
    def __init__(self, redirect_port):
        self.redirect_port = redirect_port

    def request(self, f):
        if f.request.path == "/p/201":
            # This part should have no impact, but it should also not cause any exceptions.
            addr = f.live.server_conn.address
            addr2 = ("127.0.0.1", self.redirect_port)
            f.live.set_server(addr2)
            f.live.set_server(addr)

            # This is the actual redirection.
            f.request.port = self.redirect_port

    def response(self, f):
        f.response.content = bytes(f.client_conn.address[1])
        f.response.headers["server-conn-id"] = str(f.server_conn.source_address[1])


class TestRedirectRequest(tservers.HTTPProxyTest):
    ssl = True

    def test_redirect(self):
        """
        Imagine a single HTTPS connection with three requests:

        1. First request should pass through unmodified
        2. Second request will be redirected to a different host by an inline script
        3. Third request should pass through unmodified

        This test verifies that the original destination is restored for the third request.
        """
        self.proxy.tmaster.addons.add(ARedirectRequest(self.server2.port))

        p = self.pathoc()
        with p.connect():
            self.server.clear_log()
            self.server2.clear_log()
            r1 = p.request("get:'/p/200'")
            assert r1.status_code == 200
            assert self.server.last_log()
            assert not self.server2.expect_log(1, 0.5)

            self.server.clear_log()
            self.server2.clear_log()
            r2 = p.request("get:'/p/201'")
            assert r2.status_code == 201
            assert not self.server.expect_log(1, 0.5)
            assert self.server2.last_log()

            self.server.clear_log()
            self.server2.clear_log()
            r3 = p.request("get:'/p/202'")
            assert r3.status_code == 202
            assert self.server.last_log()
            assert not self.server2.expect_log(1, 0.5)

            assert r1.content == r2.content == r3.content


class AStreamRequest:

    """
        Enables the stream flag on the flow for all requests
    """
    def responseheaders(self, f):
        f.response.stream = True


class TestStreamRequest(tservers.HTTPProxyTest):
    def test_stream_simple(self):
        self.proxy.tmaster.addons.add(AStreamRequest())
        p = self.pathoc()
        with p.connect():
            # a request with 100k of data but without content-length
            r1 = p.request("get:'%s/p/200:r:b@100k:d102400'" % self.server.urlbase)
            assert r1.status_code == 200
            assert len(r1.content) > 100000

    def test_stream_multiple(self):
        self.proxy.tmaster.addons.add(AStreamRequest())
        p = self.pathoc()
        with p.connect():
            # simple request with streaming turned on
            r1 = p.request("get:'%s/p/200'" % self.server.urlbase)
            assert r1.status_code == 200

            # now send back 100k of data, streamed but not chunked
            r1 = p.request("get:'%s/p/201:b@100k'" % self.server.urlbase)
            assert r1.status_code == 201

    def test_stream_chunked(self):
        self.proxy.tmaster.addons.add(AStreamRequest())
        connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        connection.connect(("127.0.0.1", self.proxy.port))
        fconn = connection.makefile("rb")
        spec = '200:h"Transfer-Encoding"="chunked":r:b"4\\r\\nthis\\r\\n11\\r\\nisatest__reachhex\\r\\n0\\r\\n\\r\\n"'
        connection.send(
            b"GET %s/p/%s HTTP/1.1\r\n" %
            (self.server.urlbase.encode(), spec.encode()))
        connection.send(b"\r\n")

        resp = http1.read_response_head(fconn)

        assert resp.headers["Transfer-Encoding"] == 'chunked'
        assert resp.status_code == 200

        chunks = list(http1.read_body(fconn, None))
        assert chunks == [b"this", b"isatest__reachhex"]

        connection.close()


class AFakeResponse:
    def request(self, f):
        f.response = http.HTTPResponse.wrap(mitmproxy.test.tutils.tresp())


class TestFakeResponse(tservers.HTTPProxyTest):

    def test_fake(self):
        self.proxy.tmaster.addons.add(AFakeResponse())
        f = self.pathod("200")
        assert "header-response" in f.headers


class TestServerConnect(tservers.HTTPProxyTest):
    ssl = True

    @classmethod
    def get_options(cls):
        opts = tservers.HTTPProxyTest.get_options()
        opts.upstream_cert = False
        return opts

    def test_unnecessary_serverconnect(self):
        """A replayed/fake response with no upstream_cert should not connect to an upstream server"""
        self.proxy.tmaster.addons.add(AFakeResponse())
        assert self.pathod("200").status_code == 200
        assert not self.proxy.tmaster.has_log("serverconnect")


class AKillRequest:

    def request(self, f):
        f.reply.kill()


class TestKillRequest(tservers.HTTPProxyTest):
    def test_kill(self):
        self.proxy.tmaster.addons.add(AKillRequest())
        with pytest.raises(exceptions.HttpReadDisconnect):
            self.pathod("200")
        # Nothing should have hit the server
        assert not self.server.expect_log(1, 0.5)


class AKillResponse:
    def response(self, f):
        f.reply.kill()


class TestKillResponse(tservers.HTTPProxyTest):
    def test_kill(self):
        self.proxy.tmaster.addons.add(AKillResponse())
        with pytest.raises(exceptions.HttpReadDisconnect):
            self.pathod("200")
        # The server should have seen a request
        assert self.server.last_log()


class TestTransparentResolveError(tservers.TransparentProxyTest):
    @mock.patch("mitmproxy.platform.original_addr")
    def test_resolve_error(self, original_addr):
        original_addr.side_effect = RuntimeError
        assert self.pathod("304").status_code == 502


class AIncomplete:
    def request(self, f):
        resp = http.HTTPResponse.wrap(mitmproxy.test.tutils.tresp())
        resp.content = None
        f.response = resp


class TestIncompleteResponse(tservers.HTTPProxyTest):
    def test_incomplete(self):
        self.proxy.tmaster.addons.add(AIncomplete())
        assert self.pathod("200").status_code == 502


class TestUpstreamProxy(tservers.HTTPUpstreamProxyTest, CommonMixin):
    ssl = False


class TestUpstreamProxySSL(
        tservers.HTTPUpstreamProxyTest,
        CommonMixin,
        TcpMixin):
    ssl = True

    def _host_pattern_on(self, attr):
        """
        Updates config.check_tcp or check_ignore, depending on attr.
        """
        assert not hasattr(self, "_ignore_%s_backup" % attr)
        backup = []
        for proxy in self.chain:
            old_matcher = getattr(
                proxy.tmaster.server.config,
                "check_%s" %
                attr)
            backup.append(old_matcher)
            setattr(
                proxy.tmaster.server.config,
                "check_%s" % attr,
                HostMatcher([".+:%s" % self.server.port] + old_matcher.patterns)
            )

        setattr(self, "_ignore_%s_backup" % attr, backup)

    def _host_pattern_off(self, attr):
        backup = getattr(self, "_ignore_%s_backup" % attr)
        for proxy in reversed(self.chain):
            setattr(
                proxy.tmaster.server.config,
                "check_%s" % attr,
                backup.pop()
            )

        assert not backup
        delattr(self, "_ignore_%s_backup" % attr)

    def _ignore_on(self):
        super()._ignore_on()
        self._host_pattern_on("ignore")

    def _ignore_off(self):
        super()._ignore_off()
        self._host_pattern_off("ignore")

    def _tcpproxy_on(self):
        super()._tcpproxy_on()
        self._host_pattern_on("tcp")

    def _tcpproxy_off(self):
        super()._tcpproxy_off()
        self._host_pattern_off("tcp")

    def test_simple(self):
        p = self.pathoc()
        with p.connect():
            req = p.request("get:'/p/418:b\"content\"'")
        assert req.content == b"content"
        assert req.status_code == 418

        # CONNECT from pathoc to chain[0],
        assert len(self.proxy.tmaster.state.flows) == 1
        assert self.proxy.tmaster.state.flows[0].server_conn.via
        # request from pathoc to chain[0]
        # CONNECT from proxy to chain[1],
        assert len(self.chain[0].tmaster.state.flows) == 1
        assert self.chain[0].tmaster.state.flows[0].server_conn.via
        # request from proxy to chain[1]
        # request from chain[0] (regular proxy doesn't store CONNECTs)
        assert not self.chain[1].tmaster.state.flows[0].server_conn.via
        assert len(self.chain[1].tmaster.state.flows) == 1

    def test_change_upstream_proxy_connect(self):
        # skip chain[0].
        self.proxy.tmaster.addons.add(
            UpstreamProxyChanger(
                ("127.0.0.1", self.chain[1].port)
            )
        )
        p = self.pathoc()
        with p.connect():
            req = p.request("get:'/p/418'")

        assert req.status_code == 418
        assert len(self.chain[0].tmaster.state.flows) == 0
        assert len(self.chain[1].tmaster.state.flows) == 1

    def test_connect_https_to_http(self):
        """
        https://github.com/mitmproxy/mitmproxy/issues/2329

        Client <- HTTPS -> Proxy <- HTTP -> Proxy <- HTTPS -> Server
        """
        self.proxy.tmaster.addons.add(RewriteToHttp())
        self.chain[1].tmaster.addons.add(RewriteToHttps())
        p = self.pathoc()
        with p.connect():
            resp = p.request("get:'/p/418'")

        assert self.proxy.tmaster.state.flows[0].client_conn.tls_established
        assert not self.proxy.tmaster.state.flows[0].server_conn.tls_established
        assert not self.chain[1].tmaster.state.flows[0].client_conn.tls_established
        assert self.chain[1].tmaster.state.flows[0].server_conn.tls_established
        assert resp.status_code == 418


class RewriteToHttp:
    def http_connect(self, f):
        f.request.scheme = "http"

    def request(self, f):
        f.request.scheme = "http"


class RewriteToHttps:
    def http_connect(self, f):
        f.request.scheme = "https"

    def request(self, f):
        f.request.scheme = "https"


class UpstreamProxyChanger:
    def __init__(self, addr):
        self.address = addr

    def request(self, f):
        f.live.change_upstream_proxy_server(self.address)


class RequestKiller:
    def __init__(self, exclude):
        self.exclude = exclude
        self.k = 0

    def request(self, f):
        self.k += 1
        if self.k not in self.exclude:
            f.reply.kill()


class TestProxyChainingSSLReconnect(tservers.HTTPUpstreamProxyTest):
    ssl = True

    def test_reconnect(self):
        """
        Tests proper functionality of ConnectionHandler.server_reconnect mock.
        If we have a disconnect on a secure connection that's transparently
        proxified to an upstream http proxy, we need to send the CONNECT
        request again.
        """

        class MockOnce:
            call = 0

            def mock_once(self, http1obj, req):
                self.call += 1

                if self.call == 2:
                    headers = http1.assemble_request_head(req)
                    http1obj.server_conn.wfile.write(headers)
                    http1obj.server_conn.wfile.flush()
                    raise exceptions.TcpDisconnect
                else:
                    headers = http1.assemble_request_head(req)
                    http1obj.server_conn.wfile.write(headers)
                    http1obj.server_conn.wfile.flush()

        self.chain[0].tmaster.addons.add(RequestKiller([1, 2]))
        self.chain[1].tmaster.addons.add(RequestKiller([1]))

        p = self.pathoc()
        with p.connect():
            req = p.request("get:'/p/418:b\"content\"'")
            assert req.content == b"content"
            assert req.status_code == 418

            # First request goes through all three proxies exactly once
            assert len(self.proxy.tmaster.state.flows) == 1
            assert len(self.chain[0].tmaster.state.flows) == 1
            assert len(self.chain[1].tmaster.state.flows) == 1

            with mock.patch("mitmproxy.proxy.protocol.http1.Http1Layer.send_request_headers",
                            side_effect=MockOnce().mock_once, autospec=True):
                req = p.request("get:'/p/418:b\"content2\"'")

            assert req.status_code == 502

            assert len(self.proxy.tmaster.state.flows) == 2
            assert len(self.chain[0].tmaster.state.flows) == 2
            # Upstream sees two requests due to reconnection attempt
            assert len(self.chain[1].tmaster.state.flows) == 3
            assert not self.chain[1].tmaster.state.flows[-1].response
            assert not self.chain[1].tmaster.state.flows[-2].response

            # Reconnection failed, so we're now disconnected
            with pytest.raises(exceptions.HttpException):
                p.request("get:'/p/418:b\"content3\"'")


class AddUpstreamCertsToClientChainMixin:

    ssl = True
    servercert = tutils.test_data.path("mitmproxy/data/servercert/trusted-root.pem")
    ssloptions = pathod.SSLOptions(
        cn=b"example.mitmproxy.org",
        certs=[
            (b"example.mitmproxy.org", servercert)
        ]
    )

    def test_add_upstream_certs_to_client_chain(self):
        with open(self.servercert, "rb") as f:
            d = f.read()
        upstreamCert = certs.SSLCert.from_pem(d)
        p = self.pathoc()
        with p.connect():
            upstream_cert_found_in_client_chain = False
            for receivedCert in p.server_certs:
                if receivedCert.digest('sha256') == upstreamCert.digest('sha256'):
                    upstream_cert_found_in_client_chain = True
                    break
            assert(upstream_cert_found_in_client_chain == self.master.options.add_upstream_certs_to_client_chain)


class TestHTTPSAddUpstreamCertsToClientChainTrue(
    AddUpstreamCertsToClientChainMixin,
    tservers.HTTPProxyTest
):
    """
    If --add-server-certs-to-client-chain is True, then the client should
    receive the upstream server's certificates
    """
    @classmethod
    def get_options(cls):
        opts = super().get_options()
        opts.add_upstream_certs_to_client_chain = True
        return opts


class TestHTTPSAddUpstreamCertsToClientChainFalse(
    AddUpstreamCertsToClientChainMixin,
    tservers.HTTPProxyTest
):
    """
    If --add-server-certs-to-client-chain is False, then the client should not
    receive the upstream server's certificates
    """
    @classmethod
    def get_options(cls):
        opts = super().get_options()
        opts.add_upstream_certs_to_client_chain = False
        return opts