Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

fix: validate youtube video url

Marc Seitza9cbc922d922
diff --git a/components/ui/rich-text-editor.tsx b/components/ui/rich-text-editor.tsx
index 99f86755..967c1859 100644
--- a/components/ui/rich-text-editor.tsx
+++ b/components/ui/rich-text-editor.tsx
@@ -1,6 +1,6 @@
"use client";
-import { useCallback, useState } from "react";
+import { useCallback, useMemo, useState } from "react";
import Image from "@tiptap/extension-image";
import Placeholder from "@tiptap/extension-placeholder";
@@ -33,6 +33,65 @@ import { Label } from "@/components/ui/label";
import { Button } from "@/components/ui/button";
+/**
+ * Validates if a given URL is a valid YouTube URL.
+ * Supports:
+ * - youtube.com/watch?v=VIDEO_ID
+ * - youtu.be/VIDEO_ID
+ * - youtube.com/embed/VIDEO_ID
+ * - www.youtube.com variants
+ * - youtube-nocookie.com variants
+ */
+function isValidYouTubeUrl(url: string): boolean {
+ if (!url || typeof url !== "string") return false;
+
+ try {
+ const parsedUrl = new URL(url.trim());
+ const hostname = parsedUrl.hostname.toLowerCase();
+
+ // Check for valid YouTube hostnames
+ const validHostnames = [
+ "youtube.com",
+ "www.youtube.com",
+ "youtu.be",
+ "www.youtu.be",
+ "youtube-nocookie.com",
+ "www.youtube-nocookie.com",
+ ];
+
+ if (!validHostnames.includes(hostname)) return false;
+
+ // For youtu.be short URLs, the video ID is in the pathname
+ if (hostname === "youtu.be" || hostname === "www.youtu.be") {
+ const videoId = parsedUrl.pathname.slice(1); // Remove leading slash
+ return videoId.length > 0 && /^[\w-]+$/.test(videoId);
+ }
+
+ // For youtube.com/watch URLs, check for 'v' parameter
+ if (parsedUrl.pathname === "/watch") {
+ const videoId = parsedUrl.searchParams.get("v");
+ return videoId !== null && videoId.length > 0 && /^[\w-]+$/.test(videoId);
+ }
+
+ // For youtube.com/embed/VIDEO_ID URLs
+ if (parsedUrl.pathname.startsWith("/embed/")) {
+ const videoId = parsedUrl.pathname.replace("/embed/", "").split("/")[0];
+ return videoId.length > 0 && /^[\w-]+$/.test(videoId);
+ }
+
+ // For youtube.com/v/VIDEO_ID URLs (legacy)
+ if (parsedUrl.pathname.startsWith("/v/")) {
+ const videoId = parsedUrl.pathname.replace("/v/", "").split("/")[0];
+ return videoId.length > 0 && /^[\w-]+$/.test(videoId);
+ }
+
+ return false;
+ } catch {
+ // URL parsing failed
+ return false;
+ }
+}
+
interface RichTextEditorProps {
content?: any;
onChange?: (content: any) => void;
@@ -48,6 +107,13 @@ export function RichTextEditor({
}: RichTextEditorProps) {
const [youtubeDialogOpen, setYoutubeDialogOpen] = useState(false);
const [youtubeUrl, setYoutubeUrl] = useState("");
+ const [youtubeUrlError, setYoutubeUrlError] = useState<string | null>(null);
+
+ // Memoize URL validation to avoid recalculating on every render
+ const isYoutubeUrlValid = useMemo(
+ () => isValidYouTubeUrl(youtubeUrl),
+ [youtubeUrl],
+ );
const editor = useEditor({
extensions: [
@@ -178,14 +244,32 @@ export function RichTextEditor({
const addYoutubeVideo = useCallback(() => {
if (!editor || !youtubeUrl) return;
+ // Re-validate URL before inserting
+ if (!isValidYouTubeUrl(youtubeUrl)) {
+ setYoutubeUrlError(
+ "Please enter a valid YouTube URL (e.g., youtube.com/watch?v=..., youtu.be/...)",
+ );
+ return;
+ }
+
editor.commands.setYoutubeVideo({
src: youtubeUrl,
});
setYoutubeUrl("");
+ setYoutubeUrlError(null);
setYoutubeDialogOpen(false);
}, [editor, youtubeUrl]);
+ // Clear error and URL when dialog closes
+ const handleYoutubeDialogChange = useCallback((open: boolean) => {
+ setYoutubeDialogOpen(open);
+ if (!open) {
+ setYoutubeUrl("");
+ setYoutubeUrlError(null);
+ }
+ }, []);
+
if (!editor) {
return null;
}
@@ -309,7 +393,7 @@ export function RichTextEditor({
</div>
{/* YouTube Dialog */}
- <Dialog open={youtubeDialogOpen} onOpenChange={setYoutubeDialogOpen}>
+ <Dialog open={youtubeDialogOpen} onOpenChange={handleYoutubeDialogChange}>
<DialogContent className="sm:max-w-md">
<DialogHeader>
<DialogTitle>Add YouTube Video</DialogTitle>
@@ -321,28 +405,37 @@ export function RichTextEditor({
id="youtube-url"
placeholder="https://www.youtube.com/watch?v=..."
value={youtubeUrl}
- onChange={(e) => setYoutubeUrl(e.target.value)}
+ onChange={(e) => {
+ setYoutubeUrl(e.target.value);
+ // Clear error when user starts typing
+ if (youtubeUrlError) setYoutubeUrlError(null);
+ }}
onKeyDown={(e) => {
if (e.key === "Enter") {
e.preventDefault();
addYoutubeVideo();
}
}}
+ className={youtubeUrlError ? "border-destructive" : ""}
/>
- <p className="text-xs text-muted-foreground">
- Paste a YouTube video URL (e.g., youtube.com/watch?v=...,
- youtu.be/...)
- </p>
+ {youtubeUrlError ? (
+ <p className="text-xs text-destructive">{youtubeUrlError}</p>
+ ) : (
+ <p className="text-xs text-muted-foreground">
+ Paste a YouTube video URL (e.g., youtube.com/watch?v=...,
+ youtu.be/...)
+ </p>
+ )}
</div>
</div>
<DialogFooter>
<Button
variant="outline"
- onClick={() => setYoutubeDialogOpen(false)}
+ onClick={() => handleYoutubeDialogChange(false)}
>
Cancel
</Button>
- <Button onClick={addYoutubeVideo} disabled={!youtubeUrl}>
+ <Button onClick={addYoutubeVideo} disabled={!isYoutubeUrlValid}>
Add Video
</Button>
</DialogFooter>