|
| 1 | +import type { FastifyPluginAsync } from "fastify"; |
| 2 | +import { resolveGooglePhotosLink } from "../services/photos/index.js"; |
| 3 | + |
| 4 | +/** |
| 5 | + * Allowed upstream hostname patterns for the image proxy. |
| 6 | + * Prevents abuse by only allowing known photo-source domains. |
| 7 | + */ |
| 8 | +const ALLOWED_HOSTS = [ |
| 9 | + // Wikimedia Commons |
| 10 | + "upload.wikimedia.org", |
| 11 | + "commons.wikimedia.org", |
| 12 | + // Mapillary (CDN uses regional subdomains like scontent-fra5-2.xx.fbcdn.net) |
| 13 | + "images.mapillary.com", |
| 14 | + "fbcdn.net", |
| 15 | + // Flickr |
| 16 | + "live.staticflickr.com", |
| 17 | + // Panoramax |
| 18 | + "api.panoramax.xyz", |
| 19 | + "panoramax.openstreetmap.fr", |
| 20 | + // Google (resolved Google Photos) |
| 21 | + "lh3.googleusercontent.com", |
| 22 | + "lh4.googleusercontent.com", |
| 23 | + "lh5.googleusercontent.com", |
| 24 | + "lh6.googleusercontent.com", |
| 25 | + // Google Photos (share links resolved on the fly) |
| 26 | + "photos.app.goo.gl", |
| 27 | + "photos.google.com", |
| 28 | + // OpenStreetMap / other |
| 29 | + "tile.openstreetmap.org", |
| 30 | +]; |
| 31 | + |
| 32 | +function isAllowedHost(hostname: string): boolean { |
| 33 | + return ALLOWED_HOSTS.some((h) => hostname === h || hostname.endsWith(`.${h}`)); |
| 34 | +} |
| 35 | + |
| 36 | +/** Allowed frontend origins that may use the proxy. */ |
| 37 | +function getAllowedOrigins(): string[] { |
| 38 | + return (process.env.CORS_ORIGIN ?? "http://localhost:3000").split(",").map((o) => o.trim()); |
| 39 | +} |
| 40 | + |
| 41 | +const MAX_SIZE = 15 * 1024 * 1024; // 15 MB |
| 42 | + |
| 43 | +export const imageProxyRoute: FastifyPluginAsync = async (fastify) => { |
| 44 | + fastify.get<{ Querystring: { url: string } }>("/image-proxy", { |
| 45 | + schema: { |
| 46 | + querystring: { |
| 47 | + type: "object", |
| 48 | + required: ["url"], |
| 49 | + properties: { |
| 50 | + url: { type: "string", minLength: 10 }, |
| 51 | + }, |
| 52 | + }, |
| 53 | + }, |
| 54 | + handler: async (req, reply) => { |
| 55 | + // Referrer guard: only allow requests originating from our frontend. |
| 56 | + // Browsers send Referer on <img> loads; third-party sites get blocked. |
| 57 | + const referer = req.headers.referer ?? req.headers.origin; |
| 58 | + if (referer) { |
| 59 | + const origins = getAllowedOrigins(); |
| 60 | + const allowed = origins.some((o) => referer.startsWith(o)); |
| 61 | + if (!allowed) { |
| 62 | + return reply.status(403).send({ message: "Forbidden" }); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + const { url } = req.query; |
| 67 | + |
| 68 | + let parsed: URL; |
| 69 | + try { |
| 70 | + parsed = new URL(url); |
| 71 | + } catch { |
| 72 | + return reply.status(400).send({ message: "Invalid URL" }); |
| 73 | + } |
| 74 | + |
| 75 | + if (parsed.protocol !== "https:" && parsed.protocol !== "http:") { |
| 76 | + return reply.status(400).send({ message: "Only HTTP(S) URLs allowed" }); |
| 77 | + } |
| 78 | + |
| 79 | + if (!isAllowedHost(parsed.hostname)) { |
| 80 | + return reply.status(403).send({ message: "Domain not allowed" }); |
| 81 | + } |
| 82 | + |
| 83 | + // Google Photos share links are not direct images — resolve to actual image URL |
| 84 | + let imageUrl = url; |
| 85 | + if (parsed.hostname === "photos.app.goo.gl" || parsed.hostname === "photos.google.com") { |
| 86 | + const resolved = await resolveGooglePhotosLink(url); |
| 87 | + if (resolved.length === 0) { |
| 88 | + return reply.status(404).send({ message: "Could not resolve Google Photos link" }); |
| 89 | + } |
| 90 | + imageUrl = resolved[0]; |
| 91 | + } |
| 92 | + |
| 93 | + try { |
| 94 | + const upstream = await fetch(imageUrl, { |
| 95 | + signal: AbortSignal.timeout(10_000), |
| 96 | + headers: { "User-Agent": "OpenMapX/1.0" }, |
| 97 | + redirect: "follow", |
| 98 | + }); |
| 99 | + |
| 100 | + if (!upstream.ok) { |
| 101 | + return reply.status(upstream.status).send({ message: "Upstream error" }); |
| 102 | + } |
| 103 | + |
| 104 | + const contentType = upstream.headers.get("content-type") ?? "application/octet-stream"; |
| 105 | + if (!contentType.startsWith("image/")) { |
| 106 | + return reply.status(415).send({ message: "Not an image" }); |
| 107 | + } |
| 108 | + |
| 109 | + const contentLength = upstream.headers.get("content-length"); |
| 110 | + if (contentLength && parseInt(contentLength, 10) > MAX_SIZE) { |
| 111 | + return reply.status(413).send({ message: "Image too large" }); |
| 112 | + } |
| 113 | + |
| 114 | + const bytes = await upstream.arrayBuffer(); |
| 115 | + const origins = getAllowedOrigins(); |
| 116 | + reply.header("Cache-Control", "public, max-age=86400, s-maxage=86400"); |
| 117 | + reply.header("Cross-Origin-Resource-Policy", "cross-origin"); |
| 118 | + reply.header("Access-Control-Allow-Origin", origins[0]); |
| 119 | + reply.type(contentType); |
| 120 | + return reply.send(Buffer.from(bytes)); |
| 121 | + } catch (err) { |
| 122 | + req.log.warn({ url, err }, "Image proxy fetch failed"); |
| 123 | + return reply.status(502).send({ message: "Failed to fetch image" }); |
| 124 | + } |
| 125 | + }, |
| 126 | + }); |
| 127 | +}; |
0 commit comments