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

feat: prevent special characters from url #1266

Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@
*/
protected isPagePersisted = false;

/**
* Flag to know if the query from the url has special characters.
*
* @internal
*/
protected hasSpecialKeys = false;

/**
* Computed to know which params we must get from URL. It gets the params names from the initial
* state, to get all default params names, and also from the `$attrs` to get the extra params
Expand Down Expand Up @@ -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) {
Copy link
Contributor

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?

Copy link
Contributor Author

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 ParamsLoadedFromUrlevent.

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.

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
this.preventSpecialKey(all.query);
if (!this.hasSpecialKeys) {
if (this.hasSpecialKeys(all.query)) {

this.$x.emit('ParamsLoadedFromUrl', all, metadata);
}
this.$x.emit('ExtraParamsLoadedFromUrl', extra, metadata);
// TODO: Move this logic from here.
if (all.query) {
Expand All @@ -165,6 +175,18 @@
this.urlLoaded = true;
}

/**
* Prevents the user from either typing or pasting special characters in the url query.
Copy link
Contributor

Choose a reason for hiding this comment

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

I would say:

Suggested change
* 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

*
* @internal
* @param query - Query from the url that will be checked for special characters.
*/
protected preventSpecialKey(query: string): void {
if (/[<>]/.test(query ?? '')) {
this.hasSpecialKeys = true;
Copy link
Contributor

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)) {

}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
protected preventSpecialKey(query: string): void {
if (/[<>]/.test(query ?? '')) {
this.hasSpecialKeys = true;
}
protected hasSpecialKeys(query: string): void {
return (/[<>]/.test(query ?? ''))

}

/**
* Creates the wire metadata to include in every emitted {@link XEvent}.
*
Expand Down