Skip to content

Commit

Permalink
Remove unsafe regex in trim() function
Browse files Browse the repository at this point in the history
`trim()` function contains a regular expression that is vulnerable to ReDoS but was uncaught by `safe-regex` module.
  • Loading branch information
faisalman committed Jan 22, 2023
1 parent a886604 commit a6140a1
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/ua-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
},
trim = function (str, len) {
if (typeof(str) === STR_TYPE) {
str = str.replace(/^\s\s*/, EMPTY).replace(/\s\s*$/, EMPTY);
str = str.replace(/^\s\s*/, EMPTY);
return typeof(len) === UNDEF_TYPE ? str : str.substring(0, UA_MAX_LENGTH);
}
};
Expand Down

2 comments on commit a6140a1

@kekkis
Copy link

@kekkis kekkis commented on a6140a1 Jan 27, 2023

Choose a reason for hiding this comment

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

Why not use String.prototype.trim() instead of the regex? It would seem to me that right-padded strings are left untrimmed with this change, but using the trim() method on strings would solve the issue.

@faisalman
Copy link
Owner Author

Choose a reason for hiding this comment

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

@kekkis mostly for old browser support. The code is actually meant to be removed since right-padded strings will be trimmed in the next line regardless.

Please sign in to comment.