refactor: trim v2 release hardening
This commit is contained in:
1 parent
3cf1dc8341
commit
657704340c
79 files changed
+405
-3378
No files matched your search
@@ -17,6 +17,7 @@ coverage
|
||||
dev
|
||||
.build
|
||||
e2e
|
||||
apps/docs
|
||||
apps/nextjs/.next
|
||||
.env
|
||||
.env.*
|
||||
|
||||
@@ -71,10 +71,8 @@ HOMARR_WEBSITE_URL=https://homarr.dev
|
||||
# WORKSHOP_WEB_URL=https://homarr.dev/workshop
|
||||
|
||||
# Documentation and PocketBase deployment settings.
|
||||
# DOCS_BASE_URL=/
|
||||
# WORKSHOP_PUBLIC_ORIGIN=https://homarr.dev
|
||||
# PB_ALLOWED_ORIGINS=*
|
||||
# WORKSHOP_REQUIRE_OAUTH=false
|
||||
|
||||
# Local Workshop stack:
|
||||
# HOMARR_WEBSITE_URL=http://127.0.0.1:3003
|
||||
|
||||
+1
-1
@@ -37,7 +37,7 @@ RUN --mount=type=secret,id=TURBO_API,env=TURBO_API \
|
||||
--mount=type=secret,id=TURBO_TEAM,env=TURBO_TEAM \
|
||||
--mount=type=secret,id=TURBO_TOKEN,env=TURBO_TOKEN \
|
||||
--mount=type=secret,id=TURBO_REMOTE_CACHE_SIGNATURE_KEY,env=TURBO_REMOTE_CACHE_SIGNATURE_KEY \
|
||||
TURBO_PLATFORM="${TARGETPLATFORM:-linux/amd64}" pnpm turbo build --filter='!@homarr/docs'
|
||||
TURBO_PLATFORM="${TARGETPLATFORM:-linux/amd64}" pnpm build
|
||||
|
||||
FROM base AS runner
|
||||
WORKDIR /app
|
||||
|
||||
+2
-2
@@ -26,8 +26,8 @@ pnpm dev
|
||||
WORKSHOP_API_URL=http://127.0.0.1:8090 pnpm dev:docs
|
||||
```
|
||||
|
||||
The standalone docs use `WORKSHOP_API_URL` for PocketBase, `HOMARR_WEBSITE_URL` for the public site origin, and
|
||||
`DOCS_BASE_URL` for the deployment path. The old `WORKSHOP_URL` name is a deprecated one-release alias.
|
||||
The standalone docs use `WORKSHOP_API_URL` for PocketBase and `HOMARR_WEBSITE_URL` for the public site origin. The old
|
||||
`WORKSHOP_URL` name is a deprecated one-release alias.
|
||||
|
||||
## Build
|
||||
|
||||
|
||||
@@ -85,17 +85,16 @@ Restart `pnpm dev` after changing `.env`. GitHub OAuth is optional locally. See
|
||||
[`apps/workshop/README.md`](https://github.com/homarr-labs/homarr/blob/HEAD/apps/workshop/README.md) for OAuth,
|
||||
Workshop moderator promotion, logs, tests, backup, restore, stopping, and intentional volume reset.
|
||||
|
||||
### URL and base-path testing
|
||||
### URL testing
|
||||
|
||||
Docusaurus reads `HOMARR_WEBSITE_URL`, `WORKSHOP_API_URL`, `WORKSHOP_WEB_URL`, and `DOCS_BASE_URL` at startup/build.
|
||||
Homarr renders its public URL values into the page at server startup, so Docker overrides do not rely on
|
||||
`NEXT_PUBLIC_*` build variables. Test a separate API origin and non-root documentation path before release:
|
||||
Docusaurus reads `HOMARR_WEBSITE_URL` and `WORKSHOP_API_URL` when it starts or builds. Homarr also reads
|
||||
`WORKSHOP_WEB_URL` when its server starts, so these values do not rely on `NEXT_PUBLIC_*` build variables. Test a
|
||||
separate API origin before release:
|
||||
|
||||
```sh
|
||||
HOMARR_WEBSITE_URL=http://127.0.0.1:3003 \
|
||||
WORKSHOP_API_URL=http://127.0.0.1:8090 \
|
||||
WORKSHOP_WEB_URL=http://127.0.0.1:3003/docs-site/workshop \
|
||||
DOCS_BASE_URL=/docs-site/ \
|
||||
WORKSHOP_WEB_URL=http://127.0.0.1:3003/workshop \
|
||||
pnpm dev:docs
|
||||
```
|
||||
|
||||
|
||||
@@ -108,15 +108,12 @@ External redis is currently not supported with Proxmox Community Scripts
|
||||
The advanced deployments environment variables should only be used if you know what you are doing.
|
||||
:::
|
||||
|
||||
| Environment Variable | Description |
|
||||
| -------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `DB_MIGRATIONS_DISABLED` | Disable db migrations. For example for helm charts. |
|
||||
| `DOCS_BASE_URL` | Docusaurus deployment path. It must start with `/` and is normalized with a trailing slash. Default: `/`. |
|
||||
| `WORKSHOP_PUBLIC_ORIGIN` | Origin used in PocketBase-generated links and email. It must be set explicitly in production. |
|
||||
| `WORKSHOP_REQUIRE_PUBLIC_ORIGIN` | Require `WORKSHOP_PUBLIC_ORIGIN` at startup. The production Workshop image enables this automatically. |
|
||||
| `PB_ALLOWED_ORIGINS` | Browser origins accepted by PocketBase. The central public community defaults to `*` so self-hosted Homarr instances can use OAuth. |
|
||||
| `WORKSHOP_REQUIRE_OAUTH` | Fail Workshop production readiness when GitHub OAuth is disabled. Both GitHub OAuth credentials are always required together. |
|
||||
| Environment Variable | Description |
|
||||
| ------------------------ | ----------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `DB_MIGRATIONS_DISABLED` | Disable db migrations. For example for helm charts. |
|
||||
| `WORKSHOP_PUBLIC_ORIGIN` | Origin used in PocketBase-generated links and email. It must be set explicitly in production. |
|
||||
| `PB_ALLOWED_ORIGINS` | Browser origins accepted by PocketBase. The central public community defaults to `*` so self-hosted Homarr instances can use OAuth. |
|
||||
|
||||
All public URL variables reject non-HTTP protocols, embedded credentials, query strings, and fragments, and normalize
|
||||
trailing slashes at startup. The Docusaurus-only `WORKSHOP_URL` name is accepted as a deprecated alias for
|
||||
trailing slashes when Homarr or Docusaurus starts. The Docusaurus-only `WORKSHOP_URL` name is accepted as a deprecated alias for
|
||||
`WORKSHOP_API_URL` for one release. `WORKSHOP_API_URL` takes precedence.
|
||||
@@ -94,8 +94,8 @@ irreversible development reset.
|
||||
|
||||
GitHub needs only the central callback `https://<workshop-host>/api/oauth2-redirect`. Keep PocketBase origins unrestricted
|
||||
unless every self-hosted Homarr caller is explicitly listed. Configure both `GITHUB_CLIENT_ID` and
|
||||
`GITHUB_CLIENT_SECRET`; supplying only one is a startup error. Production also requires
|
||||
`WORKSHOP_PUBLIC_ORIGIN`. Configure SMTP for comment, report, and removal notices.
|
||||
`GITHUB_CLIENT_SECRET`; supplying only one is a startup error. Set `WORKSHOP_PUBLIC_ORIGIN` in production so generated
|
||||
links and email use the public host. Configure SMTP for comment, report, and removal notices.
|
||||
|
||||
The public URL contract is:
|
||||
|
||||
@@ -104,13 +104,11 @@ The public URL contract is:
|
||||
| `HOMARR_WEBSITE_URL` | Documentation/site base used by Homarr links | `https://homarr.dev` |
|
||||
| `WORKSHOP_API_URL` | PocketBase API used by Homarr and standalone docs | `HOMARR_WEBSITE_URL` |
|
||||
| `WORKSHOP_WEB_URL` | Public Workshop page linked from Homarr | `HOMARR_WEBSITE_URL/workshop` |
|
||||
| `DOCS_BASE_URL` | Docusaurus deployment path | `/` |
|
||||
| `WORKSHOP_PUBLIC_ORIGIN` | PocketBase-generated links and email | Required in production |
|
||||
| `PB_ALLOWED_ORIGINS` | PocketBase browser origins | `*` for the central community |
|
||||
|
||||
The deprecated docs-only `WORKSHOP_URL` alias remains for one release; `WORKSHOP_API_URL` takes precedence. Homarr
|
||||
server-renders runtime values for the browser, and the combined image writes a public runtime config at container
|
||||
startup. Restart services after changes.
|
||||
The deprecated docs-only `WORKSHOP_URL` alias remains for one release; `WORKSHOP_API_URL` takes precedence. Restart
|
||||
services after changing these values.
|
||||
|
||||
Back up application and PocketBase data before deployment. Publish the combined image by immutable digest, test the
|
||||
exact digest in staging, rehearse application rollback and PocketBase restore, then promote that same digest.
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import type * as Preset from "@docusaurus/preset-classic";
|
||||
import type { Config } from "@docusaurus/types";
|
||||
import { themes as prismThemes } from "prism-react-renderer";
|
||||
import { normalizeDocsBaseUrl, resolveHomarrUrlConfig } from "@homarr/workshop/schema";
|
||||
import { resolveHomarrUrlConfig } from "@homarr/workshop/schema";
|
||||
const a11yEmoji = require("@fec/remark-a11y-emoji");
|
||||
|
||||
const deprecatedWorkshopUrl = process.env.WORKSHOP_URL;
|
||||
@@ -12,24 +12,19 @@ if (deprecatedWorkshopUrl && !process.env.WORKSHOP_API_URL) {
|
||||
const publicUrls = resolveHomarrUrlConfig({
|
||||
homarrWebsiteUrl: process.env.HOMARR_WEBSITE_URL,
|
||||
workshopApiUrl: process.env.WORKSHOP_API_URL ?? deprecatedWorkshopUrl,
|
||||
workshopWebUrl: process.env.WORKSHOP_WEB_URL,
|
||||
});
|
||||
const docsBaseUrl = normalizeDocsBaseUrl(process.env.DOCS_BASE_URL);
|
||||
|
||||
const config: Config = {
|
||||
title: "Homarr documentation",
|
||||
tagline: "A simple yet powerful dashboard for your server.",
|
||||
url: publicUrls.homarrWebsiteUrl,
|
||||
baseUrl: docsBaseUrl,
|
||||
baseUrl: "/",
|
||||
trailingSlash: undefined,
|
||||
favicon: "img/logo.png",
|
||||
organizationName: "homarr-labs",
|
||||
projectName: "homarr",
|
||||
customFields: {
|
||||
workshopUrl: publicUrls.workshopApiUrl,
|
||||
workshopWebUrl: publicUrls.workshopWebUrl,
|
||||
},
|
||||
scripts: [{ src: `${docsBaseUrl}workshop-runtime-config.js` }],
|
||||
i18n: {
|
||||
defaultLocale: "en",
|
||||
locales: ["en"],
|
||||
|
||||
@@ -2,11 +2,9 @@ import React, { useCallback, useEffect, useMemo, useRef, useState } from "react"
|
||||
import BrowserOnly from "@docusaurus/BrowserOnly";
|
||||
import Link from "@docusaurus/Link";
|
||||
import { useLocation } from "@docusaurus/router";
|
||||
import useBaseUrl from "@docusaurus/useBaseUrl";
|
||||
import useDocusaurusContext from "@docusaurus/useDocusaurusContext";
|
||||
import Layout from "@theme/Layout";
|
||||
|
||||
import { getRuntimeWorkshopApiUrl } from "@site/src/lib/runtime-config";
|
||||
import {
|
||||
IconArrowBigDown,
|
||||
IconArrowBigUp,
|
||||
@@ -179,7 +177,6 @@ const WidgetSafetySummary = ({ widget }: { widget: HomarrCustomWidgetV2 }) => {
|
||||
|
||||
const MarketplaceDetail = ({ workshopUrl }: { workshopUrl: string }) => {
|
||||
const location = useLocation();
|
||||
const workshopHomeUrl = useBaseUrl("/workshop/");
|
||||
const submissionId = parseSubmissionId(location.pathname);
|
||||
const backend = useMemo(() => getWorkshopBackend(workshopUrl), [workshopUrl]);
|
||||
|
||||
@@ -329,7 +326,7 @@ const MarketplaceDetail = ({ workshopUrl }: { workshopUrl: string }) => {
|
||||
if (!submission) return;
|
||||
try {
|
||||
await backend.delete(submission.id);
|
||||
window.location.href = workshopHomeUrl;
|
||||
window.location.href = "/workshop/";
|
||||
} catch (caught) {
|
||||
setError(errorMessage(caught, "Failed to delete submission"));
|
||||
}
|
||||
@@ -1027,7 +1024,7 @@ export default function MarketplaceDetailPage() {
|
||||
<BrowserOnly fallback={<DetailSkeleton />}>
|
||||
{() => (
|
||||
<WorkshopErrorBoundary>
|
||||
<MarketplaceDetail workshopUrl={getRuntimeWorkshopApiUrl(configuredWorkshopUrl)} />
|
||||
<MarketplaceDetail workshopUrl={configuredWorkshopUrl || window.location.origin} />
|
||||
</WorkshopErrorBoundary>
|
||||
)}
|
||||
</BrowserOnly>
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { useCallback, useEffect, useMemo, useState } from "react";
|
||||
import Link from "@docusaurus/Link";
|
||||
import {
|
||||
IconBrandGithub,
|
||||
IconExternalLink,
|
||||
@@ -125,7 +124,7 @@ export function WorkshopAdmin({ workshopUrl }: { workshopUrl?: string }) {
|
||||
</p>
|
||||
</div>
|
||||
<div className="flex flex-wrap gap-2">
|
||||
<Button variant="outline" nativeButton={false} render={<Link to="/workshop" aria-label="Back to Workshop" />}>
|
||||
<Button variant="outline" nativeButton={false} render={<a href="/workshop" aria-label="Back to Workshop" />}>
|
||||
Back to Workshop
|
||||
</Button>
|
||||
{!user && (
|
||||
@@ -266,7 +265,7 @@ export function WorkshopAdmin({ workshopUrl }: { workshopUrl?: string }) {
|
||||
variant="outline"
|
||||
size="sm"
|
||||
nativeButton={false}
|
||||
render={<Link to={`/workshop/${submission.id}`} aria-label={`Inspect ${submission.title}`} />}
|
||||
render={<a href={`/workshop/${submission.id}`} aria-label={`Inspect ${submission.title}`} />}
|
||||
>
|
||||
<IconExternalLink size={14} /> Inspect submission
|
||||
</Button>
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import React, { useMemo, useState } from "react";
|
||||
import Link from "@docusaurus/Link";
|
||||
import {
|
||||
IconAlertCircle,
|
||||
IconArrowBigDown,
|
||||
@@ -324,7 +323,7 @@ const SubmissionCard = ({ submission, backend, userVote, onVote }: SubmissionCar
|
||||
|
||||
return (
|
||||
<Card className="relative flex h-full min-w-0 w-full flex-col">
|
||||
<Link to={`/workshop/${submission.id}/`} className="block shrink-0">
|
||||
<a href={`/workshop/${submission.id}/`} className="block shrink-0">
|
||||
{hasScreenshots ? (
|
||||
<div className="relative">
|
||||
<Badge
|
||||
@@ -341,13 +340,13 @@ const SubmissionCard = ({ submission, backend, userVote, onVote }: SubmissionCar
|
||||
<TypeIcon size={32} className="text-muted-foreground/20" />
|
||||
</div>
|
||||
)}
|
||||
</Link>
|
||||
</a>
|
||||
|
||||
<CardHeader className="flex flex-col gap-2 sm:grid">
|
||||
<div className="flex items-center gap-2">
|
||||
<Link to={`/workshop/${submission.id}/`} className="min-w-0 hover:underline" title={submission.title}>
|
||||
<a href={`/workshop/${submission.id}/`} className="min-w-0 hover:underline" title={submission.title}>
|
||||
<CardTitle className="truncate">{submission.title}</CardTitle>
|
||||
</Link>
|
||||
</a>
|
||||
{!hasScreenshots && (
|
||||
<Badge variant="secondary" className="shrink-0 gap-1.5 px-2">
|
||||
<span className={cn("size-2 rounded-full", typeDotColors[submission.type])} />
|
||||
@@ -424,7 +423,7 @@ const SubmissionCard = ({ submission, backend, userVote, onVote }: SubmissionCar
|
||||
className="h-10 sm:h-7"
|
||||
size="sm"
|
||||
nativeButton={false}
|
||||
render={<Link to={`/workshop/${submission.id}/`} aria-label={`View ${submission.title} details`} />}
|
||||
render={<a href={`/workshop/${submission.id}/`} aria-label={`View ${submission.title} details`} />}
|
||||
>
|
||||
<IconEye size={14} /> View details
|
||||
</Button>
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
interface HomarrRuntimeConfig {
|
||||
homarrWebsiteUrl?: string;
|
||||
workshopApiUrl?: string;
|
||||
workshopWebUrl?: string;
|
||||
}
|
||||
|
||||
declare global {
|
||||
interface Window {
|
||||
homarrRuntimeConfig?: Readonly<HomarrRuntimeConfig>;
|
||||
}
|
||||
}
|
||||
|
||||
export const getRuntimeWorkshopApiUrl = (configuredUrl: string): string => {
|
||||
if (typeof window === "undefined") return configuredUrl;
|
||||
return window.homarrRuntimeConfig?.workshopApiUrl || configuredUrl || window.location.origin;
|
||||
};
|
||||
|
||||
export const getRuntimeWorkshopWebUrl = (configuredUrl: string): string => {
|
||||
if (typeof window === "undefined") return configuredUrl;
|
||||
return window.homarrRuntimeConfig?.workshopWebUrl || configuredUrl || `${window.location.origin}/workshop`;
|
||||
};
|
||||
@@ -1,19 +1,14 @@
|
||||
import React from "react";
|
||||
import BrowserOnly from "@docusaurus/BrowserOnly";
|
||||
import useDocusaurusContext from "@docusaurus/useDocusaurusContext";
|
||||
import Layout from "@theme/Layout";
|
||||
|
||||
import { WorkshopAdmin } from "@site/src/components/workshop/WorkshopAdmin";
|
||||
import { getRuntimeWorkshopApiUrl } from "@site/src/lib/runtime-config";
|
||||
|
||||
export default function WorkshopAdminRoute() {
|
||||
const { siteConfig } = useDocusaurusContext();
|
||||
const workshopUrl = (siteConfig.customFields?.workshopUrl as string | undefined) ?? "";
|
||||
return (
|
||||
<Layout title="Workshop moderation" noFooter>
|
||||
<BrowserOnly fallback={null}>
|
||||
{() => <WorkshopAdmin workshopUrl={getRuntimeWorkshopApiUrl(workshopUrl)} />}
|
||||
</BrowserOnly>
|
||||
<WorkshopAdmin workshopUrl={workshopUrl} />
|
||||
</Layout>
|
||||
);
|
||||
}
|
||||
@@ -5,7 +5,6 @@ import Layout from "@theme/Layout";
|
||||
|
||||
import { WorkshopApp, WorkshopListingFallback } from "@site/src/components/workshop/WorkshopApp";
|
||||
import { WorkshopErrorBoundary } from "@site/src/components/workshop/WorkshopErrorBoundary";
|
||||
import { getRuntimeWorkshopApiUrl } from "@site/src/lib/runtime-config";
|
||||
|
||||
export default function WorkshopPage() {
|
||||
const { siteConfig } = useDocusaurusContext();
|
||||
@@ -17,7 +16,7 @@ export default function WorkshopPage() {
|
||||
<BrowserOnly fallback={<WorkshopListingFallback />}>
|
||||
{() => (
|
||||
<WorkshopErrorBoundary>
|
||||
<WorkshopApp workshopUrl={getRuntimeWorkshopApiUrl(configuredWorkshopUrl)} />
|
||||
<WorkshopApp workshopUrl={configuredWorkshopUrl || window.location.origin} />
|
||||
</WorkshopErrorBoundary>
|
||||
)}
|
||||
</BrowserOnly>
|
||||
|
||||
-1
@@ -1 +0,0 @@
|
||||
globalThis.homarrRuntimeConfig = Object.freeze({});
|
||||
@@ -148,7 +148,6 @@ export default async function Layout(props: {
|
||||
<head>
|
||||
<meta name="homarr-website-url" content={publicUrls.homarrWebsiteUrl} />
|
||||
<meta name="homarr-workshop-api-url" content={publicUrls.workshopApiUrl} />
|
||||
<meta name="homarr-workshop-web-url" content={publicUrls.workshopWebUrl} />
|
||||
<SearchEngineOptimization />
|
||||
<CrowdinLiveTranslation locale={locale} />
|
||||
</head>
|
||||
|
||||
@@ -5,7 +5,7 @@ import { createOpenApiFetchHandler } from "trpc-to-openapi";
|
||||
import { createTRPCContext } from "@homarr/api";
|
||||
import { openApiRouter } from "@homarr/api/open-api";
|
||||
import { API_KEY_HEADER_NAME, getSessionFromApiKeyAsync } from "@homarr/auth/api-key";
|
||||
import { rateLimitAddressFromHeaders } from "@homarr/common/server";
|
||||
import { ipAddressFromHeaders } from "@homarr/common/server";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
import { ErrorWithMetadata } from "@homarr/core/infrastructure/logs/error";
|
||||
import { db } from "@homarr/db";
|
||||
@@ -14,7 +14,7 @@ const logger = createLogger({ module: "trpcOpenApiRoute" });
|
||||
|
||||
const handlerAsync = async (req: NextRequest) => {
|
||||
const apiKeyHeaderValue = req.headers.get(API_KEY_HEADER_NAME);
|
||||
const ipAddress = rateLimitAddressFromHeaders(req.headers);
|
||||
const ipAddress = ipAddressFromHeaders(req.headers);
|
||||
const { ua } = userAgent(req);
|
||||
|
||||
logger.info(
|
||||
|
||||
@@ -7,10 +7,10 @@ import {
|
||||
configureCustomWidgetSourceFromRequest,
|
||||
configurePreviewSessionSource,
|
||||
getCustomWidgetConfigurationRequest,
|
||||
invalidateCustomWidgetResponseCache,
|
||||
releaseCustomWidgetConfigurationRequest,
|
||||
} from "@homarr/api/custom-widget-configuration";
|
||||
import { customWidgetSourceSchema } from "@homarr/custom-widgets/core";
|
||||
import { invalidateCustomWidgetResponseCache } from "@homarr/custom-widgets/server";
|
||||
import { db } from "@homarr/db";
|
||||
|
||||
import { requireCustomWidgetAdmin } from "../../admin";
|
||||
@@ -94,7 +94,7 @@ export async function POST(request: NextRequest, context: RouteContext) {
|
||||
sourceResult.data,
|
||||
secrets,
|
||||
);
|
||||
await invalidateCustomWidgetResponseCache([`custom-jsx:preview:${claimed.target.id}:`]);
|
||||
invalidateCustomWidgetResponseCache([`custom-jsx:preview:${claimed.target.id}:`]);
|
||||
} else {
|
||||
const result = await configureCustomWidgetSourceFromRequest(db, {
|
||||
definitionId: claimed.target.id,
|
||||
|
||||
@@ -4,7 +4,7 @@ import { beforeEach, describe, expect, test, vi } from "vitest";
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
authenticate: vi.fn(),
|
||||
rateLimitAddress: vi.fn(() => "127.0.0.1"),
|
||||
ipAddress: vi.fn(() => "127.0.0.1"),
|
||||
toolProcedure: vi.fn(),
|
||||
loggerWarn: vi.fn(),
|
||||
buildPrompt: vi.fn((request?: string, documentationUrl?: string) =>
|
||||
@@ -23,7 +23,7 @@ vi.mock("@homarr/auth/api-key", () => ({
|
||||
getSessionFromApiKeyAsync: mocks.authenticate,
|
||||
}));
|
||||
vi.mock("@homarr/common", () => ({ extractBaseUrlFromHeaders: () => "http://localhost" }));
|
||||
vi.mock("@homarr/common/server", () => ({ rateLimitAddressFromHeaders: mocks.rateLimitAddress }));
|
||||
vi.mock("@homarr/common/server", () => ({ ipAddressFromHeaders: mocks.ipAddress }));
|
||||
vi.mock("@homarr/core/infrastructure/logs", () => ({
|
||||
createLogger: () => ({ info: vi.fn(), warn: mocks.loggerWarn, error: vi.fn() }),
|
||||
}));
|
||||
@@ -75,8 +75,8 @@ beforeEach(() => {
|
||||
mocks.authenticate.mockResolvedValue({ user: { id: "user-1", permissions: ["admin"] } });
|
||||
mocks.buildPrompt.mockClear();
|
||||
mocks.getComponent.mockClear();
|
||||
mocks.rateLimitAddress.mockClear();
|
||||
mocks.rateLimitAddress.mockReturnValue("127.0.0.1");
|
||||
mocks.ipAddress.mockClear();
|
||||
mocks.ipAddress.mockReturnValue("127.0.0.1");
|
||||
mocks.toolProcedure.mockReset();
|
||||
mocks.toolProcedure.mockResolvedValue([]);
|
||||
mocks.loggerWarn.mockClear();
|
||||
@@ -122,7 +122,7 @@ describe("authenticated MCP prompt protocol", () => {
|
||||
serverInfo: { name: "homarr", version: "test-version" },
|
||||
});
|
||||
expect(mocks.authenticate).toHaveBeenCalledWith({}, "key.secret", "127.0.0.1", "MCP route test");
|
||||
expect(mocks.rateLimitAddress).toHaveBeenCalledWith(expect.any(Headers));
|
||||
expect(mocks.ipAddress).toHaveBeenCalledWith(expect.any(Headers));
|
||||
});
|
||||
|
||||
test("lists and interpolates the custom-widget authoring prompt", async () => {
|
||||
|
||||
@@ -16,7 +16,7 @@ import {
|
||||
import { createTRPCContext, mcpRouter } from "@homarr/api/mcp";
|
||||
import { API_KEY_HEADER_NAME, getSessionFromApiKeyAsync } from "@homarr/auth/api-key";
|
||||
import { extractBaseUrlFromHeaders } from "@homarr/common";
|
||||
import { rateLimitAddressFromHeaders } from "@homarr/common/server";
|
||||
import { ipAddressFromHeaders } from "@homarr/common/server";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
import { buildCustomWidgetMcpPrompt } from "@homarr/custom-widgets/authoring-prompt";
|
||||
import {
|
||||
@@ -390,7 +390,7 @@ function jsonErrorResponse(status: number, body: Record<string, string>, extraHe
|
||||
|
||||
const handler = async (req: NextRequest) => {
|
||||
const apiKeyValue = extractApiKeyValue(req);
|
||||
const ipAddress = rateLimitAddressFromHeaders(req.headers);
|
||||
const ipAddress = ipAddressFromHeaders(req.headers);
|
||||
const { ua } = userAgent(req);
|
||||
|
||||
if (!checkAuthRateLimit(ipAddress)) {
|
||||
|
||||
@@ -5,7 +5,7 @@ import { appRouter, createTRPCContext } from "@homarr/api";
|
||||
import { trpcPath } from "@homarr/api/shared";
|
||||
import { API_KEY_HEADER_NAME, getSessionFromApiKeyAsync } from "@homarr/auth/api-key";
|
||||
import { auth } from "@homarr/auth/next";
|
||||
import { rateLimitAddressFromHeaders } from "@homarr/common/server";
|
||||
import { ipAddressFromHeaders } from "@homarr/common/server";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
import { ErrorWithMetadata } from "@homarr/core/infrastructure/logs/error";
|
||||
import { db } from "@homarr/db";
|
||||
@@ -34,7 +34,7 @@ export function OPTIONS() {
|
||||
const handler = auth(async (req) => {
|
||||
// Try API key auth first, fall back to session cookie
|
||||
const apiKeyHeader = req.headers.get(API_KEY_HEADER_NAME);
|
||||
const ipAddress = rateLimitAddressFromHeaders(req.headers);
|
||||
const ipAddress = ipAddressFromHeaders(req.headers);
|
||||
|
||||
const { ua } = userAgent(req);
|
||||
|
||||
|
||||
@@ -6,8 +6,6 @@ WORKSHOP_PUBLIC_ORIGIN=http://127.0.0.1:3003
|
||||
HOMARR_WEBSITE_URL=http://127.0.0.1:3003
|
||||
WORKSHOP_API_URL=http://127.0.0.1:8090
|
||||
WORKSHOP_WEB_URL=http://127.0.0.1:3003/workshop
|
||||
DOCS_BASE_URL=/
|
||||
WORKSHOP_REQUIRE_OAUTH=false
|
||||
|
||||
# Optional local GitHub OAuth application.
|
||||
# GITHUB_CLIENT_ID=
|
||||
|
||||
@@ -13,48 +13,28 @@ COPY packages/custom-widgets ./packages/custom-widgets
|
||||
COPY packages/definitions ./packages/definitions
|
||||
COPY packages/workshop ./packages/workshop
|
||||
COPY tooling/typescript ./tooling/typescript
|
||||
RUN pnpm config set store-dir /pnpm/store && pnpm install --filter @homarr/docs... --frozen-lockfile
|
||||
RUN --mount=type=cache,id=workshop-pnpm-store,target=/pnpm/store pnpm config set store-dir /pnpm/store && pnpm install --filter @homarr/docs... --frozen-lockfile
|
||||
|
||||
FROM dependencies AS website
|
||||
ARG HOMARR_WEBSITE_URL=https://homarr.dev
|
||||
ARG WORKSHOP_API_URL=https://homarr.dev
|
||||
ARG WORKSHOP_WEB_URL=https://homarr.dev/workshop
|
||||
ARG DOCS_BASE_URL=/
|
||||
ENV HOMARR_WEBSITE_URL=$HOMARR_WEBSITE_URL
|
||||
ENV WORKSHOP_API_URL=$WORKSHOP_API_URL
|
||||
ENV WORKSHOP_WEB_URL=$WORKSHOP_WEB_URL
|
||||
ENV DOCS_BASE_URL=$DOCS_BASE_URL
|
||||
RUN CI=true SKIP_ENV_VALIDATION=true pnpm turbo build --filter=@homarr/docs \
|
||||
&& mkdir -p /app/workshop-public \
|
||||
&& docs_path="$(printf '%s' "$DOCS_BASE_URL" | sed 's#^/*##; s#/*$##')" \
|
||||
&& if [ -z "$docs_path" ]; then \
|
||||
cp -R /app/apps/docs/build/. /app/workshop-public/; \
|
||||
else \
|
||||
mkdir -p "/app/workshop-public/$docs_path" \
|
||||
&& cp -R /app/apps/docs/build/. "/app/workshop-public/$docs_path/" \
|
||||
&& cp /app/apps/docs/build/index.html /app/workshop-public/index.html; \
|
||||
fi
|
||||
RUN CI=true SKIP_ENV_VALIDATION=true pnpm turbo build --filter=@homarr/docs
|
||||
|
||||
FROM ${POCKETBASE_IMAGE} AS pocketbase-runtime
|
||||
ENV PORT=8090
|
||||
RUN addgroup -S pocketbase && adduser -S -G pocketbase pocketbase
|
||||
COPY apps/workshop/entrypoint.sh /usr/local/bin/workshop-entrypoint
|
||||
COPY apps/workshop/workshop-url.awk /usr/local/lib/workshop-url.awk
|
||||
RUN chmod 755 /usr/local/bin/workshop-entrypoint \
|
||||
&& chmod 644 /usr/local/lib/workshop-url.awk \
|
||||
&& mkdir -p /pb_data /pb_public \
|
||||
&& chown pocketbase:pocketbase /pb_data /pb_public
|
||||
RUN chmod +x /usr/local/bin/workshop-entrypoint && mkdir -p /pb_data /pb_public && chown pocketbase:pocketbase /pb_data
|
||||
ENTRYPOINT ["workshop-entrypoint"]
|
||||
|
||||
FROM pocketbase-runtime AS development
|
||||
COPY --chown=pocketbase:pocketbase apps/workshop/pb_migrations /pb_migrations
|
||||
COPY --chown=pocketbase:pocketbase apps/workshop/pb_hooks /pb_hooks
|
||||
EXPOSE 8090
|
||||
HEALTHCHECK --interval=10s --timeout=5s --start-period=15s --retries=6 CMD wget -q --spider "http://127.0.0.1:${PORT}/api/health" || exit 1
|
||||
|
||||
FROM pocketbase-runtime AS production
|
||||
ENV WORKSHOP_REQUIRE_PUBLIC_ORIGIN=true
|
||||
COPY --from=website /app/workshop-public /pb_public
|
||||
COPY --from=website /app/apps/docs/build /pb_public
|
||||
COPY apps/workshop/pb_migrations /pb_migrations
|
||||
COPY apps/workshop/pb_hooks /pb_hooks
|
||||
RUN chown -R pocketbase:pocketbase /pb_hooks /pb_migrations /pb_public
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
.git
|
||||
.next
|
||||
.turbo
|
||||
**/node_modules
|
||||
**/pb_data
|
||||
apps/docs/build
|
||||
coverage
|
||||
@@ -69,8 +69,8 @@ pnpm dev
|
||||
```
|
||||
|
||||
`WORKSHOP_API_URL` controls API calls. `WORKSHOP_WEB_URL` controls links from Homarr. `HOMARR_WEBSITE_URL` is the
|
||||
documentation/site base. All three must be absolute HTTP(S) URLs. `DOCS_BASE_URL` configures a Docusaurus path such as
|
||||
`/docs-site/`. The old Docusaurus-only `WORKSHOP_URL` variable remains a deprecated alias for one release.
|
||||
documentation/site base. All three must be absolute HTTP(S) URLs. The old Docusaurus-only `WORKSHOP_URL` variable
|
||||
remains a deprecated alias for one release.
|
||||
|
||||
## Optional GitHub OAuth
|
||||
|
||||
@@ -158,7 +158,6 @@ WORKSHOP_API_URL=https://homarr.dev
|
||||
WORKSHOP_WEB_URL=https://homarr.dev/workshop
|
||||
WORKSHOP_PUBLIC_ORIGIN=https://homarr.dev
|
||||
PB_ALLOWED_ORIGINS=*
|
||||
WORKSHOP_REQUIRE_OAUTH=true
|
||||
```
|
||||
|
||||
Build and smoke-test the combined image locally:
|
||||
|
||||
@@ -10,17 +10,16 @@ services:
|
||||
environment:
|
||||
PB_ALLOWED_ORIGINS: ${PB_ALLOWED_ORIGINS:-*}
|
||||
PORT: ${PB_EXPOSE_PORT:-8090}
|
||||
HOMARR_WEBSITE_URL: ${HOMARR_WEBSITE_URL:-http://localhost:3003}
|
||||
WORKSHOP_API_URL: ${WORKSHOP_API_URL:-http://localhost:8090}
|
||||
WORKSHOP_WEB_URL: ${WORKSHOP_WEB_URL:-http://localhost:3003/workshop}
|
||||
GITHUB_CLIENT_ID: ${GITHUB_CLIENT_ID:-}
|
||||
GITHUB_CLIENT_SECRET: ${GITHUB_CLIENT_SECRET:-}
|
||||
WORKSHOP_REQUIRE_OAUTH: ${WORKSHOP_REQUIRE_OAUTH:-false}
|
||||
WORKSHOP_PUBLIC_ORIGIN: ${WORKSHOP_PUBLIC_ORIGIN:-http://localhost:3003}
|
||||
ports:
|
||||
- "127.0.0.1:${PB_EXPOSE_PORT:-8090}:${PB_EXPOSE_PORT:-8090}"
|
||||
volumes:
|
||||
- pb_data:/pb_data
|
||||
- ./pb_hooks:/pb_hooks:ro
|
||||
- ./pb_migrations:/pb_migrations:ro
|
||||
security_opt:
|
||||
- no-new-privileges:true
|
||||
|
||||
@@ -34,18 +33,12 @@ services:
|
||||
args:
|
||||
HOMARR_WEBSITE_URL: ${HOMARR_WEBSITE_URL:-http://localhost:3003}
|
||||
WORKSHOP_API_URL: ${WORKSHOP_API_URL:-http://localhost:3003}
|
||||
WORKSHOP_WEB_URL: ${WORKSHOP_WEB_URL:-http://localhost:3003/workshop}
|
||||
DOCS_BASE_URL: ${DOCS_BASE_URL:-/}
|
||||
environment:
|
||||
PB_ALLOWED_ORIGINS: ${PB_ALLOWED_ORIGINS:-*}
|
||||
PORT: ${DOCS_EXPOSE_PORT:-3003}
|
||||
HOMARR_WEBSITE_URL: ${HOMARR_WEBSITE_URL:-http://localhost:3003}
|
||||
WORKSHOP_API_URL: ${WORKSHOP_API_URL:-http://localhost:3003}
|
||||
WORKSHOP_WEB_URL: ${WORKSHOP_WEB_URL:-http://localhost:3003/workshop}
|
||||
DOCS_BASE_URL: ${DOCS_BASE_URL:-/}
|
||||
GITHUB_CLIENT_ID: ${GITHUB_CLIENT_ID:-}
|
||||
GITHUB_CLIENT_SECRET: ${GITHUB_CLIENT_SECRET:-}
|
||||
WORKSHOP_REQUIRE_OAUTH: ${WORKSHOP_REQUIRE_OAUTH:-false}
|
||||
WORKSHOP_PUBLIC_ORIGIN: ${WORKSHOP_PUBLIC_ORIGIN:-http://localhost:3003}
|
||||
ports:
|
||||
- "127.0.0.1:${DOCS_EXPOSE_PORT:-3003}:${DOCS_EXPOSE_PORT:-3003}"
|
||||
|
||||
@@ -1,72 +1,11 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
normalize_http_url() {
|
||||
variable_name=$1
|
||||
value=$2
|
||||
printf '%s\n' "$value" | awk \
|
||||
-v variable_name="$variable_name" \
|
||||
-f "${WORKSHOP_URL_VALIDATOR_PATH:-/usr/local/lib/workshop-url.awk}"
|
||||
}
|
||||
|
||||
normalize_docs_base_url() {
|
||||
value=$1
|
||||
case "$value" in
|
||||
/*) ;;
|
||||
*)
|
||||
echo "DOCS_BASE_URL must start with '/'" >&2
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
case "${value%/}/" in
|
||||
*[!A-Za-z0-9._~/-]* | */./* | */../*)
|
||||
echo "DOCS_BASE_URL must be a safe URL path without traversal" >&2
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
printf '/%s\n' "$(printf '%s' "$value" | sed 's#^/*##; s#/*$##')"
|
||||
}
|
||||
|
||||
if [ "${1:-}" = "--validate-url" ]; then
|
||||
normalize_http_url "${2:-URL}" "${3:-}"
|
||||
exit
|
||||
fi
|
||||
|
||||
if [ "${1:-}" = "--validate-docs-base-url" ]; then
|
||||
normalize_docs_base_url "${2:-}"
|
||||
exit
|
||||
fi
|
||||
|
||||
HOMARR_WEBSITE_URL=$(normalize_http_url HOMARR_WEBSITE_URL "${HOMARR_WEBSITE_URL:-https://homarr.dev}")
|
||||
WORKSHOP_API_URL=$(normalize_http_url WORKSHOP_API_URL "${WORKSHOP_API_URL:-$HOMARR_WEBSITE_URL}")
|
||||
WORKSHOP_WEB_URL=$(normalize_http_url WORKSHOP_WEB_URL "${WORKSHOP_WEB_URL:-$HOMARR_WEBSITE_URL/workshop}")
|
||||
if [ "${WORKSHOP_REQUIRE_PUBLIC_ORIGIN:-false}" = "true" ] && [ -z "${WORKSHOP_PUBLIC_ORIGIN:-}" ]; then
|
||||
echo "WORKSHOP_PUBLIC_ORIGIN must be set explicitly in production" >&2
|
||||
exit 1
|
||||
fi
|
||||
if [ -n "${WORKSHOP_PUBLIC_ORIGIN:-}" ]; then
|
||||
WORKSHOP_PUBLIC_ORIGIN=$(normalize_http_url WORKSHOP_PUBLIC_ORIGIN "$WORKSHOP_PUBLIC_ORIGIN")
|
||||
fi
|
||||
export HOMARR_WEBSITE_URL WORKSHOP_API_URL WORKSHOP_WEB_URL WORKSHOP_PUBLIC_ORIGIN
|
||||
|
||||
if [ "$(id -u)" -eq 0 ]; then
|
||||
chown -R pocketbase:pocketbase /pb_data
|
||||
exec su -p -s /bin/sh pocketbase -c 'exec /usr/local/bin/workshop-entrypoint'
|
||||
fi
|
||||
|
||||
if [ -d /pb_public ]; then
|
||||
docs_base_url=$(normalize_docs_base_url "${DOCS_BASE_URL:-/}")
|
||||
runtime_directory="/pb_public${docs_base_url%/}"
|
||||
mkdir -p "$runtime_directory"
|
||||
cat >"$runtime_directory/workshop-runtime-config.js" <<EOF
|
||||
globalThis.homarrRuntimeConfig = Object.freeze({
|
||||
homarrWebsiteUrl: "$HOMARR_WEBSITE_URL",
|
||||
workshopApiUrl: "$WORKSHOP_API_URL",
|
||||
workshopWebUrl: "$WORKSHOP_WEB_URL"
|
||||
});
|
||||
EOF
|
||||
fi
|
||||
|
||||
exec pocketbase serve \
|
||||
--http="0.0.0.0:${PORT:-8090}" \
|
||||
--origins="${PB_ALLOWED_ORIGINS:-*}" \
|
||||
|
||||
@@ -3,26 +3,17 @@
|
||||
onBootstrap((event) => {
|
||||
const clientId = String($os.getenv("GITHUB_CLIENT_ID") || "").trim();
|
||||
const clientSecret = String($os.getenv("GITHUB_CLIENT_SECRET") || "").trim();
|
||||
const requireOAuth = ["1", "true", "yes", "on"].includes(
|
||||
String($os.getenv("WORKSHOP_REQUIRE_OAUTH") || "")
|
||||
.trim()
|
||||
.toLowerCase(),
|
||||
);
|
||||
if (Boolean(clientId) !== Boolean(clientSecret)) {
|
||||
console.log(JSON.stringify({ event: "workshop_oauth_configuration_rejected", reason: "partial_credentials" }));
|
||||
throw new Error("GitHub OAuth requires both GITHUB_CLIENT_ID and GITHUB_CLIENT_SECRET");
|
||||
}
|
||||
const configured = Boolean(clientId && clientSecret);
|
||||
if (requireOAuth && !configured) {
|
||||
console.log(JSON.stringify({ event: "workshop_oauth_configuration_rejected", reason: "required_but_disabled" }));
|
||||
throw new Error("GitHub OAuth is required but no credentials were configured");
|
||||
}
|
||||
event.next();
|
||||
const users = event.app.findCollectionByNameOrId("users");
|
||||
users.oauth2.enabled = configured;
|
||||
users.oauth2.providers = configured ? [{ name: "github", clientId, clientSecret }] : [];
|
||||
event.app.save(users);
|
||||
console.log(JSON.stringify({ event: "workshop_oauth_synchronized", enabled: configured, required: requireOAuth }));
|
||||
console.log(JSON.stringify({ event: "workshop_oauth_synchronized", enabled: configured }));
|
||||
});
|
||||
|
||||
onRecordAuthWithOAuth2Request((event) => {
|
||||
|
||||
+4
-133
@@ -3,67 +3,17 @@ set -eu
|
||||
|
||||
WORKSHOP_TEST_PORT=${WORKSHOP_TEST_PORT:-18090}
|
||||
WORKSHOP_TEST_PROJECT="homarr-workshop-test-$$"
|
||||
WORKSHOP_ROLLBACK_PORT=${WORKSHOP_ROLLBACK_PORT:-$((WORKSHOP_TEST_PORT + 1))}
|
||||
WORKSHOP_ROLLBACK_PROJECT="homarr-workshop-rollback-$$"
|
||||
WORKSHOP_ROLLBACK_SNAPSHOT="/tmp/$WORKSHOP_ROLLBACK_PROJECT.json"
|
||||
WORKSHOP_MIGRATION_COUNT=$(
|
||||
node -e "const fs = require('node:fs'); console.log(fs.readdirSync('apps/workshop/pb_migrations').filter((name) => name.endsWith('.js')).length)"
|
||||
)
|
||||
ROLLBACK_CONTAINER=""
|
||||
|
||||
node apps/workshop/tests/workshop-contracts.mjs
|
||||
node apps/workshop/tests/workshop-migration.mjs
|
||||
node apps/workshop/tests/entrypoint-url.mjs
|
||||
node apps/workshop/tests/workshop-identity.mjs
|
||||
node apps/workshop/tests/workshop-oauth-hook.mjs
|
||||
|
||||
cleanup() {
|
||||
if [ -n "$ROLLBACK_CONTAINER" ]; then
|
||||
docker stop "$ROLLBACK_CONTAINER" >/dev/null 2>&1 || true
|
||||
fi
|
||||
docker compose -p "$WORKSHOP_ROLLBACK_PROJECT" \
|
||||
-f apps/workshop/docker-compose.yml down --volumes --remove-orphans >/dev/null 2>&1 || true
|
||||
PB_EXPOSE_PORT="$WORKSHOP_TEST_PORT" docker compose -p "$WORKSHOP_TEST_PROJECT" \
|
||||
-f apps/workshop/docker-compose.yml down --volumes --remove-orphans >/dev/null 2>&1 || true
|
||||
rm -f "$WORKSHOP_ROLLBACK_SNAPSHOT"
|
||||
}
|
||||
trap cleanup EXIT
|
||||
|
||||
wait_for_workshop() {
|
||||
port=$1
|
||||
for attempt in $(seq 1 60); do
|
||||
if curl --fail --silent "http://127.0.0.1:$port/api/health" >/dev/null; then
|
||||
return
|
||||
fi
|
||||
if [ "$attempt" -eq 60 ]; then
|
||||
return 1
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
}
|
||||
|
||||
start_rollback_server() {
|
||||
ROLLBACK_CONTAINER=$(
|
||||
PB_EXPOSE_PORT="$WORKSHOP_ROLLBACK_PORT" docker compose -p "$WORKSHOP_ROLLBACK_PROJECT" \
|
||||
-f apps/workshop/docker-compose.yml run --rm -d --service-ports --no-deps \
|
||||
--entrypoint pocketbase workshop \
|
||||
serve --http="0.0.0.0:$WORKSHOP_ROLLBACK_PORT" --origins="*" --dir=/pb_data \
|
||||
--hooksDir=/tmp --migrationsDir=/tmp --publicDir=/pb_public --automigrate=false
|
||||
)
|
||||
wait_for_workshop "$WORKSHOP_ROLLBACK_PORT"
|
||||
}
|
||||
|
||||
stop_rollback_server() {
|
||||
docker stop "$ROLLBACK_CONTAINER" >/dev/null
|
||||
ROLLBACK_CONTAINER=""
|
||||
}
|
||||
|
||||
run_rollback_assertion() {
|
||||
WORKSHOP_TEST_URL="http://127.0.0.1:$WORKSHOP_ROLLBACK_PORT" \
|
||||
WORKSHOP_ROLLBACK_SNAPSHOT="$WORKSHOP_ROLLBACK_SNAPSHOT" \
|
||||
node apps/workshop/tests/workshop-rollback.integration.mjs "$1"
|
||||
}
|
||||
|
||||
PB_EXPOSE_PORT="$WORKSHOP_TEST_PORT" docker compose -p "$WORKSHOP_TEST_PROJECT" \
|
||||
-f apps/workshop/docker-compose.yml up --build -d
|
||||
|
||||
@@ -101,90 +51,11 @@ for client_id in workshop-test-client workshop-rotated-client; do
|
||||
node apps/workshop/tests/oauth-sync.integration.mjs
|
||||
done
|
||||
|
||||
GITHUB_CLIENT_ID="workshop-partial-client" GITHUB_CLIENT_SECRET="" WORKSHOP_REQUIRE_OAUTH=false \
|
||||
PB_EXPOSE_PORT="$WORKSHOP_TEST_PORT" docker compose -p "$WORKSHOP_TEST_PROJECT" \
|
||||
-f apps/workshop/docker-compose.yml up -d --force-recreate workshop
|
||||
partial_rejected=false
|
||||
for attempt in $(seq 1 30); do
|
||||
if PB_EXPOSE_PORT="$WORKSHOP_TEST_PORT" docker compose -p "$WORKSHOP_TEST_PROJECT" \
|
||||
-f apps/workshop/docker-compose.yml logs --no-color workshop | grep -q "partial_credentials"; then
|
||||
partial_rejected=true
|
||||
break
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
if [ "$partial_rejected" != true ]; then
|
||||
echo "Workshop accepted a partial GitHub OAuth configuration"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
GITHUB_CLIENT_ID="" GITHUB_CLIENT_SECRET="" WORKSHOP_REQUIRE_OAUTH=true PB_EXPOSE_PORT="$WORKSHOP_TEST_PORT" \
|
||||
docker compose -p "$WORKSHOP_TEST_PROJECT" -f apps/workshop/docker-compose.yml \
|
||||
up -d --force-recreate workshop
|
||||
required_rejected=false
|
||||
for attempt in $(seq 1 30); do
|
||||
if PB_EXPOSE_PORT="$WORKSHOP_TEST_PORT" docker compose -p "$WORKSHOP_TEST_PROJECT" \
|
||||
-f apps/workshop/docker-compose.yml logs --no-color workshop | grep -q "required_but_disabled"; then
|
||||
required_rejected=true
|
||||
break
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
if [ "$required_rejected" != true ]; then
|
||||
echo "Workshop started without OAuth while WORKSHOP_REQUIRE_OAUTH=true"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
PB_EXPOSE_PORT="$WORKSHOP_TEST_PORT" docker compose -p "$WORKSHOP_TEST_PROJECT" \
|
||||
-f apps/workshop/docker-compose.yml stop workshop
|
||||
|
||||
WORKSHOP_IMAGE_ID=$(
|
||||
docker compose -p "$WORKSHOP_TEST_PROJECT" -f apps/workshop/docker-compose.yml images -q workshop
|
||||
)
|
||||
docker image tag "$WORKSHOP_IMAGE_ID" "$WORKSHOP_ROLLBACK_PROJECT-workshop:latest"
|
||||
PB_EXPOSE_PORT="$WORKSHOP_ROLLBACK_PORT" docker compose -p "$WORKSHOP_ROLLBACK_PROJECT" \
|
||||
-f apps/workshop/docker-compose.yml run --rm --no-deps --workdir /tmp --entrypoint pocketbase workshop \
|
||||
superuser create workshop-rollback@example.invalid 'WorkshopRollback123!' --dir=/pb_data \
|
||||
--hooksDir=/tmp --migrationsDir=/tmp --automigrate=false
|
||||
|
||||
start_rollback_server
|
||||
run_rollback_assertion fresh-prepare
|
||||
stop_rollback_server
|
||||
|
||||
PB_EXPOSE_PORT="$WORKSHOP_ROLLBACK_PORT" docker compose -p "$WORKSHOP_ROLLBACK_PROJECT" \
|
||||
printf 'y\n' | PB_EXPOSE_PORT="$WORKSHOP_TEST_PORT" docker compose -p "$WORKSHOP_TEST_PROJECT" \
|
||||
-f apps/workshop/docker-compose.yml run --rm --no-deps --entrypoint pocketbase workshop \
|
||||
migrate up --dir=/pb_data --migrationsDir=/pb_migrations --hooksDir=/tmp --automigrate=false
|
||||
start_rollback_server
|
||||
run_rollback_assertion fresh-migrated
|
||||
stop_rollback_server
|
||||
|
||||
printf 'y\n' | PB_EXPOSE_PORT="$WORKSHOP_ROLLBACK_PORT" docker compose -p "$WORKSHOP_ROLLBACK_PROJECT" \
|
||||
migrate down 1 --dir=/pb_data --migrationsDir=/pb_migrations
|
||||
PB_EXPOSE_PORT="$WORKSHOP_TEST_PORT" docker compose -p "$WORKSHOP_TEST_PROJECT" \
|
||||
-f apps/workshop/docker-compose.yml run --rm --no-deps --entrypoint pocketbase workshop \
|
||||
migrate down "$WORKSHOP_MIGRATION_COUNT" --dir=/pb_data --migrationsDir=/pb_migrations \
|
||||
--hooksDir=/tmp --automigrate=false
|
||||
start_rollback_server
|
||||
run_rollback_assertion fresh-restored
|
||||
run_rollback_assertion prepare
|
||||
stop_rollback_server
|
||||
|
||||
PB_EXPOSE_PORT="$WORKSHOP_ROLLBACK_PORT" docker compose -p "$WORKSHOP_ROLLBACK_PROJECT" \
|
||||
-f apps/workshop/docker-compose.yml run --rm --no-deps --entrypoint pocketbase workshop \
|
||||
migrate up --dir=/pb_data --migrationsDir=/pb_migrations --hooksDir=/tmp --automigrate=false
|
||||
start_rollback_server
|
||||
run_rollback_assertion migrated
|
||||
stop_rollback_server
|
||||
|
||||
printf 'y\n' | PB_EXPOSE_PORT="$WORKSHOP_ROLLBACK_PORT" docker compose -p "$WORKSHOP_ROLLBACK_PROJECT" \
|
||||
-f apps/workshop/docker-compose.yml run --rm --no-deps --entrypoint pocketbase workshop \
|
||||
migrate down "$WORKSHOP_MIGRATION_COUNT" --dir=/pb_data --migrationsDir=/pb_migrations \
|
||||
--hooksDir=/tmp --automigrate=false
|
||||
start_rollback_server
|
||||
run_rollback_assertion restored
|
||||
stop_rollback_server
|
||||
|
||||
PB_EXPOSE_PORT="$WORKSHOP_ROLLBACK_PORT" docker compose -p "$WORKSHOP_ROLLBACK_PROJECT" \
|
||||
-f apps/workshop/docker-compose.yml run --rm --no-deps --entrypoint pocketbase workshop \
|
||||
migrate up --dir=/pb_data --migrationsDir=/pb_migrations --hooksDir=/tmp --automigrate=false
|
||||
start_rollback_server
|
||||
run_rollback_assertion migrated
|
||||
stop_rollback_server
|
||||
migrate up --dir=/pb_data --migrationsDir=/pb_migrations
|
||||
@@ -1,72 +0,0 @@
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { resolve } from "node:path";
|
||||
|
||||
const entrypoint = resolve("apps/workshop/entrypoint.sh");
|
||||
const validator = resolve("apps/workshop/workshop-url.awk");
|
||||
|
||||
const validate = (value) =>
|
||||
spawnSync("sh", [entrypoint, "--validate-url", "TEST_URL", value], {
|
||||
encoding: "utf8",
|
||||
env: { ...process.env, WORKSHOP_URL_VALIDATOR_PATH: validator },
|
||||
});
|
||||
|
||||
const validateDocsBaseUrl = (value) =>
|
||||
spawnSync("sh", [entrypoint, "--validate-docs-base-url", value], {
|
||||
encoding: "utf8",
|
||||
});
|
||||
|
||||
for (const [value, expected] of [
|
||||
["http://127.0.0.1:8090/", "http://127.0.0.1:8090"],
|
||||
["https://example.com/docs/", "https://example.com/docs"],
|
||||
["https://example.com:0/", "https://example.com:0"],
|
||||
["https://[::1]:8090/workshop/", "https://[::1]:8090/workshop"],
|
||||
]) {
|
||||
const result = validate(value);
|
||||
if (result.status !== 0 || result.stdout.trim() !== expected) {
|
||||
throw new Error(`Expected valid runtime URL ${value}: ${result.stderr || result.stdout}`);
|
||||
}
|
||||
}
|
||||
|
||||
for (const value of [
|
||||
"",
|
||||
"http://",
|
||||
"http:///docs",
|
||||
"https://:",
|
||||
"https://example.com:65536",
|
||||
"https://::1",
|
||||
"https://[:::1]",
|
||||
"http://[:1:2:3:4:5:6:7:8]",
|
||||
"http://[1:2:3:4:5:6:7:8:]",
|
||||
"https://1.2.3.4.5",
|
||||
"http://09.0.0.1",
|
||||
"http://099.0.0.1",
|
||||
"http://[::ffff:192.168.001.001]",
|
||||
"http://[::ffff:192.168.1.01]",
|
||||
"http://[fe80::1%25eth0]",
|
||||
"https://user:password@example.com",
|
||||
"https://example.com/workshop?token=secret",
|
||||
"https://example.com/workshop#fragment",
|
||||
"https://example.com/\tworkshop",
|
||||
"https://example.com/\nworkshop",
|
||||
]) {
|
||||
const result = validate(value);
|
||||
if (result.status === 0) throw new Error(`Expected invalid runtime URL to be rejected: ${JSON.stringify(value)}`);
|
||||
}
|
||||
|
||||
for (const [value, expected] of [
|
||||
["/", "/"],
|
||||
["/docs/", "/docs"],
|
||||
["/docs..preview", "/docs..preview"],
|
||||
]) {
|
||||
const result = validateDocsBaseUrl(value);
|
||||
if (result.status !== 0 || result.stdout.trim() !== expected) {
|
||||
throw new Error(`Expected valid DOCS_BASE_URL ${value}: ${result.stderr || result.stdout}`);
|
||||
}
|
||||
}
|
||||
|
||||
for (const value of ["docs", "/../docs", "/docs/../private", "/./docs", "/docs?preview=1"]) {
|
||||
const result = validateDocsBaseUrl(value);
|
||||
if (result.status === 0) throw new Error(`Expected invalid DOCS_BASE_URL to be rejected: ${JSON.stringify(value)}`);
|
||||
}
|
||||
|
||||
console.log("Workshop runtime URL validation passed");
|
||||
@@ -9,78 +9,33 @@ if (typeof clientExport !== "string") throw new Error("Workshop client export is
|
||||
await access(resolve("packages/workshop", clientExport));
|
||||
|
||||
const hook = await read("apps/workshop/pb_hooks/workshop.pb.js");
|
||||
const dockerfile = await read("apps/workshop/Dockerfile");
|
||||
const workshopApp = await read("apps/docs/src/components/workshop/WorkshopApp.tsx");
|
||||
const workshopAdmin = await read("apps/docs/src/components/workshop/WorkshopAdmin.tsx");
|
||||
const workshopDetail = await read("apps/docs/src/components/workshop/DetailPage.tsx");
|
||||
if (!hook.includes("require(`${__hooks}/workshop-utils.js`)")) {
|
||||
throw new Error("Workshop handlers must load shared helpers inside their isolated PocketBase contexts");
|
||||
}
|
||||
if (!hook.includes("onBootstrap") || !hook.includes("users.oauth2.providers = configured")) {
|
||||
throw new Error("Workshop OAuth settings must be synchronized at every bootstrap");
|
||||
}
|
||||
if (!hook.includes("WORKSHOP_REQUIRE_OAUTH") || !hook.includes("partial_credentials")) {
|
||||
throw new Error("Workshop OAuth bootstrap must reject partial and required-but-disabled configurations");
|
||||
if (hook.includes("validateAndNormalizeSubmission")) {
|
||||
throw new Error("PocketBase must store Workshop submissions without interpreting their content");
|
||||
}
|
||||
if (
|
||||
!hook.includes("onRecordAuthWithOAuth2Request") ||
|
||||
!hook.includes("deriveGithubIdentity") ||
|
||||
!hook.includes("event.oauth2User") ||
|
||||
hook.includes("event.oAuth2User") ||
|
||||
!hook.includes("event.createData.displayName = identity.displayName") ||
|
||||
!hook.includes('event.record.set("displayName", identity.displayName)') ||
|
||||
!hook.includes("workshop_oauth_identity_synchronized")
|
||||
) {
|
||||
throw new Error("Workshop GitHub identity must be synchronized for first and returning OAuth logins");
|
||||
}
|
||||
if (!hook.includes("workshop_report_reopened") || !hook.includes("status = 'dismissed'")) {
|
||||
throw new Error("Workshop must permit a fresh report after a previous report was dismissed");
|
||||
}
|
||||
if (hook.includes("validateAndNormalizeSubmission") || hook.includes("workshop_submission_rejected")) {
|
||||
throw new Error("PocketBase must not interpret or validate Workshop submission content");
|
||||
}
|
||||
for (const removedValidator of [
|
||||
for (const artifact of [
|
||||
"apps/workshop/pb_hooks/widget-validator.js",
|
||||
"apps/workshop/pb_hooks/widget-validator.bundle.cjs",
|
||||
"packages/workshop/src/pocketbase-validator.ts",
|
||||
"scripts/build-workshop-validator.mjs",
|
||||
]) {
|
||||
let artifactExists = true;
|
||||
try {
|
||||
await access(resolve(removedValidator));
|
||||
} catch (error) {
|
||||
if (typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT") {
|
||||
artifactExists = false;
|
||||
} else {
|
||||
await access(resolve(artifact))
|
||||
.then(() => {
|
||||
throw new Error(`Workshop validator artifact must stay removed: ${artifact}`);
|
||||
})
|
||||
.catch((error) => {
|
||||
if (error instanceof Error && "code" in error && error.code === "ENOENT") return;
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
if (artifactExists) throw new Error(`Workshop validator artifact must stay removed: ${removedValidator}`);
|
||||
}
|
||||
if (!dockerfile.includes("WORKSHOP_REQUIRE_PUBLIC_ORIGIN=true")) {
|
||||
throw new Error("The production Workshop image must require an explicit public origin");
|
||||
}
|
||||
if (dockerfile.includes("widget-validator") || dockerfile.includes("build-workshop-validator")) {
|
||||
throw new Error("The Workshop image must not build or copy a submission validator");
|
||||
});
|
||||
}
|
||||
if (hook.includes('findAllRecords("users")') || !hook.includes('findRecordsByFilter("users"')) {
|
||||
throw new Error("Workshop report notifications must use a filtered, bounded administrator query");
|
||||
}
|
||||
if (!hook.includes("WORKSHOP_WEB_URL")) {
|
||||
throw new Error("PocketBase-generated Workshop links must use the configured public web URL");
|
||||
}
|
||||
for (const [name, source] of [
|
||||
["Workshop listing", workshopApp],
|
||||
["Workshop moderation", workshopAdmin],
|
||||
["Workshop detail", workshopDetail],
|
||||
]) {
|
||||
if (/<a\b[^>]*\bhref=["']\/workshop/u.test(source)) {
|
||||
throw new Error(`${name} must use base-path-aware Docusaurus links`);
|
||||
}
|
||||
if (/window\.location(?:\.href)?\s*=\s*["']\/workshop/u.test(source)) {
|
||||
throw new Error(`${name} must use a base-path-aware redirect`);
|
||||
}
|
||||
}
|
||||
|
||||
const migration = await read("apps/workshop/pb_migrations/1784240000_workshop_widgets.js");
|
||||
for (const required of [
|
||||
@@ -96,44 +51,6 @@ for (const required of [
|
||||
if (!migration.includes(required)) throw new Error(`Workshop rollback is missing state restoration: ${required}`);
|
||||
}
|
||||
|
||||
for (const protectedField of [
|
||||
"email:changed",
|
||||
"displayName:changed",
|
||||
"avatarUrl:changed",
|
||||
"githubUsername:changed",
|
||||
"githubProfileUrl:changed",
|
||||
]) {
|
||||
if (!migration.includes(protectedField)) {
|
||||
throw new Error(`Workshop OAuth identity field remains caller-controlled: ${protectedField}`);
|
||||
}
|
||||
}
|
||||
if (migration.includes("@request.body.avatar:changed")) {
|
||||
throw new Error("Workshop access rules must not use the unsupported :changed modifier for file fields");
|
||||
}
|
||||
if (
|
||||
!hook.includes('getUnsavedFiles("avatar")') ||
|
||||
!hook.includes('original().getString("avatar")') ||
|
||||
!hook.includes("Workshop avatars are managed by GitHub OAuth")
|
||||
) {
|
||||
throw new Error("Workshop avatar updates must be rejected by the user update request hook");
|
||||
}
|
||||
for (const protectedField of [
|
||||
"title:changed",
|
||||
"description:changed",
|
||||
"content:changed",
|
||||
"changelog:changed",
|
||||
"screenshots:changed",
|
||||
]) {
|
||||
if (!migration.includes(protectedField)) {
|
||||
throw new Error(`Workshop moderators can still rewrite submissions: ${protectedField}`);
|
||||
}
|
||||
}
|
||||
for (const protectedField of ["category:changed", "explanation:changed"]) {
|
||||
if (!migration.includes(protectedField)) {
|
||||
throw new Error(`Workshop moderators can still rewrite reports: ${protectedField}`);
|
||||
}
|
||||
}
|
||||
|
||||
const workflow = await read(".github/workflows/workshop.yml");
|
||||
if (/\n\s+paths:/u.test(workflow)) throw new Error("Workshop workflow must not filter out Docker build inputs");
|
||||
|
||||
|
||||
@@ -1,168 +0,0 @@
|
||||
import { createRequire } from "node:module";
|
||||
import { readFile } from "node:fs/promises";
|
||||
import vm from "node:vm";
|
||||
|
||||
const nodeRequire = createRequire(import.meta.url);
|
||||
const workshopUtils = nodeRequire("../pb_hooks/workshop-utils.js");
|
||||
const hookSource = await readFile("apps/workshop/pb_hooks/workshop.pb.js", "utf8");
|
||||
let oauthHook;
|
||||
let userUpdateHook;
|
||||
|
||||
const ignoreHook = () => {};
|
||||
vm.runInNewContext(hookSource, {
|
||||
__hooks: "/pb_hooks",
|
||||
console: { log: () => {} },
|
||||
onBootstrap: ignoreHook,
|
||||
onRecordAfterCreateSuccess: ignoreHook,
|
||||
onRecordCreateRequest: ignoreHook,
|
||||
onRecordDeleteRequest: ignoreHook,
|
||||
onRecordUpdateRequest: (handler, collection) => {
|
||||
if (collection === "users") userUpdateHook = handler;
|
||||
},
|
||||
onRecordAuthWithOAuth2Request: (handler, collection) => {
|
||||
if (collection !== "users") throw new Error(`OAuth hook targets unexpected collection: ${collection}`);
|
||||
oauthHook = handler;
|
||||
},
|
||||
require: (specifier) => {
|
||||
if (specifier !== "/pb_hooks/workshop-utils.js") {
|
||||
throw new Error(`OAuth hook loaded unexpected dependency: ${specifier}`);
|
||||
}
|
||||
return {
|
||||
...workshopUtils,
|
||||
rejectRequest: (message) => {
|
||||
throw new Error(message);
|
||||
},
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
if (typeof oauthHook !== "function") throw new Error("Workshop OAuth request hook was not registered");
|
||||
if (typeof userUpdateHook !== "function") throw new Error("Workshop user update hook was not registered");
|
||||
|
||||
const oauth2User = {
|
||||
id: "provider-user-123",
|
||||
name: "Octo Cat",
|
||||
username: "octocat",
|
||||
avatarURL: "https://avatars.githubusercontent.com/u/1?v=4",
|
||||
rawUser: {},
|
||||
};
|
||||
const createData = {};
|
||||
let createNextCalls = 0;
|
||||
oauthHook({
|
||||
app: { save: () => Promise.reject(new Error("New OAuth users must not be saved before PocketBase creates them")) },
|
||||
createData,
|
||||
next: () => {
|
||||
createNextCalls += 1;
|
||||
},
|
||||
oauth2User,
|
||||
providerName: "github",
|
||||
record: null,
|
||||
});
|
||||
if (
|
||||
createNextCalls !== 1 ||
|
||||
createData.displayName !== "Octo Cat" ||
|
||||
createData.avatarUrl !== "https://avatars.githubusercontent.com/u/1?v=4" ||
|
||||
createData.githubUsername !== "octocat" ||
|
||||
createData.githubProfileUrl !== "https://github.com/octocat"
|
||||
) {
|
||||
throw new Error(`First GitHub OAuth login did not synchronize trusted identity: ${JSON.stringify(createData)}`);
|
||||
}
|
||||
|
||||
const returningValues = new Map();
|
||||
const returningRecord = {
|
||||
id: "returning-user-123",
|
||||
set: (name, value) => returningValues.set(name, value),
|
||||
};
|
||||
let savedRecord;
|
||||
let returningNextCalls = 0;
|
||||
oauthHook({
|
||||
app: {
|
||||
save: (record) => {
|
||||
savedRecord = record;
|
||||
},
|
||||
},
|
||||
createData: {},
|
||||
next: () => {
|
||||
returningNextCalls += 1;
|
||||
},
|
||||
oauth2User,
|
||||
providerName: "github",
|
||||
record: returningRecord,
|
||||
});
|
||||
if (
|
||||
savedRecord !== returningRecord ||
|
||||
returningNextCalls !== 1 ||
|
||||
returningValues.get("displayName") !== "Octo Cat" ||
|
||||
returningValues.get("avatar") !== "" ||
|
||||
returningValues.get("avatarUrl") !== "https://avatars.githubusercontent.com/u/1?v=4" ||
|
||||
returningValues.get("githubUsername") !== "octocat" ||
|
||||
returningValues.get("githubProfileUrl") !== "https://github.com/octocat"
|
||||
) {
|
||||
throw new Error("Returning GitHub OAuth login did not refresh trusted identity");
|
||||
}
|
||||
|
||||
const ignoredCreateData = {};
|
||||
let ignoredNextCalls = 0;
|
||||
oauthHook({
|
||||
app: {},
|
||||
createData: ignoredCreateData,
|
||||
next: () => {
|
||||
ignoredNextCalls += 1;
|
||||
},
|
||||
oauth2User,
|
||||
providerName: "oidc",
|
||||
record: null,
|
||||
});
|
||||
if (ignoredNextCalls !== 1 || Object.keys(ignoredCreateData).length !== 0) {
|
||||
throw new Error("Non-GitHub OAuth providers must pass through without GitHub identity synchronization");
|
||||
}
|
||||
|
||||
let unchangedAvatarNextCalls = 0;
|
||||
userUpdateHook({
|
||||
next: () => {
|
||||
unchangedAvatarNextCalls += 1;
|
||||
},
|
||||
record: {
|
||||
getString: () => "provider-avatar.png",
|
||||
getUnsavedFiles: () => [],
|
||||
original: () => ({ getString: () => "provider-avatar.png" }),
|
||||
},
|
||||
});
|
||||
if (unchangedAvatarNextCalls !== 1) {
|
||||
throw new Error("User updates that leave the provider avatar unchanged must pass through");
|
||||
}
|
||||
|
||||
for (const avatarChange of [
|
||||
{
|
||||
name: "upload",
|
||||
current: "provider-avatar.png",
|
||||
original: "provider-avatar.png",
|
||||
unsavedFiles: [{ name: "forged-avatar.png" }],
|
||||
},
|
||||
{
|
||||
name: "removal",
|
||||
current: "",
|
||||
original: "provider-avatar.png",
|
||||
unsavedFiles: [],
|
||||
},
|
||||
]) {
|
||||
let nextCalls = 0;
|
||||
try {
|
||||
userUpdateHook({
|
||||
next: () => {
|
||||
nextCalls += 1;
|
||||
},
|
||||
record: {
|
||||
getString: () => avatarChange.current,
|
||||
getUnsavedFiles: () => avatarChange.unsavedFiles,
|
||||
original: () => ({ getString: () => avatarChange.original }),
|
||||
},
|
||||
});
|
||||
throw new Error(`Workshop user avatar ${avatarChange.name} was accepted`);
|
||||
} catch (error) {
|
||||
if (!String(error).includes("managed by GitHub OAuth")) throw error;
|
||||
}
|
||||
if (nextCalls !== 0) throw new Error(`Rejected avatar ${avatarChange.name} continued the update request`);
|
||||
}
|
||||
|
||||
console.log("Workshop OAuth hook contract passed");
|
||||
@@ -1,261 +0,0 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { readFile, writeFile } from "node:fs/promises";
|
||||
|
||||
const baseUrl = process.env.WORKSHOP_TEST_URL;
|
||||
const snapshotPath = process.env.WORKSHOP_ROLLBACK_SNAPSHOT;
|
||||
const phase = process.argv[2];
|
||||
if (!baseUrl) throw new Error("WORKSHOP_TEST_URL is required");
|
||||
if (!snapshotPath) throw new Error("WORKSHOP_ROLLBACK_SNAPSHOT is required");
|
||||
if (!["fresh-prepare", "fresh-migrated", "fresh-restored", "prepare", "migrated", "restored"].includes(phase)) {
|
||||
throw new Error("Unknown Workshop rollback test phase");
|
||||
}
|
||||
|
||||
const superuser = {
|
||||
email: "workshop-rollback@example.invalid",
|
||||
password: "WorkshopRollback123!",
|
||||
};
|
||||
const baselineUser = {
|
||||
email: "rollback-user@example.invalid",
|
||||
password: "WorkshopRollbackUser123!",
|
||||
note: "retained across the Workshop migration chain",
|
||||
};
|
||||
const workshopCollections = [
|
||||
"workshop_migration_state",
|
||||
"submissions",
|
||||
"votes",
|
||||
"comments",
|
||||
"reports",
|
||||
"workshop_listings",
|
||||
];
|
||||
const workshopUserFields = ["displayName", "avatarUrl", "avatar", "githubUsername", "githubProfileUrl", "isAdmin"];
|
||||
|
||||
const request = async (path, init = {}) => {
|
||||
const response = await fetch(`${baseUrl}${path}`, {
|
||||
...init,
|
||||
headers: { "content-type": "application/json", ...init.headers },
|
||||
});
|
||||
const body = await response.json().catch(() => null);
|
||||
if (!response.ok) throw new Error(`${response.status} ${path}: ${JSON.stringify(body)}`);
|
||||
return body;
|
||||
};
|
||||
|
||||
const auth = await request("/api/collections/_superusers/auth-with-password", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ identity: superuser.email, password: superuser.password }),
|
||||
});
|
||||
const rootHeaders = { authorization: `Bearer ${auth.token}` };
|
||||
|
||||
const collectionList = async () => {
|
||||
const result = await request("/api/collections?perPage=200", { headers: rootHeaders });
|
||||
return new Map(result.items.map((collection) => [collection.name, collection]));
|
||||
};
|
||||
const collection = (name) => request(`/api/collections/${name}`, { headers: rootHeaders });
|
||||
const records = (name) =>
|
||||
request(`/api/collections/${name}/records?perPage=200`, {
|
||||
headers: rootHeaders,
|
||||
});
|
||||
const normalized = (value) => JSON.parse(JSON.stringify(value));
|
||||
const publicOauthConfig = (oauth2) => {
|
||||
const value = normalized(oauth2);
|
||||
value.providers = value.providers?.map(({ clientSecret: _clientSecret, ...provider }) => provider);
|
||||
return value;
|
||||
};
|
||||
const userConfig = (users) => ({
|
||||
rules: {
|
||||
listRule: users.listRule,
|
||||
viewRule: users.viewRule,
|
||||
createRule: users.createRule,
|
||||
updateRule: users.updateRule,
|
||||
deleteRule: users.deleteRule,
|
||||
},
|
||||
passwordAuth: normalized(users.passwordAuth),
|
||||
oauth2: publicOauthConfig(users.oauth2),
|
||||
fieldNames: users.fields.map((field) => field.name).toSorted(),
|
||||
});
|
||||
const readSnapshot = async () => JSON.parse(await readFile(snapshotPath, "utf8"));
|
||||
|
||||
const assertBaselineData = async () => {
|
||||
const userRecords = await records("users");
|
||||
const retainedUser = userRecords.items.find((record) => record.email === baselineUser.email);
|
||||
assert.equal(retainedUser?.baselineNote, baselineUser.note, "Baseline user data was not retained");
|
||||
|
||||
const sentinelRecords = await records("rollback_sentinel");
|
||||
assert.equal(sentinelRecords.items.length, 1, "Unrelated baseline record count changed");
|
||||
assert.equal(sentinelRecords.items[0]?.value, "keep-me", "Unrelated baseline data changed");
|
||||
};
|
||||
|
||||
const assertMigrated = async () => {
|
||||
const before = await readSnapshot();
|
||||
const collections = await collectionList();
|
||||
for (const name of [...workshopCollections, "users", "rollback_sentinel"]) {
|
||||
assert.ok(collections.has(name), `Migrated database is missing collection ${name}`);
|
||||
}
|
||||
|
||||
const users = await collection("users");
|
||||
assert.equal(users.passwordAuth?.enabled, false, "Workshop migration must disable password authentication");
|
||||
assert.deepEqual(
|
||||
publicOauthConfig(users.oauth2),
|
||||
before.users.oauth2,
|
||||
"Workshop migration changed existing OAuth settings",
|
||||
);
|
||||
for (const name of workshopUserFields) {
|
||||
assert.ok(
|
||||
users.fields.some((field) => field.name === name),
|
||||
`Workshop user field ${name} is missing`,
|
||||
);
|
||||
}
|
||||
assert.match(
|
||||
users.updateRule ?? "",
|
||||
/githubProfileUrl:changed = false/u,
|
||||
"OAuth identity update rule is not hardened",
|
||||
);
|
||||
|
||||
const stateRecords = await records("workshop_migration_state");
|
||||
assert.equal(stateRecords.items.length, 1, "Workshop migration state snapshot is missing");
|
||||
const migrationState = JSON.parse(stateRecords.items[0].snapshot);
|
||||
assert.equal(migrationState.usersExisted, true, "Workshop migration did not recognize the existing users collection");
|
||||
assert.deepEqual(migrationState.users.passwordAuth, before.users.passwordAuth);
|
||||
assert.deepEqual(publicOauthConfig(migrationState.users.oauth2), before.users.oauth2);
|
||||
assert.deepEqual(
|
||||
{
|
||||
listRule: migrationState.users.listRule,
|
||||
viewRule: migrationState.users.viewRule,
|
||||
createRule: migrationState.users.createRule,
|
||||
updateRule: migrationState.users.updateRule,
|
||||
deleteRule: migrationState.users.deleteRule,
|
||||
},
|
||||
before.users.rules,
|
||||
"Workshop migration state did not snapshot the original user rules",
|
||||
);
|
||||
assert.equal(
|
||||
migrationState.users.oauth2.providers.find((provider) => provider.name === "github")?.clientSecret,
|
||||
"rollback-baseline-secret",
|
||||
"Workshop migration state did not retain the OAuth secret required for rollback",
|
||||
);
|
||||
await assertBaselineData();
|
||||
};
|
||||
|
||||
if (phase === "fresh-prepare") {
|
||||
let collections = await collectionList();
|
||||
if (collections.has("users")) {
|
||||
await request("/api/collections/users", { method: "DELETE", headers: rootHeaders });
|
||||
collections = await collectionList();
|
||||
}
|
||||
for (const name of [...workshopCollections, "users", "rollback_sentinel"]) {
|
||||
assert.ok(!collections.has(name), `Fresh database unexpectedly contains collection ${name}`);
|
||||
}
|
||||
await writeFile(snapshotPath, "{}", { mode: 0o600 });
|
||||
console.log("Workshop fresh rollback baseline captured");
|
||||
} else if (phase === "fresh-migrated") {
|
||||
const collections = await collectionList();
|
||||
for (const name of [...workshopCollections, "users"]) {
|
||||
assert.ok(collections.has(name), `Fresh migration is missing collection ${name}`);
|
||||
}
|
||||
const users = await collection("users");
|
||||
for (const name of workshopUserFields) {
|
||||
assert.ok(
|
||||
users.fields.some((field) => field.name === name),
|
||||
`Fresh migration is missing user field ${name}`,
|
||||
);
|
||||
}
|
||||
const stateRecords = await records("workshop_migration_state");
|
||||
assert.equal(stateRecords.items.length, 1, "Fresh migration state snapshot is missing");
|
||||
const migrationState = JSON.parse(stateRecords.items[0].snapshot);
|
||||
assert.equal(migrationState.usersExisted, false, "Fresh migration incorrectly recorded an existing users collection");
|
||||
assert.equal(migrationState.users, null, "Fresh migration retained nonexistent user settings");
|
||||
console.log("Workshop fresh migration chain verified");
|
||||
} else if (phase === "fresh-restored") {
|
||||
const collections = await collectionList();
|
||||
for (const name of [...workshopCollections, "users", "rollback_sentinel"]) {
|
||||
assert.ok(!collections.has(name), `Fresh rollback left collection ${name}`);
|
||||
}
|
||||
assert.ok(collections.has("_superusers"), "Fresh rollback removed PocketBase superuser data");
|
||||
console.log("Workshop fresh rollback state verified");
|
||||
} else if (phase === "prepare") {
|
||||
const collections = await collectionList();
|
||||
for (const name of [...workshopCollections, "users", "rollback_sentinel"]) {
|
||||
assert.ok(!collections.has(name), `Fresh rollback fixture unexpectedly contains collection ${name}`);
|
||||
}
|
||||
|
||||
await request("/api/collections", {
|
||||
method: "POST",
|
||||
headers: rootHeaders,
|
||||
body: JSON.stringify({
|
||||
type: "auth",
|
||||
name: "users",
|
||||
listRule: "@request.auth.id != ''",
|
||||
viewRule: "id = @request.auth.id",
|
||||
createRule: "@request.body.email != ''",
|
||||
updateRule: "id = @request.auth.id",
|
||||
deleteRule: "id = @request.auth.id",
|
||||
passwordAuth: { enabled: true, identityFields: ["email"] },
|
||||
oauth2: {
|
||||
enabled: true,
|
||||
providers: [
|
||||
{
|
||||
name: "github",
|
||||
clientId: "rollback-baseline-client",
|
||||
clientSecret: "rollback-baseline-secret",
|
||||
},
|
||||
],
|
||||
},
|
||||
fields: [{ type: "text", name: "baselineNote", max: 200 }],
|
||||
}),
|
||||
});
|
||||
await request("/api/collections", {
|
||||
method: "POST",
|
||||
headers: rootHeaders,
|
||||
body: JSON.stringify({
|
||||
type: "base",
|
||||
name: "rollback_sentinel",
|
||||
listRule: null,
|
||||
viewRule: null,
|
||||
createRule: null,
|
||||
updateRule: null,
|
||||
deleteRule: null,
|
||||
fields: [{ type: "text", name: "value", required: true, max: 100 }],
|
||||
}),
|
||||
});
|
||||
await request("/api/collections/users/records", {
|
||||
method: "POST",
|
||||
headers: rootHeaders,
|
||||
body: JSON.stringify({
|
||||
email: baselineUser.email,
|
||||
emailVisibility: false,
|
||||
verified: true,
|
||||
password: baselineUser.password,
|
||||
passwordConfirm: baselineUser.password,
|
||||
baselineNote: baselineUser.note,
|
||||
}),
|
||||
});
|
||||
await request("/api/collections/rollback_sentinel/records", {
|
||||
method: "POST",
|
||||
headers: rootHeaders,
|
||||
body: JSON.stringify({ value: "keep-me" }),
|
||||
});
|
||||
|
||||
const users = await collection("users");
|
||||
assert.equal(users.passwordAuth?.enabled, true);
|
||||
assert.equal(users.oauth2?.enabled, true);
|
||||
assert.ok(users.oauth2?.providers?.some((provider) => provider.name === "github"));
|
||||
await assertBaselineData();
|
||||
await writeFile(snapshotPath, JSON.stringify({ users: userConfig(users) }), { mode: 0o600 });
|
||||
console.log("Workshop rollback baseline prepared");
|
||||
} else if (phase === "migrated") {
|
||||
await assertMigrated();
|
||||
console.log("Workshop full migration chain verified");
|
||||
} else {
|
||||
const before = await readSnapshot();
|
||||
const collections = await collectionList();
|
||||
for (const name of workshopCollections) {
|
||||
assert.ok(!collections.has(name), `Rollback left Workshop collection ${name}`);
|
||||
}
|
||||
for (const name of ["users", "rollback_sentinel"]) {
|
||||
assert.ok(collections.has(name), `Rollback removed baseline collection ${name}`);
|
||||
}
|
||||
|
||||
const users = await collection("users");
|
||||
assert.deepEqual(userConfig(users), before.users, "Rollback did not restore user rules, fields, password, or OAuth");
|
||||
await assertBaselineData();
|
||||
console.log("Workshop full rollback state verified");
|
||||
}
|
||||
@@ -1,111 +0,0 @@
|
||||
function reject(message) {
|
||||
print variable_name " " message > "/dev/stderr"
|
||||
failed = 1
|
||||
exit 1
|
||||
}
|
||||
|
||||
function valid_port(value, number) {
|
||||
if (value !~ /^[0-9]+$/ || length(value) > 5) return 0
|
||||
number = value + 0
|
||||
return number >= 0 && number <= 65535
|
||||
}
|
||||
|
||||
function valid_ipv4(value, parts, count, part_index) {
|
||||
if (value !~ /^[0-9.]+$/) return 0
|
||||
count = split(value, parts, ".")
|
||||
if (count != 4) return 0
|
||||
for (part_index = 1; part_index <= count; part_index++) {
|
||||
if (length(parts[part_index]) > 1 && substr(parts[part_index], 1, 1) == "0") return 0
|
||||
if (parts[part_index] == "" || parts[part_index] + 0 < 0 || parts[part_index] + 0 > 255) return 0
|
||||
}
|
||||
return 1
|
||||
}
|
||||
|
||||
function valid_hostname(value, labels, count, label_index) {
|
||||
if (value == "" || length(value) > 253 || value !~ /^[A-Za-z0-9._-]+$/) return 0
|
||||
if (value ~ /^[0-9.]+$/) return valid_ipv4(value)
|
||||
count = split(value, labels, ".")
|
||||
for (label_index = 1; label_index <= count; label_index++) {
|
||||
if (labels[label_index] == "" || length(labels[label_index]) > 63) return 0
|
||||
if (labels[label_index] ~ /^-/ || labels[label_index] ~ /-$/) return 0
|
||||
}
|
||||
return 1
|
||||
}
|
||||
|
||||
function valid_ipv6(value, percent, address, starts_single_colon, ends_single_colon, collapsed, parts, count, groups, part_index) {
|
||||
percent = index(value, "%")
|
||||
if (percent) return 0
|
||||
address = value
|
||||
if (address !~ /:/ || address ~ /:::/ || address !~ /^[0-9A-Fa-f:.]+$/) return 0
|
||||
starts_single_colon = substr(address, 1, 1) == ":" && substr(address, 1, 2) != "::"
|
||||
ends_single_colon = substr(address, length(address), 1) == ":" && substr(address, length(address) - 1) != "::"
|
||||
if (starts_single_colon || ends_single_colon) return 0
|
||||
collapsed = address
|
||||
sub(/::/, "", collapsed)
|
||||
if (collapsed ~ /::/) return 0
|
||||
|
||||
count = split(address, parts, ":")
|
||||
groups = 0
|
||||
for (part_index = 1; part_index <= count; part_index++) {
|
||||
if (parts[part_index] == "") continue
|
||||
if (parts[part_index] ~ /\./) {
|
||||
if (part_index != count || !valid_ipv4(parts[part_index])) return 0
|
||||
groups += 2
|
||||
} else {
|
||||
if (parts[part_index] !~ /^[0-9A-Fa-f]+$/ || length(parts[part_index]) > 4) return 0
|
||||
groups++
|
||||
}
|
||||
}
|
||||
return address ~ /::/ ? groups < 8 : groups == 8
|
||||
}
|
||||
|
||||
{
|
||||
if (NR != 1) reject("must be a single-line URL")
|
||||
value = $0
|
||||
if (value ~ /[[:cntrl:][:space:]]/ || index(value, "\"") || index(value, "\\")) {
|
||||
reject("must not contain control characters, whitespace, quotes, or backslashes")
|
||||
}
|
||||
if (value !~ /^https?:\/\//) reject("must be an absolute HTTP(S) URL")
|
||||
if (value ~ /[?#]/) reject("must not contain a query string or fragment")
|
||||
|
||||
rest = value
|
||||
sub(/^https?:\/\//, "", rest)
|
||||
slash = index(rest, "/")
|
||||
authority = slash ? substr(rest, 1, slash - 1) : rest
|
||||
if (authority == "") reject("must include a host")
|
||||
if (authority ~ /@/) reject("must not contain credentials")
|
||||
|
||||
port = ""
|
||||
if (authority ~ /^\[/) {
|
||||
close_index = index(authority, "]")
|
||||
if (!close_index) reject("contains an invalid IPv6 host")
|
||||
host = substr(authority, 2, close_index - 2)
|
||||
suffix = substr(authority, close_index + 1)
|
||||
if (!valid_ipv6(host)) reject("contains an invalid IPv6 host")
|
||||
if (suffix != "") {
|
||||
if (suffix !~ /^:/) reject("contains an invalid host")
|
||||
port = substr(suffix, 2)
|
||||
}
|
||||
} else {
|
||||
host_with_port = authority
|
||||
colon_count = gsub(/:/, ":", host_with_port)
|
||||
if (colon_count > 1) reject("must wrap IPv6 hosts in brackets")
|
||||
colon = index(authority, ":")
|
||||
host = colon ? substr(authority, 1, colon - 1) : authority
|
||||
port = colon ? substr(authority, colon + 1) : ""
|
||||
if (!valid_hostname(host)) reject("contains an invalid host")
|
||||
}
|
||||
if (port != "" && !valid_port(port)) reject("contains an invalid port")
|
||||
|
||||
while (length(value) > 0 && substr(value, length(value), 1) == "/") {
|
||||
value = substr(value, 1, length(value) - 1)
|
||||
}
|
||||
print value
|
||||
}
|
||||
|
||||
END {
|
||||
if (!failed && NR != 1) {
|
||||
print variable_name " must be a single-line URL" > "/dev/stderr"
|
||||
exit 1
|
||||
}
|
||||
}
|
||||
@@ -1,183 +1,18 @@
|
||||
import { createServer } from "node:http";
|
||||
|
||||
import { chromium, expect } from "@playwright/test";
|
||||
import type { Browser } from "@playwright/test";
|
||||
import { stringify as stringifySuperJson } from "superjson";
|
||||
import type { StartedTestContainer } from "testcontainers";
|
||||
import { describe, test } from "vitest";
|
||||
|
||||
import { eq } from "drizzle-orm";
|
||||
import {
|
||||
customWidgetDefinitions,
|
||||
customWidgetSecrets,
|
||||
legacyCustomWidgetDefinitions,
|
||||
legacyCustomWidgetSecrets,
|
||||
} from "../packages/db/schema/sqlite";
|
||||
import type { HomarrCustomWidgetV2Input } from "../packages/custom-widgets/src/core";
|
||||
|
||||
import { createHomarrContainer } from "./shared/create-homarr-container";
|
||||
import { createSqliteDbFileAsync } from "./shared/e2e-db";
|
||||
import type { MockApiServer } from "./shared/mock-api-server";
|
||||
import { exposeHostPortToContainersAsync, startMockApiServerAsync } from "./shared/mock-api-server";
|
||||
import { startMockApiServerAsync } from "./shared/mock-api-server";
|
||||
import { seedAdminUserAsync } from "./shared/seed-admin-user";
|
||||
|
||||
const adminCredentials = { username: "admin", password: "Comp(exP4sswOrd" };
|
||||
const initialTemplate =
|
||||
"<Stack><Title order={4}>{data.status?.title}</Title><Badge>{data.status?.status}</Badge></Stack>";
|
||||
const updatedTemplate = "<Stack><Text fw={700}>{data.status?.title}</Text><Text>{data.status?.value}</Text></Stack>";
|
||||
const workshopWidgetName = "Workshop E2E widget";
|
||||
const legacyWidgetName = "Legacy E2E Widget";
|
||||
const legacyWidgetId = "legacy-e2e-widget";
|
||||
|
||||
interface WorkshopMockServer {
|
||||
url: string;
|
||||
close(): Promise<void>;
|
||||
}
|
||||
|
||||
const startWorkshopMockServerAsync = async (widget: HomarrCustomWidgetV2Input): Promise<WorkshopMockServer> => {
|
||||
const submissionId = "e2eworkshop0001";
|
||||
const timestamp = "2026-07-28T10:00:00.000Z";
|
||||
const listing = {
|
||||
id: submissionId,
|
||||
collectionId: "workshop-listings",
|
||||
collectionName: "workshop_listings",
|
||||
type: "customWidget",
|
||||
title: workshopWidgetName,
|
||||
description: "Installed from the Workshop and rendered on a Homarr board.",
|
||||
widgetSchema: "homarr-custom-widget-v2",
|
||||
screenshots: [],
|
||||
author: "e2e-author",
|
||||
authorName: "E2E Workshop author",
|
||||
authorAvatar: "",
|
||||
authorAvatarUrl: "",
|
||||
authorGithubUsername: "",
|
||||
authorGithubProfileUrl: "",
|
||||
score: 1,
|
||||
upvotes: 1,
|
||||
downvotes: 0,
|
||||
commentCount: 0,
|
||||
reportCount: 0,
|
||||
revision: 1,
|
||||
changelog: "Initial E2E submission",
|
||||
outdated: false,
|
||||
created: timestamp,
|
||||
updated: timestamp,
|
||||
};
|
||||
const submission = {
|
||||
id: submissionId,
|
||||
collectionId: "submissions",
|
||||
collectionName: "submissions",
|
||||
type: listing.type,
|
||||
title: listing.title,
|
||||
description: listing.description,
|
||||
widgetSchema: listing.widgetSchema,
|
||||
content: JSON.stringify(widget),
|
||||
revision: listing.revision,
|
||||
changelog: listing.changelog,
|
||||
outdated: listing.outdated,
|
||||
screenshots: listing.screenshots,
|
||||
author: listing.author,
|
||||
created: timestamp,
|
||||
updated: timestamp,
|
||||
};
|
||||
|
||||
const server = createServer((request, response) => {
|
||||
response.setHeader("Access-Control-Allow-Origin", "*");
|
||||
response.setHeader("Access-Control-Allow-Headers", "*");
|
||||
response.setHeader("Access-Control-Allow-Methods", "GET, OPTIONS");
|
||||
response.setHeader("Access-Control-Allow-Private-Network", "true");
|
||||
response.setHeader("Content-Type", "application/json");
|
||||
|
||||
if (request.method === "OPTIONS") {
|
||||
response.writeHead(204);
|
||||
response.end();
|
||||
return;
|
||||
}
|
||||
|
||||
const pathname = new URL(request.url ?? "/", "http://127.0.0.1").pathname;
|
||||
if (request.method === "GET" && pathname === "/api/collections/workshop_listings/records") {
|
||||
response.end(
|
||||
JSON.stringify({
|
||||
page: 1,
|
||||
perPage: 24,
|
||||
totalItems: 1,
|
||||
totalPages: 1,
|
||||
items: [listing],
|
||||
}),
|
||||
);
|
||||
return;
|
||||
}
|
||||
if (request.method === "GET" && pathname === `/api/collections/workshop_listings/records/${submissionId}`) {
|
||||
response.end(JSON.stringify(listing));
|
||||
return;
|
||||
}
|
||||
if (request.method === "GET" && pathname === `/api/collections/submissions/records/${submissionId}`) {
|
||||
response.end(JSON.stringify(submission));
|
||||
return;
|
||||
}
|
||||
|
||||
response.writeHead(404);
|
||||
response.end(JSON.stringify({ code: 404, message: "Not found.", data: {} }));
|
||||
});
|
||||
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
server.once("error", reject);
|
||||
server.listen(0, "0.0.0.0", () => {
|
||||
server.off("error", reject);
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
const address = server.address();
|
||||
if (!address || typeof address === "string") throw new Error("Workshop mock server did not bind to a TCP port");
|
||||
|
||||
try {
|
||||
return {
|
||||
url: await exposeHostPortToContainersAsync(address.port),
|
||||
close: () =>
|
||||
new Promise<void>((resolve, reject) => {
|
||||
server.close((error) => (error ? reject(error) : resolve()));
|
||||
server.closeAllConnections();
|
||||
}),
|
||||
};
|
||||
} catch (error) {
|
||||
server.closeAllConnections();
|
||||
server.close();
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
const loginAsAdmin = async (browser: Browser, baseUrl: string, clipboard = false) => {
|
||||
const context = await browser.newContext({
|
||||
permissions: clipboard ? ["clipboard-read", "clipboard-write"] : [],
|
||||
});
|
||||
const page = await context.newPage();
|
||||
await page.goto(`${baseUrl}/auth/login`);
|
||||
await page.waitForLoadState("networkidle");
|
||||
await page.getByLabel("Username").fill(adminCredentials.username);
|
||||
await page.locator("#password").fill(adminCredentials.password);
|
||||
await page.locator("button[type='submit']").click();
|
||||
await page.waitForURL(baseUrl, { timeout: 15_000 });
|
||||
return page;
|
||||
};
|
||||
|
||||
const addCustomWidgetToBoard = async (
|
||||
page: Awaited<ReturnType<typeof loginAsAdmin>>,
|
||||
baseUrl: string,
|
||||
name: string,
|
||||
) => {
|
||||
await page.goto(baseUrl);
|
||||
await page.getByRole("button", { name: "Edit", exact: true }).click();
|
||||
await page.getByRole("button", { name: "New item", exact: true }).click();
|
||||
await page.getByRole("menuitem", { name: "New item", exact: true }).click();
|
||||
const picker = page.getByRole("dialog").last();
|
||||
const customWidgetCard = picker.getByText(name, { exact: true }).locator("xpath=../../..");
|
||||
await customWidgetCard.hover();
|
||||
await customWidgetCard.getByRole("button", { name: "Add to board" }).click();
|
||||
const addDialog = page.getByRole("dialog", { name: `Edit item - ${name}`, exact: true });
|
||||
await expect(addDialog).toBeVisible();
|
||||
await addDialog.getByRole("button", { name: "Save changes", exact: true }).click();
|
||||
await page.getByRole("button", { name: "Save", exact: true }).click();
|
||||
};
|
||||
|
||||
describe("Custom JSX v2 workbench", () => {
|
||||
test("creates, previews, edits, and immediately adds a widget to a board", async () => {
|
||||
@@ -195,7 +30,14 @@ describe("Custom JSX v2 workbench", () => {
|
||||
}).start();
|
||||
const baseUrl = `http://${homarrContainer.getHost()}:${homarrContainer.getMappedPort(7575)}`;
|
||||
browser = await chromium.launch();
|
||||
const page = await loginAsAdmin(browser, baseUrl);
|
||||
const page = await browser.newPage();
|
||||
|
||||
await page.goto(`${baseUrl}/auth/login`);
|
||||
await page.waitForLoadState("networkidle");
|
||||
await page.getByLabel("Username").fill(adminCredentials.username);
|
||||
await page.locator("#password").fill(adminCredentials.password);
|
||||
await page.locator("button[type='submit']").click();
|
||||
await page.waitForURL(baseUrl, { timeout: 15_000 });
|
||||
|
||||
await page.goto(`${baseUrl}/manage/custom-widgets/new`);
|
||||
await page.locator('input[data-path="name"]:visible').fill("E2E Custom JSX v2");
|
||||
@@ -232,222 +74,24 @@ describe("Custom JSX v2 workbench", () => {
|
||||
await page.getByRole("button", { name: "Save", exact: true }).last().click();
|
||||
await expect(page.getByText('Widget "E2E Custom JSX v2" updated successfully.')).toBeVisible({ timeout: 15_000 });
|
||||
|
||||
await addCustomWidgetToBoard(page, baseUrl, "E2E Custom JSX v2");
|
||||
await expect(page.getByText("E2E Widget")).toBeVisible({ timeout: 15_000 });
|
||||
await expect(page.getByText("42", { exact: true })).toBeVisible({ timeout: 15_000 });
|
||||
await page.goto(baseUrl);
|
||||
await page.getByRole("button", { name: "Edit", exact: true }).click();
|
||||
await page.getByRole("button", { name: "New item", exact: true }).click();
|
||||
await page.getByRole("menuitem", { name: "New item", exact: true }).click();
|
||||
const picker = page.getByRole("dialog").last();
|
||||
const customWidgetCard = picker.getByText("E2E Custom JSX v2", { exact: true }).locator("xpath=../../..");
|
||||
await customWidgetCard.hover();
|
||||
await customWidgetCard.getByRole("button", { name: "Add to board" }).click();
|
||||
const addDialog = page.getByRole("dialog").last();
|
||||
await expect(addDialog.getByText("E2E Custom JSX v2")).toBeVisible();
|
||||
await addDialog.getByRole("button", { name: "Save changes", exact: true }).click();
|
||||
|
||||
await expect(page.getByText("Widget definition not found")).not.toBeVisible();
|
||||
await page.getByRole("button", { name: "Save", exact: true }).click();
|
||||
await expect(page.getByText("E2E Widget")).toBeVisible({ timeout: 15_000 });
|
||||
await expect(page.getByText("42")).toBeVisible({ timeout: 15_000 });
|
||||
} finally {
|
||||
await Promise.allSettled([browser?.close(), homarrContainer?.stop(), mockApi?.close()]);
|
||||
}
|
||||
}, 180_000);
|
||||
|
||||
test("installs a Workshop widget, places it on a board, and renders its query", async () => {
|
||||
let mockApi: MockApiServer | undefined;
|
||||
let workshopMock: WorkshopMockServer | undefined;
|
||||
let homarrContainer: StartedTestContainer | undefined;
|
||||
let browser: Browser | undefined;
|
||||
|
||||
try {
|
||||
mockApi = await startMockApiServerAsync({ title: "Workshop query result", status: "online", value: 73 });
|
||||
const workshopWidget = {
|
||||
$schema: "homarr-custom-widget-v2",
|
||||
name: workshopWidgetName,
|
||||
description: "A Workshop installation exercised by the integrated E2E suite.",
|
||||
sources: {
|
||||
default: {
|
||||
name: "E2E API",
|
||||
baseUrl: mockApi.url,
|
||||
networkScope: "private",
|
||||
auth: "none",
|
||||
},
|
||||
},
|
||||
requests: { status: { path: "/status" } },
|
||||
options: {},
|
||||
template: updatedTemplate,
|
||||
} satisfies HomarrCustomWidgetV2Input;
|
||||
workshopMock = await startWorkshopMockServerAsync(workshopWidget);
|
||||
const { db, localMountPath } = await createSqliteDbFileAsync();
|
||||
await seedAdminUserAsync(db, adminCredentials);
|
||||
homarrContainer = await createHomarrContainer({
|
||||
environment: {
|
||||
AUTH_PROVIDERS: "credentials",
|
||||
WORKSHOP_API_URL: workshopMock.url,
|
||||
},
|
||||
mounts: { "/appdata": localMountPath },
|
||||
}).start();
|
||||
const baseUrl = `http://${homarrContainer.getHost()}:${homarrContainer.getMappedPort(7575)}`;
|
||||
browser = await chromium.launch();
|
||||
const page = await loginAsAdmin(browser, baseUrl);
|
||||
|
||||
await page.goto(`${baseUrl}/manage/workshop`);
|
||||
await expect(page.getByText(workshopWidgetName, { exact: true })).toBeVisible({ timeout: 15_000 });
|
||||
await page.getByRole("button", { name: "Inspect widget" }).click();
|
||||
const workshopDialog = page.getByRole("dialog", { name: workshopWidgetName });
|
||||
await expect(
|
||||
workshopDialog.getByText("Installed from the Workshop and rendered on a Homarr board."),
|
||||
).toBeVisible();
|
||||
await workshopDialog.getByRole("button", { name: "Install widget" }).click();
|
||||
|
||||
const importDialog = page.getByRole("dialog", { name: "Review custom widget import" });
|
||||
await importDialog
|
||||
.getByRole("checkbox", {
|
||||
name: "I confirm that this server URL is correct for my Homarr installation",
|
||||
})
|
||||
.check();
|
||||
await importDialog.getByRole("button", { name: "Import widget" }).click();
|
||||
await expect(page.getByText("Widget imported successfully.")).toBeVisible({ timeout: 15_000 });
|
||||
await page.waitForURL("**/manage/custom-widgets/edit/**", { timeout: 15_000 });
|
||||
|
||||
await addCustomWidgetToBoard(page, baseUrl, workshopWidgetName);
|
||||
await expect(page.getByText("Workshop query result")).toBeVisible({ timeout: 15_000 });
|
||||
await expect(page.getByText("73")).toBeVisible({ timeout: 15_000 });
|
||||
await expect(page.getByText("Widget definition not found")).not.toBeVisible();
|
||||
} finally {
|
||||
await Promise.allSettled([browser?.close(), homarrContainer?.stop(), workshopMock?.close(), mockApi?.close()]);
|
||||
}
|
||||
}, 180_000);
|
||||
|
||||
test("migrates a redacted preserved v1 widget and resurfaces it when the v2 replacement is deleted", async () => {
|
||||
let homarrContainer: StartedTestContainer | undefined;
|
||||
let browser: Browser | undefined;
|
||||
|
||||
const encryptedValue = "preserved.ciphertext" as `${string}.${string}`;
|
||||
const migratedWidget = {
|
||||
$schema: "homarr-custom-widget-v2",
|
||||
name: legacyWidgetName,
|
||||
description: "Validated v2 replacement for the preserved legacy widget.",
|
||||
sources: {
|
||||
default: {
|
||||
name: "Legacy API",
|
||||
baseUrl: "https://legacy-api.homarr.dev/api",
|
||||
networkScope: "public",
|
||||
auth: { type: "apiKeyHeader", name: "X-E2E-Key" },
|
||||
},
|
||||
},
|
||||
requests: { status: { path: "/status" } },
|
||||
options: {},
|
||||
template: "<Stack><Text>{data.status?.value}</Text></Stack>",
|
||||
} satisfies HomarrCustomWidgetV2Input;
|
||||
|
||||
try {
|
||||
const { db, localMountPath } = await createSqliteDbFileAsync();
|
||||
const { userId } = await seedAdminUserAsync(db, adminCredentials);
|
||||
await db.insert(legacyCustomWidgetDefinitions).values({
|
||||
id: legacyWidgetId,
|
||||
name: legacyWidgetName,
|
||||
description: "Preserved v1 fixture",
|
||||
url: "https://legacy-user:super-secret@legacy-api.homarr.dev/api/status?token=token-value#fragment",
|
||||
authType: "apiKeyHeader",
|
||||
headerName: "X-E2E-Key",
|
||||
method: "POST",
|
||||
requestBody: JSON.stringify({ password: "nested-secret", filters: ["private-filter"] }),
|
||||
displayType: "singleValue",
|
||||
displayConfig: stringifySuperJson({ type: "singleValue", jsonPath: "$.value" }),
|
||||
enabled: true,
|
||||
creatorId: userId,
|
||||
});
|
||||
await db.insert(legacyCustomWidgetSecrets).values({
|
||||
definitionId: legacyWidgetId,
|
||||
kind: "apiKey",
|
||||
encryptedValue,
|
||||
updatedAt: new Date(0),
|
||||
});
|
||||
|
||||
homarrContainer = await createHomarrContainer({
|
||||
environment: { AUTH_PROVIDERS: "credentials" },
|
||||
mounts: { "/appdata": localMountPath },
|
||||
}).start();
|
||||
const baseUrl = `http://${homarrContainer.getHost()}:${homarrContainer.getMappedPort(7575)}`;
|
||||
browser = await chromium.launch();
|
||||
const page = await loginAsAdmin(browser, baseUrl, true);
|
||||
|
||||
await page.goto(`${baseUrl}/manage/custom-widgets`);
|
||||
const legacyCard = page
|
||||
.getByText(legacyWidgetName, { exact: true })
|
||||
.locator("xpath=ancestor::*[contains(@class, 'mantine-Card-root')]");
|
||||
await expect(legacyCard.getByText("Migration required", { exact: true })).toBeVisible();
|
||||
await legacyCard.getByRole("button", { name: "Actions" }).click();
|
||||
await page.getByRole("menuitem", { name: "Copy LLM migration prompt" }).click();
|
||||
await expect(page.getByText(/A redacted migration prompt was copied/u)).toBeVisible({ timeout: 15_000 });
|
||||
const prompt = await page.evaluate(() => navigator.clipboard.readText());
|
||||
expect(prompt).toContain("[REDACTED]");
|
||||
expect(prompt).not.toContain("legacy-user");
|
||||
expect(prompt).not.toContain("super-secret");
|
||||
expect(prompt).not.toContain("token-value");
|
||||
expect(prompt).not.toContain("nested-secret");
|
||||
expect(prompt).not.toContain("private-filter");
|
||||
|
||||
await page.evaluate((widget) => navigator.clipboard.writeText(JSON.stringify(widget)), migratedWidget);
|
||||
await legacyCard.getByRole("button", { name: "Actions" }).click();
|
||||
await page.getByRole("menuitem", { name: "Paste migrated widget" }).click();
|
||||
const migrationDialog = page.getByRole("dialog", { name: "Review migrated v2 replacement" });
|
||||
await migrationDialog.getByRole("button", { name: "Confirm v2 replacement" }).click();
|
||||
await expect(
|
||||
page.getByText("Legacy widget migrated successfully. Existing board placements now use the v2 definition."),
|
||||
).toBeVisible({ timeout: 15_000 });
|
||||
|
||||
expect(
|
||||
await db.query.legacyCustomWidgetDefinitions.findFirst({
|
||||
where: eq(legacyCustomWidgetDefinitions.id, legacyWidgetId),
|
||||
}),
|
||||
).toMatchObject({ id: legacyWidgetId, name: legacyWidgetName });
|
||||
expect(
|
||||
await db.query.legacyCustomWidgetSecrets.findFirst({
|
||||
where: eq(legacyCustomWidgetSecrets.definitionId, legacyWidgetId),
|
||||
}),
|
||||
).toMatchObject({ definitionId: legacyWidgetId, encryptedValue });
|
||||
expect(
|
||||
await db.query.customWidgetDefinitions.findFirst({
|
||||
where: eq(customWidgetDefinitions.id, legacyWidgetId),
|
||||
}),
|
||||
).toMatchObject({ id: legacyWidgetId, name: legacyWidgetName });
|
||||
expect(
|
||||
await db.query.customWidgetSecrets.findFirst({
|
||||
where: eq(customWidgetSecrets.definitionId, legacyWidgetId),
|
||||
}),
|
||||
).toMatchObject({
|
||||
definitionId: legacyWidgetId,
|
||||
sourceId: "default",
|
||||
kind: "apiKey",
|
||||
encryptedValue,
|
||||
});
|
||||
|
||||
await page.reload();
|
||||
const migratedCard = page
|
||||
.getByRole("main")
|
||||
.getByText(legacyWidgetName, { exact: true })
|
||||
.locator("xpath=ancestor::*[contains(@class, 'mantine-Card-root')]");
|
||||
await expect(migratedCard.getByText("Migration required", { exact: true })).not.toBeVisible();
|
||||
await migratedCard.getByRole("button", { name: "Actions" }).click();
|
||||
await page.getByRole("menuitem", { name: "Delete Custom Widget" }).click();
|
||||
const deleteDialog = page.getByRole("dialog", { name: "Delete Custom Widget" });
|
||||
await deleteDialog.getByRole("button", { name: "Confirm", exact: true }).click();
|
||||
await expect(page.getByText(`Widget "${legacyWidgetName}" deleted.`)).toBeVisible({ timeout: 15_000 });
|
||||
|
||||
expect(
|
||||
await db.query.customWidgetDefinitions.findFirst({
|
||||
where: eq(customWidgetDefinitions.id, legacyWidgetId),
|
||||
}),
|
||||
).toBeUndefined();
|
||||
expect(
|
||||
await db.query.legacyCustomWidgetDefinitions.findFirst({
|
||||
where: eq(legacyCustomWidgetDefinitions.id, legacyWidgetId),
|
||||
}),
|
||||
).toMatchObject({ id: legacyWidgetId, name: legacyWidgetName });
|
||||
expect(
|
||||
await db.query.legacyCustomWidgetSecrets.findFirst({
|
||||
where: eq(legacyCustomWidgetSecrets.definitionId, legacyWidgetId),
|
||||
}),
|
||||
).toMatchObject({ definitionId: legacyWidgetId, encryptedValue });
|
||||
|
||||
await page.reload();
|
||||
const resurfacedLegacyCard = page
|
||||
.getByRole("main")
|
||||
.getByText(legacyWidgetName, { exact: true })
|
||||
.locator("xpath=ancestor::*[contains(@class, 'mantine-Card-root')]");
|
||||
await expect(resurfacedLegacyCard.getByText("Migration required", { exact: true })).toBeVisible();
|
||||
} finally {
|
||||
await Promise.allSettled([browser?.close(), homarrContainer?.stop()]);
|
||||
}
|
||||
}, 180_000);
|
||||
});
|
||||
+44
-47
@@ -1,5 +1,4 @@
|
||||
import { chromium } from "@playwright/test";
|
||||
import type { Browser } from "@playwright/test";
|
||||
import { describe, test } from "vitest";
|
||||
|
||||
import { OnboardingActions } from "./shared/actions/onboarding-actions";
|
||||
@@ -17,32 +16,31 @@ describe("Onboarding", () => {
|
||||
},
|
||||
}).start();
|
||||
|
||||
let browser: Browser | undefined;
|
||||
try {
|
||||
browser = await chromium.launch();
|
||||
const context = await browser.newContext();
|
||||
const page = await context.newPage();
|
||||
const actions = new OnboardingActions(page, db);
|
||||
const assertions = new OnboardingAssertions(page, db);
|
||||
const browser = await chromium.launch();
|
||||
const context = await browser.newContext();
|
||||
const page = await context.newPage();
|
||||
const actions = new OnboardingActions(page, db);
|
||||
const assertions = new OnboardingAssertions(page, db);
|
||||
|
||||
// Act
|
||||
await page.goto(`http://${homarrContainer.getHost()}:${homarrContainer.getMappedPort(7575)}`);
|
||||
await actions.startOnboardingAsync("scratch");
|
||||
await actions.processUserStepAsync({
|
||||
username: "admin",
|
||||
password: "Comp(exP4sswOrd",
|
||||
confirmPassword: "Comp(exP4sswOrd",
|
||||
});
|
||||
await actions.processSettingsStepAsync();
|
||||
await actions.processIntegrationsStepAsync();
|
||||
// Act
|
||||
await page.goto(`http://${homarrContainer.getHost()}:${homarrContainer.getMappedPort(7575)}`);
|
||||
await actions.startOnboardingAsync("scratch");
|
||||
await actions.processUserStepAsync({
|
||||
username: "admin",
|
||||
password: "Comp(exP4sswOrd",
|
||||
confirmPassword: "Comp(exP4sswOrd",
|
||||
});
|
||||
await actions.processSettingsStepAsync();
|
||||
await actions.processIntegrationsStepAsync();
|
||||
|
||||
// Assert
|
||||
await assertions.assertFinishStepVisibleAsync();
|
||||
await assertions.assertUserAndAdminGroupInsertedAsync("admin");
|
||||
await assertions.assertDbOnboardingStepAsync("finish");
|
||||
} finally {
|
||||
await Promise.allSettled([browser?.close(), homarrContainer.stop()]);
|
||||
}
|
||||
// Assert
|
||||
await assertions.assertFinishStepVisibleAsync();
|
||||
await assertions.assertUserAndAdminGroupInsertedAsync("admin");
|
||||
await assertions.assertDbOnboardingStepAsync("finish");
|
||||
|
||||
// Cleanup
|
||||
await browser.close();
|
||||
await homarrContainer.stop();
|
||||
}, 60_000);
|
||||
|
||||
test("External provider onboarding setup should be successful", async () => {
|
||||
@@ -62,29 +60,28 @@ describe("Onboarding", () => {
|
||||
}).start();
|
||||
const externalGroupName = "oidc-admins";
|
||||
|
||||
let browser: Browser | undefined;
|
||||
try {
|
||||
browser = await chromium.launch();
|
||||
const context = await browser.newContext();
|
||||
const page = await context.newPage();
|
||||
const actions = new OnboardingActions(page, db);
|
||||
const assertions = new OnboardingAssertions(page, db);
|
||||
const browser = await chromium.launch();
|
||||
const context = await browser.newContext();
|
||||
const page = await context.newPage();
|
||||
const actions = new OnboardingActions(page, db);
|
||||
const assertions = new OnboardingAssertions(page, db);
|
||||
|
||||
// Act
|
||||
await page.goto(`http://${homarrContainer.getHost()}:${homarrContainer.getMappedPort(7575)}`);
|
||||
await actions.startOnboardingAsync("scratch");
|
||||
await actions.processExternalGroupStepAsync({
|
||||
name: externalGroupName,
|
||||
});
|
||||
await actions.processSettingsStepAsync();
|
||||
await actions.processIntegrationsStepAsync();
|
||||
// Act
|
||||
await page.goto(`http://${homarrContainer.getHost()}:${homarrContainer.getMappedPort(7575)}`);
|
||||
await actions.startOnboardingAsync("scratch");
|
||||
await actions.processExternalGroupStepAsync({
|
||||
name: externalGroupName,
|
||||
});
|
||||
await actions.processSettingsStepAsync();
|
||||
await actions.processIntegrationsStepAsync();
|
||||
|
||||
// Assert
|
||||
await assertions.assertFinishStepVisibleAsync();
|
||||
await assertions.assertExternalGroupInsertedAsync(externalGroupName);
|
||||
await assertions.assertDbOnboardingStepAsync("finish");
|
||||
} finally {
|
||||
await Promise.allSettled([browser?.close(), homarrContainer.stop()]);
|
||||
}
|
||||
// Assert
|
||||
await assertions.assertFinishStepVisibleAsync();
|
||||
await assertions.assertExternalGroupInsertedAsync(externalGroupName);
|
||||
await assertions.assertDbOnboardingStepAsync("finish");
|
||||
|
||||
// Cleanup
|
||||
await browser.close();
|
||||
await homarrContainer.stop();
|
||||
}, 60_000);
|
||||
});
|
||||
@@ -28,12 +28,11 @@ export class OnboardingActions {
|
||||
}
|
||||
|
||||
public async startOnboardingAsync(type: "scratch" | "before 1.0") {
|
||||
const name = type === "scratch" ? "Start from scratch" : "Import from Homarr before 1.0";
|
||||
await this.page.getByRole("button", { name, exact: true }).click();
|
||||
await this.page.locator("button", { hasText: type }).click();
|
||||
}
|
||||
|
||||
public async processUserStepAsync(input: { username: string; password: string; confirmPassword: string }) {
|
||||
await this.page.getByLabel("Username").waitFor({ state: "visible" });
|
||||
await this.page.waitForSelector("text=administrator user");
|
||||
|
||||
await this.page.getByLabel("Username").fill(input.username);
|
||||
await this.page.getByLabel("Password", { exact: true }).fill(input.password);
|
||||
|
||||
@@ -10,19 +10,6 @@ export interface MockApiServer {
|
||||
close: () => Promise<void>;
|
||||
}
|
||||
|
||||
export async function exposeHostPortToContainersAsync(port: number): Promise<string> {
|
||||
await TestContainers.exposeHostPorts(port);
|
||||
const client = await getContainerRuntimeClient();
|
||||
const forwarder = (await client.container.list()).find(
|
||||
(container) => container.State === "running" && container.Labels[TESTCONTAINERS_SSHD_LABEL] === "true",
|
||||
);
|
||||
const forwarderAddress = forwarder
|
||||
? Object.values(forwarder.NetworkSettings.Networks).find((network) => network.IPAddress)?.IPAddress
|
||||
: undefined;
|
||||
if (!forwarderAddress) throw new Error("Failed to locate the Testcontainers host-port forwarder");
|
||||
return `http://${forwarderAddress}:${port}`;
|
||||
}
|
||||
|
||||
export async function startMockApiServerAsync(responseBody: unknown): Promise<MockApiServer> {
|
||||
const body = JSON.stringify(responseBody);
|
||||
const server: Server = await new Promise((resolve) => {
|
||||
@@ -39,8 +26,18 @@ export async function startMockApiServerAsync(responseBody: unknown): Promise<Mo
|
||||
}
|
||||
|
||||
try {
|
||||
await TestContainers.exposeHostPorts(address.port);
|
||||
const client = await getContainerRuntimeClient();
|
||||
const forwarder = (await client.container.list()).find(
|
||||
(container) => container.State === "running" && container.Labels[TESTCONTAINERS_SSHD_LABEL] === "true",
|
||||
);
|
||||
const forwarderAddress = forwarder
|
||||
? Object.values(forwarder.NetworkSettings.Networks).find((network) => network.IPAddress)?.IPAddress
|
||||
: undefined;
|
||||
if (!forwarderAddress) throw new Error("Failed to locate the Testcontainers host-port forwarder");
|
||||
|
||||
return {
|
||||
url: await exposeHostPortToContainersAsync(address.port),
|
||||
url: `http://${forwarderAddress}:${address.port}`,
|
||||
close: () =>
|
||||
new Promise((resolve, reject) => {
|
||||
server.close((error) => (error ? reject(error) : resolve()));
|
||||
|
||||
@@ -63,7 +63,6 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@homarr/tsconfig": "workspace:^0.1.0",
|
||||
"ioredis": "catalog:",
|
||||
"typescript": "catalog:"
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
export * from "./router/custom-widget/configuration-requests";
|
||||
export { configurePreviewSessionSource } from "./router/custom-widget/preview-sessions";
|
||||
export { invalidateCustomWidgetResponseCache } from "./router/custom-widget/request-executor";
|
||||
export {
|
||||
parseStoredCustomWidgetDefinition,
|
||||
serializeCustomWidgetDefinition,
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
export function useProcessLocalCustomWidgetState(runtime: { CI?: string; NODE_ENV?: string } = process.env): boolean {
|
||||
if (runtime.NODE_ENV === "production") return false;
|
||||
return runtime.NODE_ENV === "test" || runtime.CI === "true";
|
||||
}
|
||||
@@ -4,8 +4,6 @@ import type { RedisClient } from "@homarr/core/infrastructure/redis";
|
||||
import type { CustomWidgetSecretKind } from "@homarr/custom-widgets/core";
|
||||
import type { CustomWidgetSource } from "@homarr/custom-widgets/core";
|
||||
|
||||
import { useProcessLocalCustomWidgetState } from "../../custom-widget-state-mode";
|
||||
|
||||
const CONFIGURATION_REQUEST_TTL_MS = 10 * 60_000;
|
||||
const CONFIGURATION_REQUEST_PREFIX = "custom-widget:configuration-request:";
|
||||
const CONFIGURATION_REQUEST_LOCK_PREFIX = "custom-widget:configuration-request-lock:";
|
||||
@@ -28,7 +26,7 @@ const localLocks = new Set<string>();
|
||||
let redis: RedisClient | undefined;
|
||||
|
||||
function getRedis() {
|
||||
if (useProcessLocalCustomWidgetState()) return undefined;
|
||||
if (process.env.CI !== undefined || process.env.NODE_ENV === "test") return undefined;
|
||||
redis ??= createRedisClient();
|
||||
return redis;
|
||||
}
|
||||
|
||||
@@ -12,9 +12,8 @@ import {
|
||||
customWidgetUpdateSchema,
|
||||
} from "@homarr/custom-widgets/core";
|
||||
|
||||
import { createTRPCRouter } from "../../trpc";
|
||||
import { createTRPCRouter, permissionRequiredProcedure } from "../../trpc";
|
||||
import { insertCustomWidgetDefinition } from "./definition-insert";
|
||||
import { permissionRequiredProcedure } from "../../trpc";
|
||||
import { managementQueryProcedures } from "./management-queries";
|
||||
import { metadataProcedures } from "./metadata-procedures";
|
||||
import { previewActionProcedures } from "./preview-action-procedures";
|
||||
|
||||
@@ -6,8 +6,8 @@ import { eq, or } from "@homarr/db";
|
||||
import { boards, customWidgetDefinitions, legacyCustomWidgetDefinitions } from "@homarr/db/schema";
|
||||
import { collectCustomWidgetRequestReferences } from "@homarr/custom-widgets/core";
|
||||
|
||||
import { throwIfActionForbiddenAsync } from "../board/board-access";
|
||||
import { permissionRequiredProcedure } from "../../trpc";
|
||||
import { throwIfActionForbiddenAsync } from "../board/board-access";
|
||||
import { parseStoredCustomWidgetDefinition } from "./stored-definition";
|
||||
import { executeCustomWidgetRequest } from "./request-executor";
|
||||
import {
|
||||
@@ -143,10 +143,16 @@ export const managementQueryProcedures = {
|
||||
await throwIfActionForbiddenAsync(ctx, eq(boards.id, input.boardId), request.permission);
|
||||
const source = definition.sources[request.source];
|
||||
if (!source) throw new TRPCError({ code: "NOT_FOUND", message: "Option query source not found" });
|
||||
const values = resolveOptionRequestValues(request, input.params);
|
||||
const authType = typeof source.auth === "string" ? source.auth : source.auth.type;
|
||||
const response = await executeCustomWidgetRequest(
|
||||
{
|
||||
const release = await acquireCustomWidgetRequestLimit({
|
||||
category: "query",
|
||||
userId: ctx.session.user.id,
|
||||
itemId: `configuration:${input.boardId}`,
|
||||
definitionId: input.definitionId,
|
||||
});
|
||||
try {
|
||||
const values = resolveOptionRequestValues(request, input.params);
|
||||
const authType = typeof source.auth === "string" ? source.auth : source.auth.type;
|
||||
const response = await executeCustomWidgetRequest({
|
||||
baseUrl: source.baseUrl,
|
||||
targetUrl: renderRequestTarget(source.baseUrl, request, values),
|
||||
method: request.method,
|
||||
@@ -171,23 +177,16 @@ export const managementQueryProcedures = {
|
||||
kind: "query",
|
||||
cacheKey: `custom-widget:options:${input.definitionId}:${getCustomWidgetCacheVersion(stored)}:${request.id}:${hashRuntimeParams(values)}`,
|
||||
cacheTtlSeconds: request.cacheSeconds ?? 30,
|
||||
},
|
||||
{
|
||||
acquireRequestLimit: () =>
|
||||
acquireCustomWidgetRequestLimit({
|
||||
category: "query",
|
||||
userId: ctx.session.user.id,
|
||||
itemId: `configuration:${input.boardId}`,
|
||||
definitionId: input.definitionId,
|
||||
}),
|
||||
},
|
||||
);
|
||||
return {
|
||||
ok: response.ok,
|
||||
status: response.status,
|
||||
data: response.data,
|
||||
error: response.ok ? undefined : `HTTP ${response.status}: ${response.statusText}`,
|
||||
};
|
||||
});
|
||||
return {
|
||||
ok: response.ok,
|
||||
status: response.status,
|
||||
data: response.data,
|
||||
error: response.ok ? undefined : `HTTP ${response.status}: ${response.statusText}`,
|
||||
};
|
||||
} finally {
|
||||
await release();
|
||||
}
|
||||
}),
|
||||
};
|
||||
|
||||
|
||||
@@ -45,9 +45,15 @@ export const previewActionProcedures = {
|
||||
if ((request.confirmation || request.method === "DELETE") && input.confirmed !== true) {
|
||||
throw new TRPCError({ code: "PRECONDITION_FAILED", message: "This action requires confirmation" });
|
||||
}
|
||||
const release = await acquireCustomWidgetRequestLimit({
|
||||
category: request.method === "DELETE" ? "delete" : "action",
|
||||
userId: ctx.session.user.id,
|
||||
itemId: `preview:${session.id}`,
|
||||
definitionId: session.definitionId ?? `preview:${session.id}`,
|
||||
});
|
||||
const startedAt = Date.now();
|
||||
const response = await executeCustomWidgetRequest(
|
||||
{
|
||||
try {
|
||||
const response = await executeCustomWidgetRequest({
|
||||
baseUrl: resolved.source.baseUrl,
|
||||
targetUrl,
|
||||
method: request.method,
|
||||
@@ -56,38 +62,31 @@ export const previewActionProcedures = {
|
||||
auth: request.auth === "none" ? undefined : resolved.auth,
|
||||
networkScope: resolved.source.networkScope,
|
||||
kind: "action",
|
||||
},
|
||||
{
|
||||
acquireRequestLimit: () =>
|
||||
acquireCustomWidgetRequestLimit({
|
||||
category: request.method === "DELETE" ? "delete" : "action",
|
||||
userId: ctx.session.user.id,
|
||||
itemId: `preview:${session.id}`,
|
||||
definitionId: session.definitionId ?? `preview:${session.id}`,
|
||||
}),
|
||||
},
|
||||
);
|
||||
if (response.ok && request.invalidates?.length) {
|
||||
await invalidateCustomWidgetResponseCache(
|
||||
request.invalidates.map((requestId) => `custom-jsx:preview:${session.id}:${requestId}:`),
|
||||
);
|
||||
});
|
||||
if (response.ok && request.invalidates?.length) {
|
||||
invalidateCustomWidgetResponseCache(
|
||||
request.invalidates.map((requestId) => `custom-jsx:preview:${session.id}:${requestId}:`),
|
||||
);
|
||||
}
|
||||
await recordPreviewJournal(session, {
|
||||
requestId: request.id,
|
||||
kind: "action",
|
||||
method: request.method,
|
||||
path: request.path,
|
||||
status: response.status,
|
||||
durationMs: Date.now() - startedAt,
|
||||
simulated: false,
|
||||
});
|
||||
return {
|
||||
ok: response.ok,
|
||||
status: response.status,
|
||||
statusText: response.statusText,
|
||||
data: response.data,
|
||||
error: response.ok ? undefined : `HTTP ${response.status}: ${response.statusText}`,
|
||||
simulated: false as const,
|
||||
};
|
||||
} finally {
|
||||
await release();
|
||||
}
|
||||
await recordPreviewJournal(session, {
|
||||
requestId: request.id,
|
||||
kind: "action",
|
||||
method: request.method,
|
||||
path: request.path,
|
||||
status: response.status,
|
||||
durationMs: Date.now() - startedAt,
|
||||
simulated: false,
|
||||
});
|
||||
return {
|
||||
ok: response.ok,
|
||||
status: response.status,
|
||||
statusText: response.statusText,
|
||||
data: response.data,
|
||||
error: response.ok ? undefined : `HTTP ${response.status}: ${response.statusText}`,
|
||||
simulated: false as const,
|
||||
};
|
||||
}),
|
||||
};
|
||||
@@ -9,7 +9,6 @@ import {
|
||||
resolvePreviewRequestParams,
|
||||
} from "./preview-procedure-helpers";
|
||||
import { executeCustomWidgetRequest } from "./request-executor";
|
||||
import { invalidateCustomWidgetResponseCache } from "./request-executor";
|
||||
import { hashRuntimeParams, renderRequestBody, renderRequestTarget } from "./request-manifest";
|
||||
import { acquireCustomWidgetRequestLimit } from "./request-limits";
|
||||
import { getPreviewJournal, getPreviewSession, setPreviewSessionLiveActions } from "./preview-sessions";
|
||||
@@ -35,9 +34,15 @@ export const previewQueryProcedures = {
|
||||
const params = resolvePreviewRequestParams(request, session.options, input.params);
|
||||
const targetUrl = renderRequestTarget(resolved.source.baseUrl, request, params);
|
||||
const body = renderRequestBody(request, params);
|
||||
const release = await acquireCustomWidgetRequestLimit({
|
||||
category: "query",
|
||||
userId: ctx.session.user.id,
|
||||
itemId: `preview:${session.id}`,
|
||||
definitionId: session.definitionId ?? `preview:${session.id}`,
|
||||
});
|
||||
const startedAt = Date.now();
|
||||
const response = await executeCustomWidgetRequest(
|
||||
{
|
||||
try {
|
||||
const response = await executeCustomWidgetRequest({
|
||||
baseUrl: resolved.source.baseUrl,
|
||||
targetUrl,
|
||||
method: request.method,
|
||||
@@ -48,67 +53,26 @@ export const previewQueryProcedures = {
|
||||
kind: "query",
|
||||
cacheKey: `custom-jsx:preview:${session.id}:${request.id}:${hashRuntimeParams(params)}`,
|
||||
cacheTtlSeconds: request.cacheSeconds,
|
||||
},
|
||||
{
|
||||
acquireRequestLimit: () =>
|
||||
acquireCustomWidgetRequestLimit({
|
||||
category: "query",
|
||||
userId: ctx.session.user.id,
|
||||
itemId: `preview:${session.id}`,
|
||||
definitionId: session.definitionId ?? `preview:${session.id}`,
|
||||
}),
|
||||
},
|
||||
);
|
||||
await recordPreviewJournal(session, {
|
||||
requestId: request.id,
|
||||
kind: "query",
|
||||
method: request.method,
|
||||
path: request.path,
|
||||
status: response.status,
|
||||
durationMs: Date.now() - startedAt,
|
||||
simulated: false,
|
||||
});
|
||||
return {
|
||||
ok: response.ok,
|
||||
status: response.status,
|
||||
statusText: response.statusText,
|
||||
data: response.data,
|
||||
error: response.ok ? undefined : `HTTP ${response.status}: ${response.statusText}`,
|
||||
};
|
||||
}),
|
||||
|
||||
previewRefresh: permissionRequiredProcedure
|
||||
.requiresPermission("admin")
|
||||
.input(
|
||||
z.object({
|
||||
sessionId: z.string().min(1),
|
||||
requestIds: z.array(z.string().min(1).max(64)).max(64).default([]),
|
||||
all: z.boolean().default(false),
|
||||
}),
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const session = await getPreviewSession(input.sessionId, ctx.session.user.id);
|
||||
const queryRequestIds = Object.entries(session.requests).flatMap(([requestId, request]) =>
|
||||
request.kind === "query" ? [requestId] : [],
|
||||
);
|
||||
const requested = new Set(input.all ? queryRequestIds : input.requestIds);
|
||||
const requestIds = queryRequestIds.filter((requestId) => requested.has(requestId));
|
||||
if (requestIds.length === 0) return { requestIds };
|
||||
|
||||
const release = await acquireCustomWidgetRequestLimit({
|
||||
category: "query",
|
||||
userId: ctx.session.user.id,
|
||||
itemId: `preview:${session.id}`,
|
||||
definitionId: session.definitionId ?? `preview:${session.id}`,
|
||||
});
|
||||
try {
|
||||
await invalidateCustomWidgetResponseCache(
|
||||
requestIds.map((requestId) => `custom-jsx:preview:${session.id}:${requestId}:`),
|
||||
);
|
||||
});
|
||||
await recordPreviewJournal(session, {
|
||||
requestId: request.id,
|
||||
kind: "query",
|
||||
method: request.method,
|
||||
path: request.path,
|
||||
status: response.status,
|
||||
durationMs: Date.now() - startedAt,
|
||||
simulated: false,
|
||||
});
|
||||
return {
|
||||
ok: response.ok,
|
||||
status: response.status,
|
||||
statusText: response.statusText,
|
||||
data: response.data,
|
||||
error: response.ok ? undefined : `HTTP ${response.status}: ${response.statusText}`,
|
||||
};
|
||||
} finally {
|
||||
await release();
|
||||
}
|
||||
return { requestIds };
|
||||
}),
|
||||
|
||||
setPreviewLiveActions: permissionRequiredProcedure
|
||||
|
||||
@@ -10,7 +10,6 @@ import type {
|
||||
PreviewSessionStore,
|
||||
} from "@homarr/custom-widgets/server";
|
||||
|
||||
import { useProcessLocalCustomWidgetState } from "../../custom-widget-state-mode";
|
||||
import { toTrpcError } from "./domain-error";
|
||||
|
||||
const SESSION_PREFIX = "custom-widget:preview-session:";
|
||||
@@ -70,7 +69,7 @@ class RedisPreviewSessionStore implements PreviewSessionStore {
|
||||
let service: CustomWidgetPreviewSessionService | undefined;
|
||||
function getService() {
|
||||
if (service) return service;
|
||||
const useLocal = useProcessLocalCustomWidgetState();
|
||||
const useLocal = process.env.CI !== undefined || process.env.NODE_ENV === "test";
|
||||
service = new CustomWidgetPreviewSessionService({
|
||||
createId,
|
||||
encrypt: encryptSecret,
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
import { TRPCError } from "@trpc/server";
|
||||
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
import {
|
||||
assertJsonBudget as assertDomainJsonBudget,
|
||||
assertSafeStaticHeaders as assertDomainSafeStaticHeaders,
|
||||
classifyAddress,
|
||||
executeCustomWidgetRequest as executeDomainRequest,
|
||||
invalidateCustomWidgetResponseCache as invalidateDomainResponseCache,
|
||||
MAX_REQUEST_BODY_BYTES,
|
||||
MAX_RESPONSE_BODY_BYTES,
|
||||
MAX_RESPONSE_JSON_DEPTH,
|
||||
@@ -15,10 +14,8 @@ import {
|
||||
validateCustomWidgetUrl as validateDomainUrl,
|
||||
} from "@homarr/custom-widgets/server";
|
||||
import type { CustomWidgetHttpRequest, CustomWidgetHttpResponse } from "@homarr/custom-widgets/server";
|
||||
import { CustomWidgetDomainError } from "@homarr/custom-widgets/server";
|
||||
|
||||
import { toTrpcError } from "./domain-error";
|
||||
import { invalidateCustomWidgetResponseCache, withCustomWidgetResponseCache } from "./response-cache";
|
||||
|
||||
export type {
|
||||
CustomWidgetAuthConfig,
|
||||
@@ -35,10 +32,6 @@ export {
|
||||
|
||||
const logger = createLogger({ module: "custom-widget:http" });
|
||||
|
||||
export interface CustomWidgetRequestExecutionOptions {
|
||||
acquireRequestLimit?: () => Promise<() => Promise<void>>;
|
||||
}
|
||||
|
||||
export function assertJsonBudget(value: unknown): void {
|
||||
try {
|
||||
assertDomainJsonBudget(value);
|
||||
@@ -79,64 +72,16 @@ export async function resolveAndValidateHost(hostname: string, scope: CustomWidg
|
||||
}
|
||||
}
|
||||
|
||||
export async function executeCustomWidgetRequest(
|
||||
input: CustomWidgetHttpRequest,
|
||||
options: CustomWidgetRequestExecutionOptions = {},
|
||||
): Promise<CustomWidgetHttpResponse> {
|
||||
export async function executeCustomWidgetRequest(input: CustomWidgetHttpRequest): Promise<CustomWidgetHttpResponse> {
|
||||
try {
|
||||
return await withCustomWidgetResponseCache(input, async () => {
|
||||
const release = await options.acquireRequestLimit?.();
|
||||
try {
|
||||
return await executeDomainRequest({
|
||||
...input,
|
||||
cacheKey: undefined,
|
||||
cacheTtlSeconds: undefined,
|
||||
logError: (event) => {
|
||||
if (event.reason === "timeout") return;
|
||||
logger.error("Custom widget request failed", {
|
||||
event: "custom_widget_request_failed",
|
||||
...event,
|
||||
});
|
||||
},
|
||||
});
|
||||
} finally {
|
||||
await release?.();
|
||||
}
|
||||
return await executeDomainRequest({
|
||||
...input,
|
||||
logError: (event) => logger.error("Custom widget request failed", event),
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof CustomWidgetDomainError) {
|
||||
logger.warn("Custom widget request rejected", {
|
||||
event:
|
||||
error.reason === "timeout"
|
||||
? "custom_widget_request_timeout"
|
||||
: error.code === "FORBIDDEN"
|
||||
? "custom_widget_network_scope_rejected"
|
||||
: error.code === "BAD_GATEWAY"
|
||||
? "custom_widget_upstream_failed"
|
||||
: "custom_widget_request_rejected",
|
||||
code: error.code,
|
||||
origin: safeOrigin(input.baseUrl),
|
||||
method: input.method,
|
||||
});
|
||||
toTrpcError(error);
|
||||
}
|
||||
if (error instanceof TRPCError) throw error;
|
||||
logger.error("Custom widget request failed", {
|
||||
event: "custom_widget_request_failed",
|
||||
errorName: "UnexpectedTransportError",
|
||||
origin: safeOrigin(input.baseUrl),
|
||||
method: input.method,
|
||||
});
|
||||
throw new TRPCError({ code: "BAD_GATEWAY", message: "External request failed" });
|
||||
toTrpcError(error);
|
||||
}
|
||||
}
|
||||
|
||||
export { invalidateCustomWidgetResponseCache };
|
||||
|
||||
function safeOrigin(value: string): string {
|
||||
try {
|
||||
return new URL(value).origin;
|
||||
} catch {
|
||||
return "invalid";
|
||||
}
|
||||
}
|
||||
export const invalidateCustomWidgetResponseCache = (prefixes: readonly string[]) =>
|
||||
invalidateDomainResponseCache(prefixes);
|
||||
@@ -1,10 +1,9 @@
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
import { createRedisClient } from "@homarr/core/infrastructure/redis";
|
||||
import type { RedisClient } from "@homarr/core/infrastructure/redis";
|
||||
import { CustomWidgetDomainError, CustomWidgetRequestLimiter } from "@homarr/custom-widgets/server";
|
||||
import { CustomWidgetRequestLimiter } from "@homarr/custom-widgets/server";
|
||||
import type { RequestLimitInput, RequestLimitStore } from "@homarr/custom-widgets/server";
|
||||
|
||||
import { useProcessLocalCustomWidgetState } from "../../custom-widget-state-mode";
|
||||
import { toTrpcError } from "./domain-error";
|
||||
|
||||
const logger = createLogger({ module: "custom-widget:limits" });
|
||||
@@ -46,13 +45,12 @@ class RedisRequestLimitStore implements RequestLimitStore {
|
||||
let limiter: CustomWidgetRequestLimiter | undefined;
|
||||
function getLimiter() {
|
||||
if (limiter) return limiter;
|
||||
const useLocal = useProcessLocalCustomWidgetState();
|
||||
const useLocal = process.env.CI !== undefined || process.env.NODE_ENV === "test";
|
||||
const store = useLocal ? undefined : new RedisRequestLimitStore(createRedisClient());
|
||||
limiter = new CustomWidgetRequestLimiter({
|
||||
store,
|
||||
onStoreError: (error) =>
|
||||
logger.error("Custom widget rate limiter is unavailable", {
|
||||
event: "custom_widget_rate_limit_store_unavailable",
|
||||
errorName: error instanceof Error ? error.name : "UnknownError",
|
||||
}),
|
||||
});
|
||||
@@ -72,14 +70,6 @@ export async function acquireCustomWidgetRequestLimit(input: RequestLimitInput):
|
||||
}
|
||||
};
|
||||
} catch (error) {
|
||||
if (error instanceof CustomWidgetDomainError && error.code === "TOO_MANY_REQUESTS") {
|
||||
logger.warn("Rejected custom widget request at rate or concurrency limit", {
|
||||
event: "custom_widget_rate_limit_rejected",
|
||||
category: input.category,
|
||||
authenticated: Boolean(input.userId),
|
||||
retryAfterMs: error.retryAfterMs,
|
||||
});
|
||||
}
|
||||
toTrpcError(error);
|
||||
}
|
||||
}
|
||||
@@ -1,296 +0,0 @@
|
||||
import { Buffer } from "node:buffer";
|
||||
import { createHash } from "node:crypto";
|
||||
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
import { createRedisClient } from "@homarr/core/infrastructure/redis";
|
||||
import type { RedisClient } from "@homarr/core/infrastructure/redis";
|
||||
import type { CustomWidgetHttpRequest, CustomWidgetHttpResponse } from "@homarr/custom-widgets/server";
|
||||
|
||||
import { useProcessLocalCustomWidgetState } from "../../custom-widget-state-mode";
|
||||
|
||||
const CACHE_KEY_PREFIX = "homarr:custom-widget:response:v1:";
|
||||
const VERSION_KEY_PREFIX = "homarr:custom-widget:response-version:v1:";
|
||||
const CACHE_INDEX_KEY = "homarr:custom-widget:response-index:v1";
|
||||
const MAX_CACHE_ENTRIES = 1_000;
|
||||
const MAX_CACHE_TTL_SECONDS = 3_600;
|
||||
const MAX_CACHE_VALUE_BYTES = 1024 * 1024 + 1024;
|
||||
const VERSION_TTL_SECONDS = MAX_CACHE_TTL_SECONDS * 2;
|
||||
|
||||
const logger = createLogger({ module: "custom-widget:response-cache" });
|
||||
|
||||
interface ResponseCacheStore {
|
||||
getVersions(namespaces: readonly string[]): Promise<number[]>;
|
||||
incrementVersion(namespace: string): Promise<void>;
|
||||
get(key: string): Promise<string | null>;
|
||||
set(key: string, namespace: string, value: string, ttlSeconds: number): Promise<void>;
|
||||
}
|
||||
|
||||
class RedisResponseCacheStore implements ResponseCacheStore {
|
||||
public constructor(private readonly redis: RedisClient) {}
|
||||
|
||||
public async getVersions(namespaces: readonly string[]): Promise<number[]> {
|
||||
const values = await this.redis.mget(...namespaces.map(getVersionKey));
|
||||
return values.map((value) => {
|
||||
const parsed = Number(value ?? 0);
|
||||
return Number.isSafeInteger(parsed) && parsed >= 0 ? parsed : 0;
|
||||
});
|
||||
}
|
||||
|
||||
public async incrementVersion(namespace: string): Promise<void> {
|
||||
const key = getVersionKey(namespace);
|
||||
await this.redis.multi().incr(key).expire(key, VERSION_TTL_SECONDS).exec();
|
||||
}
|
||||
|
||||
public get(key: string): Promise<string | null> {
|
||||
return this.redis.get(key);
|
||||
}
|
||||
|
||||
public async set(key: string, _namespace: string, value: string, ttlSeconds: number): Promise<void> {
|
||||
await this.redis.eval(
|
||||
`
|
||||
redis.call("SET", KEYS[1], ARGV[1], "EX", ARGV[2])
|
||||
redis.call("ZADD", KEYS[2], ARGV[3] + (ARGV[2] * 1000), KEYS[1])
|
||||
redis.call("ZREMRANGEBYSCORE", KEYS[2], "-inf", ARGV[3])
|
||||
local count = redis.call("ZCARD", KEYS[2])
|
||||
local maximum = tonumber(ARGV[4])
|
||||
if count > maximum then
|
||||
local victims = redis.call("ZRANGE", KEYS[2], 0, count - maximum - 1)
|
||||
for _, victim in ipairs(victims) do
|
||||
redis.call("DEL", victim)
|
||||
redis.call("ZREM", KEYS[2], victim)
|
||||
end
|
||||
end
|
||||
redis.call("EXPIRE", KEYS[2], ARGV[5])
|
||||
return 1
|
||||
`,
|
||||
2,
|
||||
key,
|
||||
CACHE_INDEX_KEY,
|
||||
value,
|
||||
ttlSeconds,
|
||||
Date.now(),
|
||||
MAX_CACHE_ENTRIES,
|
||||
VERSION_TTL_SECONDS,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class LocalResponseCacheStore implements ResponseCacheStore {
|
||||
private readonly versions = new Map<string, number>();
|
||||
private readonly responses = new Map<string, { namespace: string; value: string; expiresAt: number }>();
|
||||
|
||||
public async getVersions(namespaces: readonly string[]): Promise<number[]> {
|
||||
return namespaces.map((namespace) => this.versions.get(namespace) ?? 0);
|
||||
}
|
||||
|
||||
public async incrementVersion(namespace: string): Promise<void> {
|
||||
this.versions.set(namespace, (this.versions.get(namespace) ?? 0) + 1);
|
||||
}
|
||||
|
||||
public async get(key: string): Promise<string | null> {
|
||||
const response = this.responses.get(key);
|
||||
if (!response) return null;
|
||||
if (response.expiresAt <= Date.now()) {
|
||||
this.responses.delete(key);
|
||||
return null;
|
||||
}
|
||||
return response.value;
|
||||
}
|
||||
|
||||
public async set(key: string, namespace: string, value: string, ttlSeconds: number): Promise<void> {
|
||||
for (const [candidate, response] of this.responses) {
|
||||
if (response.expiresAt <= Date.now()) this.responses.delete(candidate);
|
||||
}
|
||||
while (this.responses.size >= MAX_CACHE_ENTRIES) {
|
||||
const oldest = this.responses.keys().next().value as string | undefined;
|
||||
if (!oldest) break;
|
||||
this.responses.delete(oldest);
|
||||
}
|
||||
this.responses.set(key, {
|
||||
namespace,
|
||||
value,
|
||||
expiresAt: Date.now() + ttlSeconds * 1000,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
let defaultStore: ResponseCacheStore | undefined;
|
||||
let defaultController: CustomWidgetResponseCacheController | undefined;
|
||||
|
||||
function getDefaultStore(): ResponseCacheStore {
|
||||
if (defaultStore) return defaultStore;
|
||||
const useLocal = useProcessLocalCustomWidgetState();
|
||||
defaultStore = useLocal ? new LocalResponseCacheStore() : new RedisResponseCacheStore(createRedisClient());
|
||||
return defaultStore;
|
||||
}
|
||||
|
||||
export interface CustomWidgetResponseCacheController {
|
||||
withCache(
|
||||
input: CustomWidgetHttpRequest,
|
||||
execute: () => Promise<CustomWidgetHttpResponse>,
|
||||
): Promise<CustomWidgetHttpResponse>;
|
||||
invalidate(prefixes: readonly string[]): Promise<void>;
|
||||
}
|
||||
|
||||
class ResponseCacheController implements CustomWidgetResponseCacheController {
|
||||
private readonly inFlight = new Map<string, Promise<CustomWidgetHttpResponse>>();
|
||||
private readonly pendingInvalidations = new Set<string>();
|
||||
|
||||
public constructor(private readonly store: ResponseCacheStore) {}
|
||||
|
||||
public async withCache(
|
||||
input: CustomWidgetHttpRequest,
|
||||
execute: () => Promise<CustomWidgetHttpResponse>,
|
||||
): Promise<CustomWidgetHttpResponse> {
|
||||
const ttlSeconds = Math.min(Math.max(input.cacheTtlSeconds ?? 0, 0), MAX_CACHE_TTL_SECONDS);
|
||||
if (input.kind !== "query" || !input.cacheKey || ttlSeconds <= 0) return execute();
|
||||
|
||||
const namespace = getCacheNamespace(input.cacheKey);
|
||||
if (!namespace) {
|
||||
logger.warn("Rejected malformed custom widget response cache key", {
|
||||
event: "custom_widget_cache_key_rejected",
|
||||
});
|
||||
return execute();
|
||||
}
|
||||
|
||||
let responseKey: string;
|
||||
let pending: Promise<CustomWidgetHttpResponse> | undefined;
|
||||
let cached: CustomWidgetHttpResponse | null = null;
|
||||
try {
|
||||
await this.flushPendingInvalidations();
|
||||
const invalidationNamespaces = getInvalidationNamespaces(namespace);
|
||||
const versions = await this.store.getVersions(invalidationNamespaces);
|
||||
responseKey = getResponseKey(input.cacheKey, versions);
|
||||
pending = this.inFlight.get(responseKey);
|
||||
if (!pending) {
|
||||
cached = parseCachedResponse(await this.store.get(responseKey));
|
||||
if (!cached) pending = this.inFlight.get(responseKey);
|
||||
}
|
||||
} catch (error) {
|
||||
logger.warn("Custom widget response cache unavailable; executing without cache", {
|
||||
event: "custom_widget_cache_unavailable",
|
||||
errorName: error instanceof Error ? error.name : "UnknownError",
|
||||
});
|
||||
return execute();
|
||||
}
|
||||
|
||||
if (pending) return pending;
|
||||
if (cached) return cached;
|
||||
|
||||
let request: Promise<CustomWidgetHttpResponse>;
|
||||
request = Promise.resolve()
|
||||
.then(execute)
|
||||
.then(async (response) => {
|
||||
if (!response.ok) return response;
|
||||
try {
|
||||
const serialized = JSON.stringify(response);
|
||||
if (Buffer.byteLength(serialized, "utf8") > MAX_CACHE_VALUE_BYTES) {
|
||||
logger.warn("Skipped oversized custom widget response cache entry", {
|
||||
event: "custom_widget_cache_entry_rejected",
|
||||
cacheNamespace: namespace,
|
||||
});
|
||||
return response;
|
||||
}
|
||||
await this.store.set(responseKey, namespace, serialized, ttlSeconds);
|
||||
} catch (error) {
|
||||
logger.warn("Failed to store custom widget response cache entry", {
|
||||
event: "custom_widget_cache_write_failed",
|
||||
errorName: error instanceof Error ? error.name : "UnknownError",
|
||||
});
|
||||
}
|
||||
return response;
|
||||
})
|
||||
.finally(() => {
|
||||
if (this.inFlight.get(responseKey) === request) this.inFlight.delete(responseKey);
|
||||
});
|
||||
this.inFlight.set(responseKey, request);
|
||||
return request;
|
||||
}
|
||||
|
||||
public async invalidate(prefixes: readonly string[]): Promise<void> {
|
||||
const namespaces = [...new Set(prefixes.filter((prefix) => prefix.length > 0))];
|
||||
if (namespaces.length === 0) return;
|
||||
try {
|
||||
await Promise.all(namespaces.map((namespace) => this.store.incrementVersion(namespace)));
|
||||
for (const namespace of namespaces) this.pendingInvalidations.delete(namespace);
|
||||
} catch (error) {
|
||||
namespaces.forEach((namespace) => this.pendingInvalidations.add(namespace));
|
||||
logger.error("Failed to invalidate custom widget response cache", {
|
||||
event: "custom_widget_cache_invalidation_failed",
|
||||
invalidationCount: namespaces.length,
|
||||
errorName: error instanceof Error ? error.name : "UnknownError",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private async flushPendingInvalidations(): Promise<void> {
|
||||
if (this.pendingInvalidations.size === 0) return;
|
||||
const namespaces = [...this.pendingInvalidations];
|
||||
await Promise.all(namespaces.map((namespace) => this.store.incrementVersion(namespace)));
|
||||
namespaces.forEach((namespace) => this.pendingInvalidations.delete(namespace));
|
||||
}
|
||||
}
|
||||
|
||||
function getDefaultController(): CustomWidgetResponseCacheController {
|
||||
defaultController ??= new ResponseCacheController(getDefaultStore());
|
||||
return defaultController;
|
||||
}
|
||||
|
||||
export function createRedisCustomWidgetResponseCache(redis: RedisClient): CustomWidgetResponseCacheController {
|
||||
return new ResponseCacheController(new RedisResponseCacheStore(redis));
|
||||
}
|
||||
|
||||
export function withCustomWidgetResponseCache(
|
||||
input: CustomWidgetHttpRequest,
|
||||
execute: () => Promise<CustomWidgetHttpResponse>,
|
||||
): Promise<CustomWidgetHttpResponse> {
|
||||
return getDefaultController().withCache(input, execute);
|
||||
}
|
||||
|
||||
export function invalidateCustomWidgetResponseCache(prefixes: readonly string[]): Promise<void> {
|
||||
return getDefaultController().invalidate(prefixes);
|
||||
}
|
||||
|
||||
function getCacheNamespace(key: string): string | null {
|
||||
const separator = key.lastIndexOf(":");
|
||||
if (separator <= 0 || separator === key.length - 1) return null;
|
||||
return key.slice(0, separator + 1);
|
||||
}
|
||||
|
||||
function getInvalidationNamespaces(namespace: string): string[] {
|
||||
const namespaces: string[] = [];
|
||||
for (let separator = namespace.indexOf(":"); separator >= 0; separator = namespace.indexOf(":", separator + 1)) {
|
||||
namespaces.push(namespace.slice(0, separator + 1));
|
||||
}
|
||||
return namespaces;
|
||||
}
|
||||
|
||||
function getResponseKey(key: string, versions: readonly number[]): string {
|
||||
return `${CACHE_KEY_PREFIX}${hash(`${versions.join(".")}:${key}`)}`;
|
||||
}
|
||||
|
||||
function getVersionKey(namespace: string): string {
|
||||
return `${VERSION_KEY_PREFIX}${hash(namespace)}`;
|
||||
}
|
||||
|
||||
function hash(value: string): string {
|
||||
return createHash("sha256").update(value).digest("hex");
|
||||
}
|
||||
|
||||
function parseCachedResponse(value: string | null): CustomWidgetHttpResponse | null {
|
||||
if (!value) return null;
|
||||
try {
|
||||
const parsed = JSON.parse(value) as Partial<CustomWidgetHttpResponse>;
|
||||
if (
|
||||
typeof parsed.ok !== "boolean" ||
|
||||
typeof parsed.status !== "number" ||
|
||||
typeof parsed.statusText !== "string" ||
|
||||
!("data" in parsed)
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
return parsed as CustomWidgetHttpResponse;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,118 +0,0 @@
|
||||
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);
|
||||
});
|
||||
});
|
||||
@@ -1,58 +0,0 @@
|
||||
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);
|
||||
});
|
||||
});
|
||||
@@ -1,91 +0,0 @@
|
||||
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();
|
||||
});
|
||||
});
|
||||
@@ -1,16 +0,0 @@
|
||||
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);
|
||||
});
|
||||
});
|
||||
@@ -1,46 +0,0 @@
|
||||
import { describe, expect, test } from "vitest";
|
||||
|
||||
import { ipAddressFromHeaders, rateLimitAddressFromHeaders } from "@homarr/common/server";
|
||||
|
||||
import { createTRPCContext } from "../../trpc";
|
||||
|
||||
describe("tRPC client address context", () => {
|
||||
test("does not use a forged forwarded address as the anonymous rate-limit identity", () => {
|
||||
const headers = new Headers({
|
||||
"user-agent": "vitest",
|
||||
"x-forwarded-for": " 198.51.100.10, 10.0.0.2 ",
|
||||
"x-real-ip": "203.0.113.20",
|
||||
});
|
||||
|
||||
expect(ipAddressFromHeaders(headers)).toBe("198.51.100.10");
|
||||
expect(rateLimitAddressFromHeaders(headers)).toBe("203.0.113.20");
|
||||
expect(createTRPCContext({ headers, session: null }).clientAddress).toBe("203.0.113.20");
|
||||
});
|
||||
|
||||
test("canonicalizes a trusted IPv6 address", () => {
|
||||
const headers = new Headers({
|
||||
"user-agent": "vitest",
|
||||
"x-real-ip": " 2001:0DB8:0000:0000:0000:0000:0000:0001 ",
|
||||
});
|
||||
|
||||
expect(rateLimitAddressFromHeaders(headers)).toBe("2001:db8::1");
|
||||
expect(createTRPCContext({ headers, session: null }).clientAddress).toBe("2001:db8::1");
|
||||
});
|
||||
|
||||
test("uses a shared anonymous bucket without a valid trusted address", () => {
|
||||
const headers = new Headers({
|
||||
"user-agent": "vitest",
|
||||
"x-forwarded-for": "198.51.100.10",
|
||||
"x-real-ip": "not-an-ip",
|
||||
});
|
||||
|
||||
expect(rateLimitAddressFromHeaders(headers)).toBeNull();
|
||||
expect(createTRPCContext({ headers, session: null }).clientAddress).toBeUndefined();
|
||||
});
|
||||
|
||||
test("retains the general client-address helper for existing API-key behavior", () => {
|
||||
const headers = new Headers({ "user-agent": "vitest", "x-real-ip": " 203.0.113.20 " });
|
||||
|
||||
expect(ipAddressFromHeaders(headers)).toBe("203.0.113.20");
|
||||
});
|
||||
});
|
||||
@@ -3,47 +3,22 @@ import { beforeEach, describe, expect, test, vi } from "vitest";
|
||||
|
||||
import type { Session } from "@homarr/auth";
|
||||
import { createId } from "@homarr/common";
|
||||
import { encryptSecret } from "@homarr/common/server";
|
||||
import { eq } from "@homarr/db";
|
||||
import {
|
||||
boardUserPermissions,
|
||||
boards,
|
||||
customWidgetDefinitions,
|
||||
customWidgetSecrets,
|
||||
items,
|
||||
legacyCustomWidgetDefinitions,
|
||||
users,
|
||||
} from "@homarr/db/schema";
|
||||
import { boardUserPermissions, boards, customWidgetDefinitions, items, users } from "@homarr/db/schema";
|
||||
import { createDb } from "@homarr/db/test";
|
||||
import type { HomarrCustomWidgetV2 } from "@homarr/custom-widgets/core";
|
||||
|
||||
const mocks = vi.hoisted(() => {
|
||||
const executeRequestImplementation = async (
|
||||
_input: unknown,
|
||||
options?: { acquireRequestLimit?: () => Promise<() => Promise<void>> },
|
||||
) => {
|
||||
const release = await options?.acquireRequestLimit?.();
|
||||
try {
|
||||
return { ok: true, status: 200, statusText: "OK", data: { value: 42 } };
|
||||
} finally {
|
||||
await release?.();
|
||||
}
|
||||
};
|
||||
return {
|
||||
acquireRequestLimit: vi.fn(async () => async () => undefined),
|
||||
invalidateCache: vi.fn(async () => undefined),
|
||||
executeRequestImplementation,
|
||||
executeRequest: vi.fn(executeRequestImplementation),
|
||||
};
|
||||
});
|
||||
const mocks = vi.hoisted(() => ({
|
||||
executeRequest: vi.fn(async () => ({ ok: true, status: 200, statusText: "OK", data: { value: 42 } })),
|
||||
}));
|
||||
|
||||
vi.mock("../../custom-widget/request-executor", () => ({
|
||||
executeCustomWidgetRequest: mocks.executeRequest,
|
||||
invalidateCustomWidgetResponseCache: mocks.invalidateCache,
|
||||
invalidateCustomWidgetResponseCache: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("../../custom-widget/request-limits", () => ({
|
||||
acquireCustomWidgetRequestLimit: mocks.acquireRequestLimit,
|
||||
acquireCustomWidgetRequestLimit: vi.fn(async () => async () => undefined),
|
||||
}));
|
||||
|
||||
import { serializeCustomWidgetDefinition } from "../../custom-widget/stored-definition";
|
||||
@@ -79,15 +54,6 @@ const definition: HomarrCustomWidgetV2 = {
|
||||
permission: "view",
|
||||
trigger: "manual",
|
||||
},
|
||||
"private-status": {
|
||||
source: "default",
|
||||
kind: "query",
|
||||
method: "GET",
|
||||
path: "/private-status",
|
||||
auth: "inherit",
|
||||
permission: "modify",
|
||||
trigger: "manual",
|
||||
},
|
||||
toggle: {
|
||||
source: "default",
|
||||
kind: "action",
|
||||
@@ -118,7 +84,7 @@ const createSession = (userId: string): Session => ({
|
||||
expires: new Date(Date.now() + 60_000).toISOString(),
|
||||
});
|
||||
|
||||
async function setup(isPublic = true, customDefinition = definition, apiKey?: string) {
|
||||
async function setup(isPublic = true) {
|
||||
const db = createDb();
|
||||
const ownerId = createId();
|
||||
const boardId = createId();
|
||||
@@ -128,18 +94,9 @@ async function setup(isPublic = true, customDefinition = definition, apiKey?: st
|
||||
await db.insert(boards).values({ id: boardId, name: createId(), creatorId: ownerId, isPublic });
|
||||
await db.insert(customWidgetDefinitions).values({
|
||||
id: definitionId,
|
||||
...serializeCustomWidgetDefinition(customDefinition),
|
||||
...serializeCustomWidgetDefinition(definition),
|
||||
creatorId: ownerId,
|
||||
});
|
||||
if (apiKey) {
|
||||
await db.insert(customWidgetSecrets).values({
|
||||
definitionId,
|
||||
sourceId: "default",
|
||||
kind: "apiKey",
|
||||
encryptedValue: encryptSecret(apiKey),
|
||||
updatedAt: new Date(),
|
||||
});
|
||||
}
|
||||
await db.insert(items).values({
|
||||
id: itemId,
|
||||
boardId,
|
||||
@@ -150,21 +107,11 @@ async function setup(isPublic = true, customDefinition = definition, apiKey?: st
|
||||
}
|
||||
|
||||
describe("Custom JSX v2 board router", () => {
|
||||
beforeEach(() => {
|
||||
mocks.acquireRequestLimit.mockClear();
|
||||
mocks.executeRequest.mockReset();
|
||||
mocks.executeRequest.mockImplementation(mocks.executeRequestImplementation);
|
||||
mocks.invalidateCache.mockClear();
|
||||
});
|
||||
beforeEach(() => mocks.executeRequest.mockClear());
|
||||
|
||||
test("allows public queries only through the definition placed on an accessible board", async () => {
|
||||
const { db, itemId } = await setup();
|
||||
const caller = customApiRouter.createCaller({
|
||||
db,
|
||||
deviceType: undefined,
|
||||
session: null,
|
||||
clientAddress: "198.51.100.10",
|
||||
});
|
||||
const caller = customApiRouter.createCaller({ db, deviceType: undefined, session: null });
|
||||
|
||||
await expect(caller.queryRequest({ itemId, requestId: "status", params: { name: "homarr" } })).resolves.toEqual(
|
||||
expect.objectContaining({ ok: true, data: { value: 42 } }),
|
||||
@@ -173,156 +120,6 @@ describe("Custom JSX v2 board router", () => {
|
||||
code: "NOT_FOUND",
|
||||
});
|
||||
expect(mocks.executeRequest).toHaveBeenCalledOnce();
|
||||
expect(mocks.acquireRequestLimit).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ userId: undefined, anonymousId: "198.51.100.10" }),
|
||||
);
|
||||
});
|
||||
|
||||
test("redacts apiKeyQuery transport details from anonymous load and manual query results", async () => {
|
||||
const apiKey = "api-key-query-secret";
|
||||
const credentialedDefinition: HomarrCustomWidgetV2 = {
|
||||
...definition,
|
||||
sources: {
|
||||
default: {
|
||||
name: "API",
|
||||
baseUrl: "https://example.com",
|
||||
networkScope: "public",
|
||||
auth: { type: "apiKeyQuery", name: "access_token" },
|
||||
},
|
||||
},
|
||||
};
|
||||
const { db, itemId } = await setup(true, credentialedDefinition, apiKey);
|
||||
mocks.executeRequest.mockImplementation(async (input) => {
|
||||
const request = input as {
|
||||
targetUrl: URL;
|
||||
auth?: { headerName?: string | null; secrets: Array<{ kind: string; value: string }> };
|
||||
};
|
||||
const secret = request.auth?.secrets.find(({ kind }) => kind === "apiKey")?.value;
|
||||
const tokenizedUrl = new URL(request.targetUrl);
|
||||
tokenizedUrl.searchParams.set(request.auth?.headerName ?? "api_key", secret ?? "");
|
||||
const failure = new Error(`Transport failed for ${tokenizedUrl.toString()}`);
|
||||
failure.name = `SocketError:${tokenizedUrl.toString()}`;
|
||||
throw failure;
|
||||
});
|
||||
const caller = customApiRouter.createCaller({
|
||||
db,
|
||||
deviceType: undefined,
|
||||
session: null,
|
||||
clientAddress: "198.51.100.10",
|
||||
});
|
||||
|
||||
const loadResult = await caller.getData({ itemId });
|
||||
const queryError = await caller.queryRequest({ itemId, requestId: "status", params: { name: "homarr" } }).then(
|
||||
() => undefined,
|
||||
(cause: unknown) => cause,
|
||||
);
|
||||
|
||||
expect(loadResult.status["load-status"]).toMatchObject({
|
||||
ok: false,
|
||||
status: 0,
|
||||
error: "Request failed",
|
||||
});
|
||||
expect(queryError).toMatchObject({
|
||||
code: "BAD_GATEWAY",
|
||||
message: "External request failed",
|
||||
});
|
||||
expect((queryError as Error).cause).toBeUndefined();
|
||||
expect(mocks.executeRequest).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
auth: expect.objectContaining({
|
||||
type: "apiKeyQuery",
|
||||
headerName: "access_token",
|
||||
secrets: [{ kind: "apiKey", value: apiKey }],
|
||||
}),
|
||||
}),
|
||||
expect.any(Object),
|
||||
);
|
||||
expect(JSON.stringify(loadResult)).not.toContain(apiKey);
|
||||
expect(JSON.stringify(loadResult)).not.toContain("access_token");
|
||||
expect(String(queryError)).not.toContain(apiKey);
|
||||
expect(String(queryError)).not.toContain("access_token");
|
||||
});
|
||||
|
||||
test("requires authentication before invalidating board-authorized query caches", async () => {
|
||||
const { db, itemId } = await setup();
|
||||
const viewerId = createId();
|
||||
await db.insert(users).values({ id: viewerId });
|
||||
const anonymous = customApiRouter.createCaller({
|
||||
db,
|
||||
deviceType: undefined,
|
||||
session: null,
|
||||
clientAddress: "198.51.100.10",
|
||||
});
|
||||
|
||||
await expect(anonymous.refreshQueries({ itemId, all: true })).rejects.toMatchObject({ code: "UNAUTHORIZED" });
|
||||
expect(mocks.invalidateCache).not.toHaveBeenCalled();
|
||||
|
||||
const authenticated = customApiRouter.createCaller({
|
||||
db,
|
||||
deviceType: undefined,
|
||||
session: createSession(viewerId),
|
||||
clientAddress: "198.51.100.10",
|
||||
});
|
||||
await expect(authenticated.refreshQueries({ itemId, all: true })).resolves.toEqual({
|
||||
requestIds: ["load-status", "status"],
|
||||
});
|
||||
expect(mocks.invalidateCache).toHaveBeenCalledOnce();
|
||||
expect(mocks.invalidateCache).toHaveBeenCalledWith([
|
||||
expect.stringMatching(new RegExp(`^custom-jsx:${itemId}:.+:load-status:$`, "u")),
|
||||
expect.stringMatching(new RegExp(`^custom-jsx:${itemId}:.+:status:$`, "u")),
|
||||
]);
|
||||
expect(mocks.acquireRequestLimit).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
category: "query",
|
||||
userId: viewerId,
|
||||
itemId,
|
||||
}),
|
||||
);
|
||||
expect(mocks.executeRequest).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test("queues valid load requests within the per-viewer concurrency limit", async () => {
|
||||
const requests = Object.fromEntries(
|
||||
Array.from({ length: 6 }, (_, index) => [
|
||||
`load-${index}`,
|
||||
{
|
||||
source: "default",
|
||||
kind: "query" as const,
|
||||
method: "GET" as const,
|
||||
path: `/status/${index}`,
|
||||
auth: "inherit" as const,
|
||||
permission: "view" as const,
|
||||
trigger: "load" as const,
|
||||
},
|
||||
]),
|
||||
);
|
||||
const { db, itemId } = await setup(true, { ...definition, requests });
|
||||
let active = 0;
|
||||
let maximumActive = 0;
|
||||
mocks.executeRequest.mockImplementation(async (_input, options) => {
|
||||
const release = await options?.acquireRequestLimit?.();
|
||||
active += 1;
|
||||
maximumActive = Math.max(maximumActive, active);
|
||||
try {
|
||||
await new Promise((resolve) => setTimeout(resolve, 5));
|
||||
return { ok: true, status: 200, statusText: "OK", data: { value: active } };
|
||||
} finally {
|
||||
active -= 1;
|
||||
await release?.();
|
||||
}
|
||||
});
|
||||
const caller = customApiRouter.createCaller({
|
||||
db,
|
||||
deviceType: undefined,
|
||||
session: null,
|
||||
clientAddress: "198.51.100.10",
|
||||
});
|
||||
|
||||
const result = await caller.getData({ itemId });
|
||||
|
||||
expect(Object.keys(result.data)).toHaveLength(6);
|
||||
expect(mocks.executeRequest).toHaveBeenCalledTimes(6);
|
||||
expect(maximumActive).toBe(4);
|
||||
});
|
||||
|
||||
test("hides private boards and disabled definitions before executing a request", async () => {
|
||||
@@ -357,25 +154,6 @@ describe("Custom JSX v2 board router", () => {
|
||||
expect(mocks.executeRequest).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test("surfaces a real preserved v1 placement as migration-required", async () => {
|
||||
const { db, itemId, definitionId, ownerId } = await setup();
|
||||
await db.delete(customWidgetDefinitions).where(eq(customWidgetDefinitions.id, definitionId));
|
||||
await db.insert(legacyCustomWidgetDefinitions).values({
|
||||
id: definitionId,
|
||||
name: "Legacy weather",
|
||||
url: "https://example.com/weather",
|
||||
displayConfig: stringifySuperJson({ type: "singleValue", jsonPath: "$.temperature" }),
|
||||
creatorId: ownerId,
|
||||
});
|
||||
const caller = customApiRouter.createCaller({ db, deviceType: undefined, session: null });
|
||||
|
||||
await expect(caller.getData({ itemId })).rejects.toMatchObject({
|
||||
code: "PRECONDITION_FAILED",
|
||||
message: "LEGACY_CUSTOM_WIDGET_MIGRATION_REQUIRED",
|
||||
});
|
||||
expect(mocks.executeRequest).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test("rejects request-kind and parameter mismatches", async () => {
|
||||
const { db, itemId, ownerId } = await setup();
|
||||
const caller = customApiRouter.createCaller({ db, deviceType: undefined, session: createSession(ownerId) });
|
||||
@@ -402,7 +180,6 @@ describe("Custom JSX v2 board router", () => {
|
||||
);
|
||||
expect(mocks.executeRequest).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ method: "POST", body: '{"enabled":true}' }),
|
||||
expect.objectContaining({ acquireRequestLimit: expect.any(Function) }),
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
@@ -12,7 +12,6 @@ import {
|
||||
validateCustomWidgetOptions,
|
||||
} from "@homarr/custom-widgets/core";
|
||||
import type { CustomJsxRequest, CustomWidgetSource } from "@homarr/custom-widgets/core";
|
||||
import { CUSTOM_WIDGET_USER_ITEM_CONCURRENCY_LIMIT } from "@homarr/custom-widgets/server";
|
||||
|
||||
import { createTRPCRouter, protectedProcedure, publicProcedure } from "../../trpc";
|
||||
import { throwIfActionForbiddenAsync } from "../board/board-access";
|
||||
@@ -33,10 +32,6 @@ const namedRequestInputSchema = itemInputSchema.extend({
|
||||
requestId: z.string().min(1).max(64),
|
||||
params: runtimeParamsSchema.default({}),
|
||||
});
|
||||
const refreshQueriesInputSchema = itemInputSchema.extend({
|
||||
requestIds: z.array(z.string().min(1).max(64)).max(64).default([]),
|
||||
all: z.boolean().default(false),
|
||||
});
|
||||
|
||||
interface CustomWidgetItemOptions {
|
||||
definitionId: string;
|
||||
@@ -45,7 +40,7 @@ interface CustomWidgetItemOptions {
|
||||
refreshInterval?: number;
|
||||
}
|
||||
|
||||
type RouterContext = Parameters<typeof throwIfActionForbiddenAsync>[0] & { clientAddress?: string };
|
||||
type RouterContext = Parameters<typeof throwIfActionForbiddenAsync>[0];
|
||||
type ResolvedDefinition = Awaited<ReturnType<typeof resolvePlacedDefinitionAsync>>;
|
||||
|
||||
const parseItemOptions = (raw: string): CustomWidgetItemOptions => {
|
||||
@@ -147,35 +142,28 @@ const getAuth = (resolved: ResolvedDefinition, source: IdentifiedSource, mode: "
|
||||
};
|
||||
};
|
||||
|
||||
const acquireRequestLimit = (ctx: RouterContext, resolved: ResolvedDefinition, request: IdentifiedRequest) =>
|
||||
acquireCustomWidgetRequestLimit({
|
||||
const withRequestLimit = async <T>(
|
||||
ctx: RouterContext,
|
||||
resolved: ResolvedDefinition,
|
||||
request: IdentifiedRequest,
|
||||
callback: () => Promise<T>,
|
||||
) => {
|
||||
const release = await acquireCustomWidgetRequestLimit({
|
||||
category: request.kind === "query" ? "query" : request.method === "DELETE" ? "delete" : "action",
|
||||
userId: ctx.session?.user.id,
|
||||
anonymousId: ctx.clientAddress,
|
||||
itemId: resolved.item.id,
|
||||
definitionId: resolved.stored.id,
|
||||
});
|
||||
try {
|
||||
return await callback();
|
||||
} finally {
|
||||
await release();
|
||||
}
|
||||
};
|
||||
|
||||
const getCacheKey = (resolved: ResolvedDefinition, request: IdentifiedRequest, params: Record<string, unknown>) =>
|
||||
`custom-jsx:${resolved.item.id}:${getCustomWidgetCacheVersion(resolved.stored)}:${request.id}:${hashRuntimeParams(params)}`;
|
||||
|
||||
async function mapLoadRequests<T, TResult>(
|
||||
requests: readonly T[],
|
||||
execute: (request: T) => Promise<TResult>,
|
||||
): Promise<TResult[]> {
|
||||
const results: TResult[] = [];
|
||||
for (let index = 0; index < requests.length; index += CUSTOM_WIDGET_USER_ITEM_CONCURRENCY_LIMIT) {
|
||||
results.push(
|
||||
...(await Promise.all(
|
||||
requests
|
||||
.slice(index, index + CUSTOM_WIDGET_USER_ITEM_CONCURRENCY_LIMIT)
|
||||
.map(async (request) => await execute(request)),
|
||||
)),
|
||||
);
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
const executeRequest = async (
|
||||
ctx: RouterContext,
|
||||
resolved: ResolvedDefinition,
|
||||
@@ -186,28 +174,20 @@ const executeRequest = async (
|
||||
const source = findSource(resolved, request.source);
|
||||
const values = resolveCustomWidgetRequestValues(request, resolved.configuration, params);
|
||||
const targetUrl = renderRequestTarget(source.baseUrl, request, values);
|
||||
try {
|
||||
return await executeCustomWidgetRequest(
|
||||
{
|
||||
baseUrl: source.baseUrl,
|
||||
targetUrl,
|
||||
method: request.method,
|
||||
body: renderRequestBody(request, values),
|
||||
staticHeaders: request.headers,
|
||||
auth: getAuth(resolved, source, request.auth),
|
||||
networkScope: source.networkScope,
|
||||
kind: request.kind,
|
||||
cacheKey: request.kind === "query" ? getCacheKey(resolved, request, values) : undefined,
|
||||
cacheTtlSeconds: request.cacheSeconds,
|
||||
},
|
||||
{
|
||||
acquireRequestLimit: () => acquireRequestLimit(ctx, resolved, request),
|
||||
},
|
||||
);
|
||||
} catch (error) {
|
||||
if (error instanceof TRPCError) throw error;
|
||||
throw new TRPCError({ code: "BAD_GATEWAY", message: "External request failed" });
|
||||
}
|
||||
return withRequestLimit(ctx, resolved, request, () =>
|
||||
executeCustomWidgetRequest({
|
||||
baseUrl: source.baseUrl,
|
||||
targetUrl,
|
||||
method: request.method,
|
||||
body: renderRequestBody(request, values),
|
||||
staticHeaders: request.headers,
|
||||
auth: getAuth(resolved, source, request.auth),
|
||||
networkScope: source.networkScope,
|
||||
kind: request.kind,
|
||||
cacheKey: request.kind === "query" ? getCacheKey(resolved, request, values) : undefined,
|
||||
cacheTtlSeconds: request.cacheSeconds,
|
||||
}),
|
||||
);
|
||||
};
|
||||
|
||||
export const customApiRouter = createTRPCRouter({
|
||||
@@ -216,37 +196,39 @@ export const customApiRouter = createTRPCRouter({
|
||||
const loadRequests = Object.entries(resolved.definition.requests).filter(
|
||||
([, request]) => request.kind === "query" && request.trigger === "load",
|
||||
);
|
||||
const entries = await mapLoadRequests(loadRequests, async ([requestId, request]) => {
|
||||
try {
|
||||
const response = await executeRequest(ctx, resolved, { id: requestId, ...request }, {});
|
||||
return [
|
||||
requestId,
|
||||
{
|
||||
data: response.data,
|
||||
status: {
|
||||
loading: false,
|
||||
ok: response.ok,
|
||||
status: response.status,
|
||||
statusText: response.statusText,
|
||||
error: response.ok ? undefined : `HTTP ${response.status}: ${response.statusText}`,
|
||||
const entries = await Promise.all(
|
||||
loadRequests.map(async ([requestId, request]) => {
|
||||
try {
|
||||
const response = await executeRequest(ctx, resolved, { id: requestId, ...request }, {});
|
||||
return [
|
||||
requestId,
|
||||
{
|
||||
data: response.data,
|
||||
status: {
|
||||
loading: false,
|
||||
ok: response.ok,
|
||||
status: response.status,
|
||||
statusText: response.statusText,
|
||||
error: response.ok ? undefined : `HTTP ${response.status}: ${response.statusText}`,
|
||||
},
|
||||
},
|
||||
},
|
||||
] as const;
|
||||
} catch {
|
||||
return [
|
||||
requestId,
|
||||
{
|
||||
data: null,
|
||||
status: {
|
||||
loading: false,
|
||||
ok: false,
|
||||
status: 0,
|
||||
error: "Request failed",
|
||||
] as const;
|
||||
} catch (error) {
|
||||
return [
|
||||
requestId,
|
||||
{
|
||||
data: null,
|
||||
status: {
|
||||
loading: false,
|
||||
ok: false,
|
||||
status: 0,
|
||||
error: error instanceof Error ? error.message : "Request failed",
|
||||
},
|
||||
},
|
||||
},
|
||||
] as const;
|
||||
}
|
||||
});
|
||||
] as const;
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
return {
|
||||
type: "customJsx" as const,
|
||||
@@ -282,46 +264,6 @@ export const customApiRouter = createTRPCRouter({
|
||||
};
|
||||
}),
|
||||
|
||||
refreshQueries: protectedProcedure.input(refreshQueriesInputSchema).mutation(async ({ ctx, input }) => {
|
||||
const resolved = await resolvePlacedDefinitionAsync(ctx, input.itemId);
|
||||
const available = Object.entries(resolved.definition.requests).flatMap(([requestId, request]) =>
|
||||
request.kind === "query" ? [{ id: requestId, ...request }] : [],
|
||||
);
|
||||
const requested = new Set(input.all ? available.map(({ id }) => id) : input.requestIds);
|
||||
const authorized: IdentifiedRequest[] = [];
|
||||
for (const request of available) {
|
||||
if (!requested.has(request.id)) continue;
|
||||
try {
|
||||
await throwIfActionForbiddenAsync(
|
||||
ctx,
|
||||
eq(boards.id, resolved.item.boardId),
|
||||
request.permission as BoardPermission,
|
||||
);
|
||||
authorized.push(request);
|
||||
} catch (error) {
|
||||
if (!(error instanceof TRPCError) || error.code !== "NOT_FOUND") throw error;
|
||||
}
|
||||
}
|
||||
if (authorized.length === 0) return { requestIds: [] };
|
||||
|
||||
const release = await acquireCustomWidgetRequestLimit({
|
||||
category: "query",
|
||||
userId: ctx.session?.user.id,
|
||||
anonymousId: ctx.clientAddress,
|
||||
itemId: resolved.item.id,
|
||||
definitionId: resolved.stored.id,
|
||||
});
|
||||
try {
|
||||
const version = getCustomWidgetCacheVersion(resolved.stored);
|
||||
await invalidateCustomWidgetResponseCache(
|
||||
authorized.map((request) => `custom-jsx:${resolved.item.id}:${version}:${request.id}:`),
|
||||
);
|
||||
} finally {
|
||||
await release();
|
||||
}
|
||||
return { requestIds: authorized.map(({ id }) => id) };
|
||||
}),
|
||||
|
||||
executeAction: protectedProcedure
|
||||
.input(namedRequestInputSchema.extend({ confirmed: z.boolean().optional() }))
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
@@ -333,7 +275,7 @@ export const customApiRouter = createTRPCRouter({
|
||||
}
|
||||
const response = await executeRequest(ctx, resolved, request, input.params);
|
||||
if (response.ok && request.invalidates?.length) {
|
||||
await invalidateCustomWidgetResponseCache(
|
||||
invalidateCustomWidgetResponseCache(
|
||||
request.invalidates.flatMap((requestId) => [
|
||||
`custom-jsx:${resolved.item.id}:${getCustomWidgetCacheVersion(resolved.stored)}:${requestId}:`,
|
||||
`custom-widget:options:${resolved.stored.id}:${getCustomWidgetCacheVersion(resolved.stored)}:${requestId}:`,
|
||||
|
||||
@@ -14,7 +14,7 @@ import { ZodError } from "zod/v4";
|
||||
|
||||
import type { Session } from "@homarr/auth";
|
||||
import { extractBaseUrlFromHeaders, FlattenError } from "@homarr/common";
|
||||
import { rateLimitAddressFromHeaders, userAgent } from "@homarr/common/server";
|
||||
import { userAgent } from "@homarr/common/server";
|
||||
import type { DeviceType } from "@homarr/common/server";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
import { db } from "@homarr/db";
|
||||
@@ -41,7 +41,6 @@ interface ApiContext {
|
||||
session: Session | null;
|
||||
deviceType: DeviceType;
|
||||
baseUrl?: `${string}://${string}`;
|
||||
clientAddress?: string;
|
||||
db: typeof db;
|
||||
}
|
||||
|
||||
@@ -59,7 +58,6 @@ export const createTRPCContext = (opts: { headers: Headers; session: Session | n
|
||||
session,
|
||||
deviceType: userAgent(opts.headers).device.type,
|
||||
baseUrl: extractBaseUrlFromHeaders(opts.headers),
|
||||
clientAddress: rateLimitAddressFromHeaders(opts.headers) ?? undefined,
|
||||
db,
|
||||
};
|
||||
};
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
import { isIP } from "node:net";
|
||||
|
||||
import { userAgent as userAgentNextServer } from "next/server";
|
||||
|
||||
import type { Modify } from "./types";
|
||||
@@ -13,31 +11,5 @@ export const userAgent = (headers: Headers) => {
|
||||
export type DeviceType = "console" | "mobile" | "tablet" | "smarttv" | "wearable" | "embedded" | undefined;
|
||||
|
||||
export const ipAddressFromHeaders = (headers: Headers): string | null => {
|
||||
const forwardedAddress = headers.get("x-forwarded-for")?.split(",", 1)[0]?.trim();
|
||||
if (forwardedAddress) return forwardedAddress;
|
||||
|
||||
const realAddress = headers.get("x-real-ip")?.trim();
|
||||
return realAddress || null;
|
||||
return headers.get("x-forwarded-for");
|
||||
};
|
||||
|
||||
const canonicalizeIpAddress = (value: string | null): string | null => {
|
||||
const trimmed = value?.trim();
|
||||
if (!trimmed) return null;
|
||||
|
||||
const unwrapped = trimmed.startsWith("[") && trimmed.endsWith("]") ? trimmed.slice(1, -1) : trimmed;
|
||||
const family = isIP(unwrapped);
|
||||
if (family === 4) return unwrapped;
|
||||
if (family !== 6) return null;
|
||||
|
||||
const hostname = new URL(`http://[${unwrapped}]/`).hostname;
|
||||
return hostname.slice(1, -1);
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns the canonical address supplied by Homarr's trusted ingress for
|
||||
* abuse-control buckets. Do not fall back to X-Forwarded-For here: clients can
|
||||
* prepend arbitrary values to that chain. Deployments without a trusted
|
||||
* X-Real-IP header deliberately share the anonymous fallback bucket.
|
||||
*/
|
||||
export const rateLimitAddressFromHeaders = (headers: Headers): string | null =>
|
||||
canonicalizeIpAddress(headers.get("x-real-ip"));
|
||||
@@ -3,20 +3,6 @@ export interface CustomWidgetAiEvaluationCase {
|
||||
request: string;
|
||||
documentationUrl: string;
|
||||
apiNotes: string;
|
||||
acceptance: {
|
||||
sourceAuth?: { type: "none" | "bearer" | "apiKeyHeader" | "apiKeyQuery"; name?: string };
|
||||
requestRules: ReadonlyArray<{
|
||||
label: string;
|
||||
pathIncludes: string;
|
||||
kind?: "query" | "action";
|
||||
method?: "GET" | "POST" | "PUT" | "DELETE" | "PATCH";
|
||||
trigger?: "load" | "manual";
|
||||
optionReference?: string;
|
||||
parameterReference?: string;
|
||||
invalidatesRequest?: string;
|
||||
}>;
|
||||
templateComponents: readonly string[];
|
||||
};
|
||||
}
|
||||
|
||||
export const CUSTOM_WIDGET_AI_EVALUATION_CASES: readonly CustomWidgetAiEvaluationCase[] = [
|
||||
@@ -27,20 +13,6 @@ export const CUSTOM_WIDGET_AI_EVALUATION_CASES: readonly CustomWidgetAiEvaluatio
|
||||
"Create a polished Pokédex browser with searchable Pokémon, a responsive result grid, manual detail loading, sprites, types, abilities, base-stat progress bars, and clear loading, empty, and failure states.",
|
||||
apiNotes:
|
||||
"Use GET /api/v2/pokemon?limit=<option>&offset=<option> for the list and GET /api/v2/pokemon/{param:name} as a manual detail query. PokeAPI needs no authentication.",
|
||||
acceptance: {
|
||||
sourceAuth: { type: "none" },
|
||||
requestRules: [
|
||||
{ label: "Pokémon list", pathIncludes: "/api/v2/pokemon", kind: "query", trigger: "load" },
|
||||
{
|
||||
label: "manual Pokémon detail",
|
||||
pathIncludes: "/api/v2/pokemon/",
|
||||
kind: "query",
|
||||
trigger: "manual",
|
||||
parameterReference: "name",
|
||||
},
|
||||
],
|
||||
templateComponents: ["TextInput", "SubFetch", "Image", "Progress"],
|
||||
},
|
||||
},
|
||||
{
|
||||
id: "portainer-containers",
|
||||
@@ -49,87 +21,22 @@ export const CUSTOM_WIDGET_AI_EVALUATION_CASES: readonly CustomWidgetAiEvaluatio
|
||||
"Create an excellent Portainer container dashboard: environment option, running/stopped summary, responsive container list, health/status badges, and explicit start, stop, and restart actions with confirmation and refresh after success.",
|
||||
apiNotes:
|
||||
"Use X-API-Key auth. List containers with GET /api/endpoints/{option:endpointId}/docker/containers/json?all=true. Actions are POST /api/endpoints/{option:endpointId}/docker/containers/{param:id}/start, /stop, and /restart and invalidate the list query.",
|
||||
acceptance: {
|
||||
sourceAuth: { type: "apiKeyHeader", name: "X-API-Key" },
|
||||
requestRules: [
|
||||
{
|
||||
label: "container list",
|
||||
pathIncludes: "/containers/json",
|
||||
kind: "query",
|
||||
optionReference: "endpointId",
|
||||
},
|
||||
{
|
||||
label: "start action",
|
||||
pathIncludes: "/start",
|
||||
kind: "action",
|
||||
method: "POST",
|
||||
optionReference: "endpointId",
|
||||
parameterReference: "id",
|
||||
invalidatesRequest: "container list",
|
||||
},
|
||||
{
|
||||
label: "stop action",
|
||||
pathIncludes: "/stop",
|
||||
kind: "action",
|
||||
method: "POST",
|
||||
optionReference: "endpointId",
|
||||
parameterReference: "id",
|
||||
invalidatesRequest: "container list",
|
||||
},
|
||||
{
|
||||
label: "restart action",
|
||||
pathIncludes: "/restart",
|
||||
kind: "action",
|
||||
method: "POST",
|
||||
optionReference: "endpointId",
|
||||
parameterReference: "id",
|
||||
invalidatesRequest: "container list",
|
||||
},
|
||||
],
|
||||
templateComponents: ["ActionButton", "Badge", "RefreshButton"],
|
||||
},
|
||||
},
|
||||
{
|
||||
id: "football-dashboard",
|
||||
documentationUrl: "https://docs.football-data.org/general/v4/competition.html",
|
||||
id: "tautulli-activity",
|
||||
documentationUrl: "https://github.com/Tautulli/Tautulli/wiki/Tautulli-API-Reference",
|
||||
request:
|
||||
"Create a polished football dashboard with a competition selector, league table, upcoming fixtures, club crests, match status and kickoff time. It must be highly scannable in narrow and wide tiles, refreshable, and include complete loading, empty, and error states.",
|
||||
"Create a beautiful Tautulli activity widget with active stream cards, user/player details, progress, transcode/direct-play badges, bandwidth summary, and a refresh control that works in narrow and wide tiles.",
|
||||
apiNotes:
|
||||
"Use https://api.football-data.org/v4 with X-Auth-Token authentication. GET /competitions/{option:competition}/standings returns standings[].table with position, team, playedGames, won, draw, lost, points and goalDifference. GET /competitions/{option:competition}/matches with query status=SCHEDULED returns matches with utcDate, status, homeTeam, awayTeam and score. Use fixed choices such as PL, CL, PD, BL1, SA and FL1 for the competition option.",
|
||||
acceptance: {
|
||||
sourceAuth: { type: "apiKeyHeader", name: "X-Auth-Token" },
|
||||
requestRules: [
|
||||
{
|
||||
label: "competition standings",
|
||||
pathIncludes: "/standings",
|
||||
kind: "query",
|
||||
optionReference: "competition",
|
||||
},
|
||||
{
|
||||
label: "competition fixtures",
|
||||
pathIncludes: "/matches",
|
||||
kind: "query",
|
||||
optionReference: "competition",
|
||||
},
|
||||
],
|
||||
templateComponents: ["Table", "Image", "Badge", "RefreshButton"],
|
||||
},
|
||||
"Use an apiKeyQuery source with parameter name apikey. Query GET /api/v2 with query cmd=get_activity. The response payload is under response.data and sessions is an array.",
|
||||
},
|
||||
{
|
||||
id: "jellyfin-activity",
|
||||
documentationUrl: "https://api.jellyfin.org/",
|
||||
id: "bambubuddy-printer",
|
||||
documentationUrl: "https://wiki.bambuddy.cool/reference/api/",
|
||||
request:
|
||||
"Create a premium Jellyfin overview with movie, series, episode and song totals plus active playback sessions. Show user, title, client, play method, progress, paused/playing state and a calm empty state when nothing is playing. It must remain useful in narrow and wide tiles.",
|
||||
"Create a premium BambuBuddy printer status widget with printer selector, current job, progress, remaining time, nozzle and bed temperatures, connection state, and safe pause/resume/stop controls when supported.",
|
||||
apiNotes:
|
||||
"Use the user's Jellyfin server URL with private network scope and X-Emby-Token authentication. GET /Items/Counts returns MovieCount, SeriesCount, EpisodeCount and SongCount. GET /Sessions with query activeWithinSeconds=300 returns sessions; active playback has NowPlayingItem, UserName, Client, PlayState.PositionTicks, PlayState.IsPaused and TranscodingInfo or NowPlayingItem.RunTimeTicks. Do not invent playback-control actions.",
|
||||
acceptance: {
|
||||
sourceAuth: { type: "apiKeyHeader", name: "X-Emby-Token" },
|
||||
requestRules: [
|
||||
{ label: "library counts", pathIncludes: "/Items/Counts", kind: "query" },
|
||||
{ label: "active sessions", pathIncludes: "/Sessions", kind: "query" },
|
||||
],
|
||||
templateComponents: ["Progress", "Badge", "RefreshButton"],
|
||||
},
|
||||
"Use X-API-Key auth and base path /api/v1. GET /printers lists printers and GET /printers/{id}/status returns state, progress, remaining_time, temperatures.nozzle, temperatures.bed, and hms_status. The official reference does not document pause, resume, or stop endpoints, so omit those actions. Configure the selected printer with choicesFrom on a widget option and use that option in the status path.",
|
||||
},
|
||||
{
|
||||
id: "home-assistant-control",
|
||||
@@ -138,39 +45,5 @@ export const CUSTOM_WIDGET_AI_EVALUATION_CASES: readonly CustomWidgetAiEvaluatio
|
||||
"Create a refined Home Assistant room widget with temperature and humidity readings, light status, a room/entity option, and an actionable light toggle. Use calm hierarchy and responsive controls, not a pile of nested cards.",
|
||||
apiNotes:
|
||||
"Use bearer auth. GET /api/states/{option:sensorEntity} and /api/states/{option:lightEntity} load entity state. POST /api/services/light/turn_on and /turn_off accept a body with entity_id from an option and should invalidate the light query.",
|
||||
acceptance: {
|
||||
sourceAuth: { type: "bearer" },
|
||||
requestRules: [
|
||||
{
|
||||
label: "sensor state",
|
||||
pathIncludes: "/api/states/",
|
||||
kind: "query",
|
||||
optionReference: "sensorEntity",
|
||||
},
|
||||
{
|
||||
label: "light state",
|
||||
pathIncludes: "/api/states/",
|
||||
kind: "query",
|
||||
optionReference: "lightEntity",
|
||||
},
|
||||
{
|
||||
label: "light on action",
|
||||
pathIncludes: "/api/services/light/turn_on",
|
||||
kind: "action",
|
||||
method: "POST",
|
||||
optionReference: "lightEntity",
|
||||
invalidatesRequest: "light state",
|
||||
},
|
||||
{
|
||||
label: "light off action",
|
||||
pathIncludes: "/api/services/light/turn_off",
|
||||
kind: "action",
|
||||
method: "POST",
|
||||
optionReference: "lightEntity",
|
||||
invalidatesRequest: "light state",
|
||||
},
|
||||
],
|
||||
templateComponents: ["ToggleSwitch", "Badge", "SimpleGrid"],
|
||||
},
|
||||
},
|
||||
] as const;
|
||||
@@ -7,7 +7,6 @@ import { buildCustomWidgetAiPrompt } from "../src/core/ai-prompt";
|
||||
import { customWidgetDefinitionSchema } from "../src/core/custom-jsx-schema";
|
||||
import type { HomarrCustomWidgetV2 } from "../src/core/custom-jsx-schema";
|
||||
import { formatCustomWidgetImportIssues, parseCustomWidgetAiResponse } from "../src/core/import";
|
||||
import { collectCustomWidgetRequestReferences } from "../src/core/request-schema";
|
||||
import type { CustomWidgetAiEvaluationCase } from "./ai-evaluation-cases";
|
||||
|
||||
export const DEFAULT_GENERATOR_MODEL = "deepseek/deepseek-v4-pro";
|
||||
@@ -101,7 +100,7 @@ export function buildRepairPrompt(
|
||||
const diagnostics = issues
|
||||
.map((issue) => `${issue.path?.length ? `${issue.path.join(".")}: ` : ""}${issue.message}`)
|
||||
.join("\n");
|
||||
return `${originalPrompt}\n\nYour previous response did not meet the release quality bar. Correct every diagnosed contract, runtime, goal-fulfillment, visual, and UX problem below. Preserve grounded API behavior and the strongest working parts, but redesign weak areas when the feedback requires it.\n\nDiagnostics:\n${diagnostics}\n\nPrevious response:\n${previousResponse}`;
|
||||
return `${originalPrompt}\n\nYour previous response did not validate. Correct only the generic contract/runtime problems below while preserving a polished design.\n\nDiagnostics:\n${diagnostics}\n\nPrevious response:\n${previousResponse}`;
|
||||
}
|
||||
|
||||
export function buildJudgePrompt(testCase: CustomWidgetAiEvaluationCase, widget: HomarrCustomWidgetV2): string {
|
||||
@@ -159,85 +158,6 @@ export function getJudgeResponseFormat() {
|
||||
};
|
||||
}
|
||||
|
||||
function matchesScenarioRequestRule(
|
||||
request: HomarrCustomWidgetV2["requests"][string],
|
||||
rule: CustomWidgetAiEvaluationCase["acceptance"]["requestRules"][number],
|
||||
) {
|
||||
if (!request.path.includes(rule.pathIncludes)) return false;
|
||||
if (rule.kind && request.kind !== rule.kind) return false;
|
||||
if (rule.method && request.method !== rule.method) return false;
|
||||
if (rule.trigger && request.trigger !== rule.trigger) return false;
|
||||
const references = collectCustomWidgetRequestReferences(request);
|
||||
if (rule.optionReference && !references.options.has(rule.optionReference)) return false;
|
||||
if (rule.parameterReference && !references.params.has(rule.parameterReference)) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
export function getScenarioAcceptanceIssues(
|
||||
testCase: CustomWidgetAiEvaluationCase,
|
||||
widget: HomarrCustomWidgetV2,
|
||||
): Array<{ path?: Array<string | number>; message: string }> {
|
||||
const issues: Array<{ path?: Array<string | number>; message: string }> = [];
|
||||
const expectedAuth = testCase.acceptance.sourceAuth;
|
||||
if (expectedAuth) {
|
||||
const matches = Object.values(widget.sources).some((source) => {
|
||||
const actualType = typeof source.auth === "string" ? source.auth : source.auth.type;
|
||||
if (actualType !== expectedAuth.type) return false;
|
||||
return (
|
||||
expectedAuth.name === undefined ||
|
||||
(typeof source.auth !== "string" && source.auth.name.toLowerCase() === expectedAuth.name.toLowerCase())
|
||||
);
|
||||
});
|
||||
if (!matches) {
|
||||
issues.push({
|
||||
path: ["sources"],
|
||||
message: `The scenario requires ${expectedAuth.type}${expectedAuth.name ? ` '${expectedAuth.name}'` : ""} authentication.`,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const requestEntries = Object.entries(widget.requests);
|
||||
const candidateMatches = new Map<string, [string, HomarrCustomWidgetV2["requests"][string]]>();
|
||||
const candidateRequestIds = new Set<string>();
|
||||
for (const rule of testCase.acceptance.requestRules) {
|
||||
const match = requestEntries.find(
|
||||
([requestId, request]) => !candidateRequestIds.has(requestId) && matchesScenarioRequestRule(request, rule),
|
||||
);
|
||||
if (match) {
|
||||
candidateMatches.set(rule.label, match);
|
||||
candidateRequestIds.add(match[0]);
|
||||
}
|
||||
}
|
||||
|
||||
const requestIdsByLabel = new Map([...candidateMatches].map(([label, [requestId]]) => [label, requestId] as const));
|
||||
for (const rule of testCase.acceptance.requestRules) {
|
||||
const match = candidateMatches.get(rule.label);
|
||||
if (!match) {
|
||||
issues.push({
|
||||
path: ["requests"],
|
||||
message: `Missing grounded ${rule.label} request (${rule.pathIncludes}).`,
|
||||
});
|
||||
continue;
|
||||
}
|
||||
if (rule.invalidatesRequest) {
|
||||
const invalidatedRequestId = requestIdsByLabel.get(rule.invalidatesRequest);
|
||||
if (!invalidatedRequestId || !match[1].invalidates?.includes(invalidatedRequestId)) {
|
||||
issues.push({
|
||||
path: ["requests"],
|
||||
message: `The ${rule.label} request must invalidate the ${rule.invalidatesRequest} request.`,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (const component of testCase.acceptance.templateComponents) {
|
||||
if (!widget.template.includes(`<${component}`)) {
|
||||
issues.push({ path: ["template"], message: `The ${testCase.id} scenario requires ${component}.` });
|
||||
}
|
||||
}
|
||||
return issues;
|
||||
}
|
||||
|
||||
export async function evaluateCustomWidgetCase(args: {
|
||||
testCase: CustomWidgetAiEvaluationCase;
|
||||
apiKey: string;
|
||||
@@ -281,14 +201,6 @@ export async function evaluateCustomWidgetCase(args: {
|
||||
|
||||
const canonical = customWidgetDefinitionSchema.parse(parsed.widget);
|
||||
await writeWidgetFiles(caseDirectory, canonical, `attempt-${attempt}`);
|
||||
const acceptanceIssues = getScenarioAcceptanceIssues(args.testCase, canonical);
|
||||
if (acceptanceIssues.length > 0) {
|
||||
errors.push(
|
||||
`Attempt ${attempt}: deterministic acceptance failed — ${acceptanceIssues.map(({ message }) => message).join("; ")}`,
|
||||
);
|
||||
prompt = buildRepairPrompt(originalPrompt, response, acceptanceIssues);
|
||||
continue;
|
||||
}
|
||||
let judge: CustomWidgetJudgeResult;
|
||||
try {
|
||||
const judgeRaw = await callOpenRouter({
|
||||
|
||||
@@ -10,28 +10,35 @@ if (!apiKey) throw new Error("OPENROUTER_API_KEY is required for the live Custom
|
||||
|
||||
const requestedCase = process.argv.find((value) => value.startsWith("--case="))?.slice("--case=".length);
|
||||
const configuredLoops = Number(process.env.CUSTOM_WIDGET_AI_MAX_LOOPS ?? 10);
|
||||
const maxLoopsPerCase = Number.isInteger(configuredLoops) && configuredLoops > 0 ? Math.min(configuredLoops, 10) : 10;
|
||||
const totalLoopBudget = Number.isInteger(configuredLoops) && configuredLoops > 0 ? Math.min(configuredLoops, 10) : 10;
|
||||
const selectedCases = requestedCase
|
||||
? CUSTOM_WIDGET_AI_EVALUATION_CASES.filter((testCase) => testCase.id === requestedCase)
|
||||
: CUSTOM_WIDGET_AI_EVALUATION_CASES;
|
||||
if (selectedCases.length === 0) throw new Error(`Unknown AI evaluation case '${requestedCase}'`);
|
||||
if (totalLoopBudget < selectedCases.length) {
|
||||
throw new Error(`CUSTOM_WIDGET_AI_MAX_LOOPS must be at least ${selectedCases.length} for this run`);
|
||||
}
|
||||
|
||||
const runId = new Date().toISOString().replaceAll(/[:.]/gu, "-");
|
||||
const outputRoot = path.resolve(process.cwd(), ".ai-evaluations", runId);
|
||||
await mkdir(outputRoot, { recursive: true });
|
||||
|
||||
const results: AiEvaluationResult[] = [];
|
||||
let remainingLoopBudget = totalLoopBudget;
|
||||
for (const testCase of selectedCases) {
|
||||
const remainingCases = selectedCases.length - results.length;
|
||||
const maxLoops = Math.max(1, Math.floor(remainingLoopBudget / remainingCases));
|
||||
process.stdout.write(`Evaluating ${testCase.id}...\n`);
|
||||
const result = await evaluateCustomWidgetCase({
|
||||
testCase,
|
||||
apiKey,
|
||||
outputRoot,
|
||||
maxLoops: maxLoopsPerCase,
|
||||
maxLoops,
|
||||
generatorModel: process.env.OPENROUTER_GENERATOR_MODEL ?? DEFAULT_GENERATOR_MODEL,
|
||||
judgeModel: process.env.OPENROUTER_JUDGE_MODEL ?? DEFAULT_JUDGE_MODEL,
|
||||
});
|
||||
results.push(result);
|
||||
remainingLoopBudget -= result.attempts;
|
||||
process.stdout.write(
|
||||
result.judge
|
||||
? ` ${result.judge.total}/100 after ${result.attempts} attempt(s)\n`
|
||||
|
||||
@@ -47,7 +47,6 @@ const program = ts.createProgram({ rootNames: [...config.fileNames, import.meta.
|
||||
const checker = program.getTypeChecker();
|
||||
const source = program.getSourceFile(import.meta.filename);
|
||||
if (!source) throw new Error("Unable to inspect the component catalog generator");
|
||||
const sourceNode: ts.Node = source;
|
||||
|
||||
const moduleSymbols = new Map<string, ts.Symbol>();
|
||||
for (const declaration of source.statements) {
|
||||
@@ -164,7 +163,7 @@ function inspectMantineComponent(component: CatalogSourceComponent): InspectedCo
|
||||
const rootName = component.name.split(".")[0];
|
||||
let symbol =
|
||||
moduleSymbol && checker.getExportsOfModule(moduleSymbol).find((candidate) => candidate.name === rootName);
|
||||
let location: ts.Node = symbol?.valueDeclaration ?? symbol?.declarations?.[0] ?? sourceNode;
|
||||
let location = symbol?.valueDeclaration ?? symbol?.declarations?.[0] ?? source;
|
||||
let type = symbol && checker.getTypeOfSymbolAtLocation(symbol, location);
|
||||
for (const segment of component.name.split(".").slice(1)) {
|
||||
symbol = type?.getProperty(segment);
|
||||
@@ -312,7 +311,7 @@ function mergeGlobalProp(name: string, candidates: UninternedPropDescriptor[]):
|
||||
const fallback = createFallbackProp(name, "global");
|
||||
const type = mostFrequent(candidates.map((candidate) => candidate.type)) ?? fallback.type;
|
||||
const descriptions = candidates.flatMap(({ description }) => (description ? [description] : []));
|
||||
const literalValues = uniqueLiterals(candidates.flatMap((candidate) => candidate.literalValues ?? []));
|
||||
const literalValues = uniqueLiterals(candidates.flatMap(({ literalValues }) => literalValues ?? []));
|
||||
return {
|
||||
name,
|
||||
type,
|
||||
|
||||
@@ -24,7 +24,6 @@ export function RefreshButton({ label, color = "gray", variant = "subtle", size
|
||||
itemId: runtime.itemId,
|
||||
previewSessionId: runtime.previewSessionId,
|
||||
targets: ["parent", "*"],
|
||||
refresh: true,
|
||||
});
|
||||
} finally {
|
||||
setLoading(false);
|
||||
@@ -38,7 +37,7 @@ export function RefreshButton({ label, color = "gray", variant = "subtle", size
|
||||
size={size as never}
|
||||
onClick={() => void refresh()}
|
||||
loading={loading}
|
||||
disabled={(!runtime.itemId && !runtime.previewSessionId) || !runtime.canInvalidateQueries || runtime.isEditMode}
|
||||
disabled={(!runtime.itemId && !runtime.previewSessionId) || runtime.isEditMode}
|
||||
>
|
||||
<IconRefresh size={16} />
|
||||
</ActionIcon>
|
||||
|
||||
@@ -43,7 +43,6 @@ export interface RuntimeInvalidationInput {
|
||||
itemId?: string;
|
||||
previewSessionId?: string;
|
||||
targets: readonly string[];
|
||||
refresh?: boolean;
|
||||
}
|
||||
|
||||
export interface RuntimeNotification {
|
||||
@@ -83,7 +82,6 @@ export interface CustomWidgetRuntimeValue {
|
||||
previewSessionId?: string;
|
||||
previewLiveActions?: boolean;
|
||||
queriesDisabled?: boolean;
|
||||
canInvalidateQueries: boolean;
|
||||
isEditMode: boolean;
|
||||
requestCapabilities: readonly CustomJsxRequestCapability[];
|
||||
port: CustomWidgetRuntimePort;
|
||||
|
||||
@@ -5,6 +5,7 @@ export {
|
||||
assertSafeStaticHeaders,
|
||||
classifyAddress,
|
||||
executeCustomWidgetRequest,
|
||||
invalidateCustomWidgetResponseCache,
|
||||
MAX_REQUEST_DURATION_MS,
|
||||
MAX_REQUEST_BODY_BYTES,
|
||||
MAX_RESPONSE_BODY_BYTES,
|
||||
|
||||
@@ -32,6 +32,7 @@ export {
|
||||
export const MAX_REQUEST_BODY_BYTES = 10 * 1024;
|
||||
const REQUEST_TIMEOUT_MS = 10_000;
|
||||
const MAX_QUERY_REDIRECTS = 3;
|
||||
const MAX_RESPONSE_CACHE_ENTRIES = 1_000;
|
||||
export const MAX_REQUEST_DURATION_MS = 45_000;
|
||||
const TIMEOUT_ERROR_CODES = new Set(["UND_ERR_CONNECT_TIMEOUT", "UND_ERR_HEADERS_TIMEOUT", "UND_ERR_BODY_TIMEOUT"]);
|
||||
const RESPONSE_TOO_LARGE_ERROR_CODE = "UND_ERR_RES_EXCEEDED_MAX_SIZE";
|
||||
@@ -62,6 +63,9 @@ export interface CustomWidgetHttpResponse {
|
||||
statusText: string;
|
||||
data: unknown;
|
||||
}
|
||||
const cache = new Map<string, { expiresAt: number; response: CustomWidgetHttpResponse }>();
|
||||
const inFlight = new Map<string, Promise<CustomWidgetHttpResponse>>();
|
||||
let cacheEpoch = 0;
|
||||
|
||||
type RequestHopResult =
|
||||
| { kind: "response"; response: CustomWidgetHttpResponse }
|
||||
@@ -262,5 +266,42 @@ function buildHeaders(input: CustomWidgetHttpRequest, url: URL, body: string | u
|
||||
}
|
||||
|
||||
export async function executeCustomWidgetRequest(input: CustomWidgetHttpRequest): Promise<CustomWidgetHttpResponse> {
|
||||
return performRequest(input);
|
||||
const key = input.kind === "query" ? input.cacheKey : undefined;
|
||||
if (!key) return performRequest(input);
|
||||
const cached = cache.get(key);
|
||||
if (cached && cached.expiresAt > Date.now()) return cached.response;
|
||||
if (cached) cache.delete(key);
|
||||
const pending = inFlight.get(key);
|
||||
if (pending) return pending;
|
||||
const requestEpoch = cacheEpoch;
|
||||
let request: Promise<CustomWidgetHttpResponse>;
|
||||
request = performRequest(input)
|
||||
.then((response) => {
|
||||
if (response.ok && (input.cacheTtlSeconds ?? 0) > 0 && requestEpoch === cacheEpoch) {
|
||||
pruneCache();
|
||||
cache.set(key, { expiresAt: Date.now() + (input.cacheTtlSeconds ?? 0) * 1000, response });
|
||||
}
|
||||
return response;
|
||||
})
|
||||
.finally(() => {
|
||||
if (inFlight.get(key) === request) inFlight.delete(key);
|
||||
});
|
||||
inFlight.set(key, request);
|
||||
return request;
|
||||
}
|
||||
|
||||
export function invalidateCustomWidgetResponseCache(prefixes: readonly string[]): void {
|
||||
if (prefixes.length === 0) return;
|
||||
cacheEpoch += 1;
|
||||
for (const key of cache.keys()) if (prefixes.some((prefix) => key.startsWith(prefix))) cache.delete(key);
|
||||
for (const key of inFlight.keys()) if (prefixes.some((prefix) => key.startsWith(prefix))) inFlight.delete(key);
|
||||
}
|
||||
|
||||
function pruneCache(): void {
|
||||
for (const [key, entry] of cache) if (entry.expiresAt <= Date.now()) cache.delete(key);
|
||||
while (cache.size >= MAX_RESPONSE_CACHE_ENTRIES) {
|
||||
const key = cache.keys().next().value as string | undefined;
|
||||
if (!key) return;
|
||||
cache.delete(key);
|
||||
}
|
||||
}
|
||||
@@ -7,13 +7,10 @@ import {
|
||||
DEFAULT_GENERATOR_MODEL,
|
||||
DEFAULT_JUDGE_MODEL,
|
||||
getJudgeResponseFormat,
|
||||
getScenarioAcceptanceIssues,
|
||||
judgePasses,
|
||||
parseJudgeResult,
|
||||
} from "../../scripts/ai-evaluation";
|
||||
import type { CustomWidgetJudgeResult } from "../../scripts/ai-evaluation";
|
||||
import { BUNDLED_CUSTOM_WIDGETS, CUSTOM_WIDGET_STARTER, customWidgetDefinitionSchema } from "../core";
|
||||
import { PORTAINER_REFERENCE_WIDGET } from "./fixtures/reference-widgets";
|
||||
|
||||
const categoryNames = [
|
||||
"schemaAndBindings",
|
||||
@@ -52,13 +49,6 @@ describe("AI authoring evaluation", () => {
|
||||
it("defines five distinct complex scenarios", () => {
|
||||
expect(CUSTOM_WIDGET_AI_EVALUATION_CASES).toHaveLength(5);
|
||||
expect(new Set(CUSTOM_WIDGET_AI_EVALUATION_CASES.map((entry) => entry.id)).size).toBe(5);
|
||||
expect(CUSTOM_WIDGET_AI_EVALUATION_CASES.map(({ id }) => id)).toEqual([
|
||||
"pokedex",
|
||||
"portainer-containers",
|
||||
"football-dashboard",
|
||||
"jellyfin-activity",
|
||||
"home-assistant-control",
|
||||
]);
|
||||
});
|
||||
|
||||
it("uses the exact clipboard prompt and grounded API notes", () => {
|
||||
@@ -77,72 +67,6 @@ describe("AI authoring evaluation", () => {
|
||||
]);
|
||||
expect(prompt).toContain("requests.list.path: Required");
|
||||
expect(prompt).toContain("bad response");
|
||||
expect(prompt).toContain("visual, and UX problem");
|
||||
expect(prompt).toContain("redesign weak areas");
|
||||
});
|
||||
|
||||
it("requires grounded scenario capabilities before model judging", () => {
|
||||
const testCase = CUSTOM_WIDGET_AI_EVALUATION_CASES.find(({ id }) => id === "pokedex");
|
||||
const pokedex = BUNDLED_CUSTOM_WIDGETS.find(({ id }) => id === "seed-pokedex");
|
||||
if (!testCase || !pokedex) throw new Error("Expected Pokédex evaluation fixtures");
|
||||
expect(getScenarioAcceptanceIssues(testCase, customWidgetDefinitionSchema.parse(pokedex.widget))).toEqual([]);
|
||||
|
||||
const issues = getScenarioAcceptanceIssues(
|
||||
testCase,
|
||||
customWidgetDefinitionSchema.parse({ ...CUSTOM_WIDGET_STARTER, template: "<Text>Pokémon</Text>" }),
|
||||
);
|
||||
expect(issues.map(({ message }) => message)).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.stringContaining("Pokémon list"),
|
||||
expect.stringContaining("manual Pokémon detail"),
|
||||
expect.stringContaining("SubFetch"),
|
||||
]),
|
||||
);
|
||||
});
|
||||
|
||||
it("requires the exact option bindings and invalidation target for scenario actions", () => {
|
||||
const testCase = CUSTOM_WIDGET_AI_EVALUATION_CASES.find(({ id }) => id === "portainer-containers");
|
||||
if (!testCase) throw new Error("Expected the Portainer evaluation case");
|
||||
const portainer = customWidgetDefinitionSchema.parse(PORTAINER_REFERENCE_WIDGET);
|
||||
expect(getScenarioAcceptanceIssues(testCase, portainer)).toEqual([]);
|
||||
expect(
|
||||
getScenarioAcceptanceIssues(
|
||||
{
|
||||
...testCase,
|
||||
acceptance: {
|
||||
...testCase.acceptance,
|
||||
requestRules: testCase.acceptance.requestRules.toReversed(),
|
||||
},
|
||||
},
|
||||
portainer,
|
||||
),
|
||||
).toEqual([]);
|
||||
|
||||
const wrongBinding = customWidgetDefinitionSchema.parse({
|
||||
...portainer,
|
||||
requests: {
|
||||
...portainer.requests,
|
||||
start: {
|
||||
...portainer.requests.start,
|
||||
path: portainer.requests.start?.path.replace("{option:endpointId}", "{option:showAll}"),
|
||||
},
|
||||
},
|
||||
});
|
||||
expect(getScenarioAcceptanceIssues(testCase, wrongBinding).map(({ message }) => message)).toContain(
|
||||
"Missing grounded start action request (/start).",
|
||||
);
|
||||
|
||||
const wrongInvalidation = customWidgetDefinitionSchema.parse({
|
||||
...portainer,
|
||||
requests: {
|
||||
...portainer.requests,
|
||||
health: { path: "/api/status" },
|
||||
start: { ...portainer.requests.start, invalidates: ["health"] },
|
||||
},
|
||||
});
|
||||
expect(getScenarioAcceptanceIssues(testCase, wrongInvalidation).map(({ message }) => message)).toContain(
|
||||
"The start action request must invalidate the container list request.",
|
||||
);
|
||||
});
|
||||
|
||||
it("uses the requested DeepSeek models and strict structured judge output", () => {
|
||||
|
||||
@@ -8,14 +8,7 @@ import { MantineProvider } from "@mantine/core";
|
||||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import {
|
||||
ActionButton,
|
||||
CustomJsxRenderer,
|
||||
CustomWidgetRuntimeProvider,
|
||||
RefreshButton,
|
||||
SubFetch,
|
||||
ToggleSwitch,
|
||||
} from "../runtime";
|
||||
import { ActionButton, CustomJsxRenderer, CustomWidgetRuntimeProvider, SubFetch, ToggleSwitch } from "../runtime";
|
||||
import { createCustomJsxComponents } from "../jsx";
|
||||
import { MAX_REFRESH_INTERVAL_MS, MAX_REFRESH_INTERVAL_SECONDS, normalizeRefreshInterval } from "../runtime/sub-fetch";
|
||||
import type {
|
||||
@@ -85,7 +78,6 @@ async function render(
|
||||
port: CustomWidgetRuntimePort,
|
||||
capabilities: readonly CustomJsxRequestCapability[] = [],
|
||||
setQueryState?: (requestId: string, value: CustomWidgetPublishedQueryState | null) => void,
|
||||
canInvalidateQueries = true,
|
||||
) {
|
||||
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
|
||||
await act(async () => {
|
||||
@@ -94,7 +86,6 @@ async function render(
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<CustomWidgetRuntimeProvider
|
||||
itemId="item-1"
|
||||
canInvalidateQueries={canInvalidateQueries}
|
||||
isEditMode={false}
|
||||
requestCapabilities={capabilities}
|
||||
port={port}
|
||||
@@ -116,16 +107,6 @@ async function settle() {
|
||||
}
|
||||
|
||||
describe("Custom Widget runtime ports", () => {
|
||||
it("disables shared-cache refresh for anonymous public-board viewers", async () => {
|
||||
const port = createPort();
|
||||
await render(<RefreshButton />, port, [], undefined, false);
|
||||
|
||||
const button = host.querySelector<HTMLButtonElement>('button[aria-label="Refresh"]');
|
||||
expect(button?.disabled).toBe(true);
|
||||
button?.click();
|
||||
expect(port.invalidate).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("clamps refresh intervals below the browser timer overflow boundary", () => {
|
||||
expect(normalizeRefreshInterval(MAX_REFRESH_INTERVAL_SECONDS)).toBe(MAX_REFRESH_INTERVAL_MS);
|
||||
expect(normalizeRefreshInterval(MAX_REFRESH_INTERVAL_SECONDS + 1)).toBe(MAX_REFRESH_INTERVAL_MS);
|
||||
|
||||
@@ -82,7 +82,6 @@ export default function CustomApiWidget({
|
||||
...query.data,
|
||||
widgetDefinitionId: definitionId,
|
||||
widgetItemId: itemId,
|
||||
canInvalidateQueries: Boolean(session),
|
||||
isEditMode,
|
||||
}}
|
||||
/>
|
||||
|
||||
@@ -1,139 +0,0 @@
|
||||
import { act, createElement } from "react";
|
||||
import { createRoot } from "react-dom/client";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import type { ComponentPropsWithoutRef, ReactNode } from "react";
|
||||
import type { Root } from "react-dom/client";
|
||||
import type * as MantineCoreExports from "@mantine/core";
|
||||
import type * as TablerIconsExports from "@tabler/icons-react";
|
||||
|
||||
import CustomJsxDisplay from "./custom-jsx-display";
|
||||
|
||||
vi.mock("@homarr/api/client", () => ({
|
||||
clientApi: {
|
||||
useUtils: () => ({
|
||||
widget: {
|
||||
customApi: {
|
||||
getData: {
|
||||
invalidate: vi.fn(),
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
},
|
||||
fetchApi: {
|
||||
customWidget: {
|
||||
previewAction: { mutate: vi.fn() },
|
||||
previewQuery: { query: vi.fn() },
|
||||
previewRefresh: { mutate: vi.fn() },
|
||||
},
|
||||
widget: {
|
||||
customApi: {
|
||||
executeAction: { mutate: vi.fn() },
|
||||
queryRequest: { query: vi.fn() },
|
||||
refreshQueries: { mutate: vi.fn() },
|
||||
},
|
||||
},
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock("@homarr/modals", () => ({
|
||||
useConfirmModal: () => ({ openConfirmModal: vi.fn() }),
|
||||
}));
|
||||
|
||||
vi.mock("@homarr/notifications", () => ({
|
||||
showErrorNotification: vi.fn(),
|
||||
showSuccessNotification: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("@homarr/translation/client", () => ({
|
||||
useScopedI18n: () => (key: string) => (key === "refresh" ? "Refresh" : key),
|
||||
}));
|
||||
|
||||
vi.mock("@tanstack/react-query", () => ({
|
||||
useQueryClient: () => ({ invalidateQueries: vi.fn() }),
|
||||
}));
|
||||
|
||||
vi.mock("@mantine/core", async (importOriginal) => {
|
||||
const original = await importOriginal<typeof MantineCoreExports>();
|
||||
const renderElement = createElement;
|
||||
const MockContainer = ({ children }: { children?: ReactNode }) => renderElement("div", null, children);
|
||||
const ActionIcon = ({
|
||||
children,
|
||||
loading: _loading,
|
||||
color: _color,
|
||||
variant: _variant,
|
||||
size: _size,
|
||||
...props
|
||||
}: ComponentPropsWithoutRef<"button"> & {
|
||||
children?: ReactNode;
|
||||
loading?: boolean;
|
||||
color?: string;
|
||||
variant?: string;
|
||||
size?: string;
|
||||
}) => renderElement("button", props, children);
|
||||
|
||||
return {
|
||||
...original,
|
||||
ActionIcon,
|
||||
Alert: MockContainer,
|
||||
Box: MockContainer,
|
||||
Stack: MockContainer,
|
||||
Text: MockContainer,
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock("@tabler/icons-react", async (importOriginal) => {
|
||||
const original = await importOriginal<typeof TablerIconsExports>();
|
||||
return {
|
||||
...original,
|
||||
IconRefresh: () => createElement("span", { "aria-hidden": true }),
|
||||
};
|
||||
});
|
||||
|
||||
describe("CustomJsxDisplay preview refresh", () => {
|
||||
let container: HTMLDivElement;
|
||||
let root: Root;
|
||||
|
||||
beforeEach(() => {
|
||||
Object.defineProperty(globalThis, "IS_REACT_ACT_ENVIRONMENT", { value: true, writable: true });
|
||||
container = document.createElement("div");
|
||||
document.body.appendChild(container);
|
||||
root = createRoot(container);
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await act(async () => root.unmount());
|
||||
container.remove();
|
||||
});
|
||||
|
||||
const renderPreview = async (canInvalidateQueries?: boolean) => {
|
||||
await act(async () => {
|
||||
root.render(
|
||||
<CustomJsxDisplay
|
||||
data={{
|
||||
template: '<RefreshButton label="Refresh" />',
|
||||
data: {},
|
||||
previewSessionId: "preview-session",
|
||||
...(canInvalidateQueries === undefined ? {} : { canInvalidateQueries }),
|
||||
}}
|
||||
/>,
|
||||
);
|
||||
});
|
||||
return container.querySelector<HTMLButtonElement>('button[aria-label="Refresh"]');
|
||||
};
|
||||
|
||||
it("enables refresh when a preview session omits the invalidation capability", async () => {
|
||||
const button = await renderPreview();
|
||||
|
||||
expect(button).not.toBeNull();
|
||||
expect(button?.disabled).toBe(false);
|
||||
});
|
||||
|
||||
it("honors an explicitly disabled invalidation capability", async () => {
|
||||
const button = await renderPreview(false);
|
||||
|
||||
expect(button).not.toBeNull();
|
||||
expect(button?.disabled).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -69,7 +69,6 @@ export default function CustomJsxDisplay({ data }: { data: Record<string, unknow
|
||||
previewSessionId={previewSessionId}
|
||||
previewLiveActions={data.previewLiveActions === true}
|
||||
queriesDisabled={data.queriesDisabled === true}
|
||||
canInvalidateQueries={typeof data.canInvalidateQueries === "boolean" ? data.canInvalidateQueries : undefined}
|
||||
isEditMode={data.isEditMode === true}
|
||||
requestCapabilities={capabilities}
|
||||
setQueryState={publishQueryState}
|
||||
|
||||
@@ -15,15 +15,12 @@ import { showErrorNotification, showSuccessNotification } from "@homarr/notifica
|
||||
import { useScopedI18n } from "@homarr/translation/client";
|
||||
import { useQueryClient } from "@tanstack/react-query";
|
||||
|
||||
import { resolveCustomWidgetInvalidationTargets } from "./widget-invalidation";
|
||||
|
||||
interface WidgetDefinitionProviderProps {
|
||||
itemId?: string;
|
||||
definitionId?: string;
|
||||
previewSessionId?: string;
|
||||
previewLiveActions?: boolean;
|
||||
queriesDisabled?: boolean;
|
||||
canInvalidateQueries?: boolean;
|
||||
isEditMode?: boolean;
|
||||
requestCapabilities?: readonly CustomJsxRequestCapability[];
|
||||
setQueryState?(requestId: string, value: CustomWidgetPublishedQueryState | null): void;
|
||||
@@ -67,7 +64,6 @@ export function InactiveWidgetDefinitionProvider({
|
||||
return (
|
||||
<CustomWidgetRuntimeProvider
|
||||
definitionId={definitionId}
|
||||
canInvalidateQueries={false}
|
||||
isEditMode={isEditMode ?? false}
|
||||
requestCapabilities={[]}
|
||||
port={INACTIVE_PORT}
|
||||
@@ -110,85 +106,29 @@ export function WidgetDefinitionProvider(props: WidgetDefinitionProviderProps) {
|
||||
params: input.params,
|
||||
confirmed: input.confirmed,
|
||||
}),
|
||||
invalidate: async ({ itemId, previewSessionId, targets, refresh }) => {
|
||||
const invalidation = resolveCustomWidgetInvalidationTargets(props.requestCapabilities ?? [], targets);
|
||||
invalidate: async ({ itemId, previewSessionId, targets }) => {
|
||||
if (previewSessionId) {
|
||||
if (refresh && invalidation.requestIds.length > 0) {
|
||||
await fetchApi.customWidget.previewRefresh.mutate({
|
||||
sessionId: previewSessionId,
|
||||
requestIds: invalidation.requestIds,
|
||||
all: invalidation.all,
|
||||
});
|
||||
}
|
||||
const tasks: Promise<unknown>[] = [];
|
||||
if (invalidation.requestIds.length > 0) {
|
||||
tasks.push(
|
||||
queryClient.invalidateQueries({
|
||||
predicate: (query) => {
|
||||
const key = query.queryKey;
|
||||
return (
|
||||
key[0] === "custom-widget" &&
|
||||
key[1] === previewSessionId &&
|
||||
invalidation.requestIds.includes(String(key[2]))
|
||||
);
|
||||
},
|
||||
}),
|
||||
);
|
||||
}
|
||||
if (invalidation.refreshParent) {
|
||||
tasks.push(
|
||||
...invalidation.loadRequestIds.map(async (requestId) => {
|
||||
try {
|
||||
const result = await fetchApi.customWidget.previewQuery.query({
|
||||
sessionId: previewSessionId,
|
||||
requestId,
|
||||
params: {},
|
||||
});
|
||||
props.setQueryState?.(requestId, {
|
||||
data: result.data,
|
||||
status: {
|
||||
loading: false,
|
||||
ok: result.ok,
|
||||
status: result.status,
|
||||
statusText: result.statusText,
|
||||
error: result.error,
|
||||
},
|
||||
});
|
||||
} catch {
|
||||
props.setQueryState?.(requestId, {
|
||||
data: null,
|
||||
status: { loading: false, ok: false, status: 0, error: messages.requestFailed },
|
||||
});
|
||||
}
|
||||
}),
|
||||
);
|
||||
}
|
||||
await Promise.all(tasks);
|
||||
await queryClient.invalidateQueries({
|
||||
predicate: (query) => {
|
||||
const key = query.queryKey;
|
||||
return key[0] === "custom-widget" && key[1] === previewSessionId && targets.includes(String(key[2]));
|
||||
},
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (!itemId) return;
|
||||
if (invalidation.requestIds.length === 0 && !invalidation.refreshParent) return;
|
||||
if (refresh && invalidation.requestIds.length > 0) {
|
||||
await fetchApi.widget.customApi.refreshQueries.mutate({
|
||||
itemId,
|
||||
requestIds: invalidation.requestIds,
|
||||
all: invalidation.all,
|
||||
});
|
||||
}
|
||||
const tasks: Promise<unknown>[] = [];
|
||||
if (invalidation.requestIds.length > 0) {
|
||||
tasks.push(
|
||||
queryClient.invalidateQueries({
|
||||
predicate: (query) => {
|
||||
const key = query.queryKey;
|
||||
return (
|
||||
key[0] === "custom-widget" && key[1] === itemId && invalidation.requestIds.includes(String(key[2]))
|
||||
);
|
||||
},
|
||||
}),
|
||||
);
|
||||
}
|
||||
if (invalidation.refreshParent) {
|
||||
if (targets.length === 0) return;
|
||||
const tasks: Promise<unknown>[] = [
|
||||
queryClient.invalidateQueries({
|
||||
predicate: (query) => {
|
||||
const key = query.queryKey;
|
||||
return key[0] === "custom-widget" && key[1] === itemId && targets.includes(String(key[2]));
|
||||
},
|
||||
}),
|
||||
];
|
||||
if (
|
||||
(props.requestCapabilities ?? []).some((request) => request.trigger === "load" && targets.includes(request.id))
|
||||
) {
|
||||
tasks.push(utils.widget.customApi.getData.invalidate({ itemId }));
|
||||
}
|
||||
await Promise.all(tasks);
|
||||
@@ -214,7 +154,6 @@ export function WidgetDefinitionProvider(props: WidgetDefinitionProviderProps) {
|
||||
previewSessionId={props.previewSessionId}
|
||||
previewLiveActions={props.previewLiveActions}
|
||||
queriesDisabled={props.queriesDisabled}
|
||||
canInvalidateQueries={props.canInvalidateQueries ?? Boolean(props.previewSessionId)}
|
||||
isEditMode={props.isEditMode ?? false}
|
||||
requestCapabilities={props.requestCapabilities ?? []}
|
||||
port={port}
|
||||
|
||||
@@ -1,58 +0,0 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import type { CustomJsxRequestCapability } from "@homarr/custom-widgets/runtime";
|
||||
|
||||
import { resolveCustomWidgetInvalidationTargets } from "./widget-invalidation";
|
||||
|
||||
const capabilities: CustomJsxRequestCapability[] = [
|
||||
{
|
||||
id: "summary",
|
||||
kind: "query",
|
||||
method: "GET",
|
||||
trigger: "load",
|
||||
minimumBoardPermission: "view",
|
||||
},
|
||||
{
|
||||
id: "details",
|
||||
kind: "query",
|
||||
method: "GET",
|
||||
trigger: "manual",
|
||||
minimumBoardPermission: "view",
|
||||
},
|
||||
{
|
||||
id: "update",
|
||||
kind: "action",
|
||||
method: "POST",
|
||||
trigger: "manual",
|
||||
minimumBoardPermission: "modify",
|
||||
},
|
||||
];
|
||||
|
||||
describe("resolveCustomWidgetInvalidationTargets", () => {
|
||||
it("expands the refresh wildcard to every query and the parent load request", () => {
|
||||
expect(resolveCustomWidgetInvalidationTargets(capabilities, ["parent", "*"])).toEqual({
|
||||
all: true,
|
||||
requestIds: ["summary", "details"],
|
||||
loadRequestIds: ["summary"],
|
||||
refreshParent: true,
|
||||
});
|
||||
});
|
||||
|
||||
it("keeps action invalidation scoped to named query targets", () => {
|
||||
expect(resolveCustomWidgetInvalidationTargets(capabilities, ["details"])).toEqual({
|
||||
all: false,
|
||||
requestIds: ["details"],
|
||||
loadRequestIds: [],
|
||||
refreshParent: false,
|
||||
});
|
||||
});
|
||||
|
||||
it("refreshes the parent when a load query is invalidated by name", () => {
|
||||
expect(resolveCustomWidgetInvalidationTargets(capabilities, ["summary"])).toEqual({
|
||||
all: false,
|
||||
requestIds: ["summary"],
|
||||
loadRequestIds: ["summary"],
|
||||
refreshParent: true,
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,27 +0,0 @@
|
||||
import type { CustomJsxRequestCapability } from "@homarr/custom-widgets/runtime";
|
||||
|
||||
export interface CustomWidgetInvalidationTargets {
|
||||
all: boolean;
|
||||
requestIds: string[];
|
||||
loadRequestIds: string[];
|
||||
refreshParent: boolean;
|
||||
}
|
||||
|
||||
export function resolveCustomWidgetInvalidationTargets(
|
||||
capabilities: readonly CustomJsxRequestCapability[],
|
||||
targets: readonly string[],
|
||||
): CustomWidgetInvalidationTargets {
|
||||
const queries = capabilities.filter((request) => request.kind === "query");
|
||||
const all = targets.includes("*");
|
||||
const requested = new Set(targets);
|
||||
const requestIds = queries.flatMap((request) => (all || requested.has(request.id) ? [request.id] : []));
|
||||
const loadRequestIds = queries.flatMap((request) =>
|
||||
request.trigger === "load" && (all || requested.has(request.id)) ? [request.id] : [],
|
||||
);
|
||||
return {
|
||||
all,
|
||||
requestIds,
|
||||
loadRequestIds,
|
||||
refreshParent: requested.has("parent") || all || loadRequestIds.length > 0,
|
||||
};
|
||||
}
|
||||
@@ -3,7 +3,6 @@ import { describe, expect, test } from "vitest";
|
||||
import { CUSTOM_WIDGET_STARTER } from "@homarr/custom-widgets/core";
|
||||
|
||||
import {
|
||||
normalizeDocsBaseUrl,
|
||||
normalizeHttpUrl,
|
||||
resolveHomarrUrlConfig,
|
||||
validateWorkshopWidget,
|
||||
@@ -31,23 +30,6 @@ describe("Workshop URL configuration", () => {
|
||||
expect(() => normalizeHttpUrl("https://example.com/?token=secret", "WORKSHOP_API_URL")).toThrow("query string");
|
||||
expect(() => normalizeHttpUrl("\nhttps://example.com", "WORKSHOP_API_URL")).toThrow("control characters");
|
||||
});
|
||||
|
||||
test.each([
|
||||
[undefined, "/"],
|
||||
["/", "/"],
|
||||
["/docs", "/docs/"],
|
||||
["/docs/", "/docs/"],
|
||||
["/docs..preview", "/docs..preview/"],
|
||||
])("normalizes DOCS_BASE_URL %s", (input, expected) => {
|
||||
expect(normalizeDocsBaseUrl(input)).toBe(expected);
|
||||
});
|
||||
|
||||
test.each(["docs", "/docs?preview=1", "/../docs", "/%2e%2e/private", "/docs path", String.raw`/docs\private`])(
|
||||
"rejects invalid DOCS_BASE_URL %s",
|
||||
(input) => {
|
||||
expect(() => normalizeDocsBaseUrl(input)).toThrow("DOCS_BASE_URL");
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
describe("Workshop widget validation", () => {
|
||||
|
||||
@@ -68,19 +68,6 @@ export function resolveHomarrUrlConfig(input: HomarrUrlConfigInput = {}): Homarr
|
||||
return { homarrWebsiteUrl, workshopApiUrl, workshopWebUrl };
|
||||
}
|
||||
|
||||
export function normalizeDocsBaseUrl(value = "/"): string {
|
||||
if (
|
||||
!value.startsWith("/") ||
|
||||
/[^A-Za-z0-9._~/-]/u.test(value) ||
|
||||
value.split("/").some((segment) => segment === "." || segment === "..")
|
||||
) {
|
||||
throw new Error("DOCS_BASE_URL must be a safe path that starts with '/'");
|
||||
}
|
||||
|
||||
const path = value.replace(/^\/+|\/+$/gu, "");
|
||||
return path ? `/${path}/` : "/";
|
||||
}
|
||||
|
||||
export const workshopReportCategorySchema = z.enum(["malicious", "spam", "copyright", "inappropriate", "other"]);
|
||||
export const workshopSubmissionTypeSchema = z.enum(["customWidget", "customCss"]);
|
||||
export type WorkshopSubmissionType = z.infer<typeof workshopSubmissionTypeSchema>;
|
||||
|
||||
Generated
-3
@@ -1483,9 +1483,6 @@ importers:
|
||||
'@homarr/tsconfig':
|
||||
specifier: workspace:^0.1.0
|
||||
version: link:../../tooling/typescript
|
||||
ioredis:
|
||||
specifier: 'catalog:'
|
||||
version: 5.11.1
|
||||
typescript:
|
||||
specifier: 'catalog:'
|
||||
version: '@typescript/typescript6@6.0.2'
|
||||
|
||||
+1
-13
@@ -36,19 +36,7 @@
|
||||
"outputs": ["migrations/sqlite/migrate.cjs", "migrations/mysql/migrate.cjs", "migrations/postgresql/migrate.cjs"]
|
||||
},
|
||||
"@homarr/docs#build": {
|
||||
"env": ["DOCS_BASE_URL", "HOMARR_WEBSITE_URL", "WORKSHOP_API_URL", "WORKSHOP_URL", "WORKSHOP_WEB_URL"],
|
||||
"inputs": [
|
||||
"$TURBO_DEFAULT$",
|
||||
"../../packages/custom-widgets/package.json",
|
||||
"../../packages/custom-widgets/src/**",
|
||||
"../../packages/custom-widgets/tsconfig.json",
|
||||
"../../packages/definitions/package.json",
|
||||
"../../packages/definitions/src/docs/**",
|
||||
"../../packages/definitions/tsconfig.json",
|
||||
"../../packages/workshop/package.json",
|
||||
"../../packages/workshop/src/**",
|
||||
"../../packages/workshop/tsconfig.json"
|
||||
],
|
||||
"inputs": ["$TURBO_DEFAULT$", "../../packages/definitions/src/docs/**"],
|
||||
"outputs": ["build/**", ".docusaurus/**"]
|
||||
},
|
||||
"dev": {
|
||||
|
||||
Reference in new issue
Block a user