-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(aliases.spec.ts): add tests for handling aliases in GraphQL quer…
…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
1 parent
cbcc3b9
commit 887a9a9
Showing
2 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters