mirror of
https://github.com/webstudio-is/webstudio.git
synced 2026-12-04 22:31:33 +09:00
## Description Project Settings starts with an empty response-header rule list. Users can add valid HTTP response headers for all paths (`/*`) or an Authentication-style route such as `/private/*`, and remove any configured rule. The Headers and Authentication views share the accessible rule-list form. The header form suggests standard response-header names and common values for the selected header, while accepting custom names and values. `Set-Cookie` is not suggested because a rule can store only one value per header and would replace existing cookies. `X-Powered-By`, `X-Content-Type-Options`, and `Strict-Transport-Security` are dispatcher-owned and cannot be configured. Users may configure `Content-Security-Policy`, `X-Frame-Options`, and `Referrer-Policy`; when absent, the dispatcher supplies their defaults. The SDK validates header names, values, routes, duplicates, and size limits. The CLI emits only configured rules. Customization requires Pro for custom-domain publishing, while unchanged defaults and staging remain available on Free plans. The server checks the saved production snapshot against the project owner's plan. Companion Cloud worker PR: https://github.com/webstudio-is/webstudio-saas/pull/664. Deploy its dispatcher support before exposing these settings in the Builder. Other hosts need an adapter that applies the generated rules; Webstudio Cloud dispatcher defaults do not apply automatically elsewhere. Static exports do not apply headers automatically. ## Validation - SDK, CLI, Builder browser, publish-warning, and production-build tests pass. - Builder, SDK, and CLI typechecks and lint pass. - The companion Cloud worker tests, typecheck, and lint pass. Replaces https://github.com/webstudio-is/webstudio/pull/6428 to use `headers.staging`. Closes https://github.com/webstudio-is/webstudio/issues/4748
204 lines
6.0 KiB
TypeScript
204 lines
6.0 KiB
TypeScript
import { useEffect, useMemo, useState } from "react";
|
|
import {
|
|
Button,
|
|
Flex,
|
|
Grid,
|
|
LinkButton,
|
|
ProChip,
|
|
Text,
|
|
theme,
|
|
Tooltip,
|
|
cssVar,
|
|
} from "@webstudio-is/design-system";
|
|
import { InfoCircleIcon } from "@webstudio-is/icons";
|
|
import { useStore } from "@nanostores/react";
|
|
import {
|
|
createBasicAuthRoute,
|
|
serializeWsAuth,
|
|
type WsAuthRoute,
|
|
} from "@webstudio-is/wsauth";
|
|
import { validateBasicAuthCredentials } from "@webstudio-is/project-build/runtime";
|
|
import { $permissions } from "~/shared/nano-states";
|
|
import { $pages, $projectSettings } from "~/shared/sync/data-stores";
|
|
import { getExistingRoutePaths, sectionSpacing } from "./utils";
|
|
import { executeRuntimeMutation } from "~/shared/instance-utils/data";
|
|
import {
|
|
parseProjectAuthRoutes,
|
|
validateProjectAuthRoute,
|
|
} from "@webstudio-is/project-build/contracts";
|
|
import {
|
|
ProjectSettingsDeleteRuleButton,
|
|
ProjectSettingsRuleList,
|
|
} from "./rule-list";
|
|
|
|
const saveAuthRoutes = (authRoutes: WsAuthRoute[]) => {
|
|
executeRuntimeMutation({
|
|
id: "projectSettings.update",
|
|
input: {
|
|
meta: {
|
|
auth: authRoutes.length === 0 ? null : serializeWsAuth(authRoutes),
|
|
},
|
|
},
|
|
});
|
|
};
|
|
|
|
export const SectionAuth = () => {
|
|
const { allowAuth } = useStore($permissions);
|
|
const pages = useStore($pages);
|
|
const projectSettings = useStore($projectSettings);
|
|
const [authRoutes, setAuthRoutes] = useState(() => {
|
|
return parseProjectAuthRoutes($projectSettings.get()?.meta.auth).routes;
|
|
});
|
|
|
|
const authContent = projectSettings?.meta.auth;
|
|
const parseResult = useMemo(() => {
|
|
return parseProjectAuthRoutes(authContent);
|
|
}, [authContent]);
|
|
const parseErrors = parseResult.errors;
|
|
|
|
useEffect(() => {
|
|
setAuthRoutes(parseResult.routes);
|
|
}, [parseResult.routes]);
|
|
|
|
const existingPaths = getExistingRoutePaths(pages);
|
|
const routeSuggestions = ["/", ...Array.from(existingPaths).sort()];
|
|
const handleSave = (nextAuthRoutes: WsAuthRoute[]) => {
|
|
setAuthRoutes(nextAuthRoutes);
|
|
saveAuthRoutes(nextAuthRoutes);
|
|
};
|
|
|
|
const handleAddAuthRoute = (values: Record<string, string>) => {
|
|
handleSave([
|
|
createBasicAuthRoute({
|
|
route: values.route.trim(),
|
|
login: values.login,
|
|
password: values.password,
|
|
}),
|
|
...authRoutes,
|
|
]);
|
|
return true;
|
|
};
|
|
|
|
const handleDeleteAuthRoute = (index: number) => {
|
|
const nextAuthRoutes = [...authRoutes];
|
|
nextAuthRoutes.splice(index, 1);
|
|
handleSave(nextAuthRoutes);
|
|
};
|
|
|
|
const handleReset = () => {
|
|
handleSave([]);
|
|
};
|
|
|
|
return (
|
|
<Grid gap={3} css={sectionSpacing}>
|
|
<Flex align="center" gap={1}>
|
|
<Text variant="titles">Authentication</Text>
|
|
{allowAuth === false && <ProChip>PRO</ProChip>}
|
|
<Tooltip
|
|
content={
|
|
<>
|
|
<Text>
|
|
Authentication asks visitors for HTTP Basic Auth credentials
|
|
before protected pages load on custom domains.
|
|
</Text>
|
|
<br />
|
|
<Text>
|
|
Routes use the same syntax as page paths, including :params and
|
|
* wildcards.
|
|
</Text>
|
|
{allowAuth === false && (
|
|
<>
|
|
<br />
|
|
<Text>
|
|
Authentication is a Pro feature. You can publish to staging
|
|
for free; upgrade to Pro to publish to custom domains.
|
|
</Text>
|
|
<LinkButton
|
|
color="primary"
|
|
css={{ marginTop: theme.spacing[5], width: "100%" }}
|
|
target="_blank"
|
|
href="https://webstudio.is/pricing"
|
|
>
|
|
Upgrade
|
|
</LinkButton>
|
|
</>
|
|
)}
|
|
</>
|
|
}
|
|
variant="wrapped"
|
|
>
|
|
<InfoCircleIcon
|
|
color={cssVar("--foreground-secondary")}
|
|
tabIndex={-1}
|
|
/>
|
|
</Tooltip>
|
|
</Flex>
|
|
|
|
{parseErrors.length > 0 && (
|
|
<Grid gap={2}>
|
|
<Grid gap={1}>
|
|
{parseErrors.map((error) => (
|
|
<Text key={`${error.path}:${error.message}`} color="destructive">
|
|
{error.path}: {error.message}
|
|
</Text>
|
|
))}
|
|
</Grid>
|
|
<Flex>
|
|
<Button color="destructive" onClick={handleReset}>
|
|
Reset authentication
|
|
</Button>
|
|
</Flex>
|
|
</Grid>
|
|
)}
|
|
|
|
<ProjectSettingsRuleList
|
|
fields={[
|
|
{
|
|
name: "route",
|
|
placeholder: "/private or /docs/*",
|
|
suggestions: routeSuggestions,
|
|
validateOnChange: (value) =>
|
|
validateProjectAuthRoute(value.trim(), authRoutes),
|
|
},
|
|
{ name: "login", placeholder: "Login" },
|
|
{ name: "password", placeholder: "Password", type: "password" },
|
|
]}
|
|
validate={(values) => ({
|
|
route: validateProjectAuthRoute(
|
|
values.route?.trim() ?? "",
|
|
authRoutes
|
|
),
|
|
...validateBasicAuthCredentials({
|
|
login: values.login ?? "",
|
|
password: values.password ?? "",
|
|
}),
|
|
})}
|
|
onSubmit={handleAddAuthRoute}
|
|
columns="1fr 1fr"
|
|
columnLabels={["Path", "Login"]}
|
|
label="Authentication rules"
|
|
rules={authRoutes.map((authRoute, index) => ({
|
|
key: authRoute.route,
|
|
values: [
|
|
<Tooltip content={authRoute.route} key="route">
|
|
<Text truncate css={{ wordBreak: "break-all" }}>
|
|
{authRoute.route}
|
|
</Text>
|
|
</Tooltip>,
|
|
<Tooltip content={authRoute.auth.login} key="login">
|
|
<Text truncate>{authRoute.auth.login}</Text>
|
|
</Tooltip>,
|
|
],
|
|
actions: (
|
|
<ProjectSettingsDeleteRuleButton
|
|
label={`Delete authentication for ${authRoute.route}`}
|
|
description={`authentication for ${authRoute.route}`}
|
|
onDelete={() => handleDeleteAuthRoute(index)}
|
|
/>
|
|
),
|
|
}))}
|
|
/>
|
|
</Grid>
|
|
);
|
|
};
|