Repository navigation
Expand file tree
/
Copy pathRemoteModuleAllowlist.cpp
More file actions
65 lines (56 loc) · 1.92 KB
/
Copy pathRemoteModuleAllowlist.cpp
File metadata and controls
65 lines (56 loc) · 1.92 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
#include <string>
#include "ns/Platform.h"
#include "ns/http/ModuleFetch.h"
namespace nativescript::common::http {
namespace {
// Stricter than a string prefix: after the entry, `url` must end or continue
// at a URL-component boundary ('/', '?', '#'), unless the entry itself ends in
// '/'. An entry of "https://cdn.example.com" must not authorize
// "https://cdn.example.com.attacker.com/x.js" or
// "https://cdn.example.com:9999/x.js"; a port has to be listed to be allowed.
bool RemoteUrlMatchesAllowlistEntry(const std::string& url,
const std::string& entry) {
if (entry.empty() || url.size() < entry.size()) {
return false;
}
if (url.compare(0, entry.size(), entry) != 0) {
return false;
}
if (url.size() == entry.size() || entry.back() == '/') {
return true;
}
const char next = url[entry.size()];
return next == '/' || next == '?' || next == '#';
}
std::string NormalizeAllowlistEntry(const std::string& entry) {
const std::string url = platform::NormalizeHttpModuleUrl(entry);
return url.empty() ? entry : url;
}
} // namespace
RemoteModuleAllowlist RemoteModuleAllowlistFromConfig(
const std::optional<std::vector<std::optional<std::string>>>& items) {
RemoteModuleAllowlist allowlist;
if (!items.has_value() || items->empty()) {
return allowlist;
}
allowlist.inForce = true;
for (const std::optional<std::string>& item : *items) {
if (item.has_value() && !item->empty()) {
allowlist.entries.push_back(NormalizeAllowlistEntry(*item));
}
}
return allowlist;
}
bool RemoteUrlAllowedByAllowlist(const std::string& url,
const RemoteModuleAllowlist& allowlist) {
if (!allowlist.inForce) {
return true;
}
for (const std::string& entry : allowlist.entries) {
if (RemoteUrlMatchesAllowlistEntry(url, entry)) {
return true;
}
}
return false;
}
} // namespace nativescript::common::http