Apply security fixes
Implement security enhancements as suggested by the security review.
This commit is contained in:
parent
cd588e391a
commit
9803c1a16c
5 changed files with 219 additions and 18 deletions
|
|
@ -5,6 +5,7 @@ import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger } from
|
|||
import { Share2, Copy, QrCode, Twitter, Facebook, Linkedin } from 'lucide-react';
|
||||
import { useToast } from '@/hooks/use-toast';
|
||||
import QRCode from 'qrcode';
|
||||
import { validateShareData, rateLimiter } from '@/utils/security';
|
||||
|
||||
interface SocialShareProps {
|
||||
title: string;
|
||||
|
|
@ -21,20 +22,38 @@ const SocialShare: React.FC<SocialShareProps> = ({
|
|||
const [isOpen, setIsOpen] = useState(false);
|
||||
const { toast } = useToast();
|
||||
|
||||
// Validate and sanitize share data
|
||||
const shareData = validateShareData(title, description, url);
|
||||
|
||||
useEffect(() => {
|
||||
if (isOpen) {
|
||||
QRCode.toDataURL(url, {
|
||||
QRCode.toDataURL(shareData.url, {
|
||||
width: 200,
|
||||
margin: 2,
|
||||
color: {
|
||||
dark: '#000000',
|
||||
light: '#FFFFFF'
|
||||
}
|
||||
}).then(setQrCodeDataUrl);
|
||||
}).then(setQrCodeDataUrl).catch(() => {
|
||||
toast({
|
||||
title: "Error",
|
||||
description: "Failed to generate QR code",
|
||||
variant: "destructive",
|
||||
});
|
||||
});
|
||||
}
|
||||
}, [url, isOpen]);
|
||||
}, [shareData.url, isOpen, toast]);
|
||||
|
||||
const copyToClipboard = async (text: string) => {
|
||||
if (!rateLimiter.isAllowed('clipboard', 10, 60000)) {
|
||||
toast({
|
||||
title: "Too many attempts",
|
||||
description: "Please wait before copying again",
|
||||
variant: "destructive",
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await navigator.clipboard.writeText(text);
|
||||
toast({
|
||||
|
|
@ -51,6 +70,15 @@ const SocialShare: React.FC<SocialShareProps> = ({
|
|||
};
|
||||
|
||||
const copyQRCode = async () => {
|
||||
if (!rateLimiter.isAllowed('qr-copy', 5, 60000)) {
|
||||
toast({
|
||||
title: "Too many attempts",
|
||||
description: "Please wait before copying again",
|
||||
variant: "destructive",
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(qrCodeDataUrl);
|
||||
const blob = await response.blob();
|
||||
|
|
@ -71,9 +99,22 @@ const SocialShare: React.FC<SocialShareProps> = ({
|
|||
};
|
||||
|
||||
const shareLinks = {
|
||||
twitter: `https://twitter.com/intent/tweet?text=${encodeURIComponent(title)}&url=${encodeURIComponent(url)}`,
|
||||
facebook: `https://www.facebook.com/sharer/sharer.php?u=${encodeURIComponent(url)}`,
|
||||
linkedin: `https://www.linkedin.com/sharing/share-offsite/?url=${encodeURIComponent(url)}`
|
||||
twitter: `https://twitter.com/intent/tweet?text=${encodeURIComponent(shareData.title)}&url=${encodeURIComponent(shareData.url)}`,
|
||||
facebook: `https://www.facebook.com/sharer/sharer.php?u=${encodeURIComponent(shareData.url)}`,
|
||||
linkedin: `https://www.linkedin.com/sharing/share-offsite/?url=${encodeURIComponent(shareData.url)}`
|
||||
};
|
||||
|
||||
const handleSocialShare = (platform: string, url: string) => {
|
||||
if (!rateLimiter.isAllowed('social-share', 10, 60000)) {
|
||||
toast({
|
||||
title: "Too many attempts",
|
||||
description: "Please wait before sharing again",
|
||||
variant: "destructive",
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
window.open(url, '_blank', 'noopener,noreferrer');
|
||||
};
|
||||
|
||||
return (
|
||||
|
|
@ -89,7 +130,7 @@ const SocialShare: React.FC<SocialShareProps> = ({
|
|||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={() => window.open(shareLinks.twitter, '_blank')}
|
||||
onClick={() => handleSocialShare('twitter', shareLinks.twitter)}
|
||||
className="flex items-center gap-2"
|
||||
>
|
||||
<Twitter className="w-4 h-4" />
|
||||
|
|
@ -99,7 +140,7 @@ const SocialShare: React.FC<SocialShareProps> = ({
|
|||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={() => window.open(shareLinks.facebook, '_blank')}
|
||||
onClick={() => handleSocialShare('facebook', shareLinks.facebook)}
|
||||
className="flex items-center gap-2"
|
||||
>
|
||||
<Facebook className="w-4 h-4" />
|
||||
|
|
@ -109,7 +150,7 @@ const SocialShare: React.FC<SocialShareProps> = ({
|
|||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={() => window.open(shareLinks.linkedin, '_blank')}
|
||||
onClick={() => handleSocialShare('linkedin', shareLinks.linkedin)}
|
||||
className="flex items-center gap-2"
|
||||
>
|
||||
<Linkedin className="w-4 h-4" />
|
||||
|
|
@ -120,7 +161,7 @@ const SocialShare: React.FC<SocialShareProps> = ({
|
|||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={() => copyToClipboard(url)}
|
||||
onClick={() => copyToClipboard(shareData.url)}
|
||||
className="flex items-center gap-2"
|
||||
>
|
||||
<Copy className="w-4 h-4" />
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue