Skip to content

Commit 91a23e5

Browse files
committed
fix: do lose permissions if read and write on config
1 parent 7847f77 commit 91a23e5

File tree

2 files changed

+96
-10
lines changed

2 files changed

+96
-10
lines changed

src/utils/auth.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -268,17 +268,24 @@ export async function getAclCtx(env, org, users, key, api) {
268268

269269
groups.split(',').map((entry) => entry.trim()).filter((entry) => entry.length > 0).forEach((group) => {
270270
if (!pathLookup.has(group)) pathLookup.set(group, []);
271-
pathLookup
272-
.get(group)
273-
.push({
271+
const groupEntries = pathLookup.get(group);
272+
const effectiveActions = actions
273+
.split(',')
274+
.map((entry) => entry.trim())
275+
.filter((entry) => entry.length > 0)
276+
.flatMap((entry) => (entry === 'write' ? ['read', 'write'] : [entry]));
277+
278+
const existingEntry = groupEntries.find((e) => e.path === effectivePath);
279+
if (existingEntry) {
280+
const merged = new Set([...existingEntry.actions, ...effectiveActions]);
281+
existingEntry.actions = [...merged];
282+
} else {
283+
groupEntries.push({
274284
group,
275285
path: effectivePath,
276-
actions: actions
277-
.split(',')
278-
.map((entry) => entry.trim())
279-
.filter((entry) => entry.length > 0)
280-
.flatMap((entry) => (entry === 'write' ? ['read', 'write'] : [entry])),
286+
actions: effectiveActions,
281287
});
288+
}
282289
});
283290
});
284291
pathLookup.forEach((value) => value.sort(pathSorter));
@@ -390,7 +397,8 @@ export function hasPermission(daCtx, path, action, keywordPath = false) {
390397
return true;
391398
}
392399

393-
const p = !path.startsWith('/') && !keywordPath ? `/${path}` : path;
400+
const isKeyword = keywordPath || path === 'CONFIG';
401+
const p = !path.startsWith('/') && !isKeyword ? `/${path}` : path;
394402
const k = daCtx.key.startsWith('/') ? daCtx.key : `/${daCtx.key}`;
395403

396404
// is it the path from the context? then return the cached value
@@ -407,7 +415,7 @@ export function hasPermission(daCtx, path, action, keywordPath = false) {
407415

408416
const permission = daCtx.users
409417
.every((u) => getUserActions(daCtx.aclCtx.pathLookup, u, p).actions.has(action));
410-
if (!permission && !keywordPath) {
418+
if (!permission && !isKeyword) {
411419
// eslint-disable-next-line no-console
412420
console.warn(`User ${daCtx.users.map((u) => u.email)} does not have permission to ${action} ${path}`);
413421
}

test/utils/auth.test.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,46 @@ describe('DA auth', () => {
452452
groups: '2345B0EA551D747/4711',
453453
actions: 'write',
454454
},
455+
{
456+
path: 'CONFIG',
457+
groups: 'read-first@bloggs.org',
458+
actions: 'read',
459+
},
460+
{
461+
path: 'CONFIG',
462+
groups: 'read-first@bloggs.org',
463+
actions: 'write',
464+
},
465+
{
466+
path: '/test',
467+
groups: 'read-first@bloggs.org',
468+
actions: 'read',
469+
},
470+
{
471+
path: '/test',
472+
groups: 'read-first@bloggs.org',
473+
actions: 'write',
474+
},
475+
{
476+
path: 'CONFIG',
477+
groups: 'write-first@bloggs.org',
478+
actions: 'write',
479+
},
480+
{
481+
path: 'CONFIG',
482+
groups: 'write-first@bloggs.org',
483+
actions: 'read',
484+
},
485+
{
486+
path: '/test',
487+
groups: 'write-first@bloggs.org',
488+
actions: 'write',
489+
},
490+
{
491+
path: '/test',
492+
groups: 'write-first@bloggs.org',
493+
actions: 'read',
494+
},
455495
],
456496
':type': 'sheet',
457497
':sheetname': 'permissions',
@@ -512,6 +552,44 @@ describe('DA auth', () => {
512552
users, org: 'test', aclCtx, key,
513553
}, '/furb', 'write'));
514554
});
555+
556+
it('test hasPermissions if read and write user', async () => {
557+
const key = '';
558+
const users = [{ email: 'read-first@bloggs.org' }];
559+
const aclCtx = await getAclCtx(env, 'test', users, key);
560+
561+
assert(hasPermission({
562+
users, org: 'test', aclCtx, key,
563+
}, 'CONFIG', 'read'));
564+
assert(hasPermission({
565+
users, org: 'test', aclCtx, key,
566+
}, 'CONFIG', 'write'));
567+
assert(hasPermission({
568+
users, org: 'test', aclCtx, key,
569+
}, '/test', 'read'));
570+
assert(hasPermission({
571+
users, org: 'test', aclCtx, key,
572+
}, '/test', 'write'));
573+
});
574+
575+
it('test hasPermissions if write and read user', async () => {
576+
const key = '';
577+
const users = [{ email: 'write-first@bloggs.org' }];
578+
const aclCtx = await getAclCtx(env, 'test', users, key);
579+
580+
assert(hasPermission({
581+
users, org: 'test', aclCtx, key,
582+
}, 'CONFIG', 'read'));
583+
assert(hasPermission({
584+
users, org: 'test', aclCtx, key,
585+
}, 'CONFIG', 'write'));
586+
assert(hasPermission({
587+
users, org: 'test', aclCtx, key,
588+
}, '/test', 'read'));
589+
assert(hasPermission({
590+
users, org: 'test', aclCtx, key,
591+
}, '/test', 'write'));
592+
});
515593
});
516594

517595
it('test getAclCtx missing props', async () => {

0 commit comments

Comments
 (0)