-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathdomain-filter.ts
More file actions
161 lines (152 loc) · 5.05 KB
/
domain-filter.ts
File metadata and controls
161 lines (152 loc) · 5.05 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
import type { BrowserContext, Route } from 'playwright-core';
/**
* Checks whether a hostname matches one of the allowed domain patterns.
* Patterns support exact match ("example.com") and wildcard prefix ("*.example.com").
*/
export function isDomainAllowed(hostname: string, allowedDomains: string[]): boolean {
for (const pattern of allowedDomains) {
if (pattern.startsWith('*.')) {
const suffix = pattern.slice(1); // ".example.com"
if (hostname === pattern.slice(2) || hostname.endsWith(suffix)) {
return true;
}
} else if (hostname === pattern) {
return true;
}
}
return false;
}
export function parseDomainList(raw: string): string[] {
return raw
.split(',')
.map((d) => d.trim().toLowerCase())
.filter((d) => d.length > 0);
}
/**
* Build the init script source that monkey-patches WebSocket, EventSource,
* and navigator.sendBeacon to block connections to non-allowed domains.
* Exported for testing.
*/
export function buildWebSocketFilterScript(allowedDomains: string[]): string {
const serialized = JSON.stringify(allowedDomains);
return `(function() {
var _allowedDomains = ${serialized};
function _isDomainAllowed(hostname) {
hostname = hostname.toLowerCase();
for (var i = 0; i < _allowedDomains.length; i++) {
var pattern = _allowedDomains[i];
if (pattern.indexOf('*.') === 0) {
var suffix = pattern.slice(1);
if (hostname === pattern.slice(2) || hostname.slice(-suffix.length) === suffix) {
return true;
}
} else if (hostname === pattern) {
return true;
}
}
return false;
}
function _checkUrl(url) {
try {
// Handle relative URLs (e.g., /__webpack_hmr) by resolving against page origin
var fullUrl = url;
if (url.startsWith('/') || url.startsWith('.')) {
fullUrl = location.origin + url;
}
var parsed = new URL(fullUrl);
return parsed.hostname ? _isDomainAllowed(parsed.hostname) : true;
} catch(e) {
return false;
}
}
if (typeof WebSocket !== 'undefined') {
var _OrigWS = WebSocket;
WebSocket = function(url, protocols) {
if (!_checkUrl(url)) {
throw new DOMException(
'WebSocket connection to ' + url + ' blocked by domain allowlist',
'SecurityError'
);
}
if (protocols !== undefined) {
return new _OrigWS(url, protocols);
}
return new _OrigWS(url);
};
WebSocket.prototype = _OrigWS.prototype;
WebSocket.CONNECTING = _OrigWS.CONNECTING;
WebSocket.OPEN = _OrigWS.OPEN;
WebSocket.CLOSING = _OrigWS.CLOSING;
WebSocket.CLOSED = _OrigWS.CLOSED;
}
if (typeof EventSource !== 'undefined') {
var _OrigES = EventSource;
EventSource = function(url, opts) {
if (!_checkUrl(url)) {
throw new DOMException(
'EventSource connection to ' + url + ' blocked by domain allowlist',
'SecurityError'
);
}
return new _OrigES(url, opts);
};
EventSource.prototype = _OrigES.prototype;
EventSource.CONNECTING = _OrigES.CONNECTING;
EventSource.OPEN = _OrigES.OPEN;
EventSource.CLOSED = _OrigES.CLOSED;
}
if (typeof navigator !== 'undefined' && typeof navigator.sendBeacon === 'function') {
var _origSendBeacon = navigator.sendBeacon.bind(navigator);
navigator.sendBeacon = function(url, data) {
if (!_checkUrl(url)) {
return false;
}
return _origSendBeacon(url, data);
};
}
})();`;
}
/**
* Installs a context-level route that enforces the domain allowlist.
* Both document navigations and sub-resource requests (scripts, images, fetch, etc.)
* to non-allowed domains are blocked, preventing data exfiltration.
* Non-http(s) schemes (data:, blob:, etc.) are allowed for sub-resources
* but blocked for document navigations.
*
* Also installs an init script that patches WebSocket, EventSource, and
* navigator.sendBeacon to block connections to non-allowed domains. This is
* a best-effort defense: if eval is permitted by action policy, page scripts
* could theoretically restore the originals. Denying the eval action
* category closes that loophole.
*/
export async function installDomainFilter(
context: BrowserContext,
allowedDomains: string[]
): Promise<void> {
if (allowedDomains.length === 0) return;
await context.addInitScript(buildWebSocketFilterScript(allowedDomains));
await context.route('**/*', async (route: Route) => {
const request = route.request();
const urlStr = request.url();
if (!urlStr.startsWith('http://') && !urlStr.startsWith('https://')) {
if (request.resourceType() === 'document') {
await route.abort('blockedbyclient');
} else {
await route.continue();
}
return;
}
let hostname: string;
try {
hostname = new URL(urlStr).hostname.toLowerCase();
} catch {
await route.abort('blockedbyclient');
return;
}
if (isDomainAllowed(hostname, allowedDomains)) {
await route.continue();
} else {
await route.abort('blockedbyclient');
}
});
}