Skip to content

Commit c8f44f8

Browse files
committed
restrict non-idir user search
1 parent a2bb0f6 commit c8f44f8

File tree

4 files changed

+39
-7
lines changed

4 files changed

+39
-7
lines changed

app/src/components/utils.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ const utils = {
7272
return key || '/'; // set empty key to '/' to match convention in COMS db
7373
},
7474

75+
hasOnlyPermittedKeys(obj, permittedKeys) {
76+
const objKeys = Object.keys(obj);
77+
return objKeys.every(key => permittedKeys.includes(key));
78+
},
79+
7580
/**
7681
* @function getBucket
7782
* Acquire core S3 bucket credential information from database or configuration
@@ -161,6 +166,8 @@ const utils = {
161166
* @function getCurrentIdentity
162167
* Attempts to acquire current identity value.
163168
* Always takes first non-default value available. Yields `defaultValue` otherwise.
169+
* looks for one of the specified claims (eg idir_user_guid,bceid_user_guid or github_id)
170+
* if not found, returns 'sub' claim
164171
* @param {object} currentUser The express request currentUser object
165172
* @param {string} [defaultValue=undefined] An optional default return value
166173
* @returns {string} The current user identifier if applicable, or `defaultValue`

app/src/middleware/authorization.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ const {
66
getAppAuthMode,
77
getCurrentIdentity,
88
getConfigBoolean,
9-
mixedQueryToArray, stripDelimit } = require('../components/utils');
9+
hasOnlyPermittedKeys,
10+
mixedQueryToArray,
11+
stripDelimit
12+
} = require('../components/utils');
1013
const { NIL: SYSTEM_USER } = require('uuid');
1114
const {
1215
bucketPermissionService,
@@ -207,6 +210,29 @@ const hasPermission = (permission) => {
207210
};
208211
};
209212

213+
/**
214+
* if non-IDIR user, require userId, email or identityId query parameter
215+
* route validationrequires a valid email
216+
*/
217+
const restrictNonIdirUserSearch = async (req, _res, next) => {
218+
try {
219+
if (req.currentUser.authType === AuthType.BEARER &&
220+
req.currentUser.tokenPayload.identity_provider !== 'idir' &&
221+
!hasOnlyPermittedKeys(req.query, ['email', 'userId', 'identityId'])
222+
) {
223+
throw new Error('User lacks permission to complete this actionnn');
224+
}
225+
}
226+
catch (err) {
227+
log.verbose(err.message, { function: 'restrictNonIdirUserSearch' });
228+
return next(new Problem(403, {
229+
detail: err.message,
230+
instance: req.originalUrl
231+
}));
232+
}
233+
next();
234+
};
235+
210236
module.exports = {
211-
_checkPermission, checkAppMode, checkS3BasicAccess, currentObject, hasPermission
237+
_checkPermission, checkAppMode, checkS3BasicAccess, currentObject, hasPermission, restrictNonIdirUserSearch
212238
};

app/src/routes/v1/user.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const router = require('express').Router();
22

33
const { userValidator } = require('../../validators');
44
const { userController } = require('../../controllers');
5-
const { checkAppMode, checkS3BasicAccess } = require('../../middleware/authorization');
5+
const { checkAppMode, checkS3BasicAccess, restrictNonIdirUserSearch } = require('../../middleware/authorization');
66
const { requireSomeAuth } = require('../../middleware/featureToggle');
77

88
router.use(checkAppMode);
@@ -15,7 +15,9 @@ router.get('/',
1515
* checkS3BasicAccess will add a bucketId query param which triggers a 422 from the userValidator
1616
*/
1717
checkS3BasicAccess,
18-
userValidator.searchUsers, (req, res, next) => {
18+
userValidator.searchUsers,
19+
restrictNonIdirUserSearch,
20+
(req, res, next) => {
1921
userController.searchUsers(req, res, next);
2022
});
2123

app/src/services/user.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,6 @@ const service = {
153153
* @returns {Promise<object>} The result of running the login operation
154154
*/
155155
login: async (token) => {
156-
157-
console.log('a', token);
158-
159156
const newUser = service._tokenToUser(token);
160157
// wrap with db transaction
161158
return await utils.trxWrapper(async (trx) => {

0 commit comments

Comments
 (0)