Skip to content

Commit c8a9d41

Browse files
authored
Merge pull request #312 from bcgov/feature/get-public-objects-no-auth
GET /object: allow no-auth access
2 parents f7c0f1d + 2aedd38 commit c8a9d41

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

app/src/controllers/object.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const cors = require('cors');
33
const { v4: uuidv4, NIL: SYSTEM_USER } = require('uuid');
44

55
const {
6+
AuthType,
67
DEFAULTCORS,
78
DownloadMode,
89
MAXCOPYOBJECTLENGTH,
@@ -26,7 +27,8 @@ const {
2627
mixedQueryToArray,
2728
toLowerKeys,
2829
getBucket,
29-
renameObjectProperty
30+
renameObjectProperty,
31+
hasOnlyPermittedKeys
3032
} = require('../components/utils');
3133
const utils = require('../db/models/utils');
3234

@@ -1057,10 +1059,34 @@ const controller = {
10571059
};
10581060
// if scoping to current user permissions on objects
10591061
if (getConfigBoolean('server.privacyMask')) {
1062+
1063+
if (req.currentUser.authType === AuthType.NONE) {
1064+
1065+
const permittedPublicSearchParams = ['bucketId', 'objectId', 'public', 'page', 'limit', 'sort'];
1066+
1067+
// no-auth requests MUST have all of the following:
1068+
// (a) only the permitted search params; (b) ?public=true; (c) an object or bucket id
1069+
if (!hasOnlyPermittedKeys(req.query, permittedPublicSearchParams) || !params.public ||
1070+
!(params.bucketId || params.id)) {
1071+
throw new Problem(403, {
1072+
detail: 'User lacks permission to complete this action',
1073+
instance: req.originalUrl
1074+
});
1075+
}
1076+
}
10601077
params.userId = await userService.getCurrentUserId(getCurrentIdentity(req.currentUser, SYSTEM_USER));
10611078
}
1079+
10621080
const response = await objectService.searchObjects(params);
1063-
res.setHeader('X-Total-Rows', response.total).status(200).json(response.data);
1081+
1082+
if (req.currentUser.authType === AuthType.NONE) {
1083+
const redactedFields = ['path', 'createdBy', 'updatedBy', 'lastSyncedDate'];
1084+
const redactedResponseData = response.data.map(object => utils.redactSecrets(object, redactedFields));
1085+
res.setHeader('X-Total-Rows', response.total).status(200).json(redactedResponseData);
1086+
}
1087+
else {
1088+
res.setHeader('X-Total-Rows', response.total).status(200).json(response.data);
1089+
}
10641090
} catch (error) {
10651091
next(error);
10661092
}

app/src/docs/v1.api-spec.yaml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,10 +411,16 @@ paths:
411411
$ref: "#/components/responses/Error"
412412
get:
413413
summary: Search for objects
414-
description: >-
414+
description: |
415415
Returns a list of objects matching all search criteria across all known
416416
versions of objects. Search criteria on string attributes will match on
417417
partial results and ignore case sensitivity.
418+
419+
This endpoint can be used without authentication. If so, some response attributes (`path`, `createdBy`, `updatedBy`, and
420+
`lastSynceDate`) are redacted. The following restrictions also apply:
421+
* Only the following query parameters are allowed: `bucketId`, `objectId`, `public`, `page`, `limit`, `sort`
422+
* `public` must be `true`
423+
* An `objectId` or `publicId` must be provided
418424
operationId: searchObjects
419425
tags:
420426
- Object

app/src/routes/v1/object.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ router.put('/',
2828

2929
/** Search for objects */
3030
router.get('/',
31-
requireSomeAuth,
3231
objectValidator.searchObjects,
3332
checkS3BasicAccess,
3433
(req, res, next) => {

0 commit comments

Comments
 (0)