Files
webstudio/apps/builder/app/shared/instance-utils/link.ts
T
Oleg Isonen 726b288a3e fix: allow Builder validation with view access (#6725)
Builder publish validation uses project view access, so admin share
links and owner sessions can run it without the separate API permission.
It still checks that requested domains belong to the project and are
active and verified. CLI/MCP API permissions and publish controls are
unchanged.

The diagnostics panel keeps local findings when server validation
reports a matching missing resource. Findings show the affected variable
and scope, and **Show instance** opens the relevant page and Settings
view. Warnings remain visible beside errors. Server-only IDs stay in
copied reports.

- [x] Allow Builder validation for view-authorized project sessions
without changing CLI/MCP API authorization.
- [x] Keep domain ownership and verification checks.
- [x] Link findings to the affected instance and the relevant Settings
view.
- [x] Identify missing-resource variables and their page or instance
scope.
- [x] Remove the repeated “Audit” heading and use “instance” in Builder
diagnostics and MCP documentation.
- [x] Cover deep links, page and global scopes, warnings, display, and
the 500-finding fallback with regression tests.
- [x] Run focused Builder and project-build tests, typechecks, lint,
formatting, and generated-file checks.
2026-11-21 18:44:40 +01:00

97 lines
2.5 KiB
TypeScript

import {
getAllPages,
isPageTemplate,
ROOT_INSTANCE_ID,
getPageById,
type Instances,
type Pages,
} from "@webstudio-is/sdk";
import type { InstanceSelector } from "@webstudio-is/project-build/runtime";
import {
$authToken,
$builderMode,
$canOpenPageTemplates,
} from "../nano-states";
import { builderUrl } from "../router-utils";
import type { BuilderView } from "../router-utils/path-utils";
import { $instances, $pages, $project } from "../sync/data-stores";
import { canResolveInstanceSelector } from "./selection";
export const getInstanceLink = (
instanceSelector: InstanceSelector | undefined,
{ pageId, view }: { pageId?: string; view?: BuilderView } = {}
) => {
const pages = $pages.get();
const project = $project.get();
if (pages === undefined || project === undefined) {
return;
}
const selection = getDeepLinkedInstanceSelection({
instanceSelector,
pageId,
pages,
instances: $instances.get(),
canOpenPageTemplates: $canOpenPageTemplates.get(),
});
if (selection === undefined) {
return;
}
const mode = $builderMode.get();
return builderUrl({
...selection,
projectId: project.id,
origin: window.location.origin,
authToken: $authToken.get(),
mode: mode === "design" ? undefined : mode,
view,
safemode:
new URLSearchParams(window.location.search).get("safemode") === "true",
});
};
export const getInstanceSelectorFromUrl = (searchParams: URLSearchParams) =>
searchParams.get("instance")?.split(",");
export const getDeepLinkedInstanceSelection = ({
instanceSelector,
pageId,
canOpenPageTemplates,
pages,
instances,
}: {
instanceSelector: InstanceSelector | undefined;
pageId?: string;
canOpenPageTemplates: boolean;
pages: Pages;
instances: Instances;
}) => {
if (instanceSelector === undefined) {
return;
}
const instanceId = instanceSelector[0];
const isGlobalRoot =
instanceSelector.length === 1 && instanceId === ROOT_INSTANCE_ID;
if (
instanceId === undefined ||
(!isGlobalRoot && instances.has(instanceId) === false) ||
canResolveInstanceSelector(instanceSelector, instances) === false
) {
return;
}
const page =
isGlobalRoot && pageId !== undefined
? (getPageById(pages, pageId) ?? pages.pageTemplates?.get(pageId))
: getAllPages(pages).find(
(page) => page.rootInstanceId === instanceSelector.at(-1)
);
if (
page === undefined ||
(isPageTemplate(page) && canOpenPageTemplates === false)
) {
return;
}
return { pageId: page.id, instanceSelector };
};