Skip to content

Commit 8bd9aad

Browse files
Danny docs sdk interactivity patch (#16422)
* Adding temporary logging to the server * Fix to host detection
1 parent c08015b commit 8bd9aad

File tree

1 file changed

+17
-31
lines changed

1 file changed

+17
-31
lines changed

docs-v2/pages/api/demo-connect/utils.js

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,23 @@ export const ALLOWED_ORIGINS = getAllowedOrigins();
5757
* Used to verify requests are coming from our frontend
5858
*/
5959
export function generateRequestToken(req) {
60-
const baseString = `${req.headers["user-agent"]}:${req.headers["host"]}:connect-demo`;
60+
// Try to use x-forwarded-host or origin's hostname instead of host to handle domain mapping
61+
// This handles the case where the request goes through a reverse proxy or domain mapping
62+
let effectiveHost = req.headers["host"];
63+
64+
// If there's an origin header, extract its hostname
65+
// as it will match the client's window.location.host
66+
if (req.headers.origin) {
67+
try {
68+
const originUrl = new URL(req.headers.origin);
69+
effectiveHost = originUrl.host;
70+
} catch (e) {
71+
// Fall back to host header if origin parsing fails
72+
console.log("Error parsing origin:", e.message);
73+
}
74+
}
75+
76+
const baseString = `${req.headers["user-agent"]}:${effectiveHost}:connect-demo`;
6177
return Buffer.from(baseString).toString("base64");
6278
}
6379

@@ -130,36 +146,6 @@ export function validateRequest(req, res, allowedMethod) {
130146
// Request token validation to prevent API automation
131147
const expectedToken = generateRequestToken(req);
132148

133-
// Debug logging to diagnose token validation issues
134-
console.log("Request headers:", {
135-
host: req.headers.host,
136-
origin: req.headers.origin,
137-
referer: req.headers.referer,
138-
// Truncate user-agent to avoid huge logs
139-
userAgent: req.headers["user-agent"]?.substring(0, 50) + "...",
140-
});
141-
142-
// Log token information
143-
console.log("Token comparison:", {
144-
received: requestToken,
145-
expected: expectedToken,
146-
matches: requestToken === expectedToken,
147-
});
148-
149-
// If there's a mismatch, decode both tokens to see what's different
150-
if (requestToken !== expectedToken) {
151-
try {
152-
const decodedReceived = Buffer.from(requestToken, "base64").toString();
153-
const decodedExpected = Buffer.from(expectedToken, "base64").toString();
154-
console.log("Decoded tokens:", {
155-
received: decodedReceived,
156-
expected: decodedExpected,
157-
});
158-
} catch (e) {
159-
console.log("Error decoding tokens:", e.message);
160-
}
161-
}
162-
163149
if (!requestToken || requestToken !== expectedToken) {
164150
return res.status(403).json({
165151
error: "Access denied",

0 commit comments

Comments
 (0)