fix: complete review hardening
This commit is contained in:
1 parent
6f85e18065
commit
2449eb0bc7
13 files changed
+99
-64
No files matched your search
@@ -1,5 +1,16 @@
|
||||
import type { QueryKey } from "@tanstack/react-query";
|
||||
|
||||
const isRecord = (value: unknown): value is Record<string, unknown> => typeof value === "object" && value !== null;
|
||||
|
||||
interface ForbiddenTrpcError {
|
||||
data: { code: "FORBIDDEN" };
|
||||
}
|
||||
|
||||
export const isTrpcForbiddenError = (error: unknown): error is ForbiddenTrpcError => {
|
||||
if (!isRecord(error) || !isRecord(error.data)) return false;
|
||||
return error.data.code === "FORBIDDEN";
|
||||
};
|
||||
|
||||
// RSC-prefetched data must survive the hydration pass without an immediate
|
||||
// duplicate fetch. Widget-specific intervals still control live refreshes.
|
||||
export const queryCacheDefaultStaleTimeMs = 30_000;
|
||||
@@ -8,6 +19,7 @@ export const queryCacheDefaultGcTimeMs = 1000 * 60 * 5;
|
||||
export const queryCacheMetadataStaleTimeMs = 1000 * 60 * 5;
|
||||
|
||||
const widgetDataQueryPaths = new Set(["app.byId", "app.byIds", "docker.getContainers", "integration.byIds"]);
|
||||
const persistableDashboardDataPaths = new Set(["widget.stockPrice.getPriceHistory"]);
|
||||
// Beszel system stats are large time-series payloads and were deliberately kept out of
|
||||
// the persisted cache in #6289; live stats come from an SSE subscription instead.
|
||||
const excludedWidgetPaths = new Set(["widget.app.ping", "widget.beszel.getSystemStats"]);
|
||||
@@ -66,3 +78,12 @@ export const isWidgetDataQueryKey = (queryKey: QueryKey) => {
|
||||
|
||||
return isWidgetDataTrpcPath(path.join("."));
|
||||
};
|
||||
|
||||
// Persist only data that is both public and independent of Homarr access grants.
|
||||
// Integration, app, Docker, board-backed, and location queries must refetch after reload.
|
||||
export const isPersistableDashboardQueryKey = (queryKey: QueryKey) => {
|
||||
const path = getTrpcPathFromQueryKey(queryKey);
|
||||
if (!path) return false;
|
||||
|
||||
return persistableDashboardDataPaths.has(path.join("."));
|
||||
};
|
||||
@@ -87,14 +87,14 @@ export const dockerRouter = createTRPCRouter({
|
||||
mcp: {
|
||||
enabled: true,
|
||||
description:
|
||||
"Invalidate the cached Docker inventory so the next Docker query performs a fresh user-requested discovery. Requires admin permission.",
|
||||
"Invalidate the local Docker inventory and publish a non-persistent Redis refresh event. Returns scope 'published' when Redis accepts the event or 'local' when only this replica is refreshed. Requires admin permission.",
|
||||
},
|
||||
})
|
||||
.concat(dockerMiddleware())
|
||||
.mutation(async () => {
|
||||
resetDockerInventory();
|
||||
const propagated = await publishDockerInventoryRefreshAsync();
|
||||
if (propagated) return { scope: "all" as const };
|
||||
if (propagated) return { scope: "published" as const };
|
||||
return { scope: "local" as const };
|
||||
}),
|
||||
getContainers: permissionRequiredProcedure
|
||||
|
||||
@@ -6644,6 +6644,10 @@
|
||||
"title": "Containers refreshed",
|
||||
"message": "You are now viewing the most recent data"
|
||||
},
|
||||
"warning": {
|
||||
"title": "Only this Homarr instance was refreshed",
|
||||
"message": "Redis did not accept the refresh event, so other Homarr instances may still show older Docker data"
|
||||
},
|
||||
"error": {
|
||||
"title": "Containers not refreshed",
|
||||
"message": "Something went wrong while refreshing the containers"
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { isTrpcForbiddenError } from "@homarr/api/query-cache";
|
||||
|
||||
interface WidgetQueryState {
|
||||
data: unknown;
|
||||
error: unknown;
|
||||
@@ -7,17 +9,6 @@ interface PendingWidgetQueryState extends WidgetQueryState {
|
||||
isPending: boolean;
|
||||
}
|
||||
|
||||
const isRecord = (value: unknown): value is Record<string, unknown> => typeof value === "object" && value !== null;
|
||||
|
||||
interface ForbiddenTrpcError {
|
||||
data: { code: "FORBIDDEN" };
|
||||
}
|
||||
|
||||
export const isTrpcForbiddenError = (error: unknown): error is ForbiddenTrpcError => {
|
||||
if (!isRecord(error) || !isRecord(error.data)) return false;
|
||||
return error.data.code === "FORBIDDEN";
|
||||
};
|
||||
|
||||
/** Throws terminal query failures while preserving successful empty values and stale cached data. */
|
||||
export const getUsableWidgetQueryData = <TQuery extends WidgetQueryState>(query: TQuery): TQuery["data"] => {
|
||||
if (isTrpcForbiddenError(query.error)) throw query.error;
|
||||
|
||||
@@ -42,7 +42,7 @@ import type { ContainerState, DockerEndpointCapability } from "@homarr/docker";
|
||||
import { containerStateColorMap, cpuUsageColor, memoryUsageColor, safeValue } from "@homarr/docker/shared";
|
||||
import { useModalAction } from "@homarr/modals";
|
||||
import { AddDockerAppToHomarr, useDockerContainerRemovalConfirmation } from "@homarr/modals-collection";
|
||||
import { showErrorNotification, showSuccessNotification } from "@homarr/notifications";
|
||||
import { showErrorNotification, showSuccessNotification, showWarningNotification } from "@homarr/notifications";
|
||||
import { useI18n } from "@homarr/translation/client";
|
||||
import { zoomCompensatedSize } from "@homarr/ui";
|
||||
|
||||
@@ -285,12 +285,18 @@ export default function DockerWidget({
|
||||
const data = getUsableWidgetQueryData(containersQuery);
|
||||
const { isFetching } = containersQuery;
|
||||
const refreshInventory = clientApi.docker.refreshInventory.useMutation({
|
||||
async onSuccess() {
|
||||
async onSuccess(result) {
|
||||
await Promise.all([
|
||||
utils.docker.getContainers.invalidate(),
|
||||
utils.docker.reconcileServices.invalidate(),
|
||||
utils.docker.getServiceHealth.invalidate(),
|
||||
]);
|
||||
if (result.scope === "local") {
|
||||
showWarningNotification({
|
||||
title: t("action.refresh.notification.warning.title"),
|
||||
message: t("action.refresh.notification.warning.message"),
|
||||
});
|
||||
}
|
||||
},
|
||||
onError() {
|
||||
showErrorNotification({
|
||||
|
||||
Reference in new issue
Block a user