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
|
// pour éviter le blocage de pistage du navigateur, le mixed-content
|
||||||
// et les 403 du CDN Facebook.
|
// et les 403 du CDN Facebook.
|
||||||
if (imageSrc.startsWith("http")) {
|
if (imageSrc.startsWith("http")) {
|
||||||
imageSrc = `/api/proxy-image?url=${encodeURIComponent(imageSrc)}`;
|
imageSrc = `/api/proxy-image/${encodeURIComponent(imageSrc)}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
+15
-3
@@ -20,9 +20,15 @@ export default defineConfig(({ mode }) => {
|
|||||||
configureServer(server) {
|
configureServer(server) {
|
||||||
server.middlewares.use("/api/proxy-image", async (req, res) => {
|
server.middlewares.use("/api/proxy-image", async (req, res) => {
|
||||||
try {
|
try {
|
||||||
// req.url contient uniquement la partie après /api/proxy-image (ex: /?url=...)
|
// req.url contient uniquement la partie après /api/proxy-image,
|
||||||
const urlObj = new URL(req.url || "", `http://${req.headers.host}`);
|
// l'URL cible y est encodée (ex: /https%3A%2F%2Fscontent...).
|
||||||
const targetUrl = urlObj.searchParams.get("url");
|
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) {
|
if (!targetUrl) {
|
||||||
res.statusCode = 400;
|
res.statusCode = 400;
|
||||||
@@ -30,6 +36,12 @@ export default defineConfig(({ mode }) => {
|
|||||||
return;
|
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
|
// On récupère l'image avec un User-Agent "normal" pour éviter les blocages 403/404
|
||||||
const response = await fetch(targetUrl, {
|
const response = await fetch(targetUrl, {
|
||||||
headers: {
|
headers: {
|
||||||
|
|||||||
Reference in New Issue
Block a user