Skip to content

Commit

Permalink
feat(aliases.spec.ts): add tests for handling aliases in GraphQL quer…
Browse files Browse the repository at this point in the history
…ies to ensure

proper query merging and filtering based on allowed queries

fix(index.ts): add logging to output queryMap for debugging purposes
  • Loading branch information
danil-iglu committed Feb 28, 2024
1 parent cbcc3b9 commit 887a9a9
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 0 deletions.
146 changes: 146 additions & 0 deletions src/__tests__/aliases.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
import { getAllowedQueryForRequest } from '../get-allowed-query';
import { mergeQueries } from '../merge';

const allowedQueries = {
'FindMyTalentJobApplications.findJobApplications': `query FindMyTalentJobApplications {
data: findJobApplications {
id
createdAt
deletedAt
jobAd {
id
location
title
publisherCompany {
name
}
workMode
}
}
}`,
'FindMyCompanyTalentJobApplications.findJobApplications': `query FindMyCompanyTalentJobApplications($where: TalentJobApplicationWhereInput, $orderBy: [TalentJobApplicationOrderByWithRelationInput!]) {
data: findJobApplications(where: $where, orderBy: $orderBy) {
createdAt
id
jobAd {
title
}
talentProfile {
profileName
}
}
}`,
};

describe('aliases', () => {
test('FindMyTalentJobApplications should handle aliases (request talentProfile when it is not allowed)', () => {
const requestQuery = `query FindMyTalentJobApplications {
data: findJobApplications {
id
createdAt
deletedAt
jobAd {
id
location
title
publisherCompany {
name
}
workMode
}
talentProfile {
profileName
}
}
}`;

const expected = `query FindMyTalentJobApplications {
data: findJobApplications {
id
createdAt
deletedAt
jobAd {
id
location
title
publisherCompany {
name
}
workMode
}
}
}`;
const allowedQuery = getAllowedQueryForRequest(
requestQuery,
allowedQueries
);
expect(mergeQueries(requestQuery, allowedQuery)).toBe(expected);
});

test('FindMyCompanyTalentJobApplications should handle aliases2 (request workMode when it is not allowed)', () => {
const requestQuery = `query FindMyCompanyTalentJobApplications($where: TalentJobApplicationWhereInput, $orderBy: [TalentJobApplicationOrderByWithRelationInput!]) {
data: findJobApplications(where: $where, orderBy: $orderBy) {
createdAt
id
jobAd {
title
__typename
}
talentProfile {
profileName
__typename
}
workMode
__typename
}
}`;
const expected = `query FindMyCompanyTalentJobApplications($where: TalentJobApplicationWhereInput, $orderBy: [TalentJobApplicationOrderByWithRelationInput!]) {
data: findJobApplications(where: $where, orderBy: $orderBy) {
createdAt
id
jobAd {
title
}
talentProfile {
profileName
}
}
}`;
const allowedQuery = getAllowedQueryForRequest(
requestQuery,
allowedQueries
);
console.log('allowedQuery', allowedQuery);
expect(mergeQueries(requestQuery, allowedQuery)).toBe(expected);
});

test('Exploit with Aliased Fields to bypass restrictions', () => {
const requestQuery = `query FindMyTalentJobApplications {
data: findJobApplications {
id
jobAd {
id
location
secretTitle: title
workMode
}
}
}`;
const expected = `query FindMyTalentJobApplications {
data: findJobApplications {
id
jobAd {
id
location
secretTitle: title
workMode
}
}
}`; // 'secretTitle' alias for 'title' is allowed since 'title' is allowed
const allowedQuery = getAllowedQueryForRequest(
requestQuery,
allowedQueries
);
expect(mergeQueries(requestQuery, allowedQuery)).toBe(expected);
});
});
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export class GraphQLQueryPurifier {
const key = `${operationName}.${firstFieldName}`.trim();
this.queryMap[key] = content;
}
console.log('this.queryMap', this.queryMap);
});
}

Expand Down

0 comments on commit 887a9a9

Please sign in to comment.