-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathr.ts
More file actions
172 lines (146 loc) · 5.57 KB
/
Copy pathr.ts
File metadata and controls
172 lines (146 loc) · 5.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import fs from 'fs';
import path from 'path';
import type { VercelRequest, VercelResponse } from '@vercel/node';
const RATE_LIMIT_WINDOW_MS = 60 * 1000; // 1 minute
const RATE_LIMIT_MAX = 60;
const rateLimitMap = new Map<string, { count: number; resetTime: number }>();
function pruneExpiredRateLimitEntries(now: number): void {
for (const [ip, entry] of rateLimitMap.entries()) {
if (now > entry.resetTime) {
rateLimitMap.delete(ip);
}
}
}
function isRateLimited(ip: string): boolean {
const now = Date.now();
pruneExpiredRateLimitEntries(now);
const entry = rateLimitMap.get(ip);
if (!entry || now > entry.resetTime) {
rateLimitMap.set(ip, { count: 1, resetTime: now + RATE_LIMIT_WINDOW_MS });
return false;
}
entry.count++;
return entry.count > RATE_LIMIT_MAX;
}
interface VersionInfo {
latest: string;
majorVersions?: Record<string, { latest: string }>;
versions?: Record<string, { status: string; major: string }>;
}
function getVersionPath(version: string, versionInfo: VersionInfo): string {
const versionData = versionInfo.versions?.[version];
if (versionData) {
return `v${versionData.major}/${version}`;
}
return `v1/${version}`;
}
function getBasePath(): string {
const paths = [
path.join(process.cwd(), 'dist', 'r'),
path.join(process.cwd(), 'r'),
path.join(process.cwd(), 'docs-site', 'dist', 'r'),
path.join(process.cwd(), 'docs-site', 'public', 'r'),
];
for (const p of paths) {
if (fs.existsSync(p)) {
return p;
}
}
return paths[0]!;
}
function getVersionInfo(basePath: string): VersionInfo {
const versionsPath = path.join(basePath, 'versions.json');
if (!fs.existsSync(versionsPath)) {
throw new Error('versions.json not found. Run the build to generate it.');
}
try {
return JSON.parse(fs.readFileSync(versionsPath, 'utf-8'));
} catch (error) {
throw new Error(`Failed to parse versions.json: ${error}`);
}
}
function sendJson(res: VercelResponse, content: string): void {
res.setHeader('Content-Type', 'application/json');
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Cache-Control', 'public, max-age=3600');
res.setHeader('Deprecation', '@1798761599');
res.setHeader('Sunset', 'Thu, 31 Dec 2026 23:59:59 GMT');
res.setHeader('Link', '<https://github.com/auth0/auth0-ui-components>; rel="successor-version"');
res.send(content);
}
export default function handler(req: VercelRequest, res: VercelResponse) {
const clientIp = (req.headers['x-forwarded-for'] as string)?.split(',')[0]?.trim() || 'unknown';
if (isRateLimited(clientIp)) {
return res.status(429).json({ error: 'Too Many Requests', message: 'Rate limit exceeded' });
}
const { file } = req.query;
const fileName = Array.isArray(file) ? file.join('/') : file || '';
if (!fileName) {
return res.status(400).json({ error: 'Bad Request', message: 'File path required' });
}
const normalizedFileName = path.normalize(fileName).replace(/^(\.\.([\\/]|$))+/, '');
if (normalizedFileName !== fileName || normalizedFileName.includes('..')) {
return res.status(400).json({ error: 'Invalid file path' });
}
const basePath = getBasePath();
const versionInfo = getVersionInfo(basePath);
const rawVersionParam = req.query.version;
const versionParam = typeof rawVersionParam === 'string' ? rawVersionParam : undefined;
const rootFilePath = path.join(basePath, normalizedFileName);
if (!versionParam && fs.existsSync(rootFilePath)) {
try {
sendJson(res, fs.readFileSync(rootFilePath, 'utf-8'));
} catch (error) {
console.error('Failed to read registry file: %s', rootFilePath);
res
.status(500)
.json({ error: 'Internal Server Error', message: 'Failed to read registry file' });
}
return;
}
let versionPath: string;
if (!versionParam || versionParam === 'latest') {
versionPath = getVersionPath(versionInfo.latest, versionInfo);
} else if (versionParam.startsWith('v') && versionParam.includes('/')) {
versionPath = versionParam;
} else if (versionParam.startsWith('v') && !versionParam.includes('/')) {
const majorVersion = versionInfo.majorVersions?.[versionParam]?.latest;
versionPath = majorVersion
? getVersionPath(majorVersion, versionInfo)
: getVersionPath(versionInfo.latest, versionInfo);
} else {
versionPath = getVersionPath(versionParam, versionInfo);
}
const normalizedVersionPath = path.normalize(versionPath);
if (
normalizedVersionPath !== versionPath ||
normalizedVersionPath.includes('..') ||
path.isAbsolute(normalizedVersionPath)
) {
return res.status(400).json({ error: 'Invalid version' });
}
const baseDir = path.resolve(basePath, versionPath);
if (!baseDir.startsWith(basePath + path.sep) && baseDir !== basePath) {
return res.status(403).json({ error: 'Access denied' });
}
const versionedPath = path.resolve(baseDir, normalizedFileName);
if (!versionedPath.startsWith(baseDir + path.sep) && versionedPath !== baseDir) {
return res.status(403).json({ error: 'Access denied' });
}
if (fs.existsSync(versionedPath)) {
try {
sendJson(res, fs.readFileSync(versionedPath, 'utf-8'));
} catch (error) {
console.error('Failed to read registry file: %s', versionedPath);
res
.status(500)
.json({ error: 'Internal Server Error', message: 'Failed to read registry file' });
}
return;
}
return res.status(404).json({
error: 'Not Found',
message: `Component "${normalizedFileName}" does not exist in version "${versionPath}"`,
hint: 'Check available versions or component name',
});
}