harden v2 custom widgets and workshop
This commit is contained in:
1 parent
e8a2c87c2d
commit
723b46926a
194 files changed
+44862
-1265
No files matched your search
@@ -9,24 +9,25 @@ import {
|
||||
users,
|
||||
} from "@homarr/db/schema";
|
||||
import { createDb } from "@homarr/db/test";
|
||||
import { BUNDLED_CUSTOM_WIDGETS } from "@homarr/custom-widgets/core";
|
||||
import { BUNDLED_CUSTOM_WIDGETS, customWidgetDefinitionSchema } from "@homarr/custom-widgets/core";
|
||||
import { describe, expect, test } from "vitest";
|
||||
|
||||
import { customWidgetRouter } from "../../custom-widget/custom-widget-router";
|
||||
import { getCustomWidgetConfigurationRequestForUser } from "../../custom-widget/configuration-requests";
|
||||
import { serializeCustomWidgetDefinition } from "../../custom-widget/stored-definition";
|
||||
|
||||
const userId = createId();
|
||||
const session = {
|
||||
user: {
|
||||
id: userId,
|
||||
permissions: ["custom-widget-manage", "custom-widget-secret-write"],
|
||||
permissions: ["admin"],
|
||||
colorScheme: "light",
|
||||
},
|
||||
expires: new Date().toISOString(),
|
||||
} satisfies Session;
|
||||
const manageOnlySession = {
|
||||
const nonAdminSession = {
|
||||
...session,
|
||||
user: { ...session.user, permissions: ["custom-widget-manage"] },
|
||||
user: { ...session.user, permissions: ["board-modify-all"] },
|
||||
} satisfies Session;
|
||||
|
||||
const jellyfin = BUNDLED_CUSTOM_WIDGETS.find(({ id }) => id === "seed-jellyfin")?.widget;
|
||||
@@ -38,11 +39,11 @@ function createCaller(db: ReturnType<typeof createDb>) {
|
||||
return customWidgetRouter.createCaller({ db, deviceType: undefined, session });
|
||||
}
|
||||
|
||||
function createManageOnlyCaller(db: ReturnType<typeof createDb>) {
|
||||
function createNonAdminCaller(db: ReturnType<typeof createDb>) {
|
||||
return customWidgetRouter.createCaller({
|
||||
db,
|
||||
deviceType: undefined,
|
||||
session: manageOnlySession,
|
||||
session: nonAdminSession,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -54,14 +55,24 @@ async function prepareDatabase() {
|
||||
|
||||
function rejectSecretInserts(db: ReturnType<typeof createDb>) {
|
||||
db.run(
|
||||
sql.raw(`CREATE TRIGGER reject_custom_widget_secret_insert
|
||||
BEFORE INSERT ON custom_widget_secret
|
||||
sql.raw(`CREATE TRIGGER reject_custom_widget_v2_secret_insert
|
||||
BEFORE INSERT ON custom_widget_v2_secret
|
||||
BEGIN
|
||||
SELECT RAISE(ABORT, 'forced secret insert failure');
|
||||
END`),
|
||||
);
|
||||
}
|
||||
|
||||
function rejectDefinitionUpdates(db: ReturnType<typeof createDb>) {
|
||||
db.run(
|
||||
sql.raw(`CREATE TRIGGER reject_custom_widget_v2_definition_update
|
||||
BEFORE UPDATE ON custom_widget_v2_definition
|
||||
BEGIN
|
||||
SELECT RAISE(ABORT, 'forced definition update failure');
|
||||
END`),
|
||||
);
|
||||
}
|
||||
|
||||
async function insertLegacyJellyfin(db: ReturnType<typeof createDb>) {
|
||||
const id = "legacy-jellyfin";
|
||||
const encryptedValue = "preserved.secret" as `${string}.${string}`;
|
||||
@@ -88,13 +99,21 @@ async function insertLegacyJellyfin(db: ReturnType<typeof createDb>) {
|
||||
const secret = { sourceId: "default", kind: "apiKey" as const, value: "test-secret" };
|
||||
|
||||
describe("custom widget definition persistence", () => {
|
||||
test("atomically replaces a confirmed legacy widget while preserving its stable ID and credential", async () => {
|
||||
test("atomically creates a v2 replacement while preserving the legacy widget and its stable ID", async () => {
|
||||
const db = await prepareDatabase();
|
||||
const legacy = await insertLegacyJellyfin(db);
|
||||
|
||||
await createCaller(db).migrateLegacy({ id: legacy.id, widget: jellyfin, secrets: [] });
|
||||
|
||||
expect(await db.query.legacyCustomWidgetDefinitions.findMany()).toHaveLength(0);
|
||||
expect(await db.query.legacyCustomWidgetDefinitions.findFirst()).toMatchObject({
|
||||
id: legacy.id,
|
||||
name: "Legacy Jellyfin",
|
||||
enabled: false,
|
||||
});
|
||||
expect(await db.query.legacyCustomWidgetSecrets.findFirst()).toMatchObject({
|
||||
definitionId: legacy.id,
|
||||
encryptedValue: legacy.encryptedValue,
|
||||
});
|
||||
expect(await db.query.customWidgetDefinitions.findFirst()).toMatchObject({
|
||||
id: legacy.id,
|
||||
enabled: false,
|
||||
@@ -106,6 +125,20 @@ describe("custom widget definition persistence", () => {
|
||||
kind: "apiKey",
|
||||
encryptedValue: legacy.encryptedValue,
|
||||
});
|
||||
|
||||
const migratedList = await createCaller(db).list();
|
||||
expect(migratedList).toHaveLength(1);
|
||||
expect(migratedList[0]).toMatchObject({ id: legacy.id, migrationRequired: false });
|
||||
|
||||
await createCaller(db).delete({ id: legacy.id });
|
||||
|
||||
const fallbackList = await createCaller(db).list();
|
||||
expect(fallbackList).toHaveLength(1);
|
||||
expect(fallbackList[0]).toMatchObject({ id: legacy.id, migrationRequired: true });
|
||||
expect(await db.query.legacyCustomWidgetSecrets.findFirst()).toMatchObject({
|
||||
definitionId: legacy.id,
|
||||
encryptedValue: legacy.encryptedValue,
|
||||
});
|
||||
});
|
||||
|
||||
test("keeps the legacy widget recoverable when confirmed replacement fails", async () => {
|
||||
@@ -149,6 +182,30 @@ describe("custom widget definition persistence", () => {
|
||||
expect(await db.query.customWidgetSecrets.findMany()).toHaveLength(0);
|
||||
});
|
||||
|
||||
test("refuses to export a stored definition containing embedded credentials", async () => {
|
||||
const db = await prepareDatabase();
|
||||
const definition = customWidgetDefinitionSchema.parse(jellyfin);
|
||||
const counts = definition.requests.counts;
|
||||
if (!counts) throw new Error("Jellyfin counts request is missing");
|
||||
const id = createId();
|
||||
await db.insert(customWidgetDefinitions).values({
|
||||
id,
|
||||
...serializeCustomWidgetDefinition({
|
||||
...definition,
|
||||
requests: {
|
||||
...definition.requests,
|
||||
counts: {
|
||||
...counts,
|
||||
headers: { ...counts.headers, "X-Auth": "Bearer sk-secret-123456" },
|
||||
},
|
||||
},
|
||||
}),
|
||||
creatorId: userId,
|
||||
});
|
||||
|
||||
await expect(createCaller(db).export({ id })).rejects.toThrow("Credentials must use source authentication");
|
||||
});
|
||||
|
||||
test("rolls back definition and secret changes when a replacement secret cannot be stored", async () => {
|
||||
const db = await prepareDatabase();
|
||||
const caller = createCaller(db);
|
||||
@@ -180,6 +237,69 @@ describe("custom widget definition persistence", () => {
|
||||
expect(storedSecret).toEqual(originalSecret);
|
||||
});
|
||||
|
||||
test("keeps the existing secret when setting its replacement fails", async () => {
|
||||
const db = await prepareDatabase();
|
||||
const caller = createCaller(db);
|
||||
const created = await caller.create({ ...jellyfin, secrets: [secret] });
|
||||
const originalDefinition = await db.query.customWidgetDefinitions.findFirst({
|
||||
where: eq(customWidgetDefinitions.id, created.id),
|
||||
});
|
||||
const originalSecret = await db.query.customWidgetSecrets.findFirst({
|
||||
where: eq(customWidgetSecrets.definitionId, created.id),
|
||||
});
|
||||
rejectSecretInserts(db);
|
||||
|
||||
await expect(
|
||||
caller.secretSet({
|
||||
definitionId: created.id,
|
||||
secret: { ...secret, value: "replacement-secret" },
|
||||
}),
|
||||
).rejects.toThrow("forced secret insert failure");
|
||||
|
||||
expect(
|
||||
await db.query.customWidgetDefinitions.findFirst({
|
||||
where: eq(customWidgetDefinitions.id, created.id),
|
||||
}),
|
||||
).toEqual(originalDefinition);
|
||||
expect(
|
||||
await db.query.customWidgetSecrets.findFirst({
|
||||
where: eq(customWidgetSecrets.definitionId, created.id),
|
||||
}),
|
||||
).toEqual(originalSecret);
|
||||
});
|
||||
|
||||
test("keeps the existing secret when clearing it cannot update the definition revision", async () => {
|
||||
const db = await prepareDatabase();
|
||||
const caller = createCaller(db);
|
||||
const created = await caller.create({ ...jellyfin, secrets: [secret] });
|
||||
const originalDefinition = await db.query.customWidgetDefinitions.findFirst({
|
||||
where: eq(customWidgetDefinitions.id, created.id),
|
||||
});
|
||||
const originalSecret = await db.query.customWidgetSecrets.findFirst({
|
||||
where: eq(customWidgetSecrets.definitionId, created.id),
|
||||
});
|
||||
rejectDefinitionUpdates(db);
|
||||
|
||||
await expect(
|
||||
caller.secretClear({
|
||||
definitionId: created.id,
|
||||
sourceId: secret.sourceId,
|
||||
kind: secret.kind,
|
||||
}),
|
||||
).rejects.toThrow("forced definition update failure");
|
||||
|
||||
expect(
|
||||
await db.query.customWidgetDefinitions.findFirst({
|
||||
where: eq(customWidgetDefinitions.id, created.id),
|
||||
}),
|
||||
).toEqual(originalDefinition);
|
||||
expect(
|
||||
await db.query.customWidgetSecrets.findFirst({
|
||||
where: eq(customWidgetSecrets.definitionId, created.id),
|
||||
}),
|
||||
).toEqual(originalSecret);
|
||||
});
|
||||
|
||||
test("configures a source URL and credential together", async () => {
|
||||
const db = await prepareDatabase();
|
||||
const caller = createCaller(db);
|
||||
@@ -249,7 +369,7 @@ describe("custom widget definition persistence", () => {
|
||||
};
|
||||
|
||||
await expect(
|
||||
createManageOnlyCaller(db).previewCreate({
|
||||
createNonAdminCaller(db).previewCreate({
|
||||
definitionId: created.id,
|
||||
definition: changedDefinition,
|
||||
secrets: [],
|
||||
@@ -260,11 +380,11 @@ describe("custom widget definition persistence", () => {
|
||||
).resolves.toMatchObject({ success: true });
|
||||
});
|
||||
|
||||
test("clears persisted credentials when a manage-only source edit changes its binding", async () => {
|
||||
test("clears persisted credentials when an admin source edit changes its binding", async () => {
|
||||
const db = await prepareDatabase();
|
||||
const created = await createCaller(db).create({ ...jellyfin, secrets: [secret] });
|
||||
|
||||
await createManageOnlyCaller(db).sourceConfigure({
|
||||
await createCaller(db).sourceConfigure({
|
||||
definitionId: created.id,
|
||||
sourceId: "default",
|
||||
baseUrl: "https://other.example/api",
|
||||
@@ -284,7 +404,7 @@ describe("custom widget definition persistence", () => {
|
||||
const caller = createCaller(db);
|
||||
const created = await caller.create({ ...jellyfin, secrets: [secret] });
|
||||
|
||||
await createManageOnlyCaller(db).update({
|
||||
await createCaller(db).update({
|
||||
id: created.id,
|
||||
sources: {
|
||||
...jellyfin.sources,
|
||||
@@ -299,12 +419,12 @@ describe("custom widget definition persistence", () => {
|
||||
).toHaveLength(0);
|
||||
});
|
||||
|
||||
test("requires secret-write permission to create a stored-definition setup link", async () => {
|
||||
test("requires an administrator to create a stored-definition setup link", async () => {
|
||||
const db = await prepareDatabase();
|
||||
const created = await createCaller(db).create({ ...jellyfin, secrets: [] });
|
||||
|
||||
await expect(
|
||||
createManageOnlyCaller(db).configurationRequestUser({ definitionId: created.id, sourceId: "default" }),
|
||||
createNonAdminCaller(db).configurationRequestUser({ definitionId: created.id, sourceId: "default" }),
|
||||
).rejects.toMatchObject({ code: "FORBIDDEN" });
|
||||
});
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { describe, expect, test } from "vitest";
|
||||
import { stringify as stringifySuperJson } from "superjson";
|
||||
|
||||
import { buildLegacyCustomWidgetMigrationPrompt } from "../../custom-widget/legacy-migration";
|
||||
|
||||
@@ -9,14 +10,14 @@ describe("legacy custom widget migration prompt", () => {
|
||||
id: "legacy",
|
||||
name: "Private service",
|
||||
description: "Status",
|
||||
iconUrl: null,
|
||||
iconUrl: "https://icons:password@example.test/private/icon.svg?access_token=icon-secret",
|
||||
url: "https://admin:password@example.test/hooks/path-token?api_key=top-secret&mode=full",
|
||||
authType: "bearer",
|
||||
headerName: null,
|
||||
method: "POST",
|
||||
requestBody: JSON.stringify({ token: "body-secret", limit: 5 }),
|
||||
displayType: "singleValue",
|
||||
displayConfig: JSON.stringify({ json: { type: "singleValue", jsonPath: "$.status" } }),
|
||||
displayConfig: "corrupted-config-with-display-secret",
|
||||
enabled: true,
|
||||
createdAt: new Date(0),
|
||||
updatedAt: new Date(0),
|
||||
@@ -32,5 +33,40 @@ describe("legacy custom widget migration prompt", () => {
|
||||
expect(prompt).not.toContain("body-secret");
|
||||
expect(prompt).not.toContain("admin:password");
|
||||
expect(prompt).not.toContain("path-token");
|
||||
expect(prompt).not.toContain("icon-secret");
|
||||
expect(prompt).not.toContain("icons:password");
|
||||
expect(prompt).not.toContain("private/icon.svg");
|
||||
expect(prompt).not.toContain("display-secret");
|
||||
});
|
||||
|
||||
test("omits valid free-text display configuration from the migration prompt", () => {
|
||||
const prompt = buildLegacyCustomWidgetMigrationPrompt(
|
||||
{
|
||||
id: "legacy-custom-jsx",
|
||||
name: "Private service",
|
||||
description: "Status",
|
||||
iconUrl: null,
|
||||
url: "https://example.test/api/status",
|
||||
authType: "bearer",
|
||||
headerName: null,
|
||||
method: "GET",
|
||||
requestBody: null,
|
||||
displayType: "customJsx",
|
||||
displayConfig: stringifySuperJson({
|
||||
type: "customJsx",
|
||||
template: "<Text>Bearer sk-secret-123456</Text>",
|
||||
}),
|
||||
enabled: true,
|
||||
createdAt: new Date(0),
|
||||
updatedAt: new Date(0),
|
||||
creatorId: null,
|
||||
},
|
||||
["apiKey"],
|
||||
);
|
||||
|
||||
expect(prompt).toContain("display configuration was intentionally omitted");
|
||||
expect(prompt).not.toContain("<Text>");
|
||||
expect(prompt).not.toContain("Bearer sk-secret-123456");
|
||||
expect(prompt).not.toContain("sk-secret-123456");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,118 @@
|
||||
import { createId } from "@homarr/common";
|
||||
import type * as CustomWidgetServer from "@homarr/custom-widgets/server";
|
||||
import { beforeEach, describe, expect, test, vi } from "vitest";
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
executeDomainRequest: vi.fn(async () => ({
|
||||
ok: true,
|
||||
status: 200,
|
||||
statusText: "OK",
|
||||
data: { value: 42 },
|
||||
})),
|
||||
logger: {
|
||||
debug: vi.fn(),
|
||||
error: vi.fn(),
|
||||
info: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock("@homarr/core/infrastructure/logs", () => ({
|
||||
createLogger: () => mocks.logger,
|
||||
}));
|
||||
|
||||
vi.mock("@homarr/custom-widgets/server", async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof CustomWidgetServer>();
|
||||
return {
|
||||
...actual,
|
||||
executeCustomWidgetRequest: mocks.executeDomainRequest,
|
||||
};
|
||||
});
|
||||
|
||||
import { executeCustomWidgetRequest } from "../../custom-widget/request-executor";
|
||||
|
||||
describe("custom widget request execution limits", () => {
|
||||
beforeEach(() => {
|
||||
mocks.executeDomainRequest.mockClear();
|
||||
mocks.logger.error.mockClear();
|
||||
});
|
||||
|
||||
test("acquires capacity only for a query cache miss", async () => {
|
||||
const release = vi.fn(async () => undefined);
|
||||
const acquireRequestLimit = vi.fn(async () => release);
|
||||
const input = {
|
||||
baseUrl: "https://example.com",
|
||||
targetUrl: "https://example.com/status",
|
||||
method: "GET" as const,
|
||||
networkScope: "public" as const,
|
||||
kind: "query" as const,
|
||||
cacheKey: `custom-jsx:${createId()}:version:status:params`,
|
||||
cacheTtlSeconds: 60,
|
||||
};
|
||||
|
||||
await executeCustomWidgetRequest(input, { acquireRequestLimit });
|
||||
await executeCustomWidgetRequest(input, { acquireRequestLimit });
|
||||
|
||||
expect(mocks.executeDomainRequest).toHaveBeenCalledOnce();
|
||||
expect(acquireRequestLimit).toHaveBeenCalledOnce();
|
||||
expect(release).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
test("keeps every action inside request accounting", async () => {
|
||||
const release = vi.fn(async () => undefined);
|
||||
const acquireRequestLimit = vi.fn(async () => release);
|
||||
const input = {
|
||||
baseUrl: "https://example.com",
|
||||
targetUrl: "https://example.com/action",
|
||||
method: "POST" as const,
|
||||
networkScope: "public" as const,
|
||||
kind: "action" as const,
|
||||
};
|
||||
|
||||
await executeCustomWidgetRequest(input, { acquireRequestLimit });
|
||||
await executeCustomWidgetRequest(input, { acquireRequestLimit });
|
||||
|
||||
expect(mocks.executeDomainRequest).toHaveBeenCalledTimes(2);
|
||||
expect(acquireRequestLimit).toHaveBeenCalledTimes(2);
|
||||
expect(release).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
test("replaces an unexpected apiKeyQuery transport error with safe public and loggable metadata", async () => {
|
||||
const apiKey = "api-key-query-secret";
|
||||
const tokenizedUrl = `https://example.com/status?access_token=${apiKey}`;
|
||||
const transportFailure = new Error(`Transport failed for ${tokenizedUrl}`);
|
||||
transportFailure.name = `SocketError:${tokenizedUrl}`;
|
||||
mocks.executeDomainRequest.mockRejectedValueOnce(transportFailure);
|
||||
|
||||
const error = await executeCustomWidgetRequest({
|
||||
baseUrl: "https://example.com",
|
||||
targetUrl: "https://example.com/status",
|
||||
method: "GET",
|
||||
networkScope: "public",
|
||||
kind: "query",
|
||||
auth: {
|
||||
type: "apiKeyQuery",
|
||||
headerName: "access_token",
|
||||
secrets: [{ kind: "apiKey", value: apiKey }],
|
||||
},
|
||||
}).then(
|
||||
() => undefined,
|
||||
(cause: unknown) => cause,
|
||||
);
|
||||
|
||||
expect(error).toMatchObject({
|
||||
code: "BAD_GATEWAY",
|
||||
message: "External request failed",
|
||||
});
|
||||
expect((error as Error).cause).toBeUndefined();
|
||||
expect(mocks.logger.error).toHaveBeenCalledWith("Custom widget request failed", {
|
||||
event: "custom_widget_request_failed",
|
||||
errorName: "UnexpectedTransportError",
|
||||
origin: "https://example.com",
|
||||
method: "GET",
|
||||
});
|
||||
expect(JSON.stringify(mocks.logger.error.mock.calls)).not.toContain(apiKey);
|
||||
expect(JSON.stringify(mocks.logger.error.mock.calls)).not.toContain("access_token");
|
||||
expect(String(error)).not.toContain(apiKey);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,58 @@
|
||||
import { randomUUID } from "node:crypto";
|
||||
|
||||
import { RedisContainer } from "@testcontainers/redis";
|
||||
import type { StartedRedisContainer } from "@testcontainers/redis";
|
||||
import { Redis } from "ioredis";
|
||||
import { afterAll, beforeAll, describe, expect, test } from "vitest";
|
||||
|
||||
import { createRedisCustomWidgetResponseCache } from "../../custom-widget/response-cache";
|
||||
|
||||
describe("custom widget response cache across replicas", () => {
|
||||
let container: StartedRedisContainer | undefined;
|
||||
let redisA: Redis | undefined;
|
||||
let redisB: Redis | undefined;
|
||||
|
||||
beforeAll(async () => {
|
||||
container = await new RedisContainer("redis:7.4-alpine").start();
|
||||
redisA = new Redis(container.getConnectionUrl());
|
||||
redisB = new Redis(container.getConnectionUrl());
|
||||
await Promise.all([redisA.ping(), redisB.ping()]);
|
||||
}, 60_000);
|
||||
|
||||
afterAll(async () => {
|
||||
await Promise.all([redisA?.quit(), redisB?.quit()]);
|
||||
await container?.stop();
|
||||
}, 30_000);
|
||||
|
||||
test("an action on one replica invalidates a query cached by another replica", async () => {
|
||||
if (!redisA || !redisB) throw new Error("Redis test clients were not initialized");
|
||||
const cacheA = createRedisCustomWidgetResponseCache(redisA);
|
||||
const cacheB = createRedisCustomWidgetResponseCache(redisB);
|
||||
const itemNamespace = `custom-jsx:${randomUUID()}:`;
|
||||
const namespace = `${itemNamespace}definition-v1:status:`;
|
||||
const input = {
|
||||
baseUrl: "https://example.com",
|
||||
method: "GET" as const,
|
||||
networkScope: "public" as const,
|
||||
kind: "query" as const,
|
||||
cacheKey: `${namespace}same-options`,
|
||||
cacheTtlSeconds: 60,
|
||||
};
|
||||
let calls = 0;
|
||||
const execute = async () => ({
|
||||
ok: true,
|
||||
status: 200,
|
||||
statusText: "OK",
|
||||
data: { calls: ++calls },
|
||||
});
|
||||
|
||||
await expect(cacheA.withCache(input, execute)).resolves.toMatchObject({ data: { calls: 1 } });
|
||||
await expect(cacheB.withCache(input, execute)).resolves.toMatchObject({ data: { calls: 1 } });
|
||||
expect(calls).toBe(1);
|
||||
|
||||
await cacheB.invalidate([itemNamespace]);
|
||||
|
||||
await expect(cacheA.withCache(input, execute)).resolves.toMatchObject({ data: { calls: 2 } });
|
||||
expect(calls).toBe(2);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,91 @@
|
||||
import { describe, expect, test, vi } from "vitest";
|
||||
|
||||
import { invalidateCustomWidgetResponseCache, withCustomWidgetResponseCache } from "../../custom-widget/response-cache";
|
||||
|
||||
const response = (calls: number) => ({
|
||||
ok: true,
|
||||
status: 200,
|
||||
statusText: "OK",
|
||||
data: { calls },
|
||||
});
|
||||
|
||||
describe("custom widget shared response cache", () => {
|
||||
test("deduplicates queries and invalidates them with a versioned namespace", async () => {
|
||||
const itemNamespace = "custom-jsx:test-item:";
|
||||
const namespace = `${itemNamespace}test-version:status:`;
|
||||
const input = {
|
||||
baseUrl: "https://example.com",
|
||||
method: "GET" as const,
|
||||
networkScope: "public" as const,
|
||||
kind: "query" as const,
|
||||
cacheKey: `${namespace}test-params`,
|
||||
cacheTtlSeconds: 60,
|
||||
};
|
||||
let calls = 0;
|
||||
const execute = async () => response(++calls);
|
||||
|
||||
await expect(
|
||||
Promise.all([withCustomWidgetResponseCache(input, execute), withCustomWidgetResponseCache(input, execute)]),
|
||||
).resolves.toEqual([response(1), response(1)]);
|
||||
await expect(withCustomWidgetResponseCache(input, execute)).resolves.toEqual(response(1));
|
||||
|
||||
await invalidateCustomWidgetResponseCache([itemNamespace]);
|
||||
|
||||
await expect(withCustomWidgetResponseCache(input, execute)).resolves.toEqual(response(2));
|
||||
});
|
||||
|
||||
test("does not cache actions or zero-TTL queries", async () => {
|
||||
let calls = 0;
|
||||
const execute = async () => response(++calls);
|
||||
const baseInput = {
|
||||
baseUrl: "https://example.com",
|
||||
method: "GET" as const,
|
||||
networkScope: "public" as const,
|
||||
cacheKey: "custom-jsx:no-cache:version:status:params",
|
||||
cacheTtlSeconds: 0,
|
||||
};
|
||||
|
||||
await withCustomWidgetResponseCache({ ...baseInput, kind: "query" }, execute);
|
||||
await withCustomWidgetResponseCache({ ...baseInput, kind: "query" }, execute);
|
||||
await withCustomWidgetResponseCache({ ...baseInput, kind: "action" }, execute);
|
||||
await withCustomWidgetResponseCache({ ...baseInput, kind: "action" }, execute);
|
||||
|
||||
expect(calls).toBe(4);
|
||||
});
|
||||
|
||||
test("does not retry a rejected query execution", async () => {
|
||||
const failure = new Error("upstream failed");
|
||||
const execute = vi.fn(async () => {
|
||||
throw failure;
|
||||
});
|
||||
const input = {
|
||||
baseUrl: "https://example.com",
|
||||
method: "GET" as const,
|
||||
networkScope: "public" as const,
|
||||
kind: "query" as const,
|
||||
cacheKey: "custom-jsx:rejected:version:status:params",
|
||||
cacheTtlSeconds: 60,
|
||||
};
|
||||
|
||||
await expect(withCustomWidgetResponseCache(input, execute)).rejects.toBe(failure);
|
||||
expect(execute).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
test("does not retry a synchronous query execution failure", async () => {
|
||||
const failure = new Error("failed before returning a promise");
|
||||
const execute = vi.fn(() => {
|
||||
throw failure;
|
||||
});
|
||||
const input = {
|
||||
baseUrl: "https://example.com",
|
||||
method: "GET" as const,
|
||||
networkScope: "public" as const,
|
||||
kind: "query" as const,
|
||||
cacheKey: "custom-jsx:synchronous-failure:version:status:params",
|
||||
cacheTtlSeconds: 60,
|
||||
};
|
||||
|
||||
await expect(withCustomWidgetResponseCache(input, execute)).rejects.toBe(failure);
|
||||
expect(execute).toHaveBeenCalledOnce();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,16 @@
|
||||
import { describe, expect, test } from "vitest";
|
||||
|
||||
import { useProcessLocalCustomWidgetState } from "../../../custom-widget-state-mode";
|
||||
|
||||
describe("Custom Widget shared-state selection", () => {
|
||||
test.each([
|
||||
[{ NODE_ENV: "test", CI: undefined }, true],
|
||||
[{ NODE_ENV: "development", CI: "true" }, true],
|
||||
[{ NODE_ENV: "development", CI: "false" }, false],
|
||||
[{ NODE_ENV: "production", CI: "true" }, false],
|
||||
[{ NODE_ENV: "production", CI: "false" }, false],
|
||||
[{ NODE_ENV: "production", CI: undefined }, false],
|
||||
])("uses process-local state only in tests: %o", (runtime, expected) => {
|
||||
expect(useProcessLocalCustomWidgetState(runtime)).toBe(expected);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,113 @@
|
||||
import type { Session } from "@homarr/auth";
|
||||
import { createId } from "@homarr/common";
|
||||
import { createDb } from "@homarr/db/test";
|
||||
import { beforeEach, describe, expect, test, vi } from "vitest";
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
get: vi.fn(async (_id: string): Promise<unknown> => undefined),
|
||||
list: vi.fn(async (_options: unknown): Promise<unknown> => undefined),
|
||||
logger: {
|
||||
debug: vi.fn(),
|
||||
error: vi.fn(),
|
||||
info: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock("@homarr/core/infrastructure/logs", () => ({
|
||||
createLogger: () => mocks.logger,
|
||||
}));
|
||||
|
||||
vi.mock("@homarr/workshop/backend", () => ({
|
||||
WorkshopBackend: class {
|
||||
public get(id: string) {
|
||||
return mocks.get(id);
|
||||
}
|
||||
|
||||
public list(options: unknown) {
|
||||
return mocks.list(options);
|
||||
}
|
||||
},
|
||||
}));
|
||||
|
||||
import { customWidgetRouter } from "../../custom-widget/custom-widget-router";
|
||||
|
||||
const session = {
|
||||
user: {
|
||||
id: createId(),
|
||||
permissions: ["admin"],
|
||||
colorScheme: "light",
|
||||
},
|
||||
expires: new Date(Date.now() + 60_000).toISOString(),
|
||||
} satisfies Session;
|
||||
|
||||
const createCaller = () =>
|
||||
customWidgetRouter.createCaller({
|
||||
db: createDb(),
|
||||
deviceType: undefined,
|
||||
session,
|
||||
});
|
||||
|
||||
const createTokenizedFailure = (apiKey: string) => {
|
||||
const tokenizedUrl = `https://workshop.example/api/submissions?access_token=${apiKey}`;
|
||||
const error = new Error(`Workshop request failed for ${tokenizedUrl}`);
|
||||
error.name = `WorkshopTransportError:${tokenizedUrl}`;
|
||||
return error;
|
||||
};
|
||||
|
||||
const expectSafeFailure = (
|
||||
error: unknown,
|
||||
apiKey: string,
|
||||
logMessage: string,
|
||||
event: "workshop_widget_lookup_failed" | "workshop_widget_search_failed",
|
||||
) => {
|
||||
expect(error).toMatchObject({
|
||||
code: "BAD_GATEWAY",
|
||||
message: "Workshop is unavailable",
|
||||
});
|
||||
expect((error as Error).cause).toBeUndefined();
|
||||
expect(String(error)).not.toContain(apiKey);
|
||||
expect(String(error)).not.toContain("access_token");
|
||||
expect(mocks.logger.error).toHaveBeenCalledWith(logMessage, {
|
||||
event,
|
||||
errorName: "WorkshopBackendError",
|
||||
});
|
||||
expect(JSON.stringify(mocks.logger.error.mock.calls)).not.toContain(apiKey);
|
||||
expect(JSON.stringify(mocks.logger.error.mock.calls)).not.toContain("access_token");
|
||||
};
|
||||
|
||||
describe("Workshop backend error privacy", () => {
|
||||
beforeEach(() => {
|
||||
mocks.get.mockReset();
|
||||
mocks.list.mockReset();
|
||||
mocks.logger.error.mockClear();
|
||||
});
|
||||
|
||||
test("redacts backend lookup failures from TRPC output and logs", async () => {
|
||||
const apiKey = "workshop-lookup-secret";
|
||||
mocks.get.mockRejectedValueOnce(createTokenizedFailure(apiKey));
|
||||
|
||||
const error = await createCaller()
|
||||
.workshopGet({ submissionId: "submission-id" })
|
||||
.then(
|
||||
() => undefined,
|
||||
(cause: unknown) => cause,
|
||||
);
|
||||
|
||||
expectSafeFailure(error, apiKey, "Workshop widget lookup failed", "workshop_widget_lookup_failed");
|
||||
});
|
||||
|
||||
test("redacts backend search failures from TRPC output and logs", async () => {
|
||||
const apiKey = "workshop-search-secret";
|
||||
mocks.list.mockRejectedValueOnce(createTokenizedFailure(apiKey));
|
||||
|
||||
const error = await createCaller()
|
||||
.workshopSearch({ query: "weather" })
|
||||
.then(
|
||||
() => undefined,
|
||||
(cause: unknown) => cause,
|
||||
);
|
||||
|
||||
expectSafeFailure(error, apiKey, "Workshop widget search failed", "workshop_widget_search_failed");
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user