Fix path detection can crash app (if req.originalUrl is not set) #137
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
hi there,
Background
the following line crashes if
req.originalUrl
is not set (it is technically possible even though unexpected in the context ofexpress
!):i18next-express-middleware/src/languageLookups/path.js
Line 16 in 095a5a9
having encountered that I decided to try and set the
lookupFromPathIndex
option toundefined
, but that is not possible due to the default value being0
in combination with how the option defaults are applied (undefined
values are always overwritten) i.e.setting of option defaults happens here:
i18next-express-middleware/src/LanguageDetector.js
Line 33 in 095a5a9
logic for merging options with defaults is here: https://github.com/i18next/i18next-express-middleware
/blob/095a5a926a2ad9e6dcaebc2a22cbb42780b4003e/src/utils.js#L26
"Solution"
this PR :-) the changes are as follows:
lookupFromPathIndex
so the "by index" lookup is only attempted if the value is a number - this allows people to turn it off by setting the option tofalse
req.originalUrl
- to avoid potential TypeErrorsreq.params
- to avoid potential TypeErrors (because I'm paranoid)number 3 in the list is not directly related to the issue I had but I figured "better safe than sorry"