fix: serve Facebook images through path-based proxy URL
Deploy React Site / build-and-deploy (push) Successful in 11s
Deploy React Site / build-and-deploy (push) Successful in 11s
nginx does not percent-decode $arg_ variables, so pass the encoded image URL in the path (nginx decodes $uri). Mirror the Meta CDN whitelist in the Vite dev middleware.
This commit is contained in:
@@ -157,7 +157,7 @@ export default function FacebookCarousel() {
|
||||
// pour éviter le blocage de pistage du navigateur, le mixed-content
|
||||
// et les 403 du CDN Facebook.
|
||||
if (imageSrc.startsWith("http")) {
|
||||
imageSrc = `/api/proxy-image?url=${encodeURIComponent(imageSrc)}`;
|
||||
imageSrc = `/api/proxy-image/${encodeURIComponent(imageSrc)}`;
|
||||
}
|
||||
|
||||
return {
|
||||
|
||||
+15
-3
@@ -20,9 +20,15 @@ export default defineConfig(({ mode }) => {
|
||||
configureServer(server) {
|
||||
server.middlewares.use("/api/proxy-image", async (req, res) => {
|
||||
try {
|
||||
// req.url contient uniquement la partie après /api/proxy-image (ex: /?url=...)
|
||||
const urlObj = new URL(req.url || "", `http://${req.headers.host}`);
|
||||
const targetUrl = urlObj.searchParams.get("url");
|
||||
// req.url contient uniquement la partie après /api/proxy-image,
|
||||
// l'URL cible y est encodée (ex: /https%3A%2F%2Fscontent...).
|
||||
const raw = (req.url || "").replace(/^\//, "");
|
||||
const targetUrl = decodeURIComponent(raw);
|
||||
|
||||
const allowed =
|
||||
/^https?:\/\/[a-z0-9.-]+\.(fbcdn\.net|fbsbx\.com|facebook\.com)([/?]|$)/i.test(
|
||||
targetUrl,
|
||||
);
|
||||
|
||||
if (!targetUrl) {
|
||||
res.statusCode = 400;
|
||||
@@ -30,6 +36,12 @@ export default defineConfig(({ mode }) => {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!allowed) {
|
||||
res.statusCode = 403;
|
||||
res.end("Forbidden");
|
||||
return;
|
||||
}
|
||||
|
||||
// On récupère l'image avec un User-Agent "normal" pour éviter les blocages 403/404
|
||||
const response = await fetch(targetUrl, {
|
||||
headers: {
|
||||
|
||||
Reference in New Issue
Block a user