Skip to content

Commit 887a9a9

Browse files
committed
feat(aliases.spec.ts): add tests for handling aliases in GraphQL queries to ensure
proper query merging and filtering based on allowed queries fix(index.ts): add logging to output queryMap for debugging purposes
1 parent cbcc3b9 commit 887a9a9

2 files changed

Lines changed: 147 additions & 0 deletions

File tree

src/__tests__/aliases.spec.ts

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
import { getAllowedQueryForRequest } from '../get-allowed-query';
2+
import { mergeQueries } from '../merge';
3+
4+
const allowedQueries = {
5+
'FindMyTalentJobApplications.findJobApplications': `query FindMyTalentJobApplications {
6+
data: findJobApplications {
7+
id
8+
createdAt
9+
deletedAt
10+
jobAd {
11+
id
12+
location
13+
title
14+
publisherCompany {
15+
name
16+
}
17+
workMode
18+
}
19+
}
20+
}`,
21+
'FindMyCompanyTalentJobApplications.findJobApplications': `query FindMyCompanyTalentJobApplications($where: TalentJobApplicationWhereInput, $orderBy: [TalentJobApplicationOrderByWithRelationInput!]) {
22+
data: findJobApplications(where: $where, orderBy: $orderBy) {
23+
createdAt
24+
id
25+
jobAd {
26+
title
27+
}
28+
talentProfile {
29+
profileName
30+
}
31+
}
32+
}`,
33+
};
34+
35+
describe('aliases', () => {
36+
test('FindMyTalentJobApplications should handle aliases (request talentProfile when it is not allowed)', () => {
37+
const requestQuery = `query FindMyTalentJobApplications {
38+
data: findJobApplications {
39+
id
40+
createdAt
41+
deletedAt
42+
jobAd {
43+
id
44+
location
45+
title
46+
publisherCompany {
47+
name
48+
}
49+
workMode
50+
}
51+
talentProfile {
52+
profileName
53+
}
54+
}
55+
}`;
56+
57+
const expected = `query FindMyTalentJobApplications {
58+
data: findJobApplications {
59+
id
60+
createdAt
61+
deletedAt
62+
jobAd {
63+
id
64+
location
65+
title
66+
publisherCompany {
67+
name
68+
}
69+
workMode
70+
}
71+
}
72+
}`;
73+
const allowedQuery = getAllowedQueryForRequest(
74+
requestQuery,
75+
allowedQueries
76+
);
77+
expect(mergeQueries(requestQuery, allowedQuery)).toBe(expected);
78+
});
79+
80+
test('FindMyCompanyTalentJobApplications should handle aliases2 (request workMode when it is not allowed)', () => {
81+
const requestQuery = `query FindMyCompanyTalentJobApplications($where: TalentJobApplicationWhereInput, $orderBy: [TalentJobApplicationOrderByWithRelationInput!]) {
82+
data: findJobApplications(where: $where, orderBy: $orderBy) {
83+
createdAt
84+
id
85+
jobAd {
86+
title
87+
__typename
88+
}
89+
talentProfile {
90+
profileName
91+
__typename
92+
}
93+
workMode
94+
__typename
95+
}
96+
}`;
97+
const expected = `query FindMyCompanyTalentJobApplications($where: TalentJobApplicationWhereInput, $orderBy: [TalentJobApplicationOrderByWithRelationInput!]) {
98+
data: findJobApplications(where: $where, orderBy: $orderBy) {
99+
createdAt
100+
id
101+
jobAd {
102+
title
103+
}
104+
talentProfile {
105+
profileName
106+
}
107+
}
108+
}`;
109+
const allowedQuery = getAllowedQueryForRequest(
110+
requestQuery,
111+
allowedQueries
112+
);
113+
console.log('allowedQuery', allowedQuery);
114+
expect(mergeQueries(requestQuery, allowedQuery)).toBe(expected);
115+
});
116+
117+
test('Exploit with Aliased Fields to bypass restrictions', () => {
118+
const requestQuery = `query FindMyTalentJobApplications {
119+
data: findJobApplications {
120+
id
121+
jobAd {
122+
id
123+
location
124+
secretTitle: title
125+
workMode
126+
}
127+
}
128+
}`;
129+
const expected = `query FindMyTalentJobApplications {
130+
data: findJobApplications {
131+
id
132+
jobAd {
133+
id
134+
location
135+
secretTitle: title
136+
workMode
137+
}
138+
}
139+
}`; // 'secretTitle' alias for 'title' is allowed since 'title' is allowed
140+
const allowedQuery = getAllowedQueryForRequest(
141+
requestQuery,
142+
allowedQueries
143+
);
144+
expect(mergeQueries(requestQuery, allowedQuery)).toBe(expected);
145+
});
146+
});

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ export class GraphQLQueryPurifier {
107107
const key = `${operationName}.${firstFieldName}`.trim();
108108
this.queryMap[key] = content;
109109
}
110+
console.log('this.queryMap', this.queryMap);
110111
});
111112
}
112113

0 commit comments

Comments
 (0)