Skip to content
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
48 changes: 27 additions & 21 deletions packages/common/src/utils/input.validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,36 +339,42 @@ module.exports.aggregateSchema = z.object({
.min(1, "Pipeline must contain at least one stage."),
});

module.exports.sanitize = (obj) => {
const clean = {};
for (const key in obj) {
if (!key.startsWith("$")) {
clean[key] = obj[key];
}
const BLOCKED_KEYS = new Set(['__proto__', 'constructor', 'prototype']);

const isDangerousKey = (key) =>
key.startsWith('$') || BLOCKED_KEYS.has(key);

const sanitizeValue = (value) => {
if (Array.isArray(value)) return value.map(sanitizeValue);

if (value !== null && typeof value === 'object') {
return sanitize(value);
}
return clean;
};

module.exports.sanitizeObjectId = (value) => {
if (typeof value !== "string") return null;
const normalized = value.trim();
if (!normalized) return null;
return mongoose.Types.ObjectId.isValid(normalized) ? normalized : null;
return value;
};

module.exports.sanitizeNonEmptyString = (value, options = {}) => {
if (typeof value !== "string") return null;
const sanitize = (obj) => {
if (Array.isArray(obj)) {
return obj.map(sanitizeValue);
}

const { maxLength = 1024, allowNullByte = false } = options;
const normalized = value.trim();
if (obj === null || typeof obj !== 'object') {
return obj;
}
const clean = {};

if (!normalized) return null;
if (normalized.length > maxLength) return null;
if (!allowNullByte && normalized.includes("\0")) return null;
for (const key of Object.keys(obj)) {
if (!isDangerousKey(key)) {
clean[key] = sanitizeValue(obj[key]);
}
}

return normalized;
return clean;
};

module.exports.sanitize = sanitize;

const emptyToUndefined = z.preprocess(
(val) => (val === "" || val === null ? undefined : val),
z.string().optional(),
Expand Down
Loading