-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck.ts
151 lines (130 loc) · 3.86 KB
/
check.ts
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
import { delay } from "https://deno.land/[email protected]/async/mod.ts";
import { Spinner } from "https://deno.land/[email protected]/cli/spinner.ts";
const localBaseUrl = "http://localhost:12345/";
const serverHealthCheck = async () => {
const loginURL = new URL("/login", localBaseUrl);
while (true) {
try {
const cookies = (await fetch(loginURL, { method: "post" })).headers
.getSetCookie();
if (cookies.length > 0) {
// Healthcheck OK!
return true;
}
} catch (_) {
// Try again
}
await delay(1000);
}
};
const startServer = async (checkDir: string) => {
const dockerUp = new Deno.Command("docker", {
args: ["compose", "up", "--build"],
cwd: checkDir,
});
// Wait until the server starts up...
const spinner = new Spinner({ message: "Starting server..." });
spinner.start();
const timeout = new Promise((resolve) => {
// Timeout = 10min
setTimeout(resolve, 600 * 1000, false);
});
const dockerKilled = async () => {
await dockerUp.output();
return false;
};
const success = await Promise.race([
serverHealthCheck(),
dockerKilled(),
timeout,
]);
spinner.stop();
if (!success) {
throw new Error("Server fails to start");
}
};
const stopServer = async (checkDir: string) => {
// Stopping server...
const dockerDown = new Deno.Command("docker", {
args: ["compose", "down"],
cwd: checkDir,
});
await dockerDown.output();
};
const check = async (checkDir: string) => {
console.log(`* Checking ${checkDir}`);
await startServer(checkDir);
const response = await checkUrl(localBaseUrl);
await stopServer(checkDir);
return response;
};
const checkUrl = async (baseURL: string) => {
const loginURL = new URL("/login", baseURL);
const logoURL = new URL("/logo", baseURL);
// Access logo as logged-in user
const cookies = (await fetch(loginURL, { method: "post" })).headers
.getSetCookie();
if (cookies.length === 0) {
throw new Error("No cookies found :(");
}
const fetchForAdmin = [...Array(5)].map((_) => {
return fetch(logoURL, { headers: { Cookie: cookies.join(";") } });
});
const resForAdmin = await Promise.all(fetchForAdmin);
const requestIds = resForAdmin.map((r) => r.headers.get("x-request-id"));
const cachedCookiesForAdmin = resForAdmin.map((r) =>
r.headers.getSetCookie()
);
await delay(100);
// Access logo as non-logged-in user
const fetchForAnonymous = [...Array(3)].map((_) => {
return fetch(logoURL);
});
const resForAnonymous = await Promise.all(fetchForAnonymous);
// Check if response is cached
let isCookieCached = false;
let isResponseCached = false;
resForAnonymous.forEach((res) => {
// Look if the server caches the cookie or not
const cachedCookies = res.headers.getSetCookie();
if (cachedCookies.length > 0) {
const sameCookie = cachedCookiesForAdmin.find((cookies) => {
return cookies.join(";") === cachedCookies.join(";");
});
if (sameCookie !== undefined) {
console.log("Cookie leaked:", sameCookie.join(";"));
isCookieCached = true;
}
}
// Look if the server caches the response or not
// Compare with x-request-id header
const requestId = res.headers.get("x-request-id");
if (requestId && requestIds.includes(requestId)) {
isResponseCached = true;
}
});
return { isCookieCached, isResponseCached };
};
const checkDefaultDirs = async () => {
const targets: { dir: string }[] = [
{ dir: "nginx" },
{ dir: "apache" },
{ dir: "haproxy" },
{ dir: "passenger" },
];
for (const target of targets) {
const result = await check(target.dir);
console.log(result);
}
};
const main = async () => {
const url = Deno.args[0];
if (url) {
const result = await checkUrl(url);
console.log(result);
} else {
await checkDefaultDirs();
}
Deno.exit();
};
main();