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

Allow return or line break in shipping / payment method description #12465

Merged
merged 5 commits into from
May 22, 2024

Conversation

MrBowmanXD
Copy link
Contributor

What? Why?

When setting up a shipping or payment method description, users are allowed to return to line or add line breaks in the admin side.

However this is not done is the shopfront where everything is only separated with a space. If description are long this can be quite ugly.

(copied from the issue)

Simply added the simple_format method in order to introduce line breaks or return in the shipping method description.
(checkout/_details.html.haml: line98)

What should we test?

  1. Login as admin, go to administration.
  2. Add shipping method description with line breaks / return
  3. Checkout and observe the same description in shopfront (access checkout details page threw shopping cart)

Note: Check security issues but simple_format sanitizes the html by default according to the documentation.

Release notes

Changelog Category (reviewers may add a label for the release notes):

  • [X ] User facing changes

The title of the pull request will be included in the release notes.

@rioug rioug assigned rioug and MrBowmanXD and unassigned rioug May 13, 2024
@rioug rioug added the user facing changes Thes pull requests affect the user experience label May 13, 2024
@rioug rioug changed the title User facing changes - Mirror return or line break in shipping / payment method description #12365 (solved) Allow return or line break in shipping / payment method description May 13, 2024
Copy link
Collaborator

@rioug rioug left a comment

Choose a reason for hiding this comment

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

Nice one, thanks for your help !
could you do the same for payment methods ? Even though the Steps to reproduce don't mention payments methods, the title of the issue implies we should also be fixing payment methods description.

@MrBowmanXD
Copy link
Contributor Author

Yes i can.

@@ -95,7 +95,7 @@
%div.checkout-input{"data-shippingmethod-target": "shippingMethodDescription", "data-shippingmethodid": shipping_method.id , style: "display: #{ship_method_is_selected ? 'block' : 'none'}" }
#distributor_address.panel
- if shipping_method.description.present?
%span #{shipping_method.description}
%span #{simple_format(shipping_method.description)}
Copy link
Member

Choose a reason for hiding this comment

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

Nice simple solution. The simple_format doesn't sanitise HTML though which would otherwise be done automatically. So your change would introduce a security issue by allowing people to insert malicious HTML. The docs recommend using the h helper which is an alias for html_escape. Maybe let's make that clear here:

simple_format(html_escape(shipping_method.description))

And we can write this a bit clearer in HAML:

Suggested change
%span #{simple_format(shipping_method.description)}
%span
= simple_format(html_escape(shipping_method.description))

Actually, the simple_format call is adding a <p> tag around the description. That shouldn't be within a span. So maybe we should leave it out:

Suggested change
%span #{simple_format(shipping_method.description)}
= simple_format(html_escape(shipping_method.description))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Went to read the doc again in the original issue that i posted (https://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#M002285) but it says that simple_format would not sanitize the html presenting a security risk like you said. Thought otherwise.

The comment is so good and clear, learned a bit by reading it, thank you.

(Will add the changes in another time, cause in portugal it's quite a bit late.)

@MrBowmanXD
Copy link
Contributor Author

I think we still need to test in order to check if the order of the methods is right, but in essence the solution might look like the above commit shows.

@MrBowmanXD
Copy link
Contributor Author

We could make a test that simulates adding mailicious html to check if html_escape is working or it's redundant?

@rioug
Copy link
Collaborator

rioug commented May 14, 2024

We could make a test that simulates adding mailicious html to check if html_escape is working or it's redundant?

It's not necessary, html_escape is part of Rails so it should be tested by Rails. If we can't trust Rails then we are in trouble 😄

Copy link
Collaborator

@rioug rioug left a comment

Choose a reason for hiding this comment

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

It looks good now, just one last change see my comment.

@@ -95,7 +95,7 @@
%div.checkout-input{"data-shippingmethod-target": "shippingMethodDescription", "data-shippingmethodid": shipping_method.id , style: "display: #{ship_method_is_selected ? 'block' : 'none'}" }
#distributor_address.panel
- if shipping_method.description.present?
%span #{shipping_method.description}
= simple_format(html_escape(shipping_method.description))
Copy link
Collaborator

Choose a reason for hiding this comment

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

You removed the span here, it should be :

Suggested change
= simple_format(html_escape(shipping_method.description))
%span
= simple_format(html_escape(shipping_method.description))

Copy link
Member

Choose a reason for hiding this comment

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

It was my suggestion to remove the span. Is it important? It's just that simple_format is wrapping the content in <p> and a paragraph is invalid within a span. But maybe we need some CSS changes to make this work?

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 think we don't need the span as well. But i can change the code to the desired effect upon settling in a specific style.

Copy link
Collaborator

@rioug rioug May 20, 2024

Choose a reason for hiding this comment

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

It was my suggestion to remove the span. Is it important?

I missed that. I just checked and the span is indeed not important, so it's good as it is.

@MrBowmanXD
Copy link
Contributor Author

MrBowmanXD commented May 19, 2024

Need to delete this last commit in github. This last commit introduces code that should not be there.

Copy link
Collaborator

@rioug rioug left a comment

Choose a reason for hiding this comment

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

It's good now, thanks for your help @MrBowmanXD 🙏

You last commit will be sorted out once we merge into master. Note for next time we prefer to rebase the branch instead of merging master.

@MrBowmanXD
Copy link
Contributor Author

It's good now, thanks for your help @MrBowmanXD 🙏

You last commit will be sorted out once we merge into master. Note for next time we prefer to rebase the branch instead of merging master.

Sorry for the late response but this last commit introduces code that should not be there. I can make another pull request if necessary.

@MrBowmanXD
Copy link
Contributor Author

Had to remove the code according to #12443

@MrBowmanXD MrBowmanXD requested review from rioug and mkllnk May 20, 2024 03:02
Copy link
Member

@mkllnk mkllnk left a comment

Choose a reason for hiding this comment

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

Great, thank you!

Copy link
Collaborator

@rioug rioug left a comment

Choose a reason for hiding this comment

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

Looks good, thanks !

@filipefurtad0 filipefurtad0 self-assigned this May 22, 2024
@filipefurtad0 filipefurtad0 added the pr-staged-fr staging.coopcircuits.fr label May 22, 2024
@filipefurtad0
Copy link
Contributor

Hey @MrBowmanXD ,

Thanks for another PR 💪

Before staging it, we can see the bug (left, admin section; right, shopfront):

image

After staging your PR we can see the line breaks are displayed correctly:

image

Awesome!! 🎉
Merging.

@filipefurtad0 filipefurtad0 removed the pr-staged-fr staging.coopcircuits.fr label May 22, 2024
@filipefurtad0
Copy link
Contributor

Ohh, I just noticed there are some merge conflicts. Can you fix these @MrBowmanXD ?

Thanks!

@MrBowmanXD MrBowmanXD requested review from mkllnk and rioug May 22, 2024 22:36
@MrBowmanXD
Copy link
Contributor Author

Check the shipping method description (in checkout details) and the payment method description (in checkout payment).

@mkllnk mkllnk merged commit 74cfa42 into openfoodfoundation:master May 22, 2024
50 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
user facing changes Thes pull requests affect the user experience
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Mirror return or line break in shipping / payment method description
4 participants