-
Notifications
You must be signed in to change notification settings - Fork 21
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
feat: prevent special characters from url #1266
feat: prevent special characters from url #1266
Conversation
Hi! I have 2 doubts:
|
@@ -156,7 +163,10 @@ | |||
protected emitEvents(): void { | |||
const { all, extra } = this.parseUrlParams(); | |||
const metadata = this.createWireMetadata(); | |||
this.$x.emit('ParamsLoadedFromUrl', all, metadata); | |||
this.preventSpecialKey(all.query); | |||
if (!this.hasSpecialKeys) { |
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.
Just curious, I think I like the approach, but why are we preventing to emit the event if we have special keys instead of removing them, and send send it without them?
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.
Do you prefer to remove the special keys instead of the whole query? I mean, if you have this query round <hola> shirt
, it would be transformed into round shirt
and sent into the ParamsLoadedFromUrl
event.
I use this approach because, in the task description, I understood that we have to clean/remove the whole query. But for me makes more sense to remove only the special characters and the string inside them.
@@ -165,6 +175,18 @@ | |||
this.urlLoaded = true; | |||
} | |||
|
|||
/** | |||
* Prevents the user from either typing or pasting special characters in the url query. |
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 would say:
* Prevents the user from either typing or pasting special characters in the url query. | |
* Detects if the user is typing or pasting special/forbidden characters in the URL query |
*/ | ||
protected preventSpecialKey(query: string): void { | ||
if (/[<>]/.test(query ?? '')) { | ||
this.hasSpecialKeys = true; |
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.
Why storing this value in hasSpecialKeys
protected property instead of returning true/false in this function and then test it on line 167. I mean, I would change this function name to hasSpecialKeys
and then test:
if (!this.hasSpecialKeys(this query)) {
this.preventSpecialKey(all.query); | ||
if (!this.hasSpecialKeys) { |
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.preventSpecialKey(all.query); | |
if (!this.hasSpecialKeys) { | |
if (this.hasSpecialKeys(all.query)) { |
protected preventSpecialKey(query: string): void { | ||
if (/[<>]/.test(query ?? '')) { | ||
this.hasSpecialKeys = true; | ||
} |
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.
protected preventSpecialKey(query: string): void { | |
if (/[<>]/.test(query ?? '')) { | |
this.hasSpecialKeys = true; | |
} | |
protected hasSpecialKeys(query: string): void { | |
return (/[<>]/.test(query ?? '')) |
packages/x-components/src/x-modules/url/components/url-handler.vue
Fixed
Show resolved
Hide resolved
packages/x-components/src/x-modules/url/components/url-handler.vue
Outdated
Show resolved
Hide resolved
Hi! I will add this reference here, maybe it would be worth to give it a chance: https://github.com/braintree/sanitize-url/blob/main/src/index.ts |
We have an util in X https://github.com/empathyco/x/blob/main/packages/x-components/src/utils/sanitize.ts . I have already made the new approach. |
@@ -255,10 +256,13 @@ | |||
const urlKey = this.getUrlKey(name); | |||
if (urlSearchParams.has(urlKey)) { | |||
if (name in initialUrlState) { | |||
const urlValue = urlSearchParams.getAll(urlKey); | |||
let urlValue = urlSearchParams.getAll(urlKey); | |||
urlValue[0] = sanitize(urlValue[0]); |
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.
To be safe we should sanitize all values and not just the first one. The query only has one occurrence but potentially any parameter could be malicious.
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.
urlValue
is not only query
param, all URL params are passing one by one. [0]
is necessary because urlValue is an array of only one element, the param that is being checked.
For example, try to search for a query and select a filter, then write the special characters (<>
) in the filter and in the query.
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.
Sorry, I think I did not explain properly 😅 What I meant is that a url param can have more than one occurrence. Think about filters, you can select more than one filter at the same time. In that scenario you get an array with multiple entries like:
['categoryIds:12fad53d7', 'price:1600.0-2000.0']
so if you only pick the first element, you will fail to sanitize the other values.
This PR is being closed as we decided that we will solve this problem in the Archetype, not letting certain information to be applied to the website through |
Pull request template
To prevent a Cross-site Scripting attack, we must be careful with the characters we allow to type a query. We already have a function to avoid that when a user types in the search box. But we also need to check if there are any special characters when a query is typed through the URL.
Motivation and context
Type of change
What is the destination branch of this PR?
Main
How has this been tested?
Try to type a special character (
<>
) in the URL query.Checklist: