forked from sblask-webextensions/webextension-skip-redirect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-url.js
154 lines (126 loc) · 13.7 KB
/
test-url.js
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
const test = require("tape");
const base64 = require("./base64");
const url = require("./url");
const queryAndFragment = "?some=parameter&some-other=parameter;another=parameter#some-fragment";
const wwwTargetUrl = "www.redirection.target.com/" + queryAndFragment;
const wwwTargetUrlEncoded = encodeURIComponent(wwwTargetUrl);
const wwwTargetUrlDoubleEncoded = encodeURIComponent(wwwTargetUrlEncoded);
const someTargetUrl = "some.www.redirection.target.com/" + queryAndFragment;
const someTargetUrlEncoded = encodeURIComponent(someTargetUrl);
const someTargetUrlDoubleEncoded = encodeURIComponent(someTargetUrlEncoded);
test("URL in query path - http URLs url-encoded and not", function(assert) {
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "http://" + someTargetUrl, []), "http://" + someTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "http%3A%2F%2F" + someTargetUrlEncoded, []), "http://" + someTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "http%3A%2F%2F" + someTargetUrlEncoded + "&unwanted", []), "http://" + someTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "http%253A%252F%252F" + someTargetUrlDoubleEncoded, []), "http://" + someTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "http%253A%252F%252F" + someTargetUrlDoubleEncoded + "&unwanted", []), "http://" + someTargetUrl);
assert.end();
});
test("URL in query path - http URLs url-encoded and not - lowercase", function(assert) {
assert.equal(url.getRedirectTarget(("http://" + "www.some.website.com" + "/" + "http://" + someTargetUrl).toLowerCase(), []), "http://" + someTargetUrl);
assert.equal(url.getRedirectTarget(("http://" + "www.some.website.com" + "/" + "http%3A%2F%2F" + someTargetUrlEncoded).toLowerCase(), []), "http://" + someTargetUrl);
assert.equal(url.getRedirectTarget(("http://" + "www.some.website.com" + "/" + "http%253A%252F%252F" + someTargetUrlDoubleEncoded).toLowerCase(), []), "http://" + someTargetUrl);
assert.end();
});
test("URL in query path - works with initial /? and ? instead of slash", function(assert) {
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/?" + "http://" + someTargetUrl, []), "http://" + someTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "?" + "http://" + someTargetUrl, []), "http://" + someTargetUrl);
assert.end();
});
test("URL in query path - correct http handling", function(assert) {
assert.equal(url.getRedirectTarget("https://" + "www.some.website.com" + "/" + "http://" + someTargetUrl, []), "http://" + someTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "https://" + someTargetUrl, []), "https://" + someTargetUrl);
assert.end();
});
test("URL in query path - http URLs with some parts being url-encoded and others not", function(assert) {
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "http://" + someTargetUrlEncoded, []), "http://" + someTargetUrl);
assert.end();
});
test("URL in query path - www URLs work without http", function(assert) {
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + wwwTargetUrl, []), "http://" + wwwTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + wwwTargetUrlDoubleEncoded, []), "http://" + wwwTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + wwwTargetUrlEncoded, []), "http://" + wwwTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "https://" + wwwTargetUrl, []), "https://" + wwwTargetUrl);
assert.end();
});
test("URL in query parameter - http URLs url-encoded and not", function(assert) {
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "?target=" + "http://" + someTargetUrl, []), "http://" + someTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "?target=" + "http://" + someTargetUrl + "?yet-another=parameter", []), "http://" + someTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "?target=" + "http%3A%2F%2F" + someTargetUrlEncoded + "&yet-another=parameter", []), "http://" + someTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "?target=" + "http%3A%2F%2F" + someTargetUrlEncoded + ";yet-another=parameter", []), "http://" + someTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "?target=" + "http%3A%2F%2F" + someTargetUrlEncoded + "#yet-another-fragment", []), "http://" + someTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "?target=" + "http%253A%252F%252F" + someTargetUrlDoubleEncoded, []), "http://" + someTargetUrl);
assert.end();
});
test("URL in query parameter - http URLs url-encoded and not - lowercase", function(assert) {
assert.equal(url.getRedirectTarget(("http://" + "www.some.website.com" + "/" + "?target=" + "http://" + someTargetUrl).toLowerCase(), []), "http://" + someTargetUrl);
assert.equal(url.getRedirectTarget(("http://" + "www.some.website.com" + "/" + "?target=" + "http://" + someTargetUrl + "?yet-another=parameter").toLowerCase(), []), "http://" + someTargetUrl);
assert.equal(url.getRedirectTarget(("http://" + "www.some.website.com" + "/" + "?target=" + "http%3A%2F%2F" + someTargetUrlEncoded + "&yet-another=parameter").toLowerCase(), []), "http://" + someTargetUrl);
assert.equal(url.getRedirectTarget(("http://" + "www.some.website.com" + "/" + "?target=" + "http%3A%2F%2F" + someTargetUrlEncoded + ";yet-another=parameter").toLowerCase(), []), "http://" + someTargetUrl);
assert.equal(url.getRedirectTarget(("http://" + "www.some.website.com" + "/" + "?target=" + "http%3A%2F%2F" + someTargetUrlEncoded + "#yet-another-fragment").toLowerCase(), []), "http://" + someTargetUrl);
assert.equal(url.getRedirectTarget(("http://" + "www.some.website.com" + "/" + "?target=" + "http%253A%252F%252F" + someTargetUrlDoubleEncoded).toLowerCase(), []), "http://" + someTargetUrl);
assert.end();
});
test("URL in query parameter - http URLs with some parts being url-encoded and others not", function(assert) {
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "?target=" + "http://" + someTargetUrlEncoded, []), "http://" + someTargetUrl);
assert.end();
});
test("URL in query parameter - www URLs work without http", function(assert) {
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "?target=" + wwwTargetUrl, []), "http://" + wwwTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "?target=" + wwwTargetUrl + "?yet-another=parameter", []), "http://" + wwwTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "?target=" + wwwTargetUrlEncoded + "&yet-another=parameter", []), "http://" + wwwTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "?target=" + wwwTargetUrlEncoded + ";yet-another=parameter", []), "http://" + wwwTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "?target=" + wwwTargetUrlEncoded + "#yet-another-fragment", []), "http://" + wwwTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "?target=" + wwwTargetUrlDoubleEncoded, []), "http://" + wwwTargetUrl);
assert.end();
});
test("recursion", function(assert) {
const wwwTargetUrlOne = wwwTargetUrl + "-ONE";
const wwwTargetUrlTwo = wwwTargetUrl + "-TWO";
assert.equal(url.getRedirectTarget("http://www.some.website.com/" + wwwTargetUrlOne + "/" + wwwTargetUrlTwo, []), "http://" + wwwTargetUrlTwo);
assert.equal(url.getRedirectTarget("http://www.some.website.com/" + "http://" + wwwTargetUrlOne + "/" + "http://" + wwwTargetUrlTwo, []), "http://" + wwwTargetUrlTwo);
assert.equal(url.getRedirectTarget("http://www.some.website.com/" + "?target=" + wwwTargetUrlOne + "&target=" + wwwTargetUrlTwo, []), "http://" + wwwTargetUrlTwo);
assert.equal(url.getRedirectTarget("http://www.some.website.com/" + "?target=" + "http://" + wwwTargetUrlOne + "&target=" + "http://" + wwwTargetUrlTwo, []), "http://" + wwwTargetUrlTwo);
assert.equal(url.getRedirectTarget("http://www.some.website.com/" + "?target=" + encodeURIComponent("http://" + wwwTargetUrlOne + "&target=" + encodeURIComponent("http://" + wwwTargetUrlTwo)), []), "http://" + wwwTargetUrlTwo);
assert.end();
});
test("no skipping if URL matches one of given exceptions", function(assert) {
const noRedirectUrls = [
wwwTargetUrl,
"http://www.some.website.com/" + wwwTargetUrl.replace("www.", "www"),
"http://www.some.website.com/" + "login?continue=" + wwwTargetUrl + "#some-fragment",
"http://www.some.website.com/" + "login?continue=" + wwwTargetUrlEncoded + "#some-fragment",
"http://www.some.website.com/" + "login?continue=" + wwwTargetUrlDoubleEncoded + "#some-fragment]",
"http://www.some.website.com/" + "Login?continue=" + wwwTargetUrlDoubleEncoded + "#some-fragment]",
];
for (let urlString of noRedirectUrls) {
assert.equal(url.getRedirectTarget(urlString, ["/login"]), urlString);
}
assert.end();
});
test("base64 encoded URLs - no valid URL", function(assert) {
const noUrlBase64Url = "http://" + "www.some.website.com" + "/" + base64.encode("wwwwwwww");
assert.equal(url.getRedirectTarget(noUrlBase64Url, []), noUrlBase64Url);
assert.end();
});
test("base64 encoded URLs - in path", function(assert) {
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + base64.encode(wwwTargetUrl), []), "http://" + wwwTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + base64.encode(wwwTargetUrl + "\n" + someTargetUrl), []), "http://" + wwwTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + base64.encode("http://" + someTargetUrl), []), "http://" + someTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + base64.encode("http://" + someTargetUrl) + "#some-fragment", []), "http://" + someTargetUrl);
assert.end();
});
test("base64 encoded URLs - in path with junk in front of it", function(assert) {
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "973abcCDE." + base64.encode(wwwTargetUrl), []), "http://" + wwwTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "973abcCDE." + base64.encode(wwwTargetUrl + "\n" + someTargetUrl), []), "http://" + wwwTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "973abcCDE." + base64.encode("http://" + someTargetUrl), []), "http://" + someTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "973abcCDE." + base64.encode("http://" + someTargetUrl) + "#some-fragment", []), "http://" + someTargetUrl);
assert.end();
});
test("base64 encoded URLs - in query string value", function(assert) {
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "?target=" + base64.encode(wwwTargetUrl), []), "http://" + wwwTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "?target=" + base64.encode(wwwTargetUrl + "\n" + someTargetUrl), []), "http://" + wwwTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "?target=" + base64.encode("http://" + someTargetUrl), []), "http://" + someTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "?target=" + base64.encode("http://" + someTargetUrl, []) + "#some-fragment", []), "http://" + someTargetUrl);
assert.end();
});