Skip to content

Commit

Permalink
fix(resourcebasedstatement): check principalType when is undefined
Browse files Browse the repository at this point in the history
this affects matchPrincipals and matchNotPrincipals

fix #42
  • Loading branch information
roggervalf committed Apr 9, 2021
1 parent 8bbc412 commit 34ce2f4
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 6 deletions.
55 changes: 55 additions & 0 deletions src/ResourceBasedPolicy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,33 @@ describe('ResourceBasedPolicy Class', () => {
})
).toBe(false);
});

describe('when there is not a matched principalType', () => {
it('returns false', () => {
const resourceBasedPolicy = new ResourceBasedPolicy({
statements: [
{
action: '*',
resource: '*',
principal: {
a: '*',
b: '*',
c: '*'
}
}
]
});

expect(
resourceBasedPolicy.evaluate({
action: 'read',
resource: 'secrets:ultraSecret',
principal: 'secret',
principalType: 'd'
})
).toBe(false);
});
});
});

describe('when match not principal', () => {
Expand Down Expand Up @@ -258,6 +285,34 @@ describe('ResourceBasedPolicy Class', () => {
})
).toBe(true);
});

describe('when there is not a matched principalType', () => {
it('returns false', () => {
const resourceBasedPolicy = new ResourceBasedPolicy({
statements: [
{
action: '*',
resource: '*',
notPrincipal: {
a: '*',
b: '*',
c: '*'
}
}
],
context: {}
});

expect(
resourceBasedPolicy.evaluate({
action: 'read',
resource: 'secrets:ultraSecret',
principal: 'secret',
principalType: 'd'
})
).toBe(true);
});
});
});

describe('when match actions', () => {
Expand Down
16 changes: 10 additions & 6 deletions src/ResourceBasedStatement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,12 @@ class ResourceBased<T extends object> extends Statement<T> {
return principalValues.some((a) =>
new Matcher(applyContext(a, context)).match(principal)
);
} else if (principalValues) {
return new Matcher(applyContext(principalValues, context)).match(
principal
);
}
return new Matcher(applyContext(principalValues, context)).match(
principal
);
return false;
}
return false;
}
Expand All @@ -212,10 +214,12 @@ class ResourceBased<T extends object> extends Statement<T> {
return !principalValues.some((a) =>
new Matcher(applyContext(a, context)).match(principal)
);
} else if (principalValues) {
return !new Matcher(applyContext(principalValues, context)).match(
principal
);
}
return !new Matcher(applyContext(principalValues, context)).match(
principal
);
return true;
}
return true;
}
Expand Down

0 comments on commit 34ce2f4

Please sign in to comment.