diff --git a/packages/app-store/giphy/api/get.ts b/packages/app-store/giphy/api/get.ts index 8cee03b9a8..7b0d467138 100644 --- a/packages/app-store/giphy/api/get.ts +++ b/packages/app-store/giphy/api/get.ts @@ -3,7 +3,9 @@ import { z, ZodError } from "zod"; import { GiphyManager } from "../lib"; -const giphyUrlRegexp = new RegExp("^https://(.*).giphy.com/media/(.*)/giphy.gif(.*)"); +const giphyUrlRegexp = new RegExp( + "^https?:\\/\\/(?:[\\w.-]+\\.)?giphy\\.com\\/media\\/(?:[^/]+\\/)*([a-zA-Z0-9_-]+)\\/giphy\\.gif(?:\\?.*)?$" +); const getSchema = z.object({ url: z.string().regex(giphyUrlRegexp, "Giphy URL is invalid"), @@ -26,10 +28,10 @@ async function handler(req: NextApiRequest, res: NextApiResponse) { const sanitisedUrl = parsedUrl.origin + parsedUrl.pathname; // Extract Giphy ID from embed url const matches = giphyUrlRegexp.exec(sanitisedUrl); - if (!matches || matches.length < 3) { + if (!matches || matches.length < 2) { return res.status(400).json({ message: "Giphy URL is invalid" }); } - const giphyId = matches[2]; + const giphyId = matches[1]; const gifImageUrl = await GiphyManager.getGiphyById(giphyId); return res.status(200).json({ image: gifImageUrl }); } catch (error: unknown) {