-
-
Notifications
You must be signed in to change notification settings - Fork 218
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
base: master
Are you sure you want to change the base?
Changes from all commits
1ca4e96
0a58349
d6708c3
df65e96
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function is using same RegExp as Also this kind of RegExp (especialy LookBehind) has limited support on a not so old browsers like iOS 16 (<16.4), that could lead developers to patching casbin to use with such browsers. |
||
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'); | ||
} | ||
} | ||
} |
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'); | ||
}); | ||
}); | ||
}); |
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"