From 965cc65ea5499a2f6931bdaa3e51bdb24f6e2daa Mon Sep 17 00:00:00 2001 From: Stefan Sundin Date: Sun, 10 May 2020 11:09:51 -0700 Subject: [PATCH] Check ENV for credentials before attempting to serve requests. --- app.rb | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/app.rb b/app.rb index 81f99a2..99ea8d7 100644 --- a/app.rb +++ b/app.rb @@ -98,6 +98,7 @@ end get "/twitter" do + return [404, "Credentials not configured"] if !ENV["TWITTER_ACCESS_TOKEN"] return [400, "Insufficient parameters"] if params[:q].empty? if params[:q].include?("twitter.com/i/") || params[:q].include?("twitter.com/who_to_follow/") @@ -132,6 +133,8 @@ end get %r{/twitter/(?\d+)/(?.+)} do |id, username| + return [404, "Credentials not configured"] if !ENV["TWITTER_ACCESS_TOKEN"] + @user_id = id response = Twitter.get("/statuses/user_timeline.json", query: { @@ -172,6 +175,7 @@ end get "/youtube" do + return [404, "Credentials not configured"] if !ENV["GOOGLE_API_KEY"] return [400, "Insufficient parameters"] if params[:q].empty? if /youtube\.com\/channel\/(?(UC|S)[^\/?#]+)(?:\/search\?query=(?[^&#]+))?/ =~ params[:q] @@ -243,6 +247,8 @@ end get "/youtube/:channel_id/:username.ics" do + return [404, "Credentials not configured"] if !ENV["GOOGLE_API_KEY"] + @channel_id = params[:channel_id] @username = params[:username] @title = "#{@username} on YouTube" @@ -289,6 +295,8 @@ end get "/youtube/:channel_id/:username" do + return [404, "Credentials not configured"] if !ENV["GOOGLE_API_KEY"] + @channel_id = params[:channel_id] playlist_id = "UU" + @channel_id[2..] @username = params[:username] @@ -379,8 +387,9 @@ end get "/facebook" do - return [404, "Facebook credentials not configured"] if ENV["FACEBOOK_APP_ID"].empty? || ENV["FACEBOOK_APP_SECRET"].empty? + return [404, "Credentials not configured"] if !ENV["FACEBOOK_APP_ID"] || !ENV["FACEBOOK_APP_SECRET"] return [400, "Insufficient parameters"] if params[:q].empty? + params[:q].gsub!("facebookcorewwwi.onion", "facebook.com") if params[:q].include?("facebookcorewwwi.onion") if /https:\/\/www\.facebook\.com\/plugins\/.+[?&]href=(?.+)$/ =~ params[:q] @@ -427,6 +436,8 @@ end get "/facebook/download" do + return [404, "Credentials not configured"] if !ENV["FACEBOOK_APP_ID"] || !ENV["FACEBOOK_APP_SECRET"] + if /\/(?\d+)/ =~ params[:url] # https://www.facebook.com/infectedmushroom/videos/10153430677732261/ # https://www.facebook.com/infectedmushroom/videos/vb.8811047260/10153371214897261/?type=2&theater @@ -466,7 +477,7 @@ end get %r{/facebook/(?\d+)/(?.+)} do |id, username| - return [404, "Facebook credentials not configured"] if ENV["FACEBOOK_APP_ID"].empty? || ENV["FACEBOOK_APP_SECRET"].empty? + return [404, "Credentials not configured"] if !ENV["FACEBOOK_APP_ID"] || !ENV["FACEBOOK_APP_SECRET"] @id = id @type = @edge = %w[videos photos live].pick(params[:type]) || "posts" @@ -726,6 +737,7 @@ end get "/soundcloud" do + return [404, "Credentials not configured"] if !ENV["SOUNDCLOUD_CLIENT_ID"] return [400, "Insufficient parameters"] if params[:q].empty? if /soundcloud\.com\/(?[^\/?#]+)/ =~ params[:q] @@ -754,6 +766,8 @@ end get "/soundcloud/download" do + return [404, "Credentials not configured"] if !ENV["SOUNDCLOUD_CLIENT_ID"] + url = params[:url] url = "https://#{url}" if !url.start_with?("http:", "https:") response = Soundcloud.get("/resolve", query: { url: url }) @@ -774,6 +788,8 @@ end get %r{/soundcloud/(?\d+)/(?.+)} do |id, username| + return [404, "Credentials not configured"] if !ENV["SOUNDCLOUD_CLIENT_ID"] + @id = id response = Soundcloud.get("/users/#{id}/tracks") @@ -821,6 +837,7 @@ end get "/twitch" do + return [404, "Credentials not configured"] if !ENV["TWITCH_CLIENT_ID"] return [400, "Insufficient parameters"] if params[:q].empty? if /twitch\.tv\/directory\/game\/(?[^\/?#]+)/ =~ params[:q] @@ -861,6 +878,7 @@ end get "/twitch/download" do + return [404, "Credentials not configured"] if !ENV["TWITCH_CLIENT_ID"] return [400, "Insufficient parameters"] if params[:url].empty? if /twitch\.tv\/[^\/]+\/clip\/(?[^?&#]+)/ =~ params[:url] || /clips\.twitch\.tv\/(?:embed\?clip=)?(?[^?&#]+)/ =~ params[:url] @@ -915,6 +933,7 @@ end get "/twitch/watch" do + return [404, "Credentials not configured"] if !ENV["TWITCH_CLIENT_ID"] return [400, "Insufficient parameters"] if params[:url].empty? if /twitch\.tv\/[^\/]+\/clip\/(?[^?&#]+)/ =~ params[:url] || /clips\.twitch\.tv\/(?:embed\?clip=)?(?[^?&#]+)/ =~ params[:url] @@ -974,6 +993,8 @@ end get %r{/twitch/directory/game/(?\d+)/(?.+)} do |id, game_name| + return [404, "Credentials not configured"] if !ENV["TWITCH_CLIENT_ID"] + @id = id @type = "game" @@ -999,6 +1020,8 @@ end get %r{/twitch/(?\d+)/(?.+)\.ics} do |id, user| + return [404, "Credentials not configured"] if !ENV["TWITCH_CLIENT_ID"] + @title = "#{user} on Twitch" type = %w[all upload archive highlight].pick(params[:type]) || "all" @@ -1013,6 +1036,8 @@ end get %r{/twitch/(?\d+)/(?.+)} do |id, user| + return [404, "Credentials not configured"] if !ENV["TWITCH_CLIENT_ID"] + @id = id @type = "user" @@ -1189,6 +1214,7 @@ end get "/imgur" do + return [404, "Credentials not configured"] if !ENV["IMGUR_CLIENT_ID"] return [400, "Insufficient parameters"] if params[:q].empty? if /imgur\.com\/user\/(?[a-zA-Z0-9]+)/ =~ params[:q] @@ -1240,6 +1266,8 @@ end get "/imgur/:user_id/:username" do + return [404, "Credentials not configured"] if !ENV["IMGUR_CLIENT_ID"] + if params[:user_id] == "r" @subreddit = params[:username] response = Imgur.get("/gallery/r/#{@subreddit}")