index.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import { serve } from "https://deno.land/std@0.177.0/http/server.ts"
  2. import { isValidExternalUrl, verifyAuth } from '../_shared/security.ts'
  3. const corsHeaders = {
  4. 'Access-Control-Allow-Origin': '*',
  5. 'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
  6. }
  7. serve(async (req) => {
  8. if (req.method === 'OPTIONS') {
  9. return new Response('ok', { headers: corsHeaders })
  10. }
  11. try {
  12. // Authentication: Require authenticated user
  13. const auth = await verifyAuth(req);
  14. if (!auth) {
  15. console.log('Unauthorized access attempt to fetch-youtube-rss');
  16. return new Response(
  17. JSON.stringify({ error: 'Unauthorized' }),
  18. {
  19. headers: { ...corsHeaders, 'Content-Type': 'application/json' },
  20. status: 401
  21. }
  22. )
  23. }
  24. const { url } = await req.json()
  25. // Input validation - must be a YouTube URL
  26. if (!url || typeof url !== 'string') {
  27. return new Response(
  28. JSON.stringify({ error: 'Valid URL is required' }),
  29. {
  30. headers: { ...corsHeaders, 'Content-Type': 'application/json' },
  31. status: 400
  32. }
  33. )
  34. }
  35. // Validate it's a YouTube URL specifically
  36. if (!url.includes('youtube.com') && !url.includes('youtu.be')) {
  37. return new Response(
  38. JSON.stringify({ error: 'Invalid YouTube URL' }),
  39. {
  40. headers: { ...corsHeaders, 'Content-Type': 'application/json' },
  41. status: 400
  42. }
  43. )
  44. }
  45. // SSRF Protection: Validate URL format
  46. const urlValidation = isValidExternalUrl(url);
  47. if (!urlValidation.valid) {
  48. console.log(`SSRF blocked in fetch-youtube-rss: ${url} - ${urlValidation.error}`);
  49. return new Response(
  50. JSON.stringify({ error: urlValidation.error }),
  51. {
  52. headers: { ...corsHeaders, 'Content-Type': 'application/json' },
  53. status: 400
  54. }
  55. )
  56. }
  57. console.log('Fetching YouTube page:', url)
  58. // Fetch the YouTube page with timeout
  59. const controller = new AbortController();
  60. const timeoutId = setTimeout(() => controller.abort(), 30000); // 30s timeout
  61. let response;
  62. try {
  63. response = await fetch(url, {
  64. signal: controller.signal,
  65. headers: {
  66. 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
  67. }
  68. })
  69. } finally {
  70. clearTimeout(timeoutId);
  71. }
  72. if (!response.ok) {
  73. throw new Error(`Failed to fetch page: ${response.status}`)
  74. }
  75. const html = await response.text()
  76. // Limit content size
  77. if (html.length > 5 * 1024 * 1024) { // 5MB limit
  78. throw new Error('Page content too large');
  79. }
  80. // Look for the RSS feed link in the HTML
  81. const rssLinkMatch = html.match(/<link[^>]+rel="alternate"[^>]+type="application\/rss\+xml"[^>]+href="([^"]+)"/i)
  82. if (!rssLinkMatch) {
  83. return new Response(
  84. JSON.stringify({ error: 'RSS feed not found on this YouTube page' }),
  85. {
  86. headers: { ...corsHeaders, 'Content-Type': 'application/json' },
  87. status: 404
  88. }
  89. )
  90. }
  91. const rssUrl = rssLinkMatch[1]
  92. // Validate the discovered RSS URL
  93. const rssUrlValidation = isValidExternalUrl(rssUrl);
  94. if (!rssUrlValidation.valid) {
  95. return new Response(
  96. JSON.stringify({ error: 'Invalid RSS feed URL discovered' }),
  97. {
  98. headers: { ...corsHeaders, 'Content-Type': 'application/json' },
  99. status: 400
  100. }
  101. )
  102. }
  103. // Also try to extract the channel name from the page title
  104. const titleMatch = html.match(/<title>([^<]+)<\/title>/i)
  105. let channelName = null
  106. if (titleMatch) {
  107. // Remove " - YouTube" from the end if present
  108. channelName = titleMatch[1].replace(/ - YouTube$/, '')
  109. }
  110. console.log('Found RSS URL:', rssUrl)
  111. console.log('Found channel name:', channelName)
  112. return new Response(
  113. JSON.stringify({
  114. rssUrl,
  115. channelName
  116. }),
  117. {
  118. headers: { ...corsHeaders, 'Content-Type': 'application/json' }
  119. }
  120. )
  121. } catch (error: any) {
  122. console.error('Error:', error)
  123. return new Response(
  124. JSON.stringify({ error: 'Failed to fetch YouTube RSS feed' }),
  125. {
  126. headers: { ...corsHeaders, 'Content-Type': 'application/json' },
  127. status: 500
  128. }
  129. )
  130. }
  131. })