-
-
Notifications
You must be signed in to change notification settings - Fork 219
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
feat: add detection for more model errors #485
Open
HashCookie
wants to merge
4
commits into
casbin:master
Choose a base branch
from
HashCookie:add_validation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1ca4e96
feat: add validation for matchers in config parsing
HashCookie 0a58349
feat: add new properties to validProps in Config class
HashCookie d6708c3
refactor: move validation logic to ValidatorEnforcer class
HashCookie df65e96
test: add unit tests for ValidatorEnforcer
HashCookie 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 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 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 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,85 @@ | ||
import { sectionNameMap, requiredSections } from './model/model'; | ||
|
||
export class ValidatorEnforcer { | ||
// Verify matcher | ||
public static validateMatcher(matcherStr: string): void { | ||
const errors: string[] = []; | ||
|
||
const validProps = ['r.sub', 'r.obj', 'r.act', 'r.dom', 'p.sub', 'p.obj', 'p.act', 'p.dom', 'p.eft', 'p.sub_rule']; | ||
const usedProps = matcherStr.match(/[rp]\.\w+/g) || []; | ||
const invalidProps = usedProps.filter((prop) => !validProps.includes(prop)); | ||
if (invalidProps.length > 0) { | ||
errors.push(`Invalid properties: ${invalidProps.join(', ')}`); | ||
} | ||
|
||
if (matcherStr.includes('..')) { | ||
errors.push('Found extra dots'); | ||
} | ||
|
||
if (matcherStr.trim().endsWith(',')) { | ||
errors.push('Unnecessary comma'); | ||
} | ||
|
||
const openBrackets = (matcherStr.match(/\(/g) || []).length; | ||
const closeBrackets = (matcherStr.match(/\)/g) || []).length; | ||
if (openBrackets !== closeBrackets) { | ||
errors.push('Mismatched parentheses'); | ||
} | ||
|
||
const invalidOperators = /(?<![&|])&(?!&)|(?![&|])\|(?!\|)|&{3,}|\|{3,}/g; | ||
if (invalidOperators.test(matcherStr)) { | ||
errors.push('Invalid operator in matcher'); | ||
} | ||
|
||
if (errors.length > 0) { | ||
throw new Error(`${errors.join(', ')}`); | ||
} | ||
} | ||
|
||
// Verify policy priority | ||
public static validatePolicyPriority(oldRule: string[], newRule: string[], priorityIndex: number): void { | ||
if (oldRule[priorityIndex] !== newRule[priorityIndex]) { | ||
throw new Error('new rule should have the same priority with old rule.'); | ||
} | ||
} | ||
|
||
// Verify required sections | ||
public static validateRequiredSections(model: Map<string, Map<string, any>>): void { | ||
const missingSections = requiredSections.filter((section) => !model.has(section)); | ||
|
||
if (missingSections.length > 0) { | ||
const missingNames = missingSections.map((s) => sectionNameMap[s]); | ||
throw new Error(`missing required sections: ${missingNames.join(',')}`); | ||
} | ||
} | ||
|
||
// Verify duplicate section | ||
public static validateDuplicateSection(section: string, lineNumber: number): void { | ||
throw new Error(`Duplicated section: ${section} at line ${lineNumber}`); | ||
} | ||
|
||
// Verify content parse | ||
public static validateContentParse(lineNum: number): void { | ||
throw new Error(`parse the content error : line ${lineNum}`); | ||
} | ||
|
||
// Verify empty key | ||
public static validateEmptyKey(): void { | ||
throw new Error('key is empty'); | ||
} | ||
|
||
// Verify operator in matcher | ||
public static validateMatcherOperators(value: string): void { | ||
const invalidOperators = /(?<![&|])&(?!&)|(?![&|])\|(?!\|)|&{3,}|\|{3,}/g; | ||
if (invalidOperators.test(value)) { | ||
throw new Error(`Invalid operator in matcher`); | ||
} | ||
} | ||
|
||
// Verify model parameters | ||
public static validateModelParameters(textLength: number): void { | ||
if (textLength !== 0 && textLength !== 1 && textLength !== 2) { | ||
throw new Error('Invalid parameters for model'); | ||
} | ||
} | ||
} |
This file contains 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,79 @@ | ||
import { ValidatorEnforcer } from '../src/validatorEnforcer'; | ||
|
||
describe('ValidatorEnforcer', () => { | ||
describe('validateMatcher', () => { | ||
it('should not throw error for valid matcher', () => { | ||
expect(() => { | ||
ValidatorEnforcer.validateMatcher('r.sub == p.sub && r.obj == p.obj && r.act == p.act'); | ||
}).not.toThrow(); | ||
}); | ||
|
||
it('should throw error for invalid properties', () => { | ||
expect(() => { | ||
ValidatorEnforcer.validateMatcher('r.invalid == p.sub'); | ||
}).toThrow('Invalid properties: r.invalid'); | ||
}); | ||
|
||
it('should throw error for extra dots', () => { | ||
expect(() => { | ||
ValidatorEnforcer.validateMatcher('r..sub == p.sub'); | ||
}).toThrow('Found extra dots'); | ||
}); | ||
|
||
it('should throw error for unnecessary comma', () => { | ||
expect(() => { | ||
ValidatorEnforcer.validateMatcher('r.sub == p.sub,'); | ||
}).toThrow('Unnecessary comma'); | ||
}); | ||
|
||
it('should throw error for mismatched parentheses', () => { | ||
expect(() => { | ||
ValidatorEnforcer.validateMatcher('(r.sub == p.sub'); | ||
}).toThrow('Mismatched parentheses'); | ||
}); | ||
|
||
it('should throw error for invalid operators', () => { | ||
expect(() => { | ||
ValidatorEnforcer.validateMatcher('r.sub & p.sub'); | ||
}).toThrow('Invalid operator in matcher'); | ||
}); | ||
}); | ||
|
||
describe('validatePolicyPriority', () => { | ||
it('should not throw error for same priority', () => { | ||
expect(() => { | ||
ValidatorEnforcer.validatePolicyPriority(['alice', 'data1', 'read', '1'], ['bob', 'data2', 'write', '1'], 3); | ||
}).not.toThrow(); | ||
}); | ||
|
||
it('should throw error for different priority', () => { | ||
expect(() => { | ||
ValidatorEnforcer.validatePolicyPriority(['alice', 'data1', 'read', '1'], ['bob', 'data2', 'write', '2'], 3); | ||
}).toThrow('new rule should have the same priority with old rule.'); | ||
}); | ||
}); | ||
|
||
describe('validateRequiredSections', () => { | ||
it('should not throw error when all required sections are present', () => { | ||
const model = new Map([ | ||
['r', new Map()], | ||
['p', new Map()], | ||
['e', new Map()], | ||
['m', new Map()], | ||
]); | ||
expect(() => { | ||
ValidatorEnforcer.validateRequiredSections(model); | ||
}).not.toThrow(); | ||
}); | ||
|
||
it('should throw error when required sections are missing', () => { | ||
const model = new Map([ | ||
['r', new Map()], | ||
['p', new Map()], | ||
]); | ||
expect(() => { | ||
ValidatorEnforcer.validateRequiredSections(model); | ||
}).toThrow('missing required sections: policy_effect,matchers'); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
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.
Extract all grammar checking code into a "validator"