Просмотр исходного кода

fix(page-grant): guard against TypeError when grantedUsers is empty in calcApplicableGrantData

When a parent page has grant=GRANT_OWNER but grantedUsers is an empty
array (possible due to data inconsistency from the grant overwrite cycle),
accessing grantedUsers[0] returns undefined and calling .toString() on
it throws a TypeError, resulting in a 500 error when accessing child pages.

Add an explicit null check before calling .toString() so that an empty
grantedUsers array is handled gracefully: GRANT_OWNER simply won't be
included in the applicable grants rather than crashing the request.

Fixes #9067

https://claude.ai/code/session_012rkDirDVCqYdE3Hz1NGtaA
Claude 2 недель назад
Родитель
Сommit
944a0036bd
1 измененных файлов с 6 добавлено и 4 удалено
  1. 6 4
      apps/app/src/server/service/page-grant.ts

+ 6 - 4
apps/app/src/server/service/page-grant.ts

@@ -850,11 +850,13 @@ class PageGrantService implements IPageGrantService {
         applicableGroups: userRelatedGroups,
       };
     } else if (grant === PageGrant.GRANT_OWNER) {
-      const grantedUser = grantedUsers[0];
+      const grantedUser = grantedUsers?.[0];
 
-      const isUserApplicable = grantedUser.toString() === user._id.toString();
-
-      if (isUserApplicable) {
+      // grantedUsers may be empty due to data inconsistency; guard against TypeError
+      if (
+        grantedUser != null &&
+        grantedUser.toString() === user._id.toString()
+      ) {
         data[PageGrant.GRANT_OWNER] = null;
       }
     } else if (grant === PageGrant.GRANT_USER_GROUP) {