Repository navigation
Expand file tree
/
Copy pathHttpPolicyTest.cpp
More file actions
223 lines (196 loc) · 8.56 KB
/
Copy pathHttpPolicyTest.cpp
File metadata and controls
223 lines (196 loc) · 8.56 KB
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
#include <cstdio>
#include <optional>
#include <string>
#include <vector>
#include "FakePlatform.h"
#include "http/Internal.h"
#include "ns/http/ModuleFetch.h"
using namespace nativescript::common::http;
namespace {
int failures = 0;
void Check(bool ok, const char* what, const std::string& detail) {
if (!ok) {
failures++;
std::fprintf(stderr, "FAIL %s: \"%s\"\n", what, detail.c_str());
}
}
void ExpectEqual(const std::string& actual, const std::string& expected,
const char* what) {
if (actual != expected) {
failures++;
std::fprintf(stderr, "FAIL %s:\n actual: \"%s\"\n expected: \"%s\"\n",
what, actual.c_str(), expected.c_str());
}
}
ModuleFetchResult Classify(bool arrived, int status,
const std::string& contentType, std::string body) {
ModuleFetchResult result;
ClassifyModuleResponse("http://h/m.js", arrived, status, contentType, body,
result);
return result;
}
void TestMime() {
ExpectEqual(MimeEssence("Text/JavaScript; charset=utf-8"), "text/javascript",
"essence lowercases and drops parameters");
ExpectEqual(MimeEssence(" \tapplication/json \t"), "application/json",
"essence trims");
ExpectEqual(MimeEssence(" ; charset=utf-8"), "", "essence of nothing");
Check(IsJavaScriptMimeEssence("text/javascript1.5"), "js essence",
"text/javascript1.5");
Check(!IsJavaScriptMimeEssence("text/typescript"), "not a js essence",
"text/typescript");
Check(IsJsonMimeEssence("application/manifest+json"), "+json is json",
"application/manifest+json");
Check(!IsJsonMimeEssence("+json"), "a bare suffix is not json", "+json");
}
void TestClassify() {
ModuleFetchResult r = Classify(false, 0, "", "");
Check(!r.ok, "transport failure fails", "");
ExpectEqual(r.failureReason,
"HTTP import failed: http://h/m.js (network error)",
"transport failure text");
r = Classify(true, 204, "text/javascript", "");
ExpectEqual(r.failureReason,
"HTTP import failed: http://h/m.js (status=204, no content)",
"204 text");
Check(r.status == 204, "status kept", "204");
r = Classify(true, 404, "text/html", "<html>");
ExpectEqual(r.failureReason, "HTTP import failed: http://h/m.js (status=404)",
"404 text");
r = Classify(true, 200, "", "x");
ExpectEqual(r.failureReason,
"Expected a JavaScript module but 'http://h/m.js' responded "
"with no MIME type",
"missing MIME text");
r = Classify(true, 200, "text/html; charset=utf-8", "<html>");
ExpectEqual(r.failureReason,
"Expected a JavaScript module but 'http://h/m.js' responded "
"with MIME type 'text/html'",
"wrong MIME text");
r = Classify(true, 200, "application/json", "");
ExpectEqual(r.failureReason,
"Expected a JSON module but 'http://h/m.js' responded with an "
"empty body",
"empty JSON text");
r = Classify(true, 200, "application/json", "{}");
Check(r.ok && r.kind == ModuleResponseKind::kJson && r.body == "{}",
"json module", r.failureReason);
fake_platform::ClearMessages();
r = Classify(true, 200, "text/javascript", "");
Check(r.ok && r.kind == ModuleResponseKind::kJavaScript, "empty js is ok",
r.failureReason);
ExpectEqual(r.body, "export {};\n", "empty js becomes the empty module");
Check(fake_platform::TracedMessages().size() == 1, "empty js is traced", "");
r = Classify(true, 299, "text/javascript; charset=utf-8", "export {}");
Check(r.ok && r.body == "export {}" && r.failureReason.empty(), "2xx js",
r.failureReason);
ExpectEqual(r.contentType, "text/javascript; charset=utf-8",
"content type kept with parameters");
}
void TestAllowlist() {
RemoteModuleAllowlist absent = RemoteModuleAllowlistFromConfig(std::nullopt);
Check(!absent.inForce, "absent list is not in force", "");
Check(RemoteUrlAllowedByAllowlist("https://any.example/x.js", absent),
"absent list allows all", "");
RemoteModuleAllowlist empty = RemoteModuleAllowlistFromConfig(
std::vector<std::optional<std::string>>{});
Check(!empty.inForce, "empty list is not in force", "");
RemoteModuleAllowlist unusable = RemoteModuleAllowlistFromConfig(
std::vector<std::optional<std::string>>{std::nullopt, std::string()});
Check(unusable.inForce && unusable.entries.empty(),
"unusable items keep the list in force", "");
Check(!RemoteUrlAllowedByAllowlist("https://cdn.example.com/x.js", unusable),
"unusable items match nothing", "");
RemoteModuleAllowlist list =
RemoteModuleAllowlistFromConfig(std::vector<std::optional<std::string>>{
std::string("HTTPS://CDN.example.com"),
std::string("https://assets.example.com/pkg/"),
std::string("not a url")});
Check(list.inForce && list.entries.size() == 3, "usable items kept", "");
ExpectEqual(list.entries[0], "https://cdn.example.com/",
"entry spelled like a fetched URL");
ExpectEqual(list.entries[2], "not a url", "non-URL entry kept verbatim");
const struct {
const char* url;
bool allowed;
} cases[] = {
{"https://cdn.example.com/x.js", true},
{"https://cdn.example.com.attacker.com/x.js", false},
{"https://cdn.example.com:9999/x.js", false},
{"https://assets.example.com/pkg/a/b.js", true},
{"https://assets.example.com/pkgx/a.js", false},
{"https://assets.example.com/other.js", false},
{"not a url", true},
{"not a urlx", false},
{"not a url?q", true},
};
for (const auto& c : cases) {
Check(RemoteUrlAllowedByAllowlist(c.url, list) == c.allowed,
c.allowed ? "allowlist allows" : "allowlist refuses", c.url);
}
}
void TestCanonicalize() {
CanonicalizationConfig vocabulary;
vocabulary.stripParams = {"t", "v"};
vocabulary.devPathPrefixes = {"/@ns/", "/src/"};
vocabulary.preserveQueryPrefixes = {"/@ns/sfc-meta"};
ExpectEqual(
CanonicalizeNormalizedHttpUrlKey("http://h/a.js?b=1#frag", nullptr),
"http://h/a.js?b=1", "no vocabulary keeps the query");
ExpectEqual(
CanonicalizeNormalizedHttpUrlKey("http://h/lib/a.js?t=1", &vocabulary),
"http://h/lib/a.js?t=1", "outside dev prefixes keeps the query");
ExpectEqual(CanonicalizeNormalizedHttpUrlKey("http://h/src/a.js?z=2&t=1&a=1",
&vocabulary),
"http://h/src/a.js?a=1&z=2", "dev endpoint strips and sorts");
ExpectEqual(CanonicalizeNormalizedHttpUrlKey("http://h/src/a.js?t=1&v=2",
&vocabulary),
"http://h/src/a.js", "dev endpoint with only busters");
ExpectEqual(
CanonicalizeNormalizedHttpUrlKey("http://h/src/a.js?&&", &vocabulary),
"http://h/src/a.js", "empty pairs dropped");
ExpectEqual(CanonicalizeNormalizedHttpUrlKey("http://h/@ns/sfc-meta/x?t=9",
&vocabulary),
"http://h/@ns/sfc-meta/x?t=9",
"preserved prefix wins over dev prefix");
ExpectEqual(CanonicalizeNormalizedHttpUrlKey("http://h", &vocabulary),
"http://h", "no path");
ExpectEqual(CanonicalizeNormalizedHttpUrlKey("weird?q#f", &vocabulary),
"weird", "no scheme drops the query");
}
void TestHostClass() {
const struct {
const char* url;
ModuleFetchHostClass hostClass;
} cases[] = {
{"http://localhost:5173/a.js", ModuleFetchHostClass::kLoopback},
{"http://LOCALHOST/a.js", ModuleFetchHostClass::kLoopback},
{"http://127.0.0.1:5173", ModuleFetchHostClass::kLoopback},
{"http://[::1]:5173/a.js", ModuleFetchHostClass::kLoopback},
{"http://10.0.2.2:5173/a.js", ModuleFetchHostClass::kLoopback},
{"http://10.0.3.2/a.js", ModuleFetchHostClass::kLoopback},
{"http://user:pw@localhost/a.js", ModuleFetchHostClass::kLoopback},
{"http://localhost.example.com/a.js", ModuleFetchHostClass::kRemote},
{"http://localhost@evil.example/a.js", ModuleFetchHostClass::kRemote},
{"https://cdn.example.com/a.js", ModuleFetchHostClass::kRemote},
{"no scheme", ModuleFetchHostClass::kRemote},
};
for (const auto& c : cases) {
Check(ModuleFetchHostClassOf(c.url) == c.hostClass,
ModuleFetchHostClassName(c.hostClass), c.url);
}
}
} // namespace
int main() {
TestMime();
TestClassify();
TestAllowlist();
TestCanonicalize();
TestHostClass();
if (failures > 0) {
std::fprintf(stderr, "%d failure(s)\n", failures);
return 1;
}
std::printf("all HTTP policy tests passed\n");
return 0;
}