Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13185 from strapi/fix/user-hidden-attributes
Sanitize hidden attributes from admin API responses
- Loading branch information
Showing
8 changed files
with
145 additions
and
3 deletions.
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
packages/core/admin/server/services/__tests__/permissions-manager-sanitize.test.js
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,100 @@ | ||
'use strict'; | ||
|
||
const { AbilityBuilder, Ability } = require('@casl/ability'); | ||
const { pick } = require('lodash/fp'); | ||
const sift = require('sift'); | ||
|
||
const createSanitizeHelpers = require('../permission/permissions-manager/sanitize'); | ||
|
||
const allowedOperations = [ | ||
'$or', | ||
'$and', | ||
'$eq', | ||
'$ne', | ||
'$in', | ||
'$nin', | ||
'$lt', | ||
'$lte', | ||
'$gt', | ||
'$gte', | ||
'$exists', | ||
'$elemMatch', | ||
]; | ||
|
||
const operations = pick(allowedOperations, sift); | ||
|
||
const conditionsMatcher = conditions => { | ||
return sift.createQueryTester(conditions, { operations }); | ||
}; | ||
|
||
const defineAbility = register => { | ||
const { can, build } = new AbilityBuilder(Ability); | ||
|
||
register(can); | ||
|
||
return build({ conditionsMatcher }); | ||
}; | ||
|
||
const fooModel = { | ||
uid: 'api::foo.foo', | ||
attributes: { | ||
a: { | ||
type: 'string', | ||
}, | ||
b: { | ||
type: 'password', | ||
}, | ||
c: { | ||
type: 'string', | ||
}, | ||
}, | ||
config: { | ||
attributes: { | ||
a: { | ||
hidden: true, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const sanitizeHelpers = { | ||
sanitizeOutput: null, | ||
sanitizeInput: null, | ||
}; | ||
|
||
describe('Permissions Manager - Sanitize', () => { | ||
beforeAll(() => { | ||
global.strapi = { | ||
getModel() { | ||
return fooModel; | ||
}, | ||
}; | ||
|
||
Object.assign( | ||
sanitizeHelpers, | ||
createSanitizeHelpers({ | ||
action: 'read', | ||
model: fooModel, | ||
ability: defineAbility(can => can('read', 'api::foo.foo')), | ||
}) | ||
); | ||
}); | ||
|
||
describe('Sanitize Output', () => { | ||
it('Removes hidden fields', async () => { | ||
const data = { a: 'Foo', c: 'Bar' }; | ||
const result = await sanitizeHelpers.sanitizeOutput(data, { subject: fooModel.uid }); | ||
|
||
expect(result).toEqual({ c: 'Bar' }); | ||
}); | ||
}); | ||
|
||
describe('Sanitize Input', () => { | ||
it('Removes hidden fields', async () => { | ||
const data = { a: 'Foo', c: 'Bar' }; | ||
const result = await sanitizeHelpers.sanitizeInput(data, { subject: fooModel.uid }); | ||
|
||
expect(result).toEqual({ c: 'Bar' }); | ||
}); | ||
}); | ||
}); |
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
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
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
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
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
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
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