-
Notifications
You must be signed in to change notification settings - Fork 5
feat: add hasPolicyGranting check #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| export * from './fsaba-types'; | ||
| export * from './AuthorizerFactory'; | ||
| export * from './SubjectAuthorizer'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import { HasPolicyGrantingOpts, PolicyWithID } from '../fsaba-types'; | ||
| import stringMatchesPattern from './string-matches-pattern'; | ||
|
|
||
| /** | ||
| * Perform an early check to see if the user has a policy to perform an action at all, | ||
| * with an optional resource prefix. | ||
| */ | ||
| export default function hasPolicyGranting(policies: { allow: readonly PolicyWithID[] }, action: string, opts?: HasPolicyGrantingOpts): boolean { | ||
| return policies.allow.some((policy) => { | ||
| const actionMatches = policy.actions.some((policyAction) => { | ||
| return stringMatchesPattern(policyAction, action); | ||
| }); | ||
|
|
||
| if (!actionMatches) { | ||
| return false; | ||
| } | ||
|
|
||
| const resourcePrefixPattern = opts?.resourcePrefixPattern; | ||
|
|
||
| if (!resourcePrefixPattern) { | ||
| return true; | ||
| } | ||
|
|
||
| if (!resourcePrefixPattern.endsWith('*') || resourcePrefixPattern.indexOf('*') !== resourcePrefixPattern.length - 1) { | ||
| throw new Error('resourcePrefixPattern must end with a wildcard and cannot contain wildcards elsewhere'); | ||
| } | ||
|
|
||
| const prefix = resourcePrefixPattern.slice(0, -1); | ||
|
|
||
| return policy.resources.some((policyResource) => { | ||
| return stringMatchesPattern(policyResource, resourcePrefixPattern) | ||
| || policyResource.startsWith(prefix); | ||
| }); | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,42 +1,111 @@ | ||
| import { expect } from 'chai'; | ||
| import { AuthorizerFactory, Claims } from '../src'; | ||
| import { ADMINISTER_OWN_AUTH, ADMINISTER_OTHER_AUTH, DO_NEARLY_EVERYTHING } from './sample-data'; | ||
| import { AuthorizerFactory, Claims, SubjectAuthorizer } from '../src'; | ||
| import { | ||
| ADMINISTER_OWN_AUTH, | ||
| ADMINISTER_OTHER_AUTH, | ||
| DO_NEARLY_EVERYTHING, | ||
| ALL_ROLES, | ||
| BUDGET_VIEWER_KAZOO_PM, | ||
| BUDGET_VIEWER_SINGLE, | ||
| BUDGET_VIEWER_MFG_HEAD, | ||
| } from './sample-data'; | ||
|
|
||
|
|
||
| describe('SubjectAuthorizer', () => { | ||
| const ALL_ROLES_1 = [ | ||
| const allRoles1 = [ | ||
| ADMINISTER_OWN_AUTH, | ||
| ADMINISTER_OTHER_AUTH, | ||
| ]; | ||
|
|
||
| const ALL_ROLES_2 = [ | ||
| const allRoles2 = [ | ||
| ADMINISTER_OWN_AUTH, | ||
| ADMINISTER_OTHER_AUTH, | ||
| DO_NEARLY_EVERYTHING, | ||
| ]; | ||
|
|
||
| const USER_ID = '73885b55-2e0d-40bd-8cb3-2e59cf78ed87'; | ||
| const userID = '73885b55-2e0d-40bd-8cb3-2e59cf78ed87'; | ||
|
|
||
| const USER: Claims = { | ||
| subjectID: USER_ID, | ||
| const user: Claims = { | ||
| subjectID: userID, | ||
| roles: [ | ||
| { roleID: ADMINISTER_OWN_AUTH.roleID }, | ||
| { roleID: ADMINISTER_OTHER_AUTH.roleID, contextValue: '8e0fa760-9a1d-43ea-8686-768318d923b4' }, | ||
| { roleID: DO_NEARLY_EVERYTHING.roleID }, | ||
| ], | ||
| }; | ||
|
|
||
| const factory1 = new AuthorizerFactory(ALL_ROLES_1), | ||
| factory2 = new AuthorizerFactory(ALL_ROLES_2); | ||
| const factory1 = new AuthorizerFactory(allRoles1), | ||
| factory2 = new AuthorizerFactory(allRoles2); | ||
|
|
||
| it('ignores unknown roles when no opts provided', () => { | ||
| expect(factory1.makeAuthorizerForSubject(USER)).to.be.ok; // eslint-disable-line no-unused-expressions | ||
| expect(factory2.makeAuthorizerForSubject(USER)).to.be.ok; // eslint-disable-line no-unused-expressions | ||
| expect(factory1.makeAuthorizerForSubject(user)).to.be.ok; // eslint-disable-line no-unused-expressions | ||
| expect(factory2.makeAuthorizerForSubject(user)).to.be.ok; // eslint-disable-line no-unused-expressions | ||
| }); | ||
|
|
||
| it('throws an error on unknown roles when requested', () => { | ||
| expect(() => { factory1.makeAuthorizerForSubject(USER, { throwOnUnknownRole: true }); }).to.throw(); | ||
| expect(factory2.makeAuthorizerForSubject(USER, { throwOnUnknownRole: true })).to.be.ok; // eslint-disable-line no-unused-expressions | ||
| expect(() => { factory1.makeAuthorizerForSubject(user, { throwOnUnknownRole: true }); }).to.throw(); | ||
| expect(factory2.makeAuthorizerForSubject(user, { throwOnUnknownRole: true })).to.be.ok; // eslint-disable-line no-unused-expressions | ||
| }); | ||
|
|
||
| describe('hasPolicyGranting', () => { | ||
|
|
||
| it('returns true if the user has a policy for the action', () => { | ||
| const authorizer = new SubjectAuthorizer(ALL_ROLES, BUDGET_VIEWER_SINGLE); | ||
|
|
||
| expect(authorizer.hasPolicyGranting('budget:View')).to.strictlyEqual(true); | ||
| }); | ||
|
|
||
| it('returns false if the user does not have a policy for the action', () => { | ||
| const authorizer = new SubjectAuthorizer(ALL_ROLES, BUDGET_VIEWER_SINGLE); | ||
|
|
||
| expect(authorizer.hasPolicyGranting('budget:Create')).to.strictlyEqual(false); | ||
| }); | ||
|
|
||
| it('returns true if the user has a policy matching resource prefix', () => { | ||
| const authorizer = new SubjectAuthorizer(ALL_ROLES, BUDGET_VIEWER_KAZOO_PM); | ||
|
|
||
| expect(authorizer.hasPolicyGranting('budget:View', { resourcePrefixPattern: 'budget:kazoo/*' })).to.strictlyEqual(true); | ||
| }); | ||
|
|
||
| it('returns true if the policy resource is broader than the requested prefix', () => { | ||
| const authorizer = new SubjectAuthorizer(ALL_ROLES, BUDGET_VIEWER_MFG_HEAD); | ||
|
|
||
| // BUDGET_VIEWER_MFG_HEAD has budget:* | ||
| expect(authorizer.hasPolicyGranting('budget:View', { resourcePrefixPattern: 'budget:kazoo/mktg/*' })).to.strictlyEqual(true); | ||
| }); | ||
|
|
||
| it('returns false if the user has the action but on different resources', () => { | ||
| const authorizer = new SubjectAuthorizer(ALL_ROLES, BUDGET_VIEWER_KAZOO_PM); | ||
|
|
||
| // KAZOO PM has budget:View on budget:* but we can test with a non-matching | ||
| // prefix. | ||
| expect(authorizer.hasPolicyGranting('budget:View', { resourcePrefixPattern: 'budget-v2/*' })).to.strictlyEqual(false); | ||
| }); | ||
|
|
||
| it('throws an error if resourcePrefixPattern does not end with a wildcard or contains internal wildcards', () => { | ||
| const authorizer = new SubjectAuthorizer(ALL_ROLES, BUDGET_VIEWER_KAZOO_PM); | ||
|
|
||
| expect(() => { authorizer.hasPolicyGranting('budget:View', { resourcePrefixPattern: 'budget:kazoo' }); }).to.throw(); | ||
|
|
||
| expect(() => { authorizer.hasPolicyGranting('budget:View', { resourcePrefixPattern: 'budget:*/foo' }); }).to.throw(); | ||
|
|
||
| expect(() => { authorizer.hasPolicyGranting('budget:View', { resourcePrefixPattern: '*budget:foo' }); }).to.throw(); | ||
| }); | ||
|
|
||
| it('correctly handles prefix match', () => { | ||
| const authorizer = new SubjectAuthorizer(ALL_ROLES, BUDGET_VIEWER_KAZOO_PM); | ||
|
|
||
| // BUDGET_VIEWER_KAZOO_PM has policy for budget:kazoo/*. The requested prefix is | ||
| // budget:kazoo/mktg/*. This should be true because the policy resource | ||
| // (budget:kazoo/*) matches the requested prefix (budget:kazoo/mktg/*). | ||
| expect(authorizer.hasPolicyGranting('budget:View', { resourcePrefixPattern: 'budget:kazoo/mktg/*' })).to.strictlyEqual(true); | ||
|
|
||
| // Now, if the requested prefix is budget:kaz*, it should still be true because | ||
| // the requested resource prefix (budget:kaz*) matches the policy resource | ||
| // (budget:kazoo/*). | ||
| expect(authorizer.hasPolicyGranting('budget:View', { resourcePrefixPattern: 'budget:kaz*' })).to.strictlyEqual(true); | ||
| }); | ||
|
|
||
| }); | ||
|
|
||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because this is a more complex security subject, could we add an example scenario where this should be used, clearly showing where isAllowed is still needed?
Example: A request is made for
budgets/it-dept/fy2025/q1. Your code needs to: (1) validate query params, and (2) load the IT Department budget for Q1 of FY2025 to see who has permission to view it (before anisAllowedcall could be made). If the query params seem invalid, or if there is no budget for those params, you'd return a Bad Response or Not Found response. But, if the user doesn't havebudget:Viewfor any combination of departments, etc, then there's no reason to even perform those steps and potentially expose information through the Bad Request or Not Found responses.Instead, you could follow this flow:
hasPolicyGranting('budget:View')- if they don't, then stop right there; if they do have a policy granting them view rights to a budget, it still may not be for this budget, but you can now continue your flow ...isAllowed