Server-side validation of SUSI.AI is required for increasing the security of user against malicious users who can bypass client-side validations easily and submit dangerous input to SUSI.AI server. SUSI.AI uses both client-side and server-side validation for various authentication purposes.
Client-side validates user input on the browser and provides a better user interaction. If the form is disabled on incorrect fields, it would save a round trip to the server, so the network traffic which will help your server perform better. Using client-side validation, an error message can be immediately shown before the user moves to the next field.
Adding Recaptcha while on new registration, change password, and more than one login attempts help to reduce abusive traffic, spam, brute force attack. For making user accounts safe from getting hacked on SUSI.AI, a strong password is supposed to be at least 8 characters, at least one special character, one number and text characters are must. The username must have 5-51 characters. So as to prevent SUSI.AI admin panel from crashing.
Client side reCaptcha validation:
Once the user verifies captcha on client-side, a callback onCaptchaSuccess fires which receives a parameter captchaResponse. This captchaResponse is sent over to server-side with key g-recaptcha-response
Validation for User Login, ReCaptcha needs to be shown if the user has tried to log in for more than 1 time. For this, sessionStorage needs to be maintained. sessionStorage has an expiration time, which expires once the user closes the tab.
In the constructor of Login, the attempt is initialized with sessionStorage using key loginAttempts. Now, we have the correct value of loginAttempts and can update it further if the user tries to make more attempt.
If the user closes the login dialog, we need to store the current attempts state on componentWillUnmount
If attempts are less than 1, render the reCaptcha component and send g-recaptcha-response parameter to api.susi.ai/aaa/login.json API endpoint.
Server-side reCaptcha Validation for user login
The checkOneOrMoreInvalidLogins function in LoginService checks if the user has attempted to login in more than once. Access the accounting object and get identity and store it in accounting.
If user has attempted to login in more than once, get from g-recaptcha-response API parameter. Check if reCaptcha is verified using VerifyRecaptcha utility class.
The VerifyRecaptcha class provides with a verify method, which can be called to check if user response if correct or not. The secretKey is key given for communication between server-side and google.
URL is created and using InputStream, content(res) is directly read from a URL using openStream() function provided. The bufferedReader reads the contents line by line(using cp), and rd.read() returns a string. The result is appended to stringBuilder sb until -1 is read.
res.close() closes the stream and releases the resources for URL reading. JSONObject is created using the string created from URL, and value mapped to success is used to check if ReCaptcha is verified. If the response is correct, it returns true.
User name validation on server side
Changing regex in config: Regular express for username to have 5-51 characters. Setting regex and tooltip in configs:
users.username.regex.tooltip=Enter atleast 5 character, upto 51 character
If a user tries to add userName more than 51 characters or less than 5 characters,
The regex is compiled using Pattern.compile and then matched against the value provided by the user in API parameter. If the pattern matches, it means the user provided the correct userName.
With server-side validation incorporated, the SUSI.AI users accounts are much safer than only with client-side validation. Validation over both client-side and server-side complement each, server-side validation being much more robust.
- Verifying the user’s response: https://developers.google.com/recaptcha/docs/verify
- Introduction to google recaptcha: https://developers.google.com/recaptcha/intro
- React-recaptcha documentation: https://github.com/appleboy/react-recaptcha
- Reading Directly from a URL: https://docs.oracle.com/javase/tutorial/networking/urls/readingURL.html
- BufferedReader in java: https://www.guru99.com/buffered-reader-in-java.html
- Link to PR on server-side: https://github.com/fossasia/susi_server/pull/1312
- Link to PR on client-side: https://github.com/fossasia/susi.ai/pull/2661, https://github.com/fossasia/susi.ai/pull/2664
SUSI.AI, FOSSASIA, GSoC19, SUSI.AI Server