@@ -54,12 +54,12 @@ export class VaultAccessGuard {
5454 * Checks if the current user has access to a meta envelope based on its ACL
5555 * @param metaEnvelopeId - The ID of the meta envelope to check access for
5656 * @param context - The GraphQL context containing the current user
57- * @returns Promise<boolean> - Whether the user has access
57+ * @returns Promise<{hasAccess: boolean, exists: boolean} > - Whether the user has access and if envelope exists
5858 */
5959 private async checkAccess (
6060 metaEnvelopeId : string ,
6161 context : VaultContext
62- ) : Promise < boolean > {
62+ ) : Promise < { hasAccess : boolean ; exists : boolean } > {
6363 // Validate token if present
6464 const authHeader =
6565 context . request ?. headers ?. get ( "authorization" ) ??
@@ -69,36 +69,40 @@ export class VaultAccessGuard {
6969 if ( tokenPayload ) {
7070 // Token is valid, set platform context and allow access
7171 context . tokenPayload = tokenPayload ;
72- return true ;
72+ // Still need to check if envelope exists
73+ if ( ! context . eName ) {
74+ return { hasAccess : true , exists : false } ;
75+ }
76+ const metaEnvelope = await this . db . findMetaEnvelopeById ( metaEnvelopeId , context . eName ) ;
77+ return { hasAccess : true , exists : metaEnvelope !== null } ;
7378 }
7479
7580 // Validate eName is present
7681 if ( ! context . eName ) {
7782 throw new Error ( "X-ENAME header is required for access control" ) ;
7883 }
7984
80- // Fallback to original ACL logic if no valid token
81- if ( ! context . currentUser ) {
82- const metaEnvelope = await this . db . findMetaEnvelopeById (
83- metaEnvelopeId ,
84- context . eName
85- ) ;
86- if ( metaEnvelope && metaEnvelope . acl . includes ( "*" ) ) return true ;
87- return false ;
88- }
89-
9085 const metaEnvelope = await this . db . findMetaEnvelopeById ( metaEnvelopeId , context . eName ) ;
9186 if ( ! metaEnvelope ) {
92- return false ;
87+ return { hasAccess : false , exists : false } ;
88+ }
89+
90+ // Fallback to original ACL logic if no valid token
91+ if ( ! context . currentUser ) {
92+ if ( metaEnvelope . acl . includes ( "*" ) ) {
93+ return { hasAccess : true , exists : true } ;
94+ }
95+ return { hasAccess : false , exists : true } ;
9396 }
9497
9598 // If ACL contains "*", anyone can access
9699 if ( metaEnvelope . acl . includes ( "*" ) ) {
97- return true ;
100+ return { hasAccess : true , exists : true } ;
98101 }
99102
100103 // Check if the current user's ID is in the ACL
101- return metaEnvelope . acl . includes ( context . currentUser ) ;
104+ const hasAccess = metaEnvelope . acl . includes ( context . currentUser ) ;
105+ return { hasAccess, exists : true } ;
102106 }
103107
104108 /**
@@ -169,13 +173,25 @@ export class VaultAccessGuard {
169173 return this . filterACL ( result ) ;
170174 }
171175
172- const hasAccess = await this . checkAccess ( metaEnvelopeId , context ) ;
176+ // Check if envelope exists and user has access
177+ const { hasAccess, exists } = await this . checkAccess ( metaEnvelopeId , context ) ;
173178 if ( ! hasAccess ) {
179+ // If envelope doesn't exist, return null (not found)
180+ if ( ! exists ) {
181+ return null ;
182+ }
183+ // Envelope exists but access denied
174184 throw new Error ( "Access denied" ) ;
175185 }
176186
177- // console.log
187+ // Execute resolver and filter ACL
178188 const result = await resolver ( parent , args , context ) ;
189+
190+ // If result is null (envelope not found), return null
191+ if ( result === null ) {
192+ return null ;
193+ }
194+
179195 return this . filterACL ( result ) ;
180196 } ;
181197 }
0 commit comments