Skip to content
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

[#8779] AccountAttributes: Remove HTML sanitization before saving for googleId, name, institute #9368

Merged
merged 17 commits into from Jan 29, 2019

Conversation

ChooJeremy
Copy link
Contributor

Fixes #8779

Outline of Solution

Basically just removed the sanitization in AccountAttributes and updated tests. I have tested with single quotes in the email (as per #8774) and it works.

I'm not sure if I need to write a migration script, let me know if I need to. Based on my checks, if there was something to be migrated, it wouldn't have made it to the database anyway because the application would have just rejected it.

Ready for review.

Copy link
Contributor

@xpdavid xpdavid left a comment

Choose a reason for hiding this comment

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

Yes, we might not need a script.

@@ -181,9 +181,9 @@ public String getJsonString() {

@Override
public void sanitizeForSaving() {
this.googleId = SanitizationHelper.sanitizeForHtml(googleId);
Copy link
Contributor

Choose a reason for hiding this comment

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

Should use SanitizationHelper.sanitizeGoogleId?

@@ -181,9 +181,9 @@ public String getJsonString() {

@Override
public void sanitizeForSaving() {
this.googleId = SanitizationHelper.sanitizeForHtml(googleId);
this.name = SanitizationHelper.sanitizeForHtml(name);
Copy link
Contributor

Choose a reason for hiding this comment

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

SanitizationHelper.sanitizeName?

@@ -181,9 +181,9 @@ public String getJsonString() {

@Override
public void sanitizeForSaving() {
this.googleId = SanitizationHelper.sanitizeForHtml(googleId);
this.name = SanitizationHelper.sanitizeForHtml(name);
this.institute = SanitizationHelper.sanitizeForHtml(institute);
Copy link
Contributor

Choose a reason for hiding this comment

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

SanitizationHelper.sanitizeName maybe?

@xpdavid xpdavid self-assigned this Jan 26, 2019
@xpdavid xpdavid added the s.Ongoing The PR is being worked on by the author(s) label Jan 26, 2019
@ChooJeremy
Copy link
Contributor Author

Done, ready for review.

I used SanitizationHelper.sanitizeTitle for institute sanitization (based on AccountAttributes's Builder build() method.)

package.json Outdated
@@ -44,7 +44,7 @@
},
"devDependencies": {
"@angular-builders/jest": "~7.1.2",
"@angular-devkit/build-angular": "~0.11.4",
Copy link
Contributor

Choose a reason for hiding this comment

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

Not relevant change?

@@ -95,9 +94,9 @@ public void testSanitizeForSaving() {
AccountAttributes expectedAccount = createAccountAttributesToSanitize();
actualAccount.sanitizeForSaving();

assertEquals(SanitizationHelper.sanitizeForHtml(expectedAccount.googleId), actualAccount.googleId);
Copy link
Contributor

Choose a reason for hiding this comment

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

So your tests should also reflect the changes?

e.g. Having some values that need to be sanitised and test whether the actualAccount.sanitizeForSaving(); work or not?

@ChooJeremy
Copy link
Contributor Author

Sorry, forgot about the tests. Interestingly, this means that the tests would have passed no matter what, because the Builder in AccountAttributes sanitized the results when building the object.

I've changed the email test to add the single quote and added whitespace around all the values since the sanitization code is effectively just trimming the values.

Ready for review.

@@ -92,6 +92,10 @@ public AccountAttributes build() {
return accountAttributes;
}

public AccountAttributes buildWithoutSanitizing() {
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think it is a good practice to introduce this method in production code just for testing.

assertEquals(SanitizationHelper.sanitizeForHtml(expectedAccount.name), actualAccount.name);
assertEquals(SanitizationHelper.sanitizeForHtml(expectedAccount.institute), actualAccount.institute);
assertEquals(SanitizationHelper.sanitizeGoogleId(expectedAccount.googleId), actualAccount.googleId);
assertEquals(SanitizationHelper.sanitizeName(expectedAccount.name), actualAccount.name);
Copy link
Contributor

Choose a reason for hiding this comment

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

Interestingly, this means that the tests would have passed no matter what, because the Builder in AccountAttributes sanitized the results when building the object.

If that is the case. Maybe we can just do assertEquals(expectedAccount.name, actualAccount.name)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not quite sure I understand how that solves the problem. Since build sanitizes all the fields, both expectedAccount and actualAccount will have the same values. Checking equality would only verify that running sanitizeForSaving on sanitized values doesn't change the values at all.

Perhaps the method createAccountAttributesToSanitize could just modify the values directly? So something like:

    private AccountAttributes createAccountAttributesToSanitize() {
        AccountAttributes unsanitizedAttributes = AccountAttributes.builder().build();
        unsanitizedAttributes.googleId = "    google'Id@gmail.com\t";
        unsanitizedAttributes.name = "'name'\n\n";
        unsanitizedAttributes.institute = "\\\t\n/";
        unsanitizedAttributes.email = "&<email>&\n";
        unsanitizedAttributes.isInstructor = true;

        return unsanitizedAttributes;
    }

Copy link
Contributor

Choose a reason for hiding this comment

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

Cool. We can do that.

@ChooJeremy
Copy link
Contributor Author

Ok, ready for review.

Copy link
Contributor

@xpdavid xpdavid left a comment

Choose a reason for hiding this comment

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

Some comment on test data selection.

unsanitizedAttributes.googleId = " google'Id@gmail.com\t";
unsanitizedAttributes.name = "'name'\n\n";
unsanitizedAttributes.institute = "\\\t\n/";
unsanitizedAttributes.email = "&<email>&\n";
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should change this to a valid email address.


AccountAttributes unsanitizedAttributes = AccountAttributes.builder().build();
unsanitizedAttributes.googleId = " google'Id@gmail.com\t";
unsanitizedAttributes.name = "'name'\n\n";
Copy link
Contributor

Choose a reason for hiding this comment

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

There should be some extra space inside name in order to make sure sanitizeName is doing the job? 'name'\n\n?

AccountAttributes unsanitizedAttributes = AccountAttributes.builder().build();
unsanitizedAttributes.googleId = " google'Id@gmail.com\t";
unsanitizedAttributes.name = "'name'\n\n";
unsanitizedAttributes.institute = "\\\t\n/";
Copy link
Contributor

Choose a reason for hiding this comment

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

Same for institute.

@ChooJeremy
Copy link
Contributor Author

Ok, test data modified, whitespace added to the middle of the strings.

Ready for review.

Copy link
Contributor

@xpdavid xpdavid left a comment

Choose a reason for hiding this comment

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

LGTM!

@xpdavid xpdavid requested a review from damithc January 29, 2019 06:23
@xpdavid xpdavid added s.FinalReview The PR is ready for final review and removed s.Ongoing The PR is being worked on by the author(s) labels Jan 29, 2019
@xpdavid xpdavid merged commit 1ba05cc into TEAMMATES:master Jan 29, 2019
@xpdavid xpdavid added s.ToMerge The PR is approved by all reviewers including final reviewer; ready for merging c.Task Other non-user-facing works, e.g. refactoring, adding tests and removed s.FinalReview The PR is ready for final review labels Jan 29, 2019
@wkurniawan07 wkurniawan07 added this to the V7.0.0-beta.0 milestone Feb 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c.Task Other non-user-facing works, e.g. refactoring, adding tests s.ToMerge The PR is approved by all reviewers including final reviewer; ready for merging
Projects
None yet
Development

Successfully merging this pull request may close these issues.

AccountAttributes: Remove HTML sanitization before saving for googleId, name, institute
4 participants