rogerc / file-stream-rotator

NodeJS file stream rotator
MIT License
143 stars 69 forks source link

Bug: Filename RegEx causing issues with unescaped characters paths #69

Closed partynikko closed 2 years ago

partynikko commented 4 years ago

There are scenarios where the FileStreamRotator will crash due to the following line: https://github.com/rogerc/file-stream-rotator/blob/d48e1ec847bd27bc7896462d0e56ba19f07c581e/FileStreamRotator.js#L483

Since the filename comparison uses a dynamic RegEx there are scenarios where the .match will crash due to unescaped characters in the path.

For example consider a path where the ( characters exists which is an opening of a capture group. If no closing exists, the script will crash with an syntax error regarding not closing a capture group.

const lastEntry = "some/path";
const tlog = "some/other/path(";

lastEntry.match(tlog); // Uncaught SyntaxError: Invalid regular expression: /some/other/path(/: Unterminated group

Is there a reason why this has to use .match? Wouldn't it suffice with something like:

lastEntry.includes(tlog);

// or even

lastEntry === tlog;

Let me know what you think and I'll create a PR for it :)

Thanks BR

rogerc commented 2 years ago

reason to use match is because the file might contain, a extension and iteration number if using size as a rotation condition and only should match the beginning of the filename. if it matches then looks for the iteration number in that filename to set the iteration to use in the next file.

Plan to rewrite the module for latest JS in the coming months and I'll escape the filename when doing the matching but since this has not been raised as an issue by others, I'll leave it as is for now.

Thanks for raising issue.