fix(desktop): guard invalid badge count payloads
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { readBadgeCount } from "./window-manager";
|
||||
|
||||
describe("window-manager", () => {
|
||||
describe("readBadgeCount", () => {
|
||||
it("returns valid non-negative integers", () => {
|
||||
expect(readBadgeCount(0)).toBe(0);
|
||||
expect(readBadgeCount(3)).toBe(3);
|
||||
});
|
||||
|
||||
it("falls back to zero for invalid payloads", () => {
|
||||
expect(readBadgeCount(undefined)).toBe(0);
|
||||
expect(readBadgeCount(null)).toBe(0);
|
||||
expect(readBadgeCount(Number.NaN)).toBe(0);
|
||||
expect(readBadgeCount(Number.POSITIVE_INFINITY)).toBe(0);
|
||||
expect(readBadgeCount(-1)).toBe(0);
|
||||
expect(readBadgeCount(1.5)).toBe(0);
|
||||
expect(readBadgeCount("2")).toBe(0);
|
||||
expect(readBadgeCount({ count: 2 })).toBe(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -6,6 +6,14 @@ import {
|
||||
type WindowMoveState,
|
||||
} from "./window-drag-coordinates.js";
|
||||
|
||||
export function readBadgeCount(input: unknown): number {
|
||||
if (typeof input !== "number" || !Number.isSafeInteger(input) || input < 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return input;
|
||||
}
|
||||
|
||||
export function registerWindowManager(): void {
|
||||
// ---------------------------------------------------------------------------
|
||||
// Manual window dragging (replaces flaky CSS -webkit-app-region: drag).
|
||||
@@ -79,9 +87,18 @@ export function registerWindowManager(): void {
|
||||
return win?.isFullScreen() ?? false;
|
||||
});
|
||||
|
||||
ipcMain.handle("paseo:window:setBadgeCount", (_event, count?: number) => {
|
||||
ipcMain.handle("paseo:window:setBadgeCount", (_event, count?: unknown) => {
|
||||
if (process.platform === "darwin" || process.platform === "linux") {
|
||||
app.setBadgeCount(count ?? 0);
|
||||
const badgeCount = readBadgeCount(count);
|
||||
try {
|
||||
app.setBadgeCount(badgeCount);
|
||||
} catch (error) {
|
||||
console.warn("[window-manager] Failed to update badge count", {
|
||||
count,
|
||||
badgeCount,
|
||||
error,
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user