Skip to content

Commit c6ebd3d

Browse files
kptdobeCopilot
andcommitted
chore(auth): stop logging expected ACL denials in listings
Listing filters (object list, bucket list, hlx6 org site list) checked each child with hasPermission, logging one "does not have permission" line per hidden item. Add canAccess, the same check without logging, and use it for those filters. Request-level and bulk-mutation denials are still logged. Compact the IMS offline-verification fallback log to error code and message, as a warning. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent e492bd8 commit c6ebd3d

5 files changed

Lines changed: 55 additions & 20 deletions

File tree

‎src/routes/hlx6.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import daResp from '../utils/daResp.js';
1313
import {
1414
DEFAULT_AEM_API, HLX6_GONE, hasHlx6Sites, isHlx6Site,
1515
} from '../utils/hlx6.js';
16-
import { hasDescendantPermission, hasPermission } from '../utils/auth.js';
16+
import { canAccess, hasDescendantPermission, hasPermission } from '../utils/auth.js';
1717
import { FORM_TYPES } from '../utils/constants.js';
1818
import { sourceRespObject } from '../helpers/source.js';
1919
import formatList from '../storage/utils/list.js';
@@ -259,7 +259,7 @@ async function fetchHlx6OrgSites(req, env, daCtx) {
259259
.map((item) => item?.name)
260260
.filter((name) => typeof name === 'string' && /^[^/.]+\/$/.test(name))
261261
.map((name) => name.slice(0, -1).toLowerCase())
262-
.filter((site) => hasPermission(daCtx, site, 'read')
262+
.filter((site) => canAccess(daCtx, site, 'read')
263263
|| hasDescendantPermission(daCtx, site, 'read'));
264264
const migrated = await Promise.allSettled(sites.map((site) => isHlx6Site(env, org, site)));
265265
return sites.filter((site, index) => {

‎src/storage/bucket/list.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
* OF ANY KIND, either express or implied. See the License for the specific language
1010
* governing permissions and limitations under the License.
1111
*/
12-
import { hasPermission } from '../../utils/auth.js';
12+
import { canAccess } from '../../utils/auth.js';
1313

1414
async function isBucketAuthed(env, daCtx, bucket) {
1515
const { name, created } = bucket;
1616

17-
if (!hasPermission(daCtx, name, 'read')) {
17+
if (!canAccess(daCtx, name, 'read')) {
1818
return null;
1919
}
2020
return { name, created };

‎src/storage/object/list.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616

1717
import getS3Config from '../utils/config.js';
1818
import formatList from '../utils/list.js';
19-
import { hasDescendantPermission, hasPermission } from '../../utils/auth.js';
19+
import { canAccess, hasDescendantPermission } from '../../utils/auth.js';
2020

2121
function buildInput({
2222
bucket, org, key, maxKeys, continuationToken,
@@ -46,7 +46,7 @@ export function filterUnauthorized(daCtx, items) {
4646
const orgPrefix = `/${daCtx.org}`;
4747
return items.filter((item) => {
4848
const relPath = item.path.slice(orgPrefix.length) || '/';
49-
return hasPermission(daCtx, relPath, 'read')
49+
return canAccess(daCtx, relPath, 'read')
5050
|| (!item.ext && hasDescendantPermission(daCtx, relPath, 'read'));
5151
});
5252
}

‎src/utils/auth.js‎

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export async function getUsers(req, env) {
138138
}
139139
} catch (e) {
140140
// eslint-disable-next-line no-console
141-
console.log('IMS token offline verification failed', e);
141+
console.warn('IMS token offline verification failed:', e.code || e.name, e.message);
142142
return { email: 'anonymous' };
143143
}
144144

@@ -527,10 +527,10 @@ export function hasDescendantPermission(daCtx, path, action = 'read') {
527527
})));
528528
}
529529

530-
export function hasPermission(daCtx, path, action, keywordPath = false) {
531-
if (path === null || path === undefined) return false;
530+
function checkPermission(daCtx, path, action, keywordPath) {
531+
if (path === null || path === undefined) return { allowed: false };
532532
if (daCtx.aclCtx.pathLookup.size === 0) {
533-
return true;
533+
return { allowed: true };
534534
}
535535

536536
const isKeyword = keywordPath || path === 'CONFIG';
@@ -539,21 +539,31 @@ export function hasPermission(daCtx, path, action, keywordPath = false) {
539539

540540
// is it the path from the context? then return the cached value
541541
if (k === p) {
542-
const perm = daCtx.aclCtx.actionSet.has(action);
543-
if (!perm) {
544-
// eslint-disable-next-line no-console
545-
console.log(`User ${daCtx.users.map((u) => u.email)} doesn't have permission to ${action} ${path}`);
546-
}
547-
return perm;
542+
return { allowed: daCtx.aclCtx.actionSet.has(action), contextPath: true };
548543
}
549544

550545
// The path is a sub-path which can happen during bulk operations
551-
552-
const permission = daCtx.users
546+
const allowed = daCtx.users
553547
.every((u) => getUserActions(daCtx.aclCtx.pathLookup, u, p).actions.has(action));
554-
if (!permission && !isKeyword) {
548+
return { allowed, isKeyword };
549+
}
550+
551+
/**
552+
* Same check as hasPermission, without logging denials. Use it to filter items the
553+
* user is not allowed to see (e.g. listings), where a denial is the expected outcome.
554+
*/
555+
export function canAccess(daCtx, path, action, keywordPath = false) {
556+
return checkPermission(daCtx, path, action, keywordPath).allowed;
557+
}
558+
559+
export function hasPermission(daCtx, path, action, keywordPath = false) {
560+
const { allowed, contextPath, isKeyword } = checkPermission(daCtx, path, action, keywordPath);
561+
if (!allowed && contextPath) {
562+
// eslint-disable-next-line no-console
563+
console.log(`User ${daCtx.users.map((u) => u.email)} doesn't have permission to ${action} ${path}`);
564+
} else if (!allowed && isKeyword === false) {
555565
// eslint-disable-next-line no-console
556566
console.warn(`User ${daCtx.users.map((u) => u.email)} does not have permission to ${action} ${path}`);
557567
}
558-
return permission;
568+
return allowed;
559569
}

‎test/utils/auth.test.js‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@
1212
/* eslint-disable no-shadow */
1313
import assert from 'node:assert';
1414
import esmock from 'esmock';
15+
import sinon from 'sinon';
1516

1617
// Mocks
1718
import reqs from './mocks/req.js';
1819
import env from './mocks/env.js';
1920
import jose from './mocks/jose.js';
2021
import fetch from './mocks/fetch.js';
2122
import {
23+
canAccess,
2224
configPermissionPath,
2325
getAclCtx,
2426
getChildRules,
@@ -309,6 +311,29 @@ describe('DA auth', () => {
309311
}, '/test', 'write'));
310312
});
311313

314+
it('canAccess checks like hasPermission without logging denials', async () => {
315+
const users = [{ email: 'anonymous' }];
316+
const aclCtx = await getAclCtx(env2, 'test', users, '/test');
317+
const daCtx = {
318+
users, org: 'test', aclCtx, key: '',
319+
};
320+
const warn = sinon.stub(console, 'warn');
321+
const log = sinon.stub(console, 'log');
322+
try {
323+
assert(!canAccess(daCtx, '/test', 'read'));
324+
assert(!canAccess({ ...daCtx, key: '/test' }, '/test', 'read'));
325+
assert.strictEqual(warn.callCount + log.callCount, 0);
326+
327+
assert(!hasPermission(daCtx, '/test', 'read'));
328+
assert(warn.calledOnceWith('User anonymous does not have permission to read /test'));
329+
assert(!hasPermission({ ...daCtx, key: '/test' }, '/test', 'read'));
330+
assert(log.calledOnceWith('User anonymous doesn\'t have permission to read /test'));
331+
} finally {
332+
warn.restore();
333+
log.restore();
334+
}
335+
});
336+
312337
it('test hasPermissions', async () => {
313338
const key = '';
314339
const users = [{ orgs: [{ orgIdent: '2345B0EA551D747', groups: [{ groupName: '4711' }] }] }];

0 commit comments

Comments
 (0)