Skip to content

Code Bugs Repair

hkevint edited this page Apr 12, 2025 · 3 revisions

🛠️ Static Analysis Bug Fixes

As part of Sprint 4, we identified and resolved several code issues flagged by static analysis tools such as SonarQube and ESLint. This document outlines five key bug fixes applied across the ChatHaven codebase. Each fix includes the issue description, the linter tool used, before/after code, and associated commit info.


✅ Fix 1: Bubble Message Fix and UI Optimization

File: MemberDM.jsx
Tool: SonarQube
Issue ID: SQ-UI-102
Problem: Message bubbles used inline styling, causing inconsistent layout across devices and violating accessibility standards.

Before

<div style={{ marginBottom: "8px" }}>
  <div style={{ fontSize: "14px" }}>{msg.message}</div>
</div>

After

<div className="message-bubble">
  <div className="message-text">{msg.message}</div>
</div>

Resolution: Refactored message styling into reusable CSS classes.
Commit: bf34ac1 - Refactored message bubble styles into reusable classes


Fix 2: Login/Signup Password Toggle and Input Validation

File: Login.jsx, Signup.jsx
Tool: ESLint
Issue: The password visibility toggle only changed to text but never back to password. Input validation was also hardcoded.

Before

<img onClick={(e) => {
  const passwordField = e.target.previousSibling;
  passwordField.type = "text";
}} />

After

<img onClick={(e) => {
  const passwordField = e.target.previousSibling;
  passwordField.type = 
    passwordField.type === "password" ? "text" : "password";
}} />

Resolution: Toggle now works both ways. Validation logic modularized.
Commit: 98c7be3 - Improved password toggle logic and abstracted validation


Fix 3: Nested Quoting Crash in Direct Messages

File: MemberDM.jsx
Tool: SonarQube
Issue ID: SQ-JSON-205
Problem: Replies that quoted messages with quotes caused JSON parsing to fail, breaking rendering.

Before

function parseChatMessage(message) {
  return JSON.parse(message);
}

After

function parseChatMessage(message) {
  try {
    const parsed = JSON.parse(message);
    if (parsed && parsed.reply && parsed.message) {
      return { text: parsed.message, replyData: parsed.reply };
    }
  } catch (e) {
    // fallback
  }
  return { text: message, replyData: null };
}

Resolution: Safer parsing with fallback for raw strings.
Commit: 34f5de2 - Improved quoting logic and crash recovery for nested replies


Fix 4: Last Seen Timestamp & Message Status Inconsistency

File: MembersList.jsx
Tool: SonarQube
Issue ID: SQ-ASYNC-308
Problem: Race conditions and repeated state updates led to unreliable "away/online/offline" status and last_seen data.

Before

window.addEventListener("mousemove", handleActivity);
window.addEventListener("keydown", handleActivity);

After

let debounceTimer;
const handleActivity = () => {
  clearTimeout(debounceTimer);
  setStatus("online");
  debounceTimer = setTimeout(() => setStatus("away"), 60000);
};

window.addEventListener("mousemove", handleActivity);
window.addEventListener("keydown", handleActivity);

Resolution: Introduced debouncing to prevent redundant updates.
Commit: 7ad9f1b - Debounced activity tracking and fixed last seen sync


Fix 5: Quoting of Quotes Cleanup and Media Sanitization

File: MemberDM.jsx, ChannelDM.jsx
Tool: SonarQube
Issue ID: SQ-SEC-415
Problem: Messages quoted multiple times nested HTML unnecessarily, making them unreadable. Potential XSS if media was unsanitized.

Before

reply: {
  message: replyMessage.message,
  sender: friendProfile.username
}

After

reply: {
  message: originalMessage.replace(/(^"+|"+$)/g, ""),
  sender: replyMessage.user_id === currentUser.id ? "You" : friendProfile.username || "Unknown",
  senderId: replyMessage.user_id
}

Resolution: Strips redundant quotation marks and escapes media content.
Commit: c0b8ef4 - Streamlined quoting logic and added media sanitization


Summary of Commits

Commit Hash Description
bf34ac1 Refactored message bubble styles into reusable classes
98c7be3 Improved password toggle logic and abstracted validation
34f5de2 Improved quoting logic and crash recovery for nested replies
7ad9f1b Debounced activity tracking and fixed last seen sync
c0b8ef4 Streamlined quoting logic and added media sanitization