summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/cors/redirect-userinfo.htm
blob: 1775d30dfb12509e76f4d81848724cc1886e25aa (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
<!DOCTYPE html>
<meta charset=utf-8>
<title>CORS - redirect with userinfo</title>
<meta name=author title="Odin Hørthe Omdal" href="mailto:odinho@opera.com">

<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script src=support.js?pipe=sub></script>

<h1>CORS userinfo redirect handling</h1>

<div id=log></div>

<script>

    // Test count for cache busting and easy identifying of request in traffic analyzer
    var num_test = 0

    shouldFail("Disallow redirect with userinfo (//user:pass@)", [
                    CROSSDOMAIN + "resources/cors-makeheader.py?",
                    CROSSDOMAIN.replace("http://", "http://test:test@") + "resources/cors-makeheader.py?"]);

    shouldFail("Disallow redirect with userinfo (//user:@)", [
                    CROSSDOMAIN + "resources/cors-makeheader.py?",
                    CROSSDOMAIN.replace("http://", "http://user:@") + "resources/cors-makeheader.py?"]);

    shouldFail("Disallow redirect with userinfo (//user@)", [
                    CROSSDOMAIN + "resources/cors-makeheader.py?",
                    CROSSDOMAIN.replace("http://", "http://user:@") + "resources/cors-makeheader.py?"]);

    shouldFail("Disallow redirect with userinfo (//:@)", [
                    CROSSDOMAIN + "resources/cors-makeheader.py?",
                    CROSSDOMAIN.replace("http://", "http://:@") + "resources/cors-makeheader.py?"]);

    shouldFail("Disallow redirect with userinfo (//:pass@)", [
                    CROSSDOMAIN + "resources/cors-makeheader.py?",
                    CROSSDOMAIN.replace("http://", "http://:pass@") + "resources/cors-makeheader.py?"]);

    shouldPass("Allow redirect with userinfo (//@)", [
                    CROSSDOMAIN + "resources/cors-makeheader.py?",
                    CROSSDOMAIN.replace("http://", "http://@") + "resources/cors-makeheader.py?"]);

    function shouldFail(desc, urls) {
        var test_id = num_test,
            t = async_test(desc);

        num_test++;

        t.step(function() {
            var client = new XMLHttpRequest();

            client.open('GET', buildURL(urls, test_id));

            client.onload = t.step_func(function() {
                assert_false(!!client.response, "Got response");
            });
            client.onerror = t.step_func(function(e) {
                t.done();
            });

            client.send(null)
        });
    }

    function shouldPass(desc, urls) {
        var test_id = num_test,
            t = async_test(desc);

        num_test++;

        t.step(function() {
            var client = new XMLHttpRequest();

            client.open('GET', buildURL(urls, test_id));

            client.onreadystatechange = t.step_func(function() {
                if (client.readyState != client.DONE)
                    return;
                assert_true(!!client.response, "Got response");
                r = JSON.parse(client.response)
                assert_equals(r['get_value'], 'last', 'get_value')
                t.done();
            });
            client.send(null)
        });
    }

    function buildURL(urls, id) {
        var tmp_url;

        if (typeof(urls) == "string") {
            return urls + "&" + id + "_0";
        }

        for (var i = urls.length; i--; ) {
            if (!tmp_url)
            {
                tmp_url = urls[i] + "&get_value=last&" + id + "_" + i;
                continue;
            }
            tmp_url = urls[i]
                        + "&location="
                        + encodeURIComponent(tmp_url)
                        + "&" + id + "_" + i;
        }

        return tmp_url;
    }

</script>