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

Prevent casting null or undefined UUID field value #13029

Merged
merged 3 commits into from
Feb 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions lib/schema/uuid.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const Binary = MongooseBuffer.Binary;

function hex2buffer(hex) {
// use buffer built-in function to convert from hex-string to buffer
const buff = Buffer.from(hex, 'hex');
const buff = hex && Buffer.from(hex, 'hex');
return buff;
}

Expand All @@ -36,7 +36,7 @@ function hex2buffer(hex) {

function binary2hex(buf) {
// use buffer built-in function to convert from buffer to hex-string
const hex = buf.toString('hex');
const hex = buf && buf.toString('hex');
return hex;
}

Expand Down Expand Up @@ -66,9 +66,8 @@ function stringToBinary(uuidStr) {
*/
function binaryToString(uuidBin) {
// i(hasezoey) dont quite know why, but "uuidBin" may sometimes also be the already processed string
let hex;
if (typeof uuidBin !== 'string') {
hex = binary2hex(uuidBin);
if (uuidBin && typeof uuidBin !== 'string') {
const hex = binary2hex(uuidBin);
const uuidStr = hex.substring(0, 8) + '-' + hex.substring(8, 8 + 4) + '-' + hex.substring(12, 12 + 4) + '-' + hex.substring(16, 16 + 4) + '-' + hex.substring(20, 20 + 12);
return uuidStr;
}
Expand Down