fix: 404 on the public form page for a sub-team form when the owner itself was removed from the org (#18495)

* wip

* fix: 404 for a form of a member that has been removed.

- Moved existing authorization check to isAuthorizedToViewFormOnOrgDomain function to handle form access control
- Add comprehensive test suite for org domain authorization scenarios
- Refactor getServerSideProps to use the new authorization function
- Improve code organization by separating domain-specific authorization logic

Test Coverage:
- Non-org domain access
- Org member's form access
- Sub-team form access within org
- Access denial for non-org members/teams
This commit is contained in:
Hariom Balhara authored and GitHub committed 2025-01-07 12:13:19 +00:00
1 parent 0c69c5dfea
commit 06d8b98b15
4 files changed
+167 -38

No files matched your search

@@ -1,44 +1,10 @@
import { orgDomainConfig } from "@calcom/features/ee/organizations/lib/orgDomains";
import type { Prisma } from "@calcom/prisma/client";
import { userMetadata } from "@calcom/prisma/zod-utils";
import { isAuthorizedToViewFormOnOrgDomain } from "@calcom/features/routing-forms/lib/isAuthorizedToViewForm";
import type { AppGetServerSidePropsContext, AppPrisma } from "@calcom/types/AppGetServerSideProps";
import { enrichFormWithMigrationData } from "../../enrichFormWithMigrationData";
import { getSerializableForm } from "../../lib/getSerializableForm";
export function isAuthorizedToViewTheForm({
user,
currentOrgDomain,
}: {
user: {
username: string | null;
metadata: Prisma.JsonValue;
movedToProfileId: number | null;
profile: {
organization: { slug: string | null; requestedSlug: string | null } | null;
};
id: number;
};
currentOrgDomain: string | null;
}) {
const formUser = {
...user,
metadata: userMetadata.parse(user.metadata),
};
const orgSlug = formUser.profile.organization?.slug ?? formUser.profile.organization?.requestedSlug ?? null;
if (!currentOrgDomain) {
// If not on org domain, let's allow serving any form belong to any organization so that even if the form owner is migrate to an organization, old links for the form keep working
return true;
} else if (currentOrgDomain !== orgSlug) {
// If on org domain,
// We don't serve the form that is of another org
// We don't serve the form that doesn't belong to any org
return false;
}
return true;
}
export const getServerSideProps = async function getServerSideProps(
context: AppGetServerSidePropsContext,
prisma: AppPrisma
@@ -108,7 +74,9 @@ export const getServerSideProps = async function getServerSideProps(
user: await UserRepository.enrichUserWithItsProfile({ user: form.user }),
};
if (!isAuthorizedToViewTheForm({ user: formWithUserProfile.user, currentOrgDomain })) {
if (
!isAuthorizedToViewFormOnOrgDomain({ user: formWithUserProfile.user, currentOrgDomain, team: form.team })
) {
return {
notFound: true,
};