36 lines
1.4 KiB
TypeScript
36 lines
1.4 KiB
TypeScript
import { useSearchParams } from "react-router";
|
|
import PageFeatureDisabled from "~/components/pages/PageFeatureDisabled";
|
|
import { appPath } from "~/utils/app/CanonicalUrlUtils";
|
|
|
|
const MAX_FEATURE_LABEL_LENGTH = 80;
|
|
const SECRET_PATTERN = /(secret|token|password|passwd|api[_-]?key|private[_-]?key|credential|bearer|session|jwt)/i;
|
|
const SAFE_FEATURE_LABEL_PATTERN = /^[\p{L}\p{N}][\p{L}\p{N} .,&'()/_-]*$/u;
|
|
const SAFE_TENANT_SLUG_PATTERN = /^[a-z0-9][a-z0-9-]{0,62}$/;
|
|
|
|
export function getSafeFeatureLabel(feature: string | null): string | undefined {
|
|
const label = feature?.trim();
|
|
|
|
if (!label || label.length > MAX_FEATURE_LABEL_LENGTH || SECRET_PATTERN.test(label) || !SAFE_FEATURE_LABEL_PATTERN.test(label)) {
|
|
return undefined;
|
|
}
|
|
|
|
return label;
|
|
}
|
|
|
|
export function getFeatureDisabledDashboardPath(tenantSlug: string | null) {
|
|
const slug = tenantSlug?.trim();
|
|
if (!slug || !SAFE_TENANT_SLUG_PATTERN.test(slug)) {
|
|
return "/dashboard";
|
|
}
|
|
|
|
return appPath({ tenantSlug: slug, path: "dashboard", mode: "host-aware" });
|
|
}
|
|
|
|
export default function FeatureDisabledRoute() {
|
|
const [searchParams] = useSearchParams();
|
|
const featureLabel = getSafeFeatureLabel(searchParams.get("feature"));
|
|
const dashboardPath = getFeatureDisabledDashboardPath(searchParams.get("tenant"));
|
|
|
|
return <PageFeatureDisabled featureLabel={featureLabel} dashboardPath={dashboardPath} />;
|
|
}
|