Skip to content

Scheduling infrastructure for absolutely everyone.

fix: regex and id extraction for giphy media urls (#22276)

abhix440d70cd1d490
diff --git a/packages/app-store/giphy/api/get.ts b/packages/app-store/giphy/api/get.ts
index 8cee03b9a89..7b0d4671388 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) {