Skip to content

Commit

Permalink
fix(#9108): block updating admin only docs
Browse files Browse the repository at this point in the history
  • Loading branch information
garethbowen committed May 16, 2024
1 parent b0fa207 commit 2bebd76
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
2 changes: 1 addition & 1 deletion ddocs/medic-db/medic/validate_doc_update.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ function(newDoc, oldDoc, userCtx, secObj) {
return;
}

if (isAdminOnlyDoc(newDoc)) {
if (isAdminOnlyDoc(newDoc) || (oldDoc && isAdminOnlyDoc(oldDoc))) {
_err('You are not authorized to edit admin only docs');
}

Expand Down
42 changes: 40 additions & 2 deletions webapp/tests/mocha/unit/validate_doc_update.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ describe('validate doc update', () => {
return forbidden(clientFn, msg, userCtx, newDoc, oldDoc, secObj);
};

describe('only db and national admins are allowed to change...', () => {
describe('only db and national admins are allowed to create...', () => {
const adminCtx = userCtx({ roles: [ '_admin' ] });
const nationalAdminCtx = userCtx({ roles: [ 'national_admin' ] });
const testUserCtx = userCtx({ roles: [ 'test' ] });
Object.entries({
Expand All @@ -76,13 +77,50 @@ describe('validate doc update', () => {
'partners': { _id: 'partners' }
}).forEach(([ name, doc ]) => {
it(name, () => {
allowedOnServer(userCtx({ roles: [ '_admin' ] }), doc);
allowedOnServer(adminCtx, doc);
forbiddenOnServer('You are not authorized to edit admin only docs', nationalAdminCtx, doc);
forbiddenOnServer('You are not authorized to edit admin only docs', testUserCtx, doc);
});
});
});

describe('only db and national admins are allowed to update...', () => {
const adminCtx = userCtx({ roles: [ '_admin' ] });
const testUserCtx = userCtx({ roles: [ 'test' ] });
[
{
name: 'forms',
oldDoc: { _id: 'a', type: 'form' },
newDoc: { _id: 'a', type: 'feedback' }
},
{
name: 'translations',
oldDoc: { _id: 'messages-en', type: 'translations' },
newDoc: { _id: 'messages-en', type: 'feedback' }
},
{
name: 'extension-libs',
oldDoc: { _id: 'extension-libs' },
newDoc: { _id: 'extension-libs', field: 'mine' }
},
{
name: 'branding',
oldDoc: { _id: 'branding' },
newDoc: { _id: 'branding', field: 'mine' }
},
{
name: 'partners',
oldDoc: { _id: 'partners' },
newDoc: { _id: 'partners', field: 'mine' }
},
].forEach(({ name, oldDoc, newDoc }) => {
it(name, () => {
allowedOnServer(adminCtx, newDoc, oldDoc);
forbiddenOnServer('You are not authorized to edit admin only docs', testUserCtx, newDoc, oldDoc);
});
});
});

it('only db admins are allowed change their own place', () => {
const doc = { _id: 'abc', type: 'clinic' };
const adminCtx = userCtx({ roles: [ '_admin' ], facility_id: 'abc' });
Expand Down

0 comments on commit 2bebd76

Please sign in to comment.