@@ -35,11 +35,13 @@ const _checkPermission = async ({ currentObject, currentUser, params }, permissi
35
35
const searchParams = { permCode : permission , userId : userId } ;
36
36
37
37
if ( params . objectId ) {
38
+ // add object permissions
38
39
permissions . push ( ...await objectPermissionService . searchPermissions ( {
39
40
objId : params . objectId , ...searchParams
40
41
} ) ) ;
41
42
}
42
43
if ( params . bucketId || currentObject . bucketId ) {
44
+ // add bucket permissions
43
45
permissions . push ( ...await bucketPermissionService . searchPermissions ( {
44
46
bucketId : params . bucketId || currentObject . bucketId , ...searchParams
45
47
} ) ) ;
@@ -52,6 +54,7 @@ const _checkPermission = async ({ currentObject, currentUser, params }, permissi
52
54
log . debug ( 'Missing user identification' , { function : '_checkPermission' } ) ;
53
55
return result ;
54
56
} ;
57
+
55
58
/**
56
59
* @function checkS3BasicAccess
57
60
* Checks and authorized access to perform operation for s3 basic authentication request
@@ -138,6 +141,31 @@ const checkAppMode = (req, _res, next) => {
138
141
next ( ) ;
139
142
} ;
140
143
144
+ /**
145
+ * @function currentBucket
146
+ * Injects a currentBucket object to the request if there is an applicable bucket record
147
+ * @param {object } req Express request object
148
+ * @param {object } _res Express response object
149
+ * @param {function } next The next callback function
150
+ * @returns {function } Express middleware function
151
+ */
152
+ // const currentBucket = async (req, _res, next) => {
153
+ // try {
154
+ // if (mixedQueryToArray(req.query.bucketId).length === 1) {
155
+ // const bucket = await bucketService.read(req.query.bucketId);
156
+ // if (bucket) {
157
+ // req.currentBucket = Object.freeze({
158
+ // ...redactSecrets(bucket, ['accessKeyId', 'secretAccessKey'])
159
+ // });
160
+ // }
161
+ // }
162
+ // } catch (err) {
163
+ // log.warn(err.message, { function: 'currentBucket' });
164
+ // }
165
+ // next();
166
+ // };
167
+
168
+
141
169
/**
142
170
* @function currentObject
143
171
* Injects a currentObject object to the request if there is an applicable object record
@@ -190,15 +218,22 @@ const hasPermission = (permission) => {
190
218
throw new Error ( 'Missing object record' ) ;
191
219
} else if ( authType === AuthType . BASIC && canBasicMode ( authMode ) ) {
192
220
log . debug ( 'Basic authTypes are always permitted' , { function : 'hasPermission' } ) ;
193
- } else if ( req . params . objectId && req . currentObject . public && permission === Permissions . READ ) {
221
+ }
222
+ // if reading a public object
223
+ else if ( req . params . objectId && await isObjectPublic ( req . currentObject ) && permission === Permissions . READ ) {
194
224
log . debug ( 'Read requests on public objects are always permitted' , { function : 'hasPermission' } ) ;
195
- } else if ( ! await _checkPermission ( req , permission ) ) {
225
+ }
226
+ // if reading a public bucket
227
+ else if ( req . params . bucketId && await isBucketPublic ( req . params . bucketId ) && permission === Permissions . READ ) {
228
+ log . debug ( 'Read requests on public buckets are always permitted' , { function : 'hasPermission' } ) ;
229
+ }
230
+ else if ( ! await _checkPermission ( req , permission ) ) {
196
231
throw new Error ( `User lacks required permission ${ permission } ` ) ;
197
232
}
198
233
} catch ( err ) {
199
234
log . verbose ( err . message , { function : 'hasPermission' } ) ;
200
235
return next ( new Problem ( 403 , {
201
- detail : 'User lacks permission to complete this action' ,
236
+ detail : 'User lacks permission to complete this action' + err ,
202
237
instance : req . originalUrl
203
238
} ) ) ;
204
239
}
@@ -207,6 +242,59 @@ const hasPermission = (permission) => {
207
242
} ;
208
243
} ;
209
244
245
+ // const isBucketPublic = async (req, _res, next) => {
246
+ // // if an unauthenticated request
247
+ // if (!req.currentUser || req.currentUser.authType === AuthType.NONE) {
248
+ // // if providing a single bucketId in query
249
+ // if (mixedQueryToArray(req.query.bucketId).length === 1) {
250
+ // const bucket = await bucketService.read(req.query.bucketId);
251
+ // // and bucket public is truthy
252
+ // if (!bucket.public) {
253
+ // return next(new Problem(403, {
254
+ // detail: 'Bucket is not public',
255
+ // instance: req.originalUrl
256
+ // }));
257
+ // }
258
+ // }
259
+ // }
260
+ // else {
261
+ // return next(new Problem(403, {
262
+ // detail: 'User lacks permission to complete this action',
263
+ // instance: req.originalUrl
264
+ // }));
265
+ // }
266
+ // next();
267
+ // };
268
+
269
+
270
+ /**
271
+ * get public status from COMS database
272
+ * checks current object and all parent folders
273
+ */
274
+ const isObjectPublic = async ( currentObject ) => {
275
+ if ( currentObject . public ) return true ;
276
+ if ( await isBucketPublic ( currentObject . bucketId ) ) return true ;
277
+ return false ;
278
+ } ;
279
+
280
+ /**
281
+ * get public status from COMS database
282
+ * checks current folder and all parent folders
283
+ */
284
+ const isBucketPublic = async ( bucketId ) => {
285
+ const bucket = await bucketService . read ( bucketId ) ;
286
+ if ( bucket . public ) return true ;
287
+ const parentBuckets = await bucketService . searchParentBuckets ( bucket ) ;
288
+ if ( parentBuckets . some ( b => b . public ) ) return true ;
289
+ return false ;
290
+ } ;
291
+
210
292
module . exports = {
211
- _checkPermission, checkAppMode, checkS3BasicAccess, currentObject, hasPermission
293
+ _checkPermission,
294
+ checkAppMode,
295
+ isBucketPublic,
296
+ isObjectPublic,
297
+ checkS3BasicAccess,
298
+ currentObject,
299
+ hasPermission
212
300
} ;
0 commit comments