-
Notifications
You must be signed in to change notification settings - Fork 2
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
fix: prevent HTML/URLs in the Full Name field #2994
fix: prevent HTML/URLs in the Full Name field #2994
Conversation
9734ecc
to
c0e7912
Compare
f132122
to
1ba7d9f
Compare
fcbea87
to
4144b19
Compare
49f2840
to
d2272c0
Compare
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.
This is the code review. However, I would like to mention a couple of things:
- Long ago we implemented the same thing in MITx Online but with username. (734 - registration validate username against openedx mitxonline#757). This was implemented in MITx Online and not edx-api-client.
- The future of this registration validation would be to serve as a common functionality for both MITx Online and xPRO. So, we should implement it that way. (This is related to edx-api-client but adding it here to keep things together)
- Once we are done with above, We also need to refactor the code in MITx Online so that it uses the edx-api-client
authentication/api_test.py
Outdated
mock_client = mocker.MagicMock() | ||
mock_client.user_info.validate_user_registration.return_value = { | ||
"validation_decisions": {"name": ""} | ||
} | ||
mocker.patch( | ||
"courseware.api.get_edx_api_registration_client", return_value=mock_client | ||
) | ||
|
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.
One of the following should be done here since this is a repetitive code and we should avoid it and make it reusable:
- Move this to TestCaseSetup if exists
- Create a fixture for this mock e.g.
mock_email_send
in code (Recommended)
courseware/api.py
Outdated
if settings.MITXPRO_REGISTRATION_ACCESS_TOKEN is None: | ||
raise ImproperlyConfigured("MITXPRO_REGISTRATION_ACCESS_TOKEN is not set") # noqa: EM101 |
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.
I don't think this is needed. Isn't the validation API public? Because if the ACCESS_TOKEN isn't set it will stop the operation.
Now if we remove this check, We might as well not need a method at all. The initialization can be done on demand wherever needed.
courseware/api.py
Outdated
@@ -534,6 +535,23 @@ def get_edx_api_service_client(): | |||
) | |||
|
|||
|
|||
def get_edx_api_registration_client(): |
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.
This should not be a part of the courseware. This should either be moved to users or opened directory
@@ -816,3 +834,26 @@ def delete_oauth_application(): | |||
name=settings.OPENEDX_OAUTH_APP_NAME | |||
).delete() | |||
return _, deleted_applications_count | |||
|
|||
|
|||
def validate_name_with_edx(name): |
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.
This should not be a part of the courseware. This should either be moved to users or opened directory
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.
We don't have an openedx
directory in xPro, but it exists in MITx Online in replace of courseware
for handling all API interactions with the edX platform. This is beneficial as it centralizes interactions with Open edX.
In xPro, courseware serves a similar role e.g. both course-related and user-specific Open edX API interactions.
The name courseware can be misleading since it implies only course-specific API interactions, while it actually includes other APIs interactions. I would recommend to lets keep it here to have common directory to interact with openedx similar to what we have in mitxonline and should have some meaningful name in future that serves the purpose to interact with openedx.
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.
Alright, I agree. Let's keep it here for now. But my recommendation would be to create and openedx app and move this there but we can do that later in a refactoring PR.
courseware/api.py
Outdated
name (str): The full name | ||
|
||
Raises: | ||
EdxApiRegistrationValidationException: Raised if response status is not OK. |
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.
not OK
is not a good representation of what we are trying to say. It should say 200 or HTTP_200_OK
users/serializers.py
Outdated
@@ -267,6 +277,28 @@ def get_unused_coupons(self, instance): | |||
return fetch_and_serialize_unused_coupons(instance) | |||
return [] | |||
|
|||
def validate(self, data): | |||
"""Validate an existing user""" |
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.
Looks like an incorrect docstring
@@ -267,6 +277,28 @@ def get_unused_coupons(self, instance): | |||
return fetch_and_serialize_unused_coupons(instance) | |||
return [] | |||
|
|||
def validate(self, data): |
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.
Should it be validate_full_name?
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.
I have used general validation outside of validate_full_name in order to make name validation required in case if openedx fails and returns an object-level error. We will not allow account creation to proceed and a proper error message will be displayed:
We have something similar mitodl/mitxonline#1389 (review) suggestion for an object-level error message/handling done in mitxonline. The error message is subjective to what we want to use but I have used it what we were used https://github.com/mitodl/mitxpro/blob/master/authentication/serializers.py#L177 in such cases.
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.
Stopping account creation can still be done even we do general validate or not. A general validate would be more useful when we want to throw multiple errors to the frontend at the same time.
if (errors && errors.length > 0) { | ||
setErrors({ | ||
email: errors[0], | ||
if (errors) { |
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.
Can we utilize the functionality of the Validation exception from the backend instead of making this code change in the frontend? But in reality are we sending multiple error messages from the backend?
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.
Can we utilize the functionality of the Validation exception from the backend instead of making this code change in the frontend?
Is that change break anything with existing functionality?
I'm not sure if you have this question due to email: errors[0],
, I tried to check email but did not get it's usage in on edit profile
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.
I mean, when we return ValidationError from the backend would that become part of the field_errors?
Currently, We return a Validation Error in case legal address validation, But that doesn't get displayed on the frontend, That however, stops the registration process keeping the user on the same page. Take a look at this for reference. And I see that you are also throwing a validation error message. So, we don't need these frontend changes If that's still not going to be shown on the fields.
@@ -84,10 +91,31 @@ export class RegisterDetailsPage extends React.Component<Props, State> { | |||
partialToken, | |||
); | |||
|
|||
if (body.errors) { |
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.
Same as above message in EditProfile page
users/serializers.py
Outdated
NAME_CONTAINS_HTML_URL_MSG = ( | ||
"Full name can not contain HTML or URL. Please try a different one." | ||
) |
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.
If we show the validation character message in this case as well, the would be fine. We don't need a new message for it.
e95c4d8
to
003b80f
Compare
@mudassir-hafeez Could you rebase this PR ? |
fe8a676
to
4f2ae1f
Compare
courseware/api.py
Outdated
return edx_client.user_validation.validate_user_registration( | ||
registration_information=dict( # noqa: C408 | ||
name=name, | ||
) | ||
).name |
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.
It is always cleaner to get the API response in a variable i.e. response
. Also, We unnecessarily ignored C408. We should try to resolve the issue instead of suppressing it here. e.g. You can pass a plain dictionary here.
courseware/api.py
Outdated
@@ -534,6 +535,26 @@ def get_edx_api_service_client(): | |||
) | |||
|
|||
|
|||
def get_edx_api_registration_client(): |
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.
def get_edx_api_registration_client(): | |
def get_edx_api_registration_validation_client(): |
courseware/api.py
Outdated
@@ -534,6 +535,26 @@ def get_edx_api_service_client(): | |||
) | |||
|
|||
|
|||
def get_edx_api_registration_client(): | |||
""" | |||
Gets an edx api client instance for the user registration |
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.
We should use Open edX in comments everywhere. I'm adding the suggestion here but you can check other cases like this.
Gets an edx api client instance for the user registration | |
Gets an Open edX api client instance for the user registration |
courseware/api.py
Outdated
registration_access_token = ( | ||
settings.MITXPRO_REGISTRATION_ACCESS_TOKEN | ||
if settings.MITXPRO_REGISTRATION_ACCESS_TOKEN | ||
else "" | ||
) | ||
|
||
return EdxApi( |
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.
This API is not authenticated, We don't need to send a token maybe? you can directly send ""
?
users/serializers.py
Outdated
HTTPError, | ||
RequestsConnectionError, |
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.
I don't think these will ever be thrown. In validate_name_with_edx
we catch a general exception and raise a EdxApiRegistrationValidationException
every time.
74eb93c
to
c9ba208
Compare
@arslanashraf7 I've addressed all the request changes you mentioned in the current iteration along with mitodl/edx-api-client#106. They are ready for review now. |
While testing the PR I was having some weird issues, I don't know what's causing them but I wanted to check with you anyway.
Screenshots: |
c9ba208
to
69d763e
Compare
Both issues seem to be unrelated to this PR. For the first issue, please check if the migrations have been applied correctly or if there have been any local changes related to the external course run. For the second issue, ensure that assets are compiling properly and that your local branch is up-to-date with I have also re-based this branch again in the meantime, and retested. I don't see any issues related to this. We can connect if those issues still persist. |
I'm not sure what was causing it but it only worked for me when I pulled your rebased branch. |
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.
👍 Looks good. Thanks for making all the changes and your patience on this.
Just on thing that we discussed over call about the error message in top bar. It stays there even if user fixes the issue and moved to the next page. Since this behaviour is pre-existing for Success case as well, we can work on that in a followup PR.
I would like to note that we should get the edx-api-client release out before merging this. We need to publish edx-api-client first and then pin the new version here for proper functioning.
…and registration code refactoring
69d763e
to
6a6c048
Compare
@arslanashraf7 I've pinned the newly released edx-api-client version and tested it. Could you please review this final change? |
What are the relevant tickets?
#4211
Description (What does it do?)
This PR adds validation for the full name. It also introduces an API to validate registration on Open edX to ensure a valid full name is provided. This change affects both the registration details page and the edit profile page.
Screenshots (if appropriate):
Validation error on registration details page:

Validation error on edit details page:

How can this be tested?
Fetch, check out to this branch, install edx-api-client PR feat: validate user registration api edx-api-client#106 with editable mode, and make sure edx is configured.
Test the front-end validation (registration/edit details pages):
Test the back-end validation (registration/edit details pages):
Test rate limiting/non-field errors (registration/edit details pages):
Note: The last two pointers are for testing validation/API with backend.
Additional Context
Further screenshots of the back-end validation: