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
21 changes: 21 additions & 0 deletions backend/controllers/directMessage.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,27 @@ const sendMessage = async (req, res) => {
return res.status(400).json({ error: 'Receiver ID and content are required' });
}

// Enforce a maximum message length to prevent oversized document storage
// and disproportionately large inbox payloads. The global express.json
// limit (100 KB) is too permissive for a single chat message.
const MAX_CONTENT_LENGTH = 2000;
if (typeof content !== 'string' || content.length > MAX_CONTENT_LENGTH) {
return res.status(400).json({
error: `Message content must not exceed ${MAX_CONTENT_LENGTH} characters`,
});
}

// Validate fileUrl when present: only allow http/https scheme to prevent
// javascript: URI injection that a frontend might render as a clickable link.
if (fileUrl && !/^https?:\/\//i.test(fileUrl)) {
return res.status(400).json({ error: 'fileUrl must be an http or https URL' });
}

// fileName may not contain path separators.
if (fileName && /[/\\]/.test(fileName)) {
return res.status(400).json({ error: 'Invalid fileName' });
}

// Check if receiver exists
const receiver = await User.findById(receiverId);
if (!receiver) {
Expand Down
Loading