-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathcreate.js
228 lines (196 loc) · 6.49 KB
/
create.js
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import { doc, setDoc } from "firebase/firestore";
import { db } from "../../utils/firebase";
import CryptoJS from "crypto-js";
import { StatusCodes } from "http-status-codes";
const regex = /^(https?|ftp|magnet):(?:\/\/[^\s/$.?#].[^\s]*|[^\s]*)$/;
const slugRegex = /^[a-z0-9](-?[a-z0-9])*$/;
const MAX_REDIRECTS = 2; // Maximum allowed redirects
// Helper function to check JavaScript-based or meta redirects in page content
function checkForJSRedirect(content) {
const jsRedirectRegex = /window\.location\.href\s*=\s*['"]([^'"]+)['"]/;
const metaRedirectRegex =
/<meta[^>]+http-equiv=["']refresh["'][^>]+url=([^'"]+)/i;
const jsMatch = content.match(jsRedirectRegex);
const metaMatch = content.match(metaRedirectRegex);
if (jsMatch && jsMatch[1]) {
return jsMatch[1];
}
if (metaMatch && metaMatch[1]) {
return metaMatch[1];
}
return null;
}
async function fetchWithRedirects(url, maxRedirects) {
let currentUrl = url;
console.log("🚀 => currentUrl:", currentUrl);
let redirectCount = 0;
console.log("🚀 => redirectCount:", redirectCount);
while (redirectCount < maxRedirects) {
const response = await fetch(currentUrl, {
redirect: "follow", // Follow redirects
});
if (response.redirected && response.url !== currentUrl) {
// Check if the response was a redirect
currentUrl = response.url;
redirectCount++;
console.log(`HTTP Redirect ${redirectCount} to: ${currentUrl}`);
// const redirectCount = response.url;
// console.log("🚀 => redirectCount:", redirectCount);
// if (redirectCount > MAX_REDIRECTS) {
// return res.status(StatusCodes.BAD_REQUEST).json({
// slug,
// message: "Link has too many redirects and may be suspicious.",
// });
// } else {
// break;
// }
} else {
// No HTTP redirect, return the response to check page content
const pageContent = await response.text();
console.log("🚀 => pageContent:", pageContent);
const jsRedirect = checkForJSRedirect(pageContent);
console.log("🚀 => jsRedirect:", jsRedirect);
if (jsRedirect) {
redirectCount++;
console.log(`JS Redirect ${redirectCount} to: ${currentUrl}`);
} else {
// No further redirects, return the final response
return { response, redirectCount };
}
}
}
// Return the final response after redirect threshold or no more redirects
return { response: await fetch(currentUrl), redirectCount };
}
export default async function handler(req, res) {
const { slug, link, password } = req.body;
console.log("🚀 => body:", req.body);
const apiKey = process.env.SAFE_BROWSING_API_KEY;
if (!apiKey) {
return res
.status(StatusCodes.INTERNAL_SERVER_ERROR)
.json({ message: "API not available" });
}
const collectionName =
process.env.NODE_ENV === "production" ? "links" : "testLinks";
// check if link is valid
if (link.length < 1) {
return res
.status(StatusCodes.BAD_REQUEST)
.json({ slug, message: "You entered an invalid link" });
}
if (!regex.test(link)) {
return res.status(StatusCodes.BAD_REQUEST).json({
slug,
message:
"Please make sure your link starts with http://, https://, ftp://, or magnet:",
});
}
// check if slug is valid
if (slug.length < 1) {
return res
.status(StatusCodes.BAD_REQUEST)
.json({ message: "Invalid Slug!" });
}
if (!slugRegex.test(slug)) {
return res.status(StatusCodes.BAD_REQUEST).json({
message:
"The slug should only contain lowercase alphabets, numbers and hyphen",
});
}
if (link.includes(".eu.org") || link.includes("nakula.fun")) {
return res.status(401).json({ message: "Malicious link entered!" });
}
// Redirection check
try {
// Step 1: Check for HTTP redirects using fetch
const { response, redirectCount } = await fetchWithRedirects(
link,
MAX_REDIRECTS,
);
if (redirectCount >= MAX_REDIRECTS) {
return res.status(400).json({
message: `Suspcious URL detected. If this is a valid URL, please report this issue.`,
});
}
} catch (error) {
console.error("Error checking link:", error);
return res.status(500).json({
message: "Error checking the link.",
});
}
if (
process.env.SKIP_SAFE_BROWSING === "true" ||
link.startsWith("magnet:") ||
link.startsWith("ftp:")
) {
console.log("Skipping safe browsing check");
} else {
try {
const response = await fetch(
"https://safebrowsing.googleapis.com/v4/threatMatches:find?key=" +
apiKey,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
client: {
clientId: "maglit-website",
clientVersion: "1.0.0",
},
threatInfo: {
threatTypes: [
"MALWARE",
"SOCIAL_ENGINEERING",
"UNWANTED_SOFTWARE",
"POTENTIALLY_HARMFUL_APPLICATION",
],
platformTypes: ["ANY_PLATFORM"],
threatEntryTypes: ["URL"],
threatEntries: [{ url: `${link}` }],
},
}),
},
);
const data = await response.json();
console.log("🚀 => data:", data);
if (data && data?.matches?.length > 0) {
// Handle error cases where the URL might not be checked by Safe Browsing
res.status(401).json({ message: "Malicious link entered!" });
}
} catch (error) {
res.status(500).json({ error: "Failed to check the URL." });
}
}
try {
// create a new link
if (!process.env.SECRET_KEY) {
return res
.status(StatusCodes.INTERNAL_SERVER_ERROR)
.json({ message: "Server error..." });
}
const encryptedLink = CryptoJS.AES.encrypt(
JSON.stringify({ link }),
password === ""
? process.env.SECRET_KEY
: process.env.SECRET_KEY + password,
).toString();
await setDoc(doc(db, collectionName, slug), {
link: encryptedLink,
slug: slug,
protected: !(password === ""),
});
res.setHeader("Cache-Control", "s-maxage=176400");
return res.status(StatusCodes.OK).json({
message: "Link lit successfully",
maglitLink: slug,
});
} catch (err) {
console.log("Error", err);
return res
.status(StatusCodes.BAD_REQUEST)
.json({ message: "Server Error, Please try again..." });
}
}