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

fix: only hexadecimal strings allowed if length of string is 24 #10010

Merged
merged 3 commits into from
Mar 15, 2021

Conversation

IslandRhythms
Copy link
Collaborator

No description provided.

@IslandRhythms IslandRhythms requested a review from vkarpov15 March 10, 2021 21:06
@IslandRhythms IslandRhythms linked an issue Mar 10, 2021 that may be closed by this pull request
@@ -812,5 +812,26 @@ describe('mongoose module:', function() {
assert.ok(!movie.genre);
});
});
it('should prevent non-hexadecimal strings (gh-9996)', function() {
return co(function* () {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This test works just fine, I'd however change a few things about it to make the intention clear.
A) We don't need the connection, nor the saving here, we can get rid of them and test only isValidObjectId(...) since the changes are related to it.
B) I'd write explicit assertions instead of implicit ones (such as saving here), and show exactly why the bad string is bad, I need to read until the third from last character (t) to figure out why. Instead we could write something obviously bad such as 'z'.repeat(24).

Here's an example:

const badIdString = 'z'.repeat(24);
assert.deepStrictEqual(mongoose.isValidObjectId(badIdString), false);

const goodIdString = '1'.repeat(12);
assert.deepStrictEqual(mongoose.isValidObjectId(goodIdString), true);

Copy link
Collaborator

Choose a reason for hiding this comment

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

I agree with @AbdelrahmanHafez 's suggestions 👍

if (typeof v === 'string' && v.length === 12) {
return true;
}
if (typeof v === 'string' && v.length === 24 && /^[a-f0-9]*$/.test(v)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

You also need to add a similar check on line 992, there's another place where we check length === 24

@IslandRhythms IslandRhythms requested a review from vkarpov15 March 11, 2021 15:32
Copy link
Collaborator

@vkarpov15 vkarpov15 left a comment

Choose a reason for hiding this comment

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

Nice 👍

@vkarpov15 vkarpov15 added this to the 5.12.1 milestone Mar 15, 2021
@vkarpov15 vkarpov15 merged commit dc8d5b8 into master Mar 15, 2021
@vkarpov15 vkarpov15 deleted the gh-9996 branch March 15, 2021 16:56
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.

isValidObjectId giving false negative for certain ObjectIds
3 participants