Skip to content

fix: invalid name for Parse.Role throws incorrect error #1481

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 4 commits into from
May 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions integration/test/ParseACLTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -533,4 +533,13 @@ describe('Parse.ACL', () => {
const obj1withInclude = await query.first();
assert(obj1withInclude.get('other').get('ACL'));
});

it('prevents save with invalid role name', async () => {
expect(() => new Parse.Role(':%#', new Parse.ACL())).toThrow(
new Parse.Error(
Parse.Error.OTHER_CAUSE,
`A role's name can be only contain alphanumeric characters, _, -, and spaces.`
)
);
});
});
25 changes: 17 additions & 8 deletions src/ParseRole.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class ParseRole extends ParseObject {
* @returns {(ParseObject|boolean)} true if the set succeeded.
*/
setName(name: string, options?: mixed): ParseObject | boolean {
this._validateName(name);
return this.set('name', name, options);
}

Expand Down Expand Up @@ -108,6 +109,18 @@ class ParseRole extends ParseObject {
return this.relation('roles');
}

_validateName(newName) {
if (typeof newName !== 'string') {
throw new ParseError(ParseError.OTHER_CAUSE, "A role's name must be a String.");
}
if (!/^[0-9a-zA-Z\-_ ]+$/.test(newName)) {
throw new ParseError(
ParseError.OTHER_CAUSE,
"A role's name can be only contain alphanumeric characters, _, " + '-, and spaces.'
);
}
}

validate(attrs: AttributeMap, options?: mixed): ParseError | boolean {
const isInvalid = super.validate(attrs, options);
if (isInvalid) {
Expand All @@ -125,14 +138,10 @@ class ParseRole extends ParseObject {
"A role's name can only be set before it has been saved."
);
}
if (typeof newName !== 'string') {
return new ParseError(ParseError.OTHER_CAUSE, "A role's name must be a String.");
}
if (!/^[0-9a-zA-Z\-_ ]+$/.test(newName)) {
return new ParseError(
ParseError.OTHER_CAUSE,
"A role's name can be only contain alphanumeric characters, _, " + '-, and spaces.'
);
try {
this._validateName(newName);
} catch (e) {
return e;
}
}
return false;
Expand Down
9 changes: 9 additions & 0 deletions src/__tests__/ParseRole-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ describe('ParseRole', () => {
expect(role.getName()).toBe('');
});

it('should throw error string with invalid name', () => {
expect(() => new ParseRole('invalid:name', new ParseACL())).toThrow(
new ParseError(
ParseError.OTHER_CAUSE,
"A role's name can be only contain alphanumeric characters, _, " + '-, and spaces.'
)
);
});

it('can validate attributes', () => {
const acl = new ParseACL({ aUserId: { read: true, write: true } });
const role = new ParseRole('admin', acl);
Expand Down