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
31 changes: 31 additions & 0 deletions docs-v2/pages/api/demo-connect/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,37 @@ export function validateRequest(req, res, allowedMethod) {

// Request token validation to prevent API automation
const expectedToken = generateRequestToken(req);

// Debug logging to diagnose token validation issues
console.log("Request headers:", {
host: req.headers.host,
origin: req.headers.origin,
referer: req.headers.referer,
// Truncate user-agent to avoid huge logs
userAgent: req.headers["user-agent"]?.substring(0, 50) + "...",
});

// Log token information
console.log("Token comparison:", {
received: requestToken,
expected: expectedToken,
matches: requestToken === expectedToken,
});

// If there's a mismatch, decode both tokens to see what's different
if (requestToken !== expectedToken) {
try {
const decodedReceived = Buffer.from(requestToken, "base64").toString();
const decodedExpected = Buffer.from(expectedToken, "base64").toString();
console.log("Decoded tokens:", {
received: decodedReceived,
expected: decodedExpected,
});
} catch (e) {
console.log("Error decoding tokens:", e.message);
}
}
Comment on lines +149 to +161
Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Verification agent

🧩 Analysis chain

Good approach for diagnosing token mismatches.

The approach of decoding tokens when they don't match provides valuable debugging information and includes proper error handling. The truncation of user-agent also shows good attention to log size management.

Since this is labeled as "temporary logging" in the PR title, please consider adding a TODO comment indicating when these logs should be removed, or implementing a more permanent solution with proper log levels:


🏁 Script executed:

#!/bin/bash
# Check if there are any TODO comments about removing logs
grep -n "TODO.*remov.*log" docs-v2/pages/api/demo-connect/utils.js || echo "No removal plan found for temporary logs"

Length of output: 159


Add a removal plan for temporary debug logs

I didn’t find any TODO or similar comment indicating when to remove these temporary logs. Since this PR is explicitly adding diagnostic logging, please:

  • Add a TODO (or FIXME) comment in docs-v2/pages/api/demo-connect/utils.js noting when or under what condition these logs should be removed.
  • Or replace the console.log calls with a proper logger at a debug level so they can be toggled off in production.

Example:

   // If there's a mismatch, decode both tokens to see what's different
   if (requestToken !== expectedToken) {
     try {
-      const decodedReceived = Buffer.from(requestToken, "base64").toString();
+      // TODO: Remove or lower log level once token mismatch issue is resolved
+      const decodedReceived = Buffer.from(requestToken, "base64").toString();
       const decodedExpected = Buffer.from(expectedToken, "base64").toString();
       console.log("Decoded tokens:", {
         received: decodedReceived,
         expected: decodedExpected,
       });
     } catch (e) {
-      console.log("Error decoding tokens:", e.message);
+      // TODO: Remove or lower log level once token mismatch issue is resolved
+      console.log("Error decoding tokens:", e.message);
     }
   }

Let’s ensure these logs are either removed or controlled by log levels before merging.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// If there's a mismatch, decode both tokens to see what's different
if (requestToken !== expectedToken) {
try {
const decodedReceived = Buffer.from(requestToken, "base64").toString();
const decodedExpected = Buffer.from(expectedToken, "base64").toString();
console.log("Decoded tokens:", {
received: decodedReceived,
expected: decodedExpected,
});
} catch (e) {
console.log("Error decoding tokens:", e.message);
}
}
// If there's a mismatch, decode both tokens to see what's different
if (requestToken !== expectedToken) {
try {
// TODO: Remove or lower log level once token mismatch issue is resolved
const decodedReceived = Buffer.from(requestToken, "base64").toString();
const decodedExpected = Buffer.from(expectedToken, "base64").toString();
console.log("Decoded tokens:", {
received: decodedReceived,
expected: decodedExpected,
});
} catch (e) {
// TODO: Remove or lower log level once token mismatch issue is resolved
console.log("Error decoding tokens:", e.message);
}
}

Comment on lines +133 to +161
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider security implications of debug logging sensitive information.

While these debug logs are valuable for diagnosing token validation issues, they contain potentially sensitive information such as request headers and tokens. Consider:

  1. Adding a toggle mechanism based on environment variables to enable/disable logging
  2. Using a proper logging framework with configurable log levels instead of console.log
  3. Adding a comment indicating when these logs should be removed or under what conditions they should remain active
+ // Environment-based logging control
+ const ENABLE_DEBUG_LOGGING = process.env.ENABLE_DEBUG_LOGGING === 'true';

  // Debug logging to diagnose token validation issues
- console.log("Request headers:", {
-   host: req.headers.host,
-   origin: req.headers.origin,
-   referer: req.headers.referer,
-   // Truncate user-agent to avoid huge logs
-   userAgent: req.headers["user-agent"]?.substring(0, 50) + "...",
- });
+ if (ENABLE_DEBUG_LOGGING) {
+   console.log("Request headers:", {
+     host: req.headers.host, 
+     origin: req.headers.origin,
+     referer: req.headers.referer,
+     // Truncate user-agent to avoid huge logs
+     userAgent: req.headers["user-agent"]?.substring(0, 50) + "...",
+   });

Apply similar conditional logging to the other log statements below as well.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// Debug logging to diagnose token validation issues
console.log("Request headers:", {
host: req.headers.host,
origin: req.headers.origin,
referer: req.headers.referer,
// Truncate user-agent to avoid huge logs
userAgent: req.headers["user-agent"]?.substring(0, 50) + "...",
});
// Log token information
console.log("Token comparison:", {
received: requestToken,
expected: expectedToken,
matches: requestToken === expectedToken,
});
// If there's a mismatch, decode both tokens to see what's different
if (requestToken !== expectedToken) {
try {
const decodedReceived = Buffer.from(requestToken, "base64").toString();
const decodedExpected = Buffer.from(expectedToken, "base64").toString();
console.log("Decoded tokens:", {
received: decodedReceived,
expected: decodedExpected,
});
} catch (e) {
console.log("Error decoding tokens:", e.message);
}
}
// Environment-based logging control
const ENABLE_DEBUG_LOGGING = process.env.ENABLE_DEBUG_LOGGING === 'true';
// Debug logging to diagnose token validation issues
if (ENABLE_DEBUG_LOGGING) {
console.log("Request headers:", {
host: req.headers.host,
origin: req.headers.origin,
referer: req.headers.referer,
// Truncate user-agent to avoid huge logs
userAgent: req.headers["user-agent"]?.substring(0, 50) + "...",
});
}
// Log token information
if (ENABLE_DEBUG_LOGGING) {
console.log("Token comparison:", {
received: requestToken,
expected: expectedToken,
matches: requestToken === expectedToken,
});
}
// If there's a mismatch, decode both tokens to see what's different
if (requestToken !== expectedToken) {
try {
const decodedReceived = Buffer.from(requestToken, "base64").toString();
const decodedExpected = Buffer.from(expectedToken, "base64").toString();
if (ENABLE_DEBUG_LOGGING) {
console.log("Decoded tokens:", {
received: decodedReceived,
expected: decodedExpected,
});
}
} catch (e) {
if (ENABLE_DEBUG_LOGGING) {
console.log("Error decoding tokens:", e.message);
}
}
}


if (!requestToken || requestToken !== expectedToken) {
return res.status(403).json({
error: "Access denied",
Expand Down
Loading