Skip to content

Commit bfb3974

Browse files
committed
chore: fix tests e2e tests
1 parent a5495c1 commit bfb3974

File tree

5 files changed

+594
-397
lines changed

5 files changed

+594
-397
lines changed

infrastructure/evault-core/src/core/protocol/graphql-server.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -339,11 +339,19 @@ export class GraphQLServer {
339339
const eName = request.headers.get("x-ename") ?? request.headers.get("X-ENAME") ?? null;
340340

341341
if (token) {
342-
const id = getJWTHeader(token).kid?.split("#")[0];
343-
return {
344-
currentUser: id ?? null,
345-
eName: eName,
346-
};
342+
try {
343+
const id = getJWTHeader(token).kid?.split("#")[0];
344+
return {
345+
currentUser: id ?? null,
346+
eName: eName,
347+
};
348+
} catch (error) {
349+
// Invalid JWT token - ignore and continue without currentUser
350+
return {
351+
currentUser: null,
352+
eName: eName,
353+
};
354+
}
347355
}
348356

349357
return {

infrastructure/evault-core/src/core/protocol/vault-access-guard.ts

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)