Skip to content

Commit 501741a

Browse files
allowlist
1 parent da42358 commit 501741a

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

resources/data-provider.mjs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import { defaultSuites } from "./tests.mjs";
22
import { params } from "./shared/params.mjs";
33

44
const DEFAULT_TAGS = ["all", "default", "experimental"];
5+
const ALLOWED_DOMAINS = {
6+
"app.netlify.com": "/sites/webkit-speedometer-preview/",
7+
};
58

69
// http://localhost:8080/?config=http://localhost:8080/resources/config.json
710

@@ -17,6 +20,45 @@ export class DataProvider{
1720
return this._suites;
1821
}
1922

23+
_containsAllowedUrl(suite) {
24+
// 1. Check for relative URL
25+
if (!suite.url.startsWith("http://") && !suite.url.startsWith("https://") && !suite.url.startsWith("//") && !suite.url.startsWith("./")) {
26+
const baseUrl = "http://www.example.com";
27+
try {
28+
const parsedUrl = new URL(suite.url, baseUrl);
29+
if (parsedUrl.origin === baseUrl)
30+
return true;
31+
} catch (error) {
32+
return false;
33+
}
34+
}
35+
36+
// 2. Check for localhost URL
37+
if (suite.url.startsWith("http://localhost:") || suite.url.startsWith("https://localhost:")) {
38+
try {
39+
const parsedUrl = new URL(suite.url);
40+
if (parsedUrl.hostname === "localhost")
41+
return true;
42+
43+
} catch (e) {
44+
// Invalid URL format for localhost
45+
}
46+
return false;
47+
}
48+
49+
// 3. Check for allowed domains
50+
try {
51+
const parsedUrl = new URL(suite.url);
52+
if (ALLOWED_DOMAINS[parsedUrl.hostname] && ALLOWED_DOMAINS[parsedUrl.hostname].includes(parsedUrl.pathname))
53+
return true;
54+
55+
} catch (e) {
56+
// invalid URL
57+
}
58+
59+
return false;
60+
}
61+
2062
_freezeSuites() {
2163
Object.freeze(this._suites);
2264
this._suites.forEach((suite) => {
@@ -43,7 +85,10 @@ export class DataProvider{
4385
const config = await response.json();
4486

4587
config.suites.flatMap((suite) => suite.tags).forEach(tag => this._tags.add(tag));
46-
config.suites.forEach(suite => this._suites.push(suite));
88+
config.suites.forEach(suite => {
89+
if (this._containsAllowedUrl(suite))
90+
this._suites.push(suite);
91+
});
4792
} else {
4893
defaultSuites.flatMap((suite) => suite.tags).forEach(tag => this._tags.add(tag));
4994
defaultSuites.forEach(suite => this._suites.push(suite));

0 commit comments

Comments
 (0)