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

Ensure path is a string before replacement #325

Closed
wants to merge 1 commit into from

Conversation

NewEraCracker
Copy link

Fixes: expressjs/express#5955

Ensure the path variable is explicitly converted to a string before applying regex replacements to prevent potential errors with non-string inputs.

This change enhances robustness and correctness in handling various input types.

@NewEraCracker NewEraCracker changed the title Update index.js (ensure path is a string before replacement) Ensure path is a string before replacement Sep 12, 2024
@alewitt2
Copy link

alewitt2 commented Sep 12, 2024

what about when path is undefined? shouldn't undefined be returned in that case?

if path is undefined, String(path) would result in a literal string 'undefined' which doesn't seem desirable here

@NewEraCracker
Copy link
Author

It was being silently coerced to string before:
path = ('^' + path + (strict ? '' : path[path.length - 1] === '/' ? '?' : '/?'))

Feel free to think about other fixes to avoid crash.

@alewitt2
Copy link

alewitt2 commented Sep 12, 2024

i guess the question is what's the desired behavior then. is it acceptable to return undefined for path if its not a string?

path = path?.replace(...)

@blakeembrey
Copy link
Member

I'm going to close this out. Although the library previously accepted non-strings accidentally here, it wasn't intentional or a guaranteed part of the API, and it looks like it may have actually hidden real bugs from users and upstream libraries like Express. Thanks for the attempt at patching though!

@blakeembrey
Copy link
Member

The 0.1.x branch is fairly clear in the README https://github.com/pillarjs/path-to-regexp/tree/0.1.x?tab=readme-ov-file#pathtoregexppath-keys-options

A string in the express format, an array of such strings, or a regular expression

@az-faro
Copy link

az-faro commented Sep 12, 2024

Maybe you could throw an exception saying it "has to be a string, an array of strings or a regex" if it's neither of them? That way it's crystal clear if you use the library incorrectly. Whereas as it is now, one could mistake this for a bug in the library instead of a mistake on your own side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants