Skip to content

Commit 50356d8

Browse files
Code cleanup
1 parent e80ca0b commit 50356d8

File tree

2 files changed

+40
-16
lines changed

2 files changed

+40
-16
lines changed

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

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ export function getAllowedOrigins() {
2121

2222
// Vercel preview deployment support - match any Vercel preview URL
2323
const vercelPreviewRegexes = [
24+
// Standard preview URLs: project-branch-username.vercel.app
2425
/^https:\/\/[a-zA-Z0-9-]+-[a-zA-Z0-9-]+-[a-zA-Z0-9-]+\.vercel\.app$/,
26+
// Shortened preview URLs: project-username.vercel.app
27+
/^https:\/\/[a-zA-Z0-9-]+-[a-zA-Z0-9-]+\.vercel\.app$/,
28+
// Any subdomain on vercel.app (most permissive)
29+
/^https:\/\/[a-zA-Z0-9-]+\.vercel\.app$/,
2530
];
2631

2732
return {
@@ -88,19 +93,38 @@ export function validateRequest(req, res, allowedMethod) {
8893
}
8994

9095
// Referer validation for docs context
91-
if (
92-
referer &&
93-
// Check if referer starts with any allowed origin
94-
!ALLOWED_ORIGINS.originsList.some((allowed) => referer.startsWith(allowed)) &&
95-
// Check if referer matches any regex pattern
96-
!ALLOWED_ORIGINS.regexPatterns.some((pattern) =>
97-
pattern.test(referer.split("/")[0] + "//" + referer.split("/")[2])) &&
98-
// Allow if it contains the docs path
99-
!referer.includes("/docs/connect/")
100-
) {
101-
return res.status(403).json({
102-
error: "Access denied",
103-
});
96+
if (referer) {
97+
// Extract the origin part of the referer URL (protocol + hostname)
98+
let refererOrigin;
99+
try {
100+
// Try to parse the referer as a URL
101+
const refererUrl = new URL(referer);
102+
refererOrigin = refererUrl.origin;
103+
} catch (e) {
104+
// If parsing fails, construct it manually
105+
const parts = referer.split("/");
106+
if (parts.length >= 3) {
107+
refererOrigin = parts[0] + "//" + parts[2];
108+
}
109+
}
110+
111+
// Check if the referer origin is allowed
112+
const isRefererAllowed =
113+
// Check if referer matches allowed origins list
114+
ALLOWED_ORIGINS.originsList.some((allowed) => referer.startsWith(allowed)) ||
115+
// Check if referer origin matches any regex pattern
116+
(refererOrigin &&
117+
ALLOWED_ORIGINS.regexPatterns.some((pattern) =>
118+
pattern.test(refererOrigin))
119+
) ||
120+
// Allow if it contains the docs path
121+
referer.includes("/docs/connect/");
122+
123+
if (!isRefererAllowed) {
124+
return res.status(403).json({
125+
error: "Access denied",
126+
});
127+
}
104128
}
105129

106130
// Request token validation to prevent API automation

docs-v2/styles/globals.css

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
/* Import custom Prism styles */
2+
@import './prism-custom.css';
3+
14
@tailwind base;
25
@tailwind components;
36
@tailwind utilities;
47

5-
/* Import custom Prism styles */
6-
@import './prism-custom.css';
7-
88
:not(pre,h1,h2,h3,h4,h5,h6,td) > code::before, :not(pre,h1,h2,h3,h4,h5,h6,td) > code::after {
99
content: '`';
1010
}

0 commit comments

Comments
 (0)