Merge main and preserve email and log investigation styles

This commit is contained in:
Thomas Durieux committed 2026-11-14 17:24:13 +00:00
commit 35e69b3291
33 files changed
+1142 -69

No files matched your search

+157
View File
@@ -0,0 +1,157 @@
const { URL, URLSearchParams } = require("node:url");
const { expect } = require("chai");
const fs = require("node:fs");
const vm = require("node:vm");
const source = fs.readFileSync(require("node:path").join(__dirname, "../public/script/admin.js"), "utf8");
const script = source.slice(source.indexOf("export const emailsAdminController")).replace("export const", "const") + ";this.controller = emailsAdminController;";
const sandbox = { console, Date, Intl }; vm.runInNewContext(script, sandbox);
const tick = () => new Promise(resolve => require("node:timers").setImmediate(resolve));
describe("email admin UI requests", () => {
let state, pending, summaries, dispose;
beforeEach(() => {
pending = []; summaries = [];
state = { user: {}, on(event, callback) { if (event === "dispose") dispose = callback; } };
sandbox.controller(state, { get: (url, options) => new Promise((resolve, reject) => (url.endsWith("/summary") ? summaries : pending).push({ url, options, resolve, reject })) }, { url() {} });
});
const response = (entries, nextCursor = null) => ({ data: { entries, nextCursor } });
it("keeps visible history when a refresh fails", async () => {
pending[0].resolve(response([{ _id: "first" }])); await tick();
const refresh = state.loadEmails();
expect(state.emailRows).to.have.length(1);
pending[1].reject(Error("offline")); await refresh;
expect(state.emailRows[0]._id).to.equal("first"); expect(state.emailError).not.to.equal("");
});
it("retries the failed older page without discarding previous rows", async () => {
pending[0].resolve(response([{ _id: "first" }], "cursor")); await tick();
const more = state.loadEmails(true); pending[1].reject(Error("offline")); await more;
const retry = state.retryEmails();
expect(pending[2].options.params.before).to.equal("cursor");
pending[2].resolve(response([{ _id: "second" }])); await retry;
expect(state.emailRows.map(row => row._id)).to.deep.equal(["first", "second"]);
});
it("clears rows on filter changes and ignores stale responses", async () => {
pending[0].resolve(response([{ _id: "first" }])); await tick();
const old = state.loadEmails();
state.emailState = "rejected";
const current = state.loadEmails();
expect(state.emailRows).to.have.length(0);
pending[2].resolve(response([{ _id: "rejected" }])); await current;
pending[1].resolve(response([{ _id: "stale" }])); await old;
expect(state.emailRows[0]._id).to.equal("rejected");
});
it("exposes whether filters are active and clears them in one action", async () => {
pending[0].resolve(response([])); await tick();
state.emailKind = "owner-access"; expect(state.hasEmailFilters()).to.equal(true);
const clear = state.clearEmailFilters();
expect(state.hasEmailFilters()).to.equal(false);
pending[1].resolve(response([])); summaries[1].resolve({ data: { counts: {}, total: 0, series: [] } }); await clear;
});
it("loads bounded pages and fetches older history only at the loaded boundary", async () => {
pending[0].resolve(response(Array.from({ length: 50 }, (_, i) => ({ _id: String(i) })), "older")); await tick();
expect(state.emailVisibleRows()).to.have.length(25);
await state.emailNextPage(); expect(state.emailPage).to.equal(2); expect(pending).to.have.length(1);
const next = state.emailNextPage();
expect(pending[1].options.params.before).to.equal("older");
pending[1].resolve(response([{ _id: "50" }])); await next;
expect(state.emailPage).to.equal(3); expect(state.emailVisibleRows()[0]._id).to.equal("50");
state.emailPreviousPage(); expect(state.emailVisibleRows()).to.have.length(25);
});
it("does not turn unavailable summary data into zero counts", async () => {
summaries[0].reject(Error("offline")); await tick();
expect(state.emailTotal()).to.equal(null); expect(state.emailPageCount()).to.equal(null);
expect(state.emailSummaryError).not.to.equal("");
});
it("ignores stale summary responses and applies search only on submit", async () => {
state.emailSearch = " author@example.com ";
expect(pending[0].options.params.search).to.equal(undefined);
const current = state.applyEmailFilters();
expect(pending[1].options.params.search).to.equal("author@example.com");
expect(summaries[1].options.params.state).to.equal(undefined);
pending[1].resolve(response([])); summaries[1].resolve({ data: { counts: { rejected: 3 }, total: 3, series: [] } }); await current;
summaries[0].resolve({ data: { counts: {}, total: 99, series: [] } }); await tick();
expect(state.emailTotal()).to.equal(3);
const status = state.setEmailStatus("rejected"); expect(summaries).to.have.length(2);
pending[2].resolve(response([])); await status; expect(state.emailTotal()).to.equal(3);
});
it("ignores responses after disposal", async () => {
dispose(); pending[0].resolve(response([{ _id: "late" }]));
summaries[0].resolve({ data: { total: 99 } }); await tick();
expect(state.emailRows).to.have.length(0); expect(state.emailSummary).to.equal(null);
});
it("retains summary totals and the current page when requests fail", async () => {
pending[0].resolve(response(Array.from({ length: 25 }, (_, i) => ({ _id: String(i) })), "older"));
summaries[0].resolve({ data: { counts: { accepted: 30 }, total: 30, series: [] } }); await tick();
const next = state.emailNextPage(); pending[1].reject(Error("offline")); await next;
expect(state.emailPage).to.equal(1); expect(state.emailVisibleRows()).to.have.length(25);
const summary = state.loadEmailSummary(); summaries[1].reject(Error("offline")); await summary;
expect(state.emailTotal()).to.equal(30); expect(state.emailSummaryError).to.include("Previously loaded");
});
it("restores valid URL filters and preserves unrelated URL state", async () => {
let saved;
const local = { console, Date, Intl, URL, URLSearchParams, window: {
location: { search: "?state=rejected&period=30d&search=author%40example.com&other=keep", href: "http://localhost/admin/emails?state=rejected&period=30d&search=author%40example.com&other=keep#history" },
history: { replaceState(a, b, value) { saved = value; } },
} };
vm.runInNewContext(script, local);
const restored = { user: {}, on() {} };
local.controller(restored, { get: () => new Promise(() => {}) }, { url() {} });
expect(restored.emailState).to.equal("rejected"); expect(restored.emailPeriod).to.equal("30d");
expect(restored.emailSearch).to.equal("author@example.com");
expect(saved).to.include("other=keep"); expect(saved).to.include("#history");
});
it("advances to the intended page after retrying a failed Next", async () => {
pending[0].resolve(response(Array.from({ length: 25 }, (_, i) => ({ _id: String(i) })), "older")); await tick();
const next = state.emailNextPage(); pending[1].reject(Error("offline")); await next;
expect(state.emailPage).to.equal(1);
const retry = state.retryEmails(); pending[2].resolve(response([{ _id: "next" }])); await retry;
expect(state.emailPage).to.equal(2); expect(state.emailVisibleRows()[0]._id).to.equal("next");
expect(state.emailAnnouncement).to.equal("Page 2. Showing attempts 26 to 26.");
state.emailPreviousPage(); expect(state.emailAnnouncement).to.equal("Page 1. Showing attempts 1 to 25.");
await state.emailNextPage(); expect(state.emailAnnouncement).to.equal("Page 2. Showing attempts 26 to 26.");
});
it("resets a failed page target on filter changes", async () => {
pending[0].resolve(response(Array.from({ length: 25 }, (_, i) => ({ _id: String(i) })), "older")); await tick();
const next = state.emailNextPage(); pending[1].reject(Error("offline")); await next;
const filtered = state.setEmailStatus("rejected"); pending[2].resolve(response([{ _id: "filtered" }], "filteredOlder")); await filtered;
const more = state.loadEmails(true); pending[3].resolve(response(Array.from({ length: 25 }, (_, i) => ({ _id: "extra" + i })))); await more;
expect(state.emailPage).to.equal(1);
});
it("handles empty chart date endpoints", () => {
expect(state.emailDayLabel(undefined)).to.equal(""); expect(state.emailDayLabel({})).to.equal("");
expect(state.emailDayLabel({ date: "not-a-date" })).to.equal("");
expect(state.emailDayLabel({ date: "2026-11-14" })).not.to.equal("");
});
it("focuses and scrolls the history heading only on explicit paging", async () => {
let focused = 0, scrolled = 0;
const heading = { setAttribute(key, value) { expect([key, value]).to.deep.equal(["tabindex", "-1"]); }, focus(options) { expect(options.preventScroll).to.equal(true); focused++; }, scrollIntoView() { scrolled++; } };
const local = { console, Date, Intl, URL, URLSearchParams, window: {
location: { search: "", href: "http://localhost/admin/emails" }, history: { replaceState() {} },
document: { querySelector(selector) { return selector === "#email-history-title" ? heading : null; } },
} };
vm.runInNewContext(script, local);
const view = { user: {}, on() {} };
local.controller(view, { get: url => Promise.resolve(url.endsWith("/summary") ? { data: { counts: {}, total: 50, series: [] } } : response(Array.from({ length: 50 }, (_, i) => ({ _id: String(i) })))) }, { url() {} });
await tick(); expect(focused).to.equal(0);
await view.emailNextPage(); expect(focused).to.equal(1); expect(scrolled).to.equal(1);
await view.refreshEmails(); expect(focused).to.equal(1);
});
it("shows years for older dates and all-time history", () => {
const currentYear = new Date().getFullYear();
expect(state.emailShortTime(`${currentYear - 1}-06-01T12:00:00Z`)).to.include(String(currentYear - 1));
state.emailPeriod = "all";
expect(state.emailShortTime(`${currentYear}-06-01T12:00:00Z`)).to.include(String(currentYear));
});
it("explains outcomes without inventing delivery evidence", () => {
expect(state.emailExplanation({ state: "accepted" })).to.include("not confirmed");
expect(state.emailExplanation({ state: "rejected", httpStatus: 429 })).to.include("rate limiting");
expect(state.emailExplanation({ state: "unknown" })).to.include("avoid duplicates");
});
it("uses displayed chart activity independently of all-time totals", () => {
state.emailSummary = {total: 100, series: [{date: "2026-11-14", accepted: 0, rejected: 0, unknown: 0, sending: 0}]};
expect(state.emailChartTotal()).to.equal(0);
state.emailSummary.series[0].unknown = 3;
expect(state.emailChartTotal()).to.equal(3);
});
});
+167
View File
@@ -0,0 +1,167 @@
const { setImmediate } = require("node:timers");
const { expect } = require("chai");
require("ts-node/register/transpile-only");
const Users = require("../src/core/model/users/users.model").default;
const history = require("../src/core/email-history");
const admin = require("../src/server/routes/admin").default;
const { emailHistoryQuery, emailHistoryPeriod, emailHistorySummary } = require("../src/server/routes/admin-emails");
describe("email delivery history", () => {
const restores = [];
const stub = (obj, key, value) => { const old = obj[key]; restores.push(() => obj[key] = old); obj[key] = value; };
let rows, updates, failWrite, failCompletion;
const metadata = { kind: "repository-expiration", recipient: "owner@example.com", ownerId: "owner", resourceId: "repo", resourceKind: "repository" };
beforeEach(() => {
rows = []; updates = []; failWrite = false; failCompletion = false;
Object.defineProperty(Users.db, "readyState", { configurable: true, value: 1 });
restores.push(() => { delete Users.db.readyState; });
stub(Users.db, "collection", () => ({ createIndex: async () => {},
insertOne: async row => { if (failWrite) throw Error("database unavailable"); rows.push(row); },
updateOne: async (filter, update) => { if (failCompletion) throw Error("database unavailable"); updates.push({ filter, update }); },
}));
stub(global, "fetch", async () => ({ ok: true, status: 200 }));
});
afterEach(() => { while (restores.length) restores.pop()(); });
it("records recipient and outcome without retaining credentials or the message", async () => {
await history.sendTrackedEmail(metadata, "https://api.resend.com/emails", { headers: { Authorization: "secret" }, body: "private content" });
expect(rows).to.have.length(1);
expect(rows[0]).to.include({ recipient: metadata.recipient, state: "sending" });
expect(JSON.stringify(rows)).not.to.include("secret");
expect(JSON.stringify(rows)).not.to.include("private content");
expect(updates[0].filter._id).to.equal(rows[0]._id);
expect(updates[0].update.$set).to.include({ state: "accepted", httpStatus: 200 });
});
it("starts delivery without waiting for audit storage", async () => {
let release, started = false;
stub(Users.db, "collection", () => ({ insertOne: () => new Promise(resolve => { release = resolve; }), updateOne: async () => {} }));
stub(global, "fetch", async () => { started = true; return { ok: true, status: 200 }; });
const sending = history.sendTrackedEmail(metadata, "url", {});
expect(started).to.equal(true);
await new Promise(resolve => setImmediate(resolve)); release(); await sending;
});
it("records rejection separately from an unknown outcome", async () => {
stub(global, "fetch", async () => ({ ok: false, status: 429 }));
await history.sendTrackedEmail(metadata, "url", {});
expect(updates[0].update.$set).to.include({ state: "rejected", httpStatus: 429 });
stub(global, "fetch", async () => { throw Error("timeout"); });
try { await history.sendTrackedEmail(metadata, "url", {}); throw Error("expected failure"); }
catch (error) { expect(error.message).to.equal("timeout"); }
expect(updates[1].update.$set.state).to.equal("unknown");
});
it("keeps delivery independent of audit write failures", async () => {
failWrite = true;
expect((await history.sendTrackedEmail(metadata, "url", {})).ok).to.equal(true);
failWrite = false; failCompletion = true;
expect((await history.sendTrackedEmail(metadata, "url", {})).ok).to.equal(true);
});
it("validates filters and uses a stable cursor for equal timestamps", () => {
const date = "2026-01-01T00:00:00.000Z", id = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa";
expect(emailHistoryQuery({ state: "accepted", kind: "owner-access", before: date + "|" + id })).to.deep.equal({
state: "accepted", kind: "owner-access", $or: [{ createdAt: { $lt: new Date(date) } }, { createdAt: new Date(date), _id: { $lt: id } }],
});
for (const query of [{ state: { $ne: "" } }, { kind: "invalid" }, { before: "bad" }, { before: [] }]) expect(() => emailHistoryQuery(query)).to.throw();
});
it("returns bounded pages with a cursor and no browser caching", async () => {
const router = require("../src/server/routes/admin-emails").default;
const handler = router.stack.find(layer => layer.route?.path === "/").route.stack[0].handle;
const records = Array.from({ length: 51 }, (_, index) => ({ _id: String(index), createdAt: new Date("2026-01-01"), state: "accepted" }));
let limit, filter, sort, result, cache, options;
stub(history, "emailHistoryCollection", () => ({ find: (query, value) => {
filter = query; options = value;
return { sort: value => { sort = value; return { limit: count => { limit = count; return { toArray: async () => records }; } }; } };
} }));
await handler({ query: { state: "accepted" } }, { set: (key, value) => { cache = [key, value]; }, json: value => { result = value; } });
expect(options.maxTimeMS).to.equal(5000);
expect(limit).to.equal(51); expect(sort).to.deep.equal({ createdAt: -1, _id: -1 });
expect(filter).to.deep.equal({ state: "accepted" });
expect(result.entries).to.have.length(50);
expect(result.nextCursor).to.equal("2026-01-01T00:00:00.000Z|49");
expect(cache).to.deep.equal(["Cache-Control", "no-store"]);
});
it("returns an explicit error for malformed queries and database failures", async () => {
const router = require("../src/server/routes/admin-emails").default;
const handler = router.stack.find(layer => layer.route?.path === "/").route.stack[0].handle;
let code, body;
const res = { set() {}, status(value) { code = value; return this; }, json(value) { body = value; } };
await handler({ query: { before: "broken" } }, res);
expect(code).to.equal(400);
stub(history, "emailHistoryCollection", () => { throw Error("private database info"); });
await handler({ query: {} }, res);
expect(code).to.equal(503); expect(body.error).not.to.include("private");
});
it("uses UTC calendar periods and rejects malformed period or search input", () => {
const now = new Date("2026-11-14T23:59:00.000Z");
expect(emailHistoryPeriod("7d", now)).to.deep.equal({ period: "7d", chartDays: 7, start: new Date("2026-11-08T00:00:00.000Z") });
expect(emailHistoryQuery({ period: "30d" }, now).createdAt.$gte.toISOString()).to.equal("2026-10-16T00:00:00.000Z");
expect(emailHistoryQuery({ period: "all" }, now)).to.deep.equal({});
expect(emailHistoryQuery({}, now)).to.deep.equal({});
for (const query of [{ period: "1d" }, { period: [] }, { period: "" }, { search: [] }, { search: { $ne: "" } }, { search: "a".repeat(201) }]) {
expect(() => emailHistoryQuery(query)).to.throw();
}
});
it("searches literal recipient text and exact IDs without losing the cursor or period", () => {
const now = new Date("2026-11-14T12:00:00.000Z");
const before = "2026-11-12T12:00:00.000Z|aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa";
const query = emailHistoryQuery({ search: " a+b.[test]@example.com ", before, period: "7d" }, now);
const regex = new RegExp(query.$or[0].recipient.$regex, query.$or[0].recipient.$options);
expect(regex.test("A+B.[test]@example.com")).to.equal(true);
expect(regex.test("abxt@example.com")).to.equal(false);
expect(query.$or.slice(1)).to.deep.equal([{ _id: "a+b.[test]@example.com" }, { ownerId: "a+b.[test]@example.com" }, { resourceId: "a+b.[test]@example.com" }]);
expect(query.$and[0].$or).to.have.length(2);
expect(query.createdAt.$gte.toISOString()).to.equal("2026-11-08T00:00:00.000Z");
expect(emailHistoryQuery({ search: " " })).to.deep.equal({});
});
it("zero fills UTC daily series and keeps all-time totals independent of the chart", () => {
const period = emailHistoryPeriod("all", new Date("2026-11-14T12:00:00.000Z"));
const result = emailHistorySummary([{ _id: "accepted", count: 75 }, { _id: "unknown", count: 3 }], [
{ _id: { date: "2026-11-14", state: "accepted" }, count: 2 },
{ _id: { date: "2025-01-01", state: "accepted" }, count: 50 },
], period);
expect(result.total).to.equal(78);
expect(result.counts).to.deep.equal({ accepted: 75, rejected: 0, unknown: 3, sending: 0 });
expect(result.series).to.have.length(30);
expect(result.series[0]).to.deep.equal({ date: "2026-10-16", accepted: 0, rejected: 0, unknown: 0, sending: 0 });
expect(result.series.at(-1).accepted).to.equal(2);
expect(result.chartDays).to.equal(30);
expect(result.period).to.equal("all");
});
it("aggregates the whole filtered dataset independently of state and cursor with bounded database time", async () => {
const router = require("../src/server/routes/admin-emails").default;
const handler = router.stack.find(layer => layer.route?.path === "/summary").route.stack[0].handle;
let pipeline, options, result, cache;
stub(history, "emailHistoryCollection", () => ({ aggregate: (stages, opts) => {
pipeline = stages; options = opts;
return { toArray: async () => [{ counts: [{ _id: "accepted", count: 120 }, { _id: "rejected", count: 8 }], daily: [] }] };
} }));
await handler({ query: { state: "rejected", before: "ignored", period: "7d", kind: "owner-access", search: "research" } }, {
set: (key, value) => { cache = [key, value]; }, json: value => { result = value; },
});
expect(pipeline[0].$match).not.to.have.property("state");
expect(pipeline[0].$match).not.to.have.property("$and");
expect(pipeline[0].$match.kind).to.equal("owner-access");
expect(pipeline[0].$match.$or[0].recipient.$regex).to.equal("research");
expect(pipeline[1].$facet.daily[1].$group._id.date.$dateToString.timezone).to.equal("UTC");
expect(options.maxTimeMS).to.equal(5000);
expect(result.total).to.equal(128);
expect(result.series).to.have.length(7);
expect(cache).to.deep.equal(["Cache-Control", "no-store"]);
});
it("reports invalid summary filters and aggregate failures without exposing database errors", async () => {
const router = require("../src/server/routes/admin-emails").default;
const handler = router.stack.find(layer => layer.route?.path === "/summary").route.stack[0].handle;
let code, body;
const res = { set() {}, status(value) { code = value; return this; }, json(value) { body = value; } };
await handler({ query: { period: "wrong" } }, res);
expect(code).to.equal(400);
stub(history, "emailHistoryCollection", () => ({ aggregate: () => ({ toArray: async () => { throw Error("private timeout info"); } }) }));
await handler({ query: {} }, res);
expect(code).to.equal(503);
expect(body.error).not.to.include("private");
});
it("mounts history behind authentication and administrator checks", () => {
const emails = require("../src/server/routes/admin-emails").default;
const index = admin.stack.findIndex(layer => layer.handle === emails);
expect(index).to.be.greaterThan(1);
expect(admin.stack.slice(0, index).filter(layer => !layer.route && layer.handle.length >= 3).length).to.be.at.least(2);
});
});
+84
View File
@@ -0,0 +1,84 @@
const { expect } = require("chai");
require("ts-node/register/transpile-only");
const config = require("../src/config").default;
const Repo = require("../src/core/model/anonymizedRepositories/anonymizedRepositories.model").default;
const Users = require("../src/core/model/users/users.model").default;
const { notifyRepositoryExpired } = require("../src/core/repository-expiration-email");
describe("repository expiration email", function () {
const restores = [];
const stub = (object, key, value) => { const old = object[key]; restores.push(() => object[key] = old); object[key] = value; };
let repo, owner, calls, claims, claimedFor;
beforeEach(() => {
repo = { _id: "repo-object-id", repoId: "anonymous-name", owner: "owner-id", statusDate: new Date(),
options: { expirationMode: "remove", expirationDate: new Date("2025-01-01") } };
owner = { emails: [{ email: "default@example.com", default: true }], notificationEmail: "preferred@example.com" };
calls = []; claims = []; claimedFor = undefined;
stub(config, "RESEND_API_KEY", "test-key"); stub(config, "EMAIL_FROM", "alerts@example.com");
stub(config, "APP_HOSTNAME", "https://anonymous.example.com");
stub(Repo, "findOne", filter => {
expect(filter.status).to.equal("expired");
return { lean: async () => repo };
});
stub(Users, "findOne", filter => {
expect(filter.status.$nin).to.deep.equal(["removed", "banned"]);
return { lean: async () => owner };
});
stub(Repo, "updateOne", (filter, update) => ({ exec: async () => {
claims.push(filter);
const date = update.$set.expirationEmailClaimedFor.getTime();
if (claimedFor === date) return { matchedCount: 0 };
claimedFor = date;
return { matchedCount: 1 };
} }));
stub(global, "fetch", async (url, options) => { calls.push({ url, options }); return { ok: true }; });
});
afterEach(() => { while (restores.length) restores.pop()(); });
it("sends the preferred address an expiration notice with a dashboard link", async () => {
await notifyRepositoryExpired(repo._id);
const body = JSON.parse(calls[0].options.body);
expect(body.to).to.deep.equal(["preferred@example.com"]);
expect(body.text).to.include('"anonymous-name" expired on 2025-01-01');
expect(body.text).to.include("https://anonymous.example.com/dashboard");
expect(body.text).not.to.include("redirects");
expect(claims[0]).to.include({ owner: repo.owner, status: "expired", statusDate: repo.statusDate });
expect(claims[0]["options.expirationDate"]).to.equal(repo.options.expirationDate);
});
it("explains redirect expiration", async () => {
repo.options.expirationMode = "redirect";
await notifyRepositoryExpired(repo._id);
expect(JSON.parse(calls[0].options.body).text).to.include("redirects to the original repository");
});
it("claims concurrent notifications once, and allows a later expiration date", async () => {
await Promise.all([notifyRepositoryExpired(repo._id), notifyRepositoryExpired(repo._id)]);
expect(calls).to.have.length(1);
repo.options.expirationDate = new Date("2025-02-01");
await notifyRepositoryExpired(repo._id);
expect(calls).to.have.length(2);
});
for (const scenario of ["disabled", "missing-repo", "never", "future", "missing-owner", "missing-email"]) {
it(`skips ${scenario} without claiming delivery`, async () => {
if (scenario === "disabled") config.RESEND_API_KEY = "";
if (scenario === "missing-repo") repo = null;
if (scenario === "never") repo.options.expirationMode = "never";
if (scenario === "future") repo.options.expirationDate = new Date(Date.now() + 86400000);
if (scenario === "missing-owner") owner = null;
if (scenario === "missing-email") owner = {};
await notifyRepositoryExpired("repo-object-id");
expect(calls).to.have.length(0); expect(claims).to.have.length(0);
});
}
it("does not send when the repository changed before the claim", async () => {
stub(Repo, "updateOne", () => ({ exec: async () => ({ matchedCount: 0 }) }));
await notifyRepositoryExpired(repo._id);
expect(calls).to.have.length(0);
});
for (const failure of ["timeout", "rejection"]) it(`retains the guard on provider ${failure} without interrupting expiration`, async () => {
let attempts = 0;
stub(global, "fetch", async () => { attempts++; if (failure === "timeout") throw Error("timeout"); return { ok: false, status: 503 }; });
await notifyRepositoryExpired(repo._id);
await notifyRepositoryExpired(repo._id);
expect(attempts).to.equal(1);
});
});
+70
View File
@@ -0,0 +1,70 @@
const { expect } = require("chai");
const http = require("node:http");
const got = require("got");
require("ts-node/register/transpile-only");
const { streamResponse } = require("../src/core/response-stream");
const { rejectStreamerRedirects } = require("../src/core/streamer-response");
const { probeStreamerCache } = require("../src/core/streamer-cache-probe");
describe("streamer response isolation", function () {
const servers = [];
async function serve(handler) {
const server = http.createServer(handler);
servers.push(server);
await new Promise(resolve => server.listen(0, "127.0.0.1", resolve));
return `http://127.0.0.1:${server.address().port}`;
}
afterEach(async () => {
await Promise.all(servers.splice(0).map(server => new Promise(resolve => {
server.close(resolve);
server.closeAllConnections();
})));
});
it("does not let Got copy identity-bearing upstream headers or status", async () => {
const upstream = await serve((_req, res) => {
res.writeHead(201, {
Location: "https://github.com/private-owner/private-repository",
Refresh: "0; url=https://github.com/private-owner/private-repository",
"Content-Location": "/private-owner/private-repository",
Link: "<https://github.com/private-owner/private-repository>; rel=canonical",
"Set-Cookie": "owner=private-owner",
"Content-Disposition": "attachment; filename=private-owner.zip",
});
res.end("anonymized content");
});
const proxy = await serve((_req, res) => {
res.setHeader("Content-Type", "text/plain");
void streamResponse(got.stream(upstream), res, () => res.end("error"));
});
const result = await got(proxy);
expect(result.statusCode).to.equal(200);
expect(result.body).to.equal("anonymized content");
for (const header of ["location", "refresh", "content-location", "link", "set-cookie", "content-disposition"]) {
expect(result.headers).not.to.have.property(header);
}
});
for (const status of [301, 302, 303, 307, 308]) {
it(`rejects ${status} without forwarding its body or contacting the destination`, async () => {
let contacted = 0;
const target = await serve((_req, res) => { contacted++; res.end("private-owner"); });
const upstream = await serve((_req, res) => {
res.writeHead(status, { Location: target + "/private-owner/private-repository" });
res.end("private-owner/private-repository");
});
const proxy = await serve((_req, res) => {
const source = rejectStreamerRedirects(got.stream(upstream, { followRedirect: false }));
void streamResponse(source, res, () => { res.statusCode = 502; res.end("upstream_error"); });
});
const result = await got(proxy, { throwHttpErrors: false, retry: 0 });
expect(result.statusCode).to.equal(502);
expect(result.body).to.equal("upstream_error");
expect(result.headers).not.to.have.property("location");
let failure;
try { await probeStreamerCache(upstream, {}, {}); } catch (error) { failure = error; }
expect(failure).to.be.instanceOf(Error);
expect(contacted).to.equal(0);
});
}
});