Skip to content
This repository has been archived by the owner on Aug 30, 2021. It is now read-only.

feat(user): add strict validations for username #1574

Merged
merged 2 commits into from
Oct 20, 2016

Conversation

sujeethk
Copy link
Contributor

feat(user): add strict validations for username

Idea proposed by @sparshy #1204
Suggestions, rules and tests from Trustroots @simison
Added validations on user server model
Added relevant tests on user server tests

Fixes #1204

@hyperreality
Copy link
Contributor

hyperreality commented Oct 16, 2016

Good stuff. Client-side validation should be added too using ng-pattern and relevant ng-messages.

illegalUsernames: ['meanjs', 'fwd', 'fwd:', 'reply', 'administrator', 'password', 'community',
'unknown', 'anonymous', 'null', 'undefined', 'home', 'signup', 'signin', 'login',
'edit', 'settings', 'demo', 'support', 'networks', 'profile', 'avatar', 'mini',
'photo', 'account', 'api', 'modify', 'feedback', 'security', 'accounts', 'tribe', 'tag'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend get rid of 'tribe' and add 'admin' to the list.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact I recommend getting rid of lots of these, such as avatar, demo, unknown etc... it should be up to the user to populate this list and MEAN should only reserve very important ones like admin out the box IMO.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true, I didnt really spend time looking at what was in this array :)

@sujeethk
Copy link
Contributor Author

@hyperreality ^[^.](?=.*[0-9a-z])[0-9a-z.\-_](?!.*(\.)\1).*[^.]{3,34} can you please validate this? I am not really good with regex

@hyperreality
Copy link
Contributor

hyperreality commented Oct 16, 2016

Yeah, that regex is difficult to read and doesn't do the right thing at all. For instance, the {3,34} length rule is actually being applied only to [^.], the positive lookahead (?=.*[0-9a-z]) isn't covering the whole username, [0-9a-z.\-_] only covers a single character, and there are lots of .* which allow any character as many times as possible, which we don't want.

I think we should use this:

^(?=.{3,34}$)(?![_.])(?!.*[_.]{2})[a-zA-Z0-9._]+(?<![_.])$

Adapted from here which has a nice explanation about what it does.

@sujeethk
Copy link
Contributor Author

@hyperreality Lint is unhappy with that regex. It says the group is invalid on that regex.

Added a commit with existing regex, added client side validation with ng-pattern and relevant e2e tests. There is some issue with my protractor running e2e tests on my machine, so i couldnt run the tests. But client and server tests passed. Will wait for travis build to complete so i can check the e2e test results there.

@sujeethk sujeethk force-pushed the strictusername branch 4 times, most recently from 9fb5b38 to fd0aa03 Compare October 16, 2016 22:47
@sujeethk
Copy link
Contributor Author

was able to fix my protractor issue. e2e tests passed.

@mleanos
Copy link
Member

mleanos commented Oct 17, 2016

@sujeethk Did you solve the issue with the linting errors on that regex? Also, what is the problem with the regex that @hyperreality said was incorrect? Was the actual issue with just combining it into one for the client-side check? Currently, it appears that the regex you're using here is the same as what @simison shared.

@sujeethk
Copy link
Contributor Author

@mleanos when I used the Regex that hyperreality provided above I got a lint error saying it was invalid regex. Then I resorted to using the same regex that Simison referred and used the same on the client side. All tests passed. So it's good for now.

Copy link
Member

@mleanos mleanos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left a comment about the client-side regex. I think there's an issue there.

Other than that everything looks good, and seems to be working great. Glad to see the additional tests as well.

@@ -15,6 +15,7 @@
vm.signup = signup;
vm.signin = signin;
vm.callOauthProvider = callOauthProvider;
vm.usernameRegex = /^(?=.*[0-9a-z])[0-9a-z.\-_]{3,34}$/ && /^[^.](?!.*(\.)\1).*[^.]$/;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I notice the client-side doesn't catch "user$lastname" as invalid, but the server-side does. This must be an issue with this client-side regex.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed this in the latest commit and added a test case for the same

@mleanos mleanos added this to the 0.5.0 milestone Oct 17, 2016
@mleanos mleanos self-assigned this Oct 17, 2016
@mleanos
Copy link
Member

mleanos commented Oct 17, 2016

@simison Care to take a quick glance at this?

@meanjs/contributors

@simison
Copy link
Member

simison commented Oct 17, 2016

On holiday this week but i'll see if I can have a look at this tonight,
can't promise tho.

That forbidden words list is very TR specific, you should clean it out for
Meanjs.

On 17 Oct 2016 07:04, "Michael Leanos" notifications@github.com wrote:

@simison https://github.com/simison Care to take a quick glance at this?

@meanjs/contributors https://github.com/orgs/meanjs/teams/contributors


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#1574 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAFUgPOTK6E0TUVis3mcB3E65sLAecKPks5q0vPOgaJpZM4KX19G
.

@sujeethk
Copy link
Contributor Author

@mleanos @hyperreality When using hyperreality's suggestion above this is the error I get
18:25 error Parsing error: Error parsing regular expression: Invalid regular expression: /^(?=.{3,34}$)(?![_.])(?!.*[_.]{2})[a-zA-Z0-9._]+(?<![_.])$/: Invalid group

Hence I used simison's suggestion. @mleanos you are right the same regex works on server side, but on the client side it allows special characters. I am trying to find why, is ngPattern using the regex differently or is it because i concatenated the two regex using &&. If any regex experts out here, your help is much appreciated!

@sujeethk
Copy link
Contributor Author

@mleanos Fixed the regex on client side using help from a regex IRC :). Used the same regex on the server side eliminating the need for a usernameRegex and dotRegex. Also added a couple of tests to check for minimum length and log$in special character

Idea proposed by @sparshy meanjs#1204
Suggestions, rules and tests from Trustroots @simison
Added validations on user server model
Added client side validations
Added relevant tests on user server tests
Added relevant tests on user e2e tests

Fixes meanjs#1204
@mleanos
Copy link
Member

mleanos commented Oct 17, 2016

@sujeethk LGTM. I'll try to get this merged in the next couple of days. Thanks for taking this on.

@mleanos
Copy link
Member

mleanos commented Oct 20, 2016

@sujeethk Would you mind rebasing? I'll merge right after.

@sujeethk
Copy link
Contributor Author

@mleanos Rebased

@mleanos mleanos merged commit fb9d9d9 into meanjs:master Oct 20, 2016
@mleanos
Copy link
Member

mleanos commented Oct 20, 2016

Thank you @sujeethk!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants