forked from btsouth/ceiling
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.test.mjs
More file actions
181 lines (158 loc) · 7.12 KB
/
Copy pathworker.test.mjs
File metadata and controls
181 lines (158 loc) · 7.12 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
import assert from "node:assert/strict";
import test from "node:test";
import worker, {
aggregateReleases,
anonymousVisitorId,
hasAdminSession,
safePath,
sessionCookie,
} from "./worker.mjs";
test("aggregateReleases counts executable downloads without checksum assets", () => {
const result = aggregateReleases([
{
tag_name: "v0.43.2",
name: "Ceiling 0.43.2",
published_at: "2026-07-13T00:00:00Z",
assets: [
{ name: "Ceiling-0.43.2-Setup.exe", download_count: 14 },
{ name: "Ceiling-0.43.2-portable.exe", download_count: 6 },
{ name: "Ceiling-0.43.2-Setup.exe.sha256", download_count: 80 },
],
},
]);
assert.equal(result.total, 20);
assert.equal(result.installer, 14);
assert.equal(result.portable, 6);
assert.equal(result.latest, 20);
assert.equal(result.releases[0].assets.length, 2);
});
test("safePath accepts local paths and rejects untrusted values", () => {
assert.equal(safePath("/download?from=hero"), "/download?from=hero");
assert.equal(safePath("https://example.com"), "/");
assert.equal(safePath(null), "/");
});
test("visitor identifiers are pseudonymous and rotate every 30 days", async () => {
const request = new Request("https://ceiling.win", {
headers: { "CF-Connecting-IP": "192.0.2.10", "User-Agent": "Ceiling test" },
});
const first = await anonymousVisitorId(request, "test-salt", 0);
const samePeriod = await anonymousVisitorId(request, "test-salt", 29 * 24 * 60 * 60 * 1000);
const nextPeriod = await anonymousVisitorId(request, "test-salt", 30 * 24 * 60 * 60 * 1000);
assert.equal(first, samePeriod);
assert.notEqual(first, nextPeriod);
});
test("admin session signatures enforce their server-side expiry", async () => {
const issuedAt = 1_000_000;
const setCookie = await sessionCookie("test-admin-token", issuedAt);
const cookie = setCookie.split(";")[0];
const request = new Request("https://ceiling.win/admin", { headers: { Cookie: cookie } });
const expiresAt = Number(cookie.split("=")[1].split(".")[0]);
assert.equal(await hasAdminSession(request, { ADMIN_TOKEN: "test-admin-token" }, expiresAt - 1), true);
assert.equal(await hasAdminSession(request, { ADMIN_TOKEN: "test-admin-token" }, expiresAt), false);
assert.equal(await hasAdminSession(request, { ADMIN_TOKEN: "different-token" }, expiresAt - 1), false);
});
test("admin routes fail closed and accept a valid token", async () => {
const env = {
ADMIN_TOKEN: "test-admin-token",
ASSETS: {
fetch: async () => new Response("admin dashboard", { headers: { "Content-Type": "text/html" } }),
},
};
const ctx = { waitUntil() {} };
const login = await worker.fetch(new Request("https://ceiling.win/admin"), env, ctx);
assert.equal(login.status, 401);
assert.match(await login.text(), /Ceiling analytics/);
const wrong = await worker.fetch(new Request("https://ceiling.win/admin/session", {
method: "POST",
headers: { "Content-Type": "application/json", Origin: "https://ceiling.win" },
body: JSON.stringify({ token: "wrong" }),
}), env, ctx);
assert.equal(wrong.status, 401);
const session = await worker.fetch(new Request("https://ceiling.win/admin/session", {
method: "POST",
headers: { "Content-Type": "application/json", Origin: "https://ceiling.win" },
body: JSON.stringify({ token: "test-admin-token" }),
}), env, ctx);
assert.equal(session.status, 200);
const setCookie = session.headers.get("Set-Cookie");
assert.match(setCookie, /HttpOnly/);
assert.match(setCookie, /Secure/);
assert.match(setCookie, /SameSite=Strict/);
const cookie = setCookie.split(";")[0];
const dashboard = await worker.fetch(new Request("https://ceiling.win/admin", {
headers: { Cookie: cookie },
}), env, ctx);
assert.equal(dashboard.status, 200);
assert.equal(dashboard.headers.get("Cache-Control"), "private, no-store");
assert.equal(await dashboard.text(), "admin dashboard");
});
test("admin routes reject cross-origin login and direct private assets", async () => {
const env = { ADMIN_TOKEN: "test-admin-token" };
const crossOrigin = await worker.fetch(new Request("https://ceiling.win/admin/session", {
method: "POST",
headers: { "Content-Type": "application/json", Origin: "https://example.com" },
body: JSON.stringify({ token: "test-admin-token" }),
}), env, { waitUntil() {} });
assert.equal(crossOrigin.status, 401);
const privateAsset = await worker.fetch(
new Request("https://ceiling.win/_private/admin-dashboard.txt"),
env,
{ waitUntil() {} },
);
assert.equal(privateAsset.status, 404);
});
test("star count endpoint returns a number and degrades to null on failure", async () => {
const store = new Map();
const originalCaches = globalThis.caches;
const originalFetch = globalThis.fetch;
globalThis.caches = {
default: {
match: async (request) => store.get(request.url),
put: async (request, response) => { store.set(request.url, response); },
},
};
const ctx = { waitUntil(promise) { return promise; } };
try {
globalThis.fetch = async () => new Response(JSON.stringify({ stargazers_count: 42 }), { status: 200 });
const ok = await worker.fetch(new Request("https://ceiling.win/api/stars"), {}, ctx);
assert.equal(ok.status, 200);
assert.deepEqual(await ok.json(), { stars: 42 });
store.clear();
globalThis.fetch = async () => { throw new Error("network down"); };
const degraded = await worker.fetch(new Request("https://ceiling.win/api/stars"), {}, ctx);
assert.deepEqual(await degraded.json(), { stars: null });
assert.equal(degraded.headers.get("Cache-Control"), "no-store");
const rejected = await worker.fetch(new Request("https://ceiling.win/api/stars", { method: "POST" }), {}, ctx);
assert.equal(rejected.status, 405);
} finally {
globalThis.fetch = originalFetch;
globalThis.caches = originalCaches;
}
});
test("event capture is a no-op when analytics is not configured", async () => {
const response = await worker.fetch(new Request("https://ceiling.win/api/events", {
method: "POST",
headers: { "Content-Type": "application/json", Origin: "https://ceiling.win" },
body: JSON.stringify({ event: "$pageview", pathname: "/" }),
}), {}, { waitUntil() {} });
assert.equal(response.status, 204);
});
test("public assets include baseline security headers", async () => {
const env = {
ASSETS: { fetch: async () => new Response("page", { headers: { "Content-Type": "text/html" } }) },
};
const response = await worker.fetch(new Request("https://ceiling.win/"), env, { waitUntil() {} });
assert.equal(response.status, 200);
assert.equal(response.headers.get("Referrer-Policy"), "strict-origin-when-cross-origin");
assert.equal(response.headers.get("X-Content-Type-Options"), "nosniff");
assert.match(response.headers.get("Permissions-Policy"), /camera=\(\)/);
});
test("www requests redirect to the canonical host", async () => {
const response = await worker.fetch(
new Request("https://www.ceiling.win/privacy/?from=test"),
{},
{ waitUntil() {} },
);
assert.equal(response.status, 301);
assert.equal(response.headers.get("Location"), "https://ceiling.win/privacy/?from=test");
});