Skip to content

Commit 4bb055f

Browse files
committed
Meta tweaks
1 parent e93d9c8 commit 4bb055f

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"semantic"
3434
],
3535
"devDependencies": {
36-
"ava": "^4.2.0",
36+
"ava": "^4.3.0",
3737
"tsd": "^0.20.0",
3838
"xo": "^0.49.0",
3939
"semver": "^7.3.7"

readme.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
55
## Install
66

7-
```
8-
$ npm install semver-regex
7+
```sh
8+
npm install semver-regex
99
```
1010

1111
## Usage
@@ -26,7 +26,9 @@ semverRegex().exec('unicorn 1.0.0 rainbow')[0];
2626
//=> ['1.0.0', '2.1.3']
2727
```
2828

29-
**Note:** For versions coming from user-input, you are recommended to truncate the string to a sensible length to prevent abuse. For example, 100 length.
29+
## Important
30+
31+
If you run the regex against untrusted user input, it's recommended to truncate the string to a sensible length (for example, 100). And if you use this in a server context, you should also [give it a timeout](https://github.com/sindresorhus/super-regex).
3032

3133
## Related
3234

test.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,28 +192,33 @@ test('invalid version does not cause catatrophic backtracking', t => {
192192
for (let index = 1; index <= 100; index++) {
193193
const start = Date.now();
194194
const shuffle = array => array.sort(() => Math.random() - 0.5);
195+
195196
// Adapted from https://gist.github.com/6174/6062387
196-
const rndstr = (() => {
197+
const randomString = (() => {
197198
const gen = (min, max) => max++ && Array.from({length: max - min}).map((s, i) => String.fromCodePoint(min + i));
199+
198200
const sets = {
199201
num: gen(48, 57),
200202
alphaLower: gen(97, 122),
201203
alphaUpper: gen(65, 90),
202204
special: [...'~!@#$%^&*()_+-=[]{}|;:\'",./<>?'],
203205
};
206+
204207
function * iter(length, set) {
205208
if (set.length === 0) {
206209
set = Object.values(sets).flat();
207210
}
208211

209-
for (let i = 0; i < length; i++) {
212+
for (let index = 0; index < length; index++) {
210213
yield set[Math.trunc(Math.random() * set.length)];
211214
}
212215
}
213216

214217
return Object.assign(((length, ...set) => [...iter(length, set.flat())].join('')), sets);
215218
})();
216-
const fuzz = Array.from({length: 100}).map(() => rndstr(100 * Math.random(), rndstr.alphaUpper, rndstr.special, rndstr.alphaLower, rndstr.num));
219+
220+
const fuzz = Array.from({length: 100}).map(() => randomString(100 * Math.random(), randomString.alphaUpper, randomString.special, randomString.alphaLower, randomString.num));
221+
217222
const fixture = shuffle(Array.from({length: index}).map(() => [validStrings, invalidStrings, fuzz]).flat(2)).join(' ');
218223

219224
semverRegex().test(fixture);

0 commit comments

Comments
 (0)