-
Notifications
You must be signed in to change notification settings - Fork 45
/
test-url.js
230 lines (195 loc) · 19.6 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
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
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) {
const urlExceptions = [];
const parameterExceptions = [];
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "http://" + someTargetUrl, urlExceptions, parameterExceptions), "http://" + someTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "http%3A%2F%2F" + someTargetUrlEncoded, urlExceptions, parameterExceptions), "http://" + someTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "http%3A%2F%2F" + someTargetUrlEncoded + "&unwanted", urlExceptions, parameterExceptions), "http://" + someTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "http%253A%252F%252F" + someTargetUrlDoubleEncoded, urlExceptions, parameterExceptions), "http://" + someTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "http%253A%252F%252F" + someTargetUrlDoubleEncoded + "&unwanted", urlExceptions, parameterExceptions), "http://" + someTargetUrl);
assert.end();
});
test("URL in query path - http URLs url-encoded and not - lowercase encoding (e.g. %2f instead of %2F)", function(assert) {
const urlExceptions = [];
const parameterExceptions = [];
assert.equal(url.getRedirectTarget(("http://" + "www.some.website.com" + "/" + "http://" + someTargetUrl).toLowerCase(), urlExceptions, parameterExceptions), "http://" + someTargetUrl);
assert.equal(url.getRedirectTarget(("http://" + "www.some.website.com" + "/" + "http%3A%2F%2F" + someTargetUrlEncoded).toLowerCase(), urlExceptions, parameterExceptions), "http://" + someTargetUrl);
assert.equal(url.getRedirectTarget(("http://" + "www.some.website.com" + "/" + "http%253A%252F%252F" + someTargetUrlDoubleEncoded).toLowerCase(), urlExceptions, parameterExceptions), "http://" + someTargetUrl);
assert.end();
});
test("URL in query path - works with initial /? and ? instead of slash", function(assert) {
const urlExceptions = [];
const parameterExceptions = [];
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/?" + "http://" + someTargetUrl, urlExceptions, parameterExceptions), "http://" + someTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "?" + "http://" + someTargetUrl, urlExceptions, parameterExceptions), "http://" + someTargetUrl);
assert.end();
});
test("URL in query path - correct http handling", function(assert) {
const urlExceptions = [];
const parameterExceptions = [];
assert.equal(url.getRedirectTarget("https://" + "www.some.website.com" + "/" + "http://" + someTargetUrl, urlExceptions, parameterExceptions), "http://" + someTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "https://" + someTargetUrl, urlExceptions, parameterExceptions), "https://" + someTargetUrl);
assert.end();
});
test("URL in query path - http URLs with some parts being url-encoded and others not", function(assert) {
const urlExceptions = [];
const parameterExceptions = [];
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "http://" + someTargetUrlEncoded, urlExceptions, parameterExceptions), "http://" + someTargetUrl);
assert.end();
});
test("URL in query path - www URLs work without http", function(assert) {
const urlExceptions = [];
const parameterExceptions = [];
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + wwwTargetUrl, urlExceptions, parameterExceptions), "http://" + wwwTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + wwwTargetUrlDoubleEncoded, urlExceptions, parameterExceptions), "http://" + wwwTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + wwwTargetUrlEncoded, urlExceptions, parameterExceptions), "http://" + wwwTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "https://" + wwwTargetUrl, urlExceptions, parameterExceptions), "https://" + wwwTargetUrl);
assert.end();
});
test("URL in query parameter - http URLs url-encoded and not", function(assert) {
const urlExceptions = [];
const parameterExceptions = [];
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "?target=" + "http://" + someTargetUrl, urlExceptions, parameterExceptions), "http://" + someTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "?target=" + "http://" + someTargetUrl + "?yet-another=parameter", urlExceptions, parameterExceptions), "http://" + someTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "?target=" + "http%3A%2F%2F" + someTargetUrlEncoded + "&yet-another=parameter", urlExceptions, parameterExceptions), "http://" + someTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "?target=" + "http%3A%2F%2F" + someTargetUrlEncoded + ";yet-another=parameter", urlExceptions, parameterExceptions), "http://" + someTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "?target=" + "http%3A%2F%2F" + someTargetUrlEncoded + "#yet-another-fragment", urlExceptions, parameterExceptions), "http://" + someTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "?target=" + "http%253A%252F%252F" + someTargetUrlDoubleEncoded, urlExceptions, parameterExceptions), "http://" + someTargetUrl);
assert.end();
});
test("URL in query parameter - http URLs url-encoded and not - lowercase encoding (e.g. %2f instead of %2F)", function(assert) {
const urlExceptions = [];
const parameterExceptions = [];
assert.equal(url.getRedirectTarget(("http://" + "www.some.website.com" + "/" + "?target=" + "http://" + someTargetUrl).toLowerCase(), urlExceptions, parameterExceptions), "http://" + someTargetUrl);
assert.equal(url.getRedirectTarget(("http://" + "www.some.website.com" + "/" + "?target=" + "http://" + someTargetUrl + "?yet-another=parameter").toLowerCase(), urlExceptions, parameterExceptions), "http://" + someTargetUrl);
assert.equal(url.getRedirectTarget(("http://" + "www.some.website.com" + "/" + "?target=" + "http%3A%2F%2F" + someTargetUrlEncoded + "&yet-another=parameter").toLowerCase(), urlExceptions, parameterExceptions), "http://" + someTargetUrl);
assert.equal(url.getRedirectTarget(("http://" + "www.some.website.com" + "/" + "?target=" + "http%3A%2F%2F" + someTargetUrlEncoded + ";yet-another=parameter").toLowerCase(), urlExceptions, parameterExceptions), "http://" + someTargetUrl);
assert.equal(url.getRedirectTarget(("http://" + "www.some.website.com" + "/" + "?target=" + "http%3A%2F%2F" + someTargetUrlEncoded + "#yet-another-fragment").toLowerCase(), urlExceptions, parameterExceptions), "http://" + someTargetUrl);
assert.equal(url.getRedirectTarget(("http://" + "www.some.website.com" + "/" + "?target=" + "http%253A%252F%252F" + someTargetUrlDoubleEncoded).toLowerCase(), urlExceptions, parameterExceptions), "http://" + someTargetUrl);
assert.end();
});
test("Protocol with colon in query parameter is ignored", function(assert) {
const urlExceptions = [];
const parameterExceptions = [];
const noRedirectUrls = [
"http://" + "www.some.website.com" + "/" + "?target=" + "http",
"http://" + "www.some.website.com" + "/" + "?target=" + "http:",
"http://" + "www.some.website.com" + "/" + "?target=" + "https",
"http://" + "www.some.website.com" + "/" + "?target=" + "https:",
];
for (const urlString of noRedirectUrls) {
assert.equal(url.getRedirectTarget(urlString, urlExceptions, parameterExceptions), urlString);
}
assert.end();
});
test("URL in query parameter - http URLs with some parts being url-encoded and others not", function(assert) {
const urlExceptions = [];
const parameterExceptions = [];
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "?target=" + "http://" + someTargetUrlEncoded, urlExceptions, parameterExceptions), "http://" + someTargetUrl);
assert.end();
});
test("URL in query parameter - www URLs work without http", function(assert) {
const urlExceptions = [];
const parameterExceptions = [];
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "?target=" + wwwTargetUrl, urlExceptions, parameterExceptions), "http://" + wwwTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "?target=" + wwwTargetUrl + "?yet-another=parameter", urlExceptions, parameterExceptions), "http://" + wwwTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "?target=" + wwwTargetUrlEncoded + "&yet-another=parameter", urlExceptions, parameterExceptions), "http://" + wwwTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "?target=" + wwwTargetUrlEncoded + ";yet-another=parameter", urlExceptions, parameterExceptions), "http://" + wwwTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "?target=" + wwwTargetUrlEncoded + "#yet-another-fragment", urlExceptions, parameterExceptions), "http://" + wwwTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "?target=" + wwwTargetUrlDoubleEncoded, urlExceptions, parameterExceptions), "http://" + wwwTargetUrl);
assert.end();
});
test("recursion", function(assert) {
const urlExceptions = [];
const parameterExceptions = [];
const wwwTargetUrlOne = wwwTargetUrl + "-ONE";
const wwwTargetUrlTwo = wwwTargetUrl + "-TWO";
assert.equal(url.getRedirectTarget("http://www.some.website.com/" + wwwTargetUrlOne + "/" + wwwTargetUrlTwo, urlExceptions, parameterExceptions), "http://" + wwwTargetUrlTwo);
assert.equal(url.getRedirectTarget("http://www.some.website.com/" + "http://" + wwwTargetUrlOne + "/" + "http://" + wwwTargetUrlTwo, urlExceptions, parameterExceptions), "http://" + wwwTargetUrlTwo);
assert.equal(url.getRedirectTarget("http://www.some.website.com/" + "?target=" + wwwTargetUrlOne + "&target=" + wwwTargetUrlTwo, urlExceptions, parameterExceptions), "http://" + wwwTargetUrlTwo);
assert.equal(url.getRedirectTarget("http://www.some.website.com/" + "?target=" + "http://" + wwwTargetUrlOne + "&target=" + "http://" + wwwTargetUrlTwo, urlExceptions, parameterExceptions), "http://" + wwwTargetUrlTwo);
assert.equal(url.getRedirectTarget("http://www.some.website.com/" + "?target=" + encodeURIComponent("http://" + wwwTargetUrlOne + "&target=" + encodeURIComponent("http://" + wwwTargetUrlTwo)), urlExceptions, parameterExceptions), "http://" + wwwTargetUrlTwo);
assert.end();
});
test("URL in query parameter - multiple parameters", function(assert) {
const urlExceptions = [];
const someTargetUrlOne = "http://" + someTargetUrl + "-ONE";
const someTargetUrlTwo = "http://" + someTargetUrl + "-TWO";
const someTargetUrlOneEncoded = encodeURIComponent(someTargetUrlOne);
const someTargetUrlTwoEncoded = encodeURIComponent(someTargetUrlTwo);
assert.equal(url.getRedirectTarget(("http://www.some.website.com/" + "?first=" + someTargetUrlOneEncoded + "&second=" + someTargetUrlTwoEncoded), urlExceptions, ["first"]), someTargetUrlTwo);
assert.equal(url.getRedirectTarget(("http://www.some.website.com/" + "?first=" + someTargetUrlOneEncoded + "&second=" + someTargetUrlTwoEncoded), urlExceptions, ["First"]), someTargetUrlTwo);
assert.equal(url.getRedirectTarget(("http://www.some.website.com/" + "?first=" + someTargetUrlOneEncoded + "&second=" + someTargetUrlTwoEncoded), urlExceptions, ["second"]), someTargetUrlOne);
assert.equal(url.getRedirectTarget(("http://www.some.website.com/" + "?first=" + someTargetUrlOneEncoded + "&second=" + someTargetUrlTwoEncoded), urlExceptions, ["Second"]), someTargetUrlOne);
const original = "http://www.some.website.com/" + "?first=" + someTargetUrlOneEncoded + "&second=" + someTargetUrlTwoEncoded;
assert.equal(url.getRedirectTarget(original, urlExceptions, ["first", "second"]), original);
assert.end();
});
test("no skipping if URL matches one of given exceptions", function(assert) {
const urlExceptions = ["/login"];
const parameterExceptions = [];
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 (const urlString of noRedirectUrls) {
assert.equal(url.getRedirectTarget(urlString, urlExceptions, parameterExceptions), urlString);
}
assert.end();
});
test("base64 encoded URLs - no valid URL", function(assert) {
const urlExceptions = [];
const parameterExceptions = [];
const noUrlBase64Url = "http://" + "www.some.website.com" + "/" + base64.encode("wwwwwwww");
assert.equal(url.getRedirectTarget(noUrlBase64Url, urlExceptions, parameterExceptions), noUrlBase64Url);
assert.end();
});
test("base64 encoded URLs - in path", function(assert) {
const urlExceptions = [];
const parameterExceptions = [];
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + base64.encode(wwwTargetUrl), urlExceptions, parameterExceptions), "http://" + wwwTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + base64.encode(wwwTargetUrl + "\n" + someTargetUrl), urlExceptions, parameterExceptions), "http://" + wwwTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + base64.encode("http://" + someTargetUrl), urlExceptions, parameterExceptions), "http://" + someTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + base64.encode("http://" + someTargetUrl) + "#some-fragment", urlExceptions, parameterExceptions), "http://" + someTargetUrl);
assert.end();
});
test("base64 encoded URLs - in anchor", function(assert) {
const urlExceptions = [];
const parameterExceptions = [];
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/#" + base64.encode(wwwTargetUrl), urlExceptions, parameterExceptions), "http://" + wwwTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/#" + base64.encode(wwwTargetUrl + "\n" + someTargetUrl), urlExceptions, parameterExceptions), "http://" + wwwTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/#" + base64.encode("http://" + someTargetUrl), urlExceptions, parameterExceptions), "http://" + someTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/#" + base64.encode("http://" + someTargetUrl) + "#some-fragment", urlExceptions, parameterExceptions), "http://" + someTargetUrl);
assert.end();
});
test("base64 encoded URLs - in path with junk in front of it", function(assert) {
const urlExceptions = [];
const parameterExceptions = [];
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "973abcCDE." + base64.encode(wwwTargetUrl), urlExceptions, parameterExceptions), "http://" + wwwTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "973abcCDE." + base64.encode(wwwTargetUrl + "\n" + someTargetUrl), urlExceptions, parameterExceptions), "http://" + wwwTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "973abcCDE." + base64.encode("http://" + someTargetUrl), urlExceptions, parameterExceptions), "http://" + someTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "973abcCDE." + base64.encode("http://" + someTargetUrl) + "#some-fragment", urlExceptions, parameterExceptions), "http://" + someTargetUrl);
assert.end();
});
test("base64 encoded URLs - in query string value", function(assert) {
const urlExceptions = [];
const parameterExceptions = [] ;
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "?target=" + base64.encode(wwwTargetUrl), urlExceptions, parameterExceptions), "http://" + wwwTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "?target=" + base64.encode(wwwTargetUrl + "\n" + someTargetUrl), urlExceptions, parameterExceptions), "http://" + wwwTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "?target=" + base64.encode("http://" + someTargetUrl), urlExceptions, parameterExceptions), "http://" + someTargetUrl);
assert.equal(url.getRedirectTarget("http://" + "www.some.website.com" + "/" + "?target=" + base64.encode("http://" + someTargetUrl, urlExceptions, parameterExceptions) + "#some-fragment", urlExceptions, parameterExceptions), "http://" + someTargetUrl);
assert.end();
});