Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(usage): no mtime fallback for the install week, validate the stored week

EtienneLescot93e9602faec4
diff --git a/electron/usage-ping.test.ts b/electron/usage-ping.test.ts
index 74131c184..91cfb73e0 100644
--- a/electron/usage-ping.test.ts
+++ b/electron/usage-ping.test.ts
@@ -1,4 +1,4 @@
-import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
+import { mkdtempSync, rmSync, statSync, writeFileSync } from "node:fs";
import os from "node:os";
import path from "node:path";
import { afterEach, describe, expect, it } from "vitest";
@@ -134,15 +134,11 @@ describe("sendUsagePing", () => {
});
describe("install week", () => {
- it("dates the install by the userData directory, birthtime first, then mtime", () => {
- const may2025 = Date.parse("2025-05-07T12:00:00Z");
- expect(installWeekFrom({ birthtimeMs: may2025, mtimeMs: wednesday.getTime() })).toBe(
- "2025-W19",
- );
- // Filesystems that record no birthtime report 0.
- expect(installWeekFrom({ birthtimeMs: 0, mtimeMs: may2025 })).toBe("2025-W19");
- expect(installWeekFrom({ birthtimeMs: 0, mtimeMs: 0 })).toBe("unknown");
- expect(installWeekFrom({ birthtimeMs: Number.NaN, mtimeMs: Number.NaN })).toBe("unknown");
+ it("dates the install by the userData directory's birthtime, never its mtime", () => {
+ expect(installWeekFrom({ birthtimeMs: Date.parse("2025-05-07T12:00:00Z") })).toBe("2025-W19");
+ // Filesystems that record no birthtime report 0; mtime would move, so no fallback.
+ expect(installWeekFrom({ birthtimeMs: 0 })).toBe("unknown");
+ expect(installWeekFrom({ birthtimeMs: Number.NaN })).toBe("unknown");
expect(installWeekFrom(null)).toBe("unknown");
});
@@ -157,7 +153,8 @@ describe("install week", () => {
it("gives a fresh install the current week, then keeps it on later pings", async () => {
const dir = tmp();
- const thisWeek = periodKeys(new Date()).week;
+ // From the directory, not the clock, so a UTC Monday between the two cannot flake.
+ const thisWeek = installWeekFrom(statSync(dir));
expect(await sent(dir, new Date())).toBe(thisWeek);
expect(loadUsagePingState(dir).installWeek).toBe(thisWeek);
expect(await sent(dir, new Date(Date.now() + 40 * 86_400_000))).toBe(thisWeek);
@@ -173,6 +170,20 @@ describe("install week", () => {
}
});
+ it("re-derives a stored week that is not a valid ISO week", async () => {
+ for (const installWeek of ["2026-W54", "2026-W00", "abc", "2026-W4"]) {
+ const dir = tmp();
+ writeFileSync(path.join(dir, "usage-ping.json"), JSON.stringify({ ...fresh, installWeek }));
+ expect(loadUsagePingState(dir).installWeek).toBeNull();
+ }
+ const dir = tmp();
+ writeFileSync(
+ path.join(dir, "usage-ping.json"),
+ JSON.stringify({ ...fresh, installWeek: "2026-W53" }),
+ );
+ expect(loadUsagePingState(dir).installWeek).toBe("2026-W53");
+ });
+
it('sends "unknown" when the directory cannot be dated', async () => {
expect(await sent(path.join(tmp(), "gone"), wednesday)).toBe("unknown");
});
diff --git a/electron/usage-ping.ts b/electron/usage-ping.ts
index 5424d1e61..0349caeae 100644
--- a/electron/usage-ping.ts
+++ b/electron/usage-ping.ts
@@ -77,13 +77,17 @@ export function periodKeys(now: Date): { day: string; week: string; month: strin
/** The install week, dated by when the userData directory was created. Electron creates it
* on first launch, so a fresh install gets the current week, and an install older than
- * this field, even one that never pinged, gets its real one. Birthtime is 0 where the
- * filesystem does not record it; mtime is the fallback. */
-export function installWeekFrom(stats: { birthtimeMs: number; mtimeMs: number } | null): string {
- const ms = stats && (stats.birthtimeMs > 0 ? stats.birthtimeMs : stats.mtimeMs);
+ * this field, even one that never pinged, gets its real one. Where the filesystem records
+ * no birthtime (0), the week is "unknown": mtime moves whenever the folder changes, and the
+ * week is kept for good once sent. */
+export function installWeekFrom(stats: { birthtimeMs: number } | null): string {
+ const ms = stats?.birthtimeMs;
return ms && ms > 0 ? periodKeys(new Date(ms)).week : "unknown";
}
+/** "2026-W44" (weeks 01 to 53) or "unknown"; anything else is re-derived. */
+const INSTALL_WEEK = /^(\d{4}-W(0[1-9]|[1-4]\d|5[0-3])|unknown)$/;
+
function statOrNull(dir: string) {
try {
return statSync(dir);
@@ -122,7 +126,10 @@ export function loadUsagePingState(userData: string): UsagePingState {
day: text(raw.day),
week: text(raw.week),
month: text(raw.month),
- installWeek: text(raw.installWeek),
+ installWeek:
+ typeof raw.installWeek === "string" && INSTALL_WEEK.test(raw.installWeek)
+ ? raw.installWeek
+ : null,
};
} catch {
return { ...DEFAULT_STATE };