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

getSlug leaves characters that will be escaped in a URL #4344

Closed
alexandros84 opened this issue Jun 20, 2018 · 5 comments
Closed

getSlug leaves characters that will be escaped in a URL #4344

alexandros84 opened this issue Jun 20, 2018 · 5 comments
Labels
bug For issues that describe a defect or regression in the released software

Comments

@alexandros84
Copy link

alexandros84 commented Jun 20, 2018

Prerequisites

  • [ X ] Are you running the latest version?
  • [ X ] Are you able to consistently reproduce the issue?
  • [ X ] Did you search the issue queue for existing issue? Search issues

Issue Description

It seems that the default replace options of either the transliteration https://github.com/reactioncommerce/transliteration or slugify https://github.com/simov/slugify packages Reaction is using, do currently allow characters that will be escaped within a URL to be included in shopSlugs and product handles.

Steps to Reproduce

When for example, a user creates a new shop with name "@sarah's Corner#$%" the resulting shopSlug will be "@sarah's-cornerdollar" which is going to become "%40sarah's-cornerdollar" when inside a URL.

This kind of default behavior can result in plenty of unnecessary confusion and can cost in terms of customer support resources. The same behavior can be seen in the making of product handles as the same function (getSlug) is responsible for this as well.

Possible Solution

getSlug uses whatever definition of slugify is passed on to her from lazyLoadSlugify. In that sense since the getSlug slugify might be either transliteration or slugify, the solution I came up with was to replace the following lines of code from lazyLoadSlugify.

// slugify is exported to modules.default while transliteration is exported to modules.slugify slugify = mod.default || mod.slugify;

with this:

// slugify is exported to modules.default while transliteration is exported to modules.slugify if (mod.default) { slugify = mod.default; slugify.extend({ " ": "-", "$": "-dlr-", "&": "-n-", "": "-opn-qte-", ":": "-cln-", "<": "-l-an-br-", ">": "-r-an-br-",
"[": "-l-sq-br-", "]": "-r-sq-br-", "{": "-l-cu-br-", "}": "-r-cu-br-", '"': "-qts-", "+": "-plus-", "#": "-otrp-",
"%": "-pct-", "@": "-at-", "/": "-sls-", ";": "-scl-", "=": "-eql-", "?": "-qstn-", "\": "-bsl-", "^": "-crt-",
"|": "-or-", "~": "-tld-", ",": "-cma-"
});
} else {
slugify = mod.slugify;
slugify.config({
replace: {
" ": "-", "$": "-dlr-", "&": "-n-", "": "-opn-qte-", ":": "-cln-", "<": "-l-an-br-", ">": "-r-an-br-", "[": "-l-sq-br-", "]": "-r-sq-br-", "{": "-l-cu-br-", "}": "-r-cu-br-", '"': "-qts-", "+": "-plus-", "#": "-otrp-", "%": "-pct-", "@": "-at-", "/": "-sls-", ";": "-scl-", "=": "-eql-", "?": "-qstn-", "\\": "-bsl-", "^": "-crt-", "|": "-or-", "~": "-tld-", ",": "-cma-" } }); }

Like getSlug, lazyLoadSlugify is defined multiple times throughout the latest working version of Reaction. So in order to centralize all necessary replacements I have defined this once as a const just below the latinLangs const in lib/api/helpers:

export const replacementOptions = { " ": "-", "$": "-dlr-", "&": "-n-", "": "-opn-qte-",
":": "-cln-",
"<": "-l-an-br-",
">": "-r-an-br-",
"[": "-l-sq-br-",
"]": "-r-sq-br-",
"{": "-l-cu-br-",
"}": "-r-cu-br-",
'"': "-qts-",
"+": "-plus-",
"#": "-otrp-",
"%": "-pct-",
"@": "-at-",
"/": "-sls-",
";": "-scl-",
"=": "-eql-",
"?": "-qstn-",
"\": "-bsl-",
"^": "-crt-",
"|": "-or-",
"~": "-tld-",
",": "-cma-"
};
`

Versions

While I was not able to test this in the latest Reaction version 1.13 because of this issue #4202 I have tested this on our team's 1.11 backed development site and it works as expected.

@prosf prosf changed the title getSlug allows the inclusion of characters that will be escaped in a URL getSlug allows the inclusion of characters that should be escaped in a URL Jun 20, 2018
@alexandros84 alexandros84 changed the title getSlug allows the inclusion of characters that should be escaped in a URL getSlug leaves characters that will be escaped in a URL Jun 20, 2018
@brent-hoover brent-hoover added the bug For issues that describe a defect or regression in the released software label Jun 21, 2018
@brent-hoover
Copy link
Collaborator

@alexandros84 I wonder if a better solution wouldn't be to put some validation on the client-side and not let users input these invalid characters in the first place rather than giving them unexpected results?

@alexandros84
Copy link
Author

alexandros84 commented Jun 25, 2018

That is definitely one valid approach to the whole issue. However since Reaction is giving us the option to deal with it effectively while giving the opportunity to sellers to differentiate creatively the name of their shops (for example " #hot? ") I decided to go down that way. What I am not sure of, is whether @nnnnat has already implemented the suggested solution #6 #4266 "Refactor server/api/core/utils.js getSlug method to just import transliteration by default...". That would simplify my suggested solution even more as the slugify replacement option would be rendered immediately redundant!

@brent-hoover
Copy link
Collaborator

I believe that all server-side instances were changed to just import transliteration since there is no point in lazy-loading on the server-side.

@alexandros84
Copy link
Author

Ok. I ve noticed also that the issue with Promise.await has been resolved #4202 so I will test this and modify my suggestion accordingly, hopefully in the next 48 hours. Thank you!

@alexandros84
Copy link
Author

@zenweasel based on our discussion I have created this PR #4381

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug For issues that describe a defect or regression in the released software
Projects
None yet
Development

No branches or pull requests

3 participants