Skip to content

⭐️⭐️⭐️ Open-source, self-hosted alternative to Linear and Plane. Project management and issue tracking where teams and AI agents work side by side to plan and ship products.

fix: hide invite links from members who cannot send the invite (#631)

Croff27eddf396995
diff --git a/apps/api/src/modules/invites/__tests__/integration/invites.test.ts b/apps/api/src/modules/invites/__tests__/integration/invites.test.ts
index 5dda1547..c2daad54 100644
--- a/apps/api/src/modules/invites/__tests__/integration/invites.test.ts
+++ b/apps/api/src/modules/invites/__tests__/integration/invites.test.ts
@@ -361,7 +361,51 @@ describe('invites', () => {
const res = await owner.api.projects({ projectKey: 'MKT' }).invites.get();
expect(res.status).toBe(200);
- expect(res.data?.map((i) => i.email)).toEqual(['[email protected]', '[email protected]']);
+ expect(res.data?.map((i) => [i.email, i.token !== null])).toEqual([
+ ['[email protected]', true],
+ ['[email protected]', true],
+ ]);
+ });
+
+ it('hides the tokens from a member who may only read the invites', async () => {
+ const owner = await setupOwner();
+ const teamId = await ownTeamId(owner.api);
+ const reader = await addTeamMember(owner, teamId, 'member');
+ await owner.api
+ .projects({ projectKey: 'MKT' })
+ .members.post({ userId: reader.user.userId, role: 'member' });
+ await owner.api
+ .projects({ projectKey: 'MKT' })
+ .invites.post({ email: '[email protected]', role: 'owner' });
+
+ const res = await reader.api.projects({ projectKey: 'MKT' }).invites.get();
+ expect(res.status).toBe(200);
+ expect(res.data).toMatchObject([{ email: '[email protected]', token: null }]);
+ });
+
+ it('hides the token of an owner invite from a member who may not grant ownership', async () => {
+ const owner = await setupOwner();
+ const teamId = await ownTeamId(owner.api);
+ const inviter = await addTeamMember(owner, teamId, 'member');
+ const role = await createRole(owner.api, 'MKT', {
+ name: 'Recruiter',
+ permissions: { members_invite: { create: true, read: true } },
+ });
+ await owner.api
+ .projects({ projectKey: 'MKT' })
+ .members.post({ userId: inviter.user.userId, role: 'member', roleId: role.data!.id });
+ await owner.api
+ .projects({ projectKey: 'MKT' })
+ .invites.post({ email: '[email protected]', role: 'member' });
+ await owner.api
+ .projects({ projectKey: 'MKT' })
+ .invites.post({ email: '[email protected]', role: 'owner' });
+
+ const res = await inviter.api.projects({ projectKey: 'MKT' }).invites.get();
+ expect(res.data?.map((i) => [i.email, i.token !== null])).toEqual([
+ ['[email protected]', false],
+ ['[email protected]', true],
+ ]);
});
it('denies a non-member with 403', async () => {
@@ -829,6 +873,25 @@ describe('invites', () => {
]);
});
+ it('hides the token of a manager invite from a manager', async () => {
+ const owner = await setupOwner();
+ const teamId = await ownTeamId(owner.api);
+ const manager = await addTeamMember(owner, teamId, 'manager');
+ await owner.api
+ .teams({ teamId })
+ .invites.post({ email: '[email protected]', role: 'manager' });
+ await owner.api
+ .teams({ teamId })
+ .invites.post({ email: '[email protected]', role: 'member' });
+
+ const res = await manager.api.teams({ teamId }).invites.get();
+ const pending = res.data?.filter((i) => i.status === 'pending');
+ expect(pending?.map((i) => [i.email, i.token !== null])).toEqual([
+ ['[email protected]', true],
+ ['[email protected]', false],
+ ]);
+ });
+
it('revokes an invite of the team', async () => {
const owner = await setupOwner();
const teamId = await ownTeamId(owner.api);
diff --git a/apps/api/src/modules/invites/index.ts b/apps/api/src/modules/invites/index.ts
index 8435355c..d9e8a0a4 100644
--- a/apps/api/src/modules/invites/index.ts
+++ b/apps/api/src/modules/invites/index.ts
@@ -3,7 +3,7 @@ import { mcpTool } from '#mcp/generate';
import { noContent } from '#shared/http';
import { authContext } from '#shared/auth-context';
import { guards } from '#shared/guards';
-import { requireUser, type AuthUser } from '#shared/access';
+import { checkMemberAdmin, requireUser, type AuthUser } from '#shared/access';
import { inviteValidSince } from '@repo/auth';
import { hasConfiguredEmailProvider } from '@repo/db';
import { HttpError } from '#shared/lib';
@@ -219,13 +219,21 @@ export const inviteRoutes = new Elysia({ name: 'invites', detail: { tags: ['Invi
.get(
'/projects/:projectKey/invites',
- async ({ project }) => {
- return listProjectInvites(project.id);
+ async ({ project, user }) => {
+ const viewer = requireUser(user);
+ const mayInvite = await checkMemberAdmin(project, viewer, 'members_invite', 'create');
+ return listProjectInvites(project.id, mayInvite ? viewer.id : null);
},
{
memberAdmin: ['members_invite', 'read'],
response: { 200: InviteRowListResponse, ...accessErrors },
- detail: { summary: "List a project's invites", ...mcpTool('list_invites') },
+ detail: {
+ summary: "List a project's invites",
+ description:
+ 'The token is null on an invite the caller could not have sent: without permission ' +
+ 'to create invites, or on an owner invite the caller may not grant.',
+ ...mcpTool('list_invites'),
+ },
},
)
@@ -294,7 +302,7 @@ export const inviteRoutes = new Elysia({ name: 'invites', detail: { tags: ['Invi
.get(
'/teams/:teamId/invites',
async ({ membership }) => {
- return listTeamInvites(membership.teamId);
+ return listTeamInvites(membership.teamId, membership.userId);
},
{
teamManager: true,
@@ -303,7 +311,8 @@ export const inviteRoutes = new Elysia({ name: 'invites', detail: { tags: ['Invi
detail: {
summary: "List a team's invites",
description:
- 'Every invite into the team, including the ones that also name one of its projects.',
+ 'Every invite into the team, including the ones that also name one of its projects. ' +
+ 'The token is null on an invite granting a rank only a team owner may grant.',
},
},
)
diff --git a/apps/api/src/modules/invites/model.ts b/apps/api/src/modules/invites/model.ts
index 5690ec80..64242701 100644
--- a/apps/api/src/modules/invites/model.ts
+++ b/apps/api/src/modules/invites/model.ts
@@ -25,10 +25,10 @@ export const createTeamInviteBody = t.Object({
role: teamRole,
});
-// The manager-facing invite row (InviteRow from the service).
+// The manager-facing invite row (ListedInvite from the service).
export const InviteRowResponse = t.Object({
id: t.Number(),
- token: t.String(),
+ token: t.Nullable(t.String()),
email: t.String(),
teamRole,
projectKey: t.Nullable(t.String()),
@@ -47,8 +47,8 @@ export const InviteRowResponse = t.Object({
// emailFailed: a mail provider is set but the invite's email could not go out; the link
// still works. Without a provider both are false.
export const InviteCreateResponse = t.Composite([
- InviteRowResponse,
- t.Object({ emailQueued: t.Boolean(), emailFailed: t.Boolean() }),
+ t.Omit(InviteRowResponse, ['token']),
+ t.Object({ token: t.String(), emailQueued: t.Boolean(), emailFailed: t.Boolean() }),
]);
export const InviteEmailResponse = t.Object({ emailQueued: t.Boolean() });
diff --git a/apps/api/src/modules/invites/service.ts b/apps/api/src/modules/invites/service.ts
index 28649fcb..8f62535a 100644
--- a/apps/api/src/modules/invites/service.ts
+++ b/apps/api/src/modules/invites/service.ts
@@ -54,6 +54,10 @@ export interface InviteRow {
invitedByEmail: string | null;
}
+// A row of an invite listing. The token is null where the viewer could not have issued
+// the invite: whoever holds the link can register the invited address and accept it.
+export type ListedInvite = Omit<InviteRow, 'token'> & { token: string | null };
+
// Row shown to the invitee opening the link, with enough context to decide. Never
// exposes the internal project id; the team is named by its ref, which the project's
// own URLs carry.
@@ -107,6 +111,8 @@ function selectInvites() {
.select({
id: teamInvite.id,
token: teamInvite.token,
+ teamId: teamInvite.teamId,
+ projectId: teamInvite.projectId,
email: teamInvite.email,
teamRole: teamInvite.teamRole,
projectKey: project.key,
@@ -288,22 +294,37 @@ export async function getInviteById(id: number): Promise<InviteRow | null> {
return rows[0] ? toRow(rows[0], await inviteTtlDays()) : null;
}
-export async function listProjectInvites(projectId: number): Promise<InviteRow[]> {
+// issuerId is the viewer when they may send invites here, null when they may only read
+// them; the ranks of each invite are then checked against that standing.
+async function toListed(rows: InviteSelection[], issuerId: string | null): Promise<ListedInvite[]> {
+ const ttlDays = await inviteTtlDays();
+ return Promise.all(
+ rows.map(async (r) => {
+ const row = toRow(r, ttlDays);
+ const grant = { ...r, projectRole: r.role };
+ if (issuerId && (await mayGrantInviteRanks(grant, issuerId))) return row;
+ return { ...row, token: null };
+ }),
+ );
+}
+
+export async function listProjectInvites(
+ projectId: number,
+ issuerId: string | null,
+): Promise<ListedInvite[]> {
const rows = await selectInvites()
.where(eq(teamInvite.projectId, projectId))
.orderBy(desc(teamInvite.createdAt));
- const ttlDays = await inviteTtlDays();
- return rows.map((r) => toRow(r, ttlDays));
+ return toListed(rows, issuerId);
}
// Every invite of the team, including the ones that also name one of its projects:
// whoever runs the team sees who was invited anywhere in it.
-export async function listTeamInvites(teamId: number): Promise<InviteRow[]> {
+export async function listTeamInvites(teamId: number, issuerId: string): Promise<ListedInvite[]> {
const rows = await selectInvites()
.where(eq(teamInvite.teamId, teamId))
.orderBy(desc(teamInvite.createdAt));
- const ttlDays = await inviteTtlDays();
- return rows.map((r) => toRow(r, ttlDays));
+ return toListed(rows, issuerId);
}
// Removes an invite of the given project. Returns true when one existed. Used to
diff --git a/apps/api/src/shared/access.ts b/apps/api/src/shared/access.ts
index 39d39b41..f892ef77 100644
--- a/apps/api/src/shared/access.ts
+++ b/apps/api/src/shared/access.ts
@@ -171,6 +171,17 @@ export async function requireMemberAdmin(
return project;
}
+// Whether the user passes requireMemberAdmin, without throwing.
+export async function checkMemberAdmin(
+ project: ProjectRow,
+ user: AuthUser,
+ resource: PermissionResource,
+ action: PermissionAction,
+): Promise<boolean> {
+ if (runsTeam((await getTeamAccess(project.teamId, user.id))?.role ?? null)) return true;
+ return checkPermission(project.id, user, resource, action);
+}
+
// Resolves the :projectKey path param for a route acting on one member's own row.
// A member acts on themselves — leaving the project, saying what they do in it —
// without any member permission; anyone else needs what requireMemberAdmin asks
diff --git a/apps/web/src/features/members/components/invites/InviteRow.tsx b/apps/web/src/features/members/components/invites/InviteRow.tsx
index 5f10ed75..8b8d4e29 100644
--- a/apps/web/src/features/members/components/invites/InviteRow.tsx
+++ b/apps/web/src/features/members/components/invites/InviteRow.tsx
@@ -14,9 +14,10 @@ import { Item, ItemActions, ItemContent, ItemTitle } from '@/components/ui/item'
const STATUS_VARIANT = { pending: 'secondary', accepted: 'default', rejected: 'outline' } as const;
// One invite row: the invited email and role, its status, who sent it, and — for
-// a pending invite — email, copy-link, and revoke actions. Copy reads the link from
-// the current web origin so it works in any deployment. An expired invite offers no
-// link to copy or mail, only a renewal, which sends a new one.
+// a pending invite — email, copy-link (when the API sent its token), and revoke
+// actions. Copy reads the link from the current web origin so it works in any
+// deployment. An expired invite offers no link to copy or mail, only a renewal,
+// which sends a new one.
export default function InviteRow({
invite,
onResend,
@@ -53,6 +54,7 @@ export default function InviteRow({
}
async function copy() {
+ if (!invite.token) return;
const origin = typeof window === 'undefined' ? '' : window.location.origin;
try {
await navigator.clipboard.writeText(inviteLink(origin, invite.token));
@@ -125,7 +127,7 @@ export default function InviteRow({
{resending ? t('resendingEmail') : t('resendEmail')}
</Button>
)}
- {pending && (
+ {pending && invite.token && (
<Button
variant="ghost"
size="sm"
diff --git a/apps/web/src/features/teams/components/members/TeamInviteRow.tsx b/apps/web/src/features/teams/components/members/TeamInviteRow.tsx
index 6b91a248..e40c1ff6 100644
--- a/apps/web/src/features/teams/components/members/TeamInviteRow.tsx
+++ b/apps/web/src/features/teams/components/members/TeamInviteRow.tsx
@@ -35,6 +35,7 @@ export default function TeamInviteRow({
const expired = isInviteExpired(invite);
async function copy() {
+ if (!invite.token) return;
const origin = typeof window === 'undefined' ? '' : window.location.origin;
try {
await navigator.clipboard.writeText(inviteLink(origin, invite.token));
@@ -86,15 +87,17 @@ export default function TeamInviteRow({
<Mail className="size-3.5" />
{resending ? t('resendingEmail') : t('resendEmail')}
</Button>
- <Button
- variant="ghost"
- size="sm"
- className="h-7 gap-1.5 text-xs text-muted-foreground hover:text-foreground"
- onClick={copy}
- >
- {copied ? <Check className="size-3.5" /> : <Copy className="size-3.5" />}
- {copied ? t('copied') : t('copyLink')}
- </Button>
+ {invite.token && (
+ <Button
+ variant="ghost"
+ size="sm"
+ className="h-7 gap-1.5 text-xs text-muted-foreground hover:text-foreground"
+ onClick={copy}
+ >
+ {copied ? <Check className="size-3.5" /> : <Copy className="size-3.5" />}
+ {copied ? t('copied') : t('copyLink')}
+ </Button>
+ )}
</>
)}
<Button
diff --git a/apps/web/src/lib/api/endpoints/invites.ts b/apps/web/src/lib/api/endpoints/invites.ts
index 537a03e4..d0533983 100644
--- a/apps/web/src/lib/api/endpoints/invites.ts
+++ b/apps/web/src/lib/api/endpoints/invites.ts
@@ -8,10 +8,11 @@ export type InviteStatus = 'pending' | 'accepted' | 'rejected';
export type InviteTeamRole = 'owner' | 'manager' | 'member';
// An invite as shown to whoever manages a team's or a project's invites: carries the
-// token so they can share the link, and who sent it.
+// token so they can share the link, and who sent it. The token is null where the
+// viewer could not have sent the invite.
export interface InviteRow {
id: number;
- token: string;
+ token: string | null;
email: string;
teamRole: InviteTeamRole;
// The project the invitee joins along with the team, or null for an invite into
@@ -35,6 +36,7 @@ export interface InviteRow {
}
export interface InviteCreateResult extends InviteRow {
+ token: string;
emailQueued: boolean;
// A mail provider is set but the email could not go out; the link still works.
emailFailed: boolean;