Skip to content

Commit b3d266e

Browse files
committed
fix special character bug
1 parent 8605532 commit b3d266e

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

packages/scope-validator/lib/pattern.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default class Pattern {
55
const regex = this.str
66
.replace(/(\${\w+})/g, '*')
77
.replace(/[$^.+?]/g, '\\$&')
8-
.replace(/\*/g, '(\\w+)');
8+
.replace(/\*/g, `([!@#$%^&*\\-\\w]+)`);
99

1010
return new RegExp(`^${regex}$`);
1111
}
@@ -29,9 +29,9 @@ export default class Pattern {
2929
}
3030

3131
const regexStr = this.str
32-
.replace(/(\${\w+})/g, '*')
32+
.replace(/(\${[!@#$%^&*\-\w]+})/g, '*')
3333
.replace(/[$^.+?]/g, '\\$&')
34-
.replace(/\*/g, '(\\w+)')
34+
.replace(/\*/g, '([!@#$%^&*\\-\\w]+)')
3535
.replace(/(\*)/g, ')(.+)(?:')
3636
.replace(/^(.*)/, '(?:$&')
3737
.replace(/(.*)$/, '$&)');

packages/scope-validator/lib/scope-validator-manager.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export default class ScopeValidatorManager<T> {
2222
const matchValidators = this.scopeValidators.filter((validator) =>
2323
validator.canValidate(scope)
2424
);
25+
2526
return (
2627
matchValidators.length >= 1 &&
2728
matchValidators.every((validator) =>

packages/scope-validator/test/integration/single-parameter.spec.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ const TestValidator1 = ScopeValidatorFactory.create(
88

99
if (restricter === 'all') return true;
1010
if (restricter === 'me') return received?.ownerId === 'asdf';
11-
return false;
11+
12+
return restricter === 'test-all';
1213
}
1314
);
1415

@@ -29,6 +30,12 @@ const TestValidator4 = ScopeValidatorFactory.create(
2930
() => true
3031
);
3132

33+
const TestValidator5 = ScopeValidatorFactory.create(
34+
// eslint-disable-next-line no-template-curly-in-string
35+
'create:${param1}:${param2}:last',
36+
() => true
37+
);
38+
3239
describe('success', () => {
3340
it('validate scope', () => {
3441
const scopeValidatorManager = new ScopeValidatorManager();
@@ -44,6 +51,17 @@ describe('success', () => {
4451
expect(result).toEqual([true, true, false]);
4552
});
4653

54+
it('validate scope: special character', () => {
55+
const scopeValidatorManager = new ScopeValidatorManager();
56+
scopeValidatorManager.use(TestValidator1);
57+
58+
const result = scopeValidatorManager.validate(['create:client:test-all'], {
59+
ownerId: 'asdf',
60+
});
61+
62+
expect(result).toEqual([true]);
63+
});
64+
4765
it('validate one scope', () => {
4866
const scopeValidatorManager = new ScopeValidatorManager();
4967
scopeValidatorManager.use(TestValidator1);
@@ -79,6 +97,18 @@ describe('success', () => {
7997

8098
expect(result).toEqual([true]);
8199
});
100+
101+
it('validate scope special character 2', () => {
102+
const scopeValidatorManager = new ScopeValidatorManager();
103+
scopeValidatorManager.use(TestValidator5);
104+
105+
const result = scopeValidatorManager.validate(
106+
['create:test1:test2:last'],
107+
'test'
108+
);
109+
110+
expect(result).toEqual([true]);
111+
});
82112
});
83113

84114
describe('failed', () => {

0 commit comments

Comments
 (0)