Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

🔗 fix: Preserve resource owner access when sharing (key share diff by stable id) (#14317)

JOJO8751cc1c5c83
diff --git a/client/src/components/Sharing/GenericGrantAccessDialog.tsx b/client/src/components/Sharing/GenericGrantAccessDialog.tsx
index e28d58a7ba..1557897a5e 100644
--- a/client/src/components/Sharing/GenericGrantAccessDialog.tsx
+++ b/client/src/components/Sharing/GenericGrantAccessDialog.tsx
@@ -26,6 +26,7 @@ import UnifiedPeopleSearch from './PeoplePicker/UnifiedPeopleSearch';
import PeoplePickerAdminSettings from './PeoplePickerAdminSettings';
import PublicSharingToggle from './PublicSharingToggle';
import { SelectedPrincipalsList } from './PeoplePicker';
+import { computeShareChanges } from './shareChanges';
import { cn } from '~/utils';
export default function GenericGrantAccessDialog({
@@ -162,26 +163,10 @@ export default function GenericGrantAccessDialog({
}
try {
- // Calculate changes for unified list
- const originalSharesMap = new Map(
- currentShares.map((share) => [`${share.type}-${share.idOnTheSource}`, share]),
- );
- const allSharesMap = new Map(
- allShares.map((share) => [`${share.type}-${share.idOnTheSource}`, share]),
- );
-
- // Find newly added and updated shares
- const updated = allShares.filter((share) => {
- const key = `${share.type}-${share.idOnTheSource}`;
- const original = originalSharesMap.get(key);
- return !original || original.accessRoleId !== share.accessRoleId;
- });
-
- // Find removed shares
- const removed = currentShares.filter((share) => {
- const key = `${share.type}-${share.idOnTheSource}`;
- return !allSharesMap.has(key);
- });
+ // Diff persisted shares against the working list. Keyed by stable `id`
+ // (falling back to idOnTheSource) so the same principal is never simultaneously
+ // granted and revoked — see computeShareChanges.
+ const { updated, removed } = computeShareChanges(currentShares, allShares);
const publicChanged = isPublic !== currentIsPublic;
const publicRoleChanged = isPublic && publicRole !== currentPublicRole;
diff --git a/client/src/components/Sharing/__tests__/shareChanges.spec.ts b/client/src/components/Sharing/__tests__/shareChanges.spec.ts
new file mode 100644
index 0000000000..c287688d9e
--- /dev/null
+++ b/client/src/components/Sharing/__tests__/shareChanges.spec.ts
@@ -0,0 +1,67 @@
+import { AccessRoleIds, PrincipalType } from 'librechat-data-provider';
+import type { TPrincipal } from 'librechat-data-provider';
+import { computeShareChanges, principalKey } from '../shareChanges';
+
+const principal = (over: Partial<TPrincipal>): TPrincipal => ({
+ type: PrincipalType.USER,
+ id: 'id',
+ name: 'name',
+ accessRoleId: AccessRoleIds.AGENT_VIEWER,
+ ...over,
+});
+
+describe('computeShareChanges', () => {
+ it('does not revoke a principal when the same id appears with a different idOnTheSource', () => {
+ // Loaded ACL returns the owner keyed by the external oid...
+ const currentShares = [
+ principal({
+ id: 'owner',
+ accessRoleId: AccessRoleIds.AGENT_OWNER,
+ idOnTheSource: 'entra-oid-abc',
+ }),
+ ];
+ // ...while the working list (people-picker) carries the local `_id`.
+ const allShares = [
+ principal({ id: 'owner', accessRoleId: AccessRoleIds.AGENT_OWNER, idOnTheSource: 'owner' }),
+ principal({
+ id: 'viewer',
+ accessRoleId: AccessRoleIds.AGENT_VIEWER,
+ idOnTheSource: 'viewer',
+ }),
+ ];
+
+ const { updated, removed } = computeShareChanges(currentShares, allShares);
+
+ expect(removed.some((p) => p.id === 'owner')).toBe(false);
+ expect(updated.some((p) => p.id === 'viewer')).toBe(true);
+ });
+
+ it('still revokes a principal that is genuinely gone from the working list', () => {
+ const currentShares = [
+ principal({ id: 'owner', accessRoleId: AccessRoleIds.AGENT_OWNER, idOnTheSource: 'owner' }),
+ principal({ id: 'gone', accessRoleId: AccessRoleIds.AGENT_VIEWER, idOnTheSource: 'gone' }),
+ ];
+ const allShares = [
+ principal({ id: 'owner', accessRoleId: AccessRoleIds.AGENT_OWNER, idOnTheSource: 'owner' }),
+ ];
+
+ const { removed } = computeShareChanges(currentShares, allShares);
+ expect(removed.map((p) => p.id)).toEqual(['gone']);
+ });
+
+ it('falls back to idOnTheSource for principals without a local id (unsynced Entra)', () => {
+ const currentShares = [
+ principal({
+ type: PrincipalType.GROUP,
+ id: undefined,
+ idOnTheSource: 'group-oid',
+ accessRoleId: AccessRoleIds.AGENT_VIEWER,
+ }),
+ ];
+ const allShares: TPrincipal[] = [];
+
+ const { removed } = computeShareChanges(currentShares, allShares);
+ expect(removed.some((p) => p.idOnTheSource === 'group-oid')).toBe(true);
+ expect(principalKey(currentShares[0])).toBe(`${PrincipalType.GROUP}-group-oid`);
+ });
+});
diff --git a/client/src/components/Sharing/shareChanges.ts b/client/src/components/Sharing/shareChanges.ts
new file mode 100644
index 0000000000..3b29beb8fc
--- /dev/null
+++ b/client/src/components/Sharing/shareChanges.ts
@@ -0,0 +1,42 @@
+import type { TPrincipal } from 'librechat-data-provider';
+
+/**
+ * Key a principal by its stable local `id` when present, falling back to
+ * `idOnTheSource` for principals not yet synced locally (e.g. Entra groups/users
+ * that only carry an external oid).
+ *
+ * Keying by `idOnTheSource` alone is unsafe: the same user can be represented with a
+ * different `idOnTheSource` across sources. `getResourcePermissions` returns
+ * `userInfo.idOnTheSource || _id` (the external oid for OpenID/Entra users), while the
+ * people-picker returns the local `_id`. That mismatch places the same principal in
+ * both the `updated` and `removed` sets — and because the server applies grants before
+ * revocations, the resource owner's own grant is silently revoked when they share.
+ */
+export const principalKey = (share: TPrincipal): string =>
+ `${share.type}-${share.id ?? share.idOnTheSource}`;
+
+/**
+ * Diff the currently-persisted shares (`currentShares`) against the working list
+ * (`allShares`) to derive which principals to grant/update and which to revoke.
+ */
+export function computeShareChanges(
+ currentShares: TPrincipal[],
+ allShares: TPrincipal[],
+): { updated: TPrincipal[]; removed: TPrincipal[] } {
+ const originalSharesMap = new Map(currentShares.map((share) => [principalKey(share), share]));
+ const allSharesMap = new Map(allShares.map((share) => [principalKey(share), share]));
+
+ // Diff over the de-duplicated map values so a principal that appears more than once
+ // in the input (possible while the add/dedupe path still keys on idOnTheSource) is
+ // never emitted multiple times.
+ const updated = [...allSharesMap.values()].filter((share) => {
+ const original = originalSharesMap.get(principalKey(share));
+ return !original || original.accessRoleId !== share.accessRoleId;
+ });
+
+ const removed = [...originalSharesMap.values()].filter(
+ (share) => !allSharesMap.has(principalKey(share)),
+ );
+
+ return { updated, removed };
+}