Skip to content

A platform for community discussion. Free, open, simple.

FIX: send RSS polling API credentials as headers instead of query params (#38044)

Régis Hanol2a52a1bda59b
diff --git a/plugins/discourse-rss-polling/app/jobs/jobs/discourse_rss_polling/poll_feed.rb b/plugins/discourse-rss-polling/app/jobs/jobs/discourse_rss_polling/poll_feed.rb
index 1a0eed654b3..9e84e4325a5 100644
--- a/plugins/discourse-rss-polling/app/jobs/jobs/discourse_rss_polling/poll_feed.rb
+++ b/plugins/discourse-rss-polling/app/jobs/jobs/discourse_rss_polling/poll_feed.rb
@@ -122,28 +122,65 @@ module Jobs
def topics_polled_from_feed
raw_feed = fetch_raw_feed
- return [] if raw_feed.blank?
+
+ if raw_feed.blank?
+ Rails.logger.warn("RSS Polling: Failed to fetch feed from #{feed_url}")
+ return []
+ end
parsed_feed = RSS::Parser.parse(raw_feed, false)
- return [] if parsed_feed.blank?
+
+ if parsed_feed.blank?
+ Rails.logger.warn("RSS Polling: Unable to parse feed from #{feed_url}")
+ return []
+ end
parsed_feed.items.map { |item| ::DiscourseRssPolling::FeedItem.new(item) }
- rescue RSS::NotWellFormedError, RSS::InvalidRSSError
+ rescue RSS::NotWellFormedError, RSS::InvalidRSSError => e
+ Discourse.warn_exception(e, message: "RSS Polling: Invalid RSS from #{feed_url}")
[]
end
def fetch_raw_feed
+ url, headers = extract_api_credentials(@feed_url)
body = +""
- FinalDestination
- .new(@feed_url)
- .get do |response, chunk, uri|
- throw :done if uri.blank? || !response.is_a?(Net::HTTPSuccess)
- body << chunk
+ fd = FinalDestination.new(url, headers:)
+ response_status = nil
+
+ fd.get do |response, chunk, uri|
+ if uri.blank? || !response.is_a?(Net::HTTPSuccess)
+ response_status = response&.code
+ throw :done
end
+ body << chunk
+ end
+
+ if body.blank? && response_status.present?
+ Rails.logger.warn(
+ "RSS Polling: HTTP #{response_status} when fetching #{feed_url} (status: #{fd.status})",
+ )
+ end
body.presence
end
+
+ def extract_api_credentials(url)
+ uri = URI.parse(url)
+ return url, {} if uri.query.blank?
+
+ params = CGI.parse(uri.query)
+ api_key = params.delete("api_key")&.first
+ api_username = params.delete("api_username")&.first
+
+ return url, {} if api_key.blank?
+
+ headers = { "Api-Key" => api_key }
+ headers["Api-Username"] = api_username if api_username.present?
+
+ uri.query = params.empty? ? nil : URI.encode_www_form(params)
+ [uri.to_s, headers]
+ end
end
end
end
diff --git a/plugins/discourse-rss-polling/spec/jobs/poll_feed_spec.rb b/plugins/discourse-rss-polling/spec/jobs/poll_feed_spec.rb
index 0693e527c24..89fb79b3a7b 100644
--- a/plugins/discourse-rss-polling/spec/jobs/poll_feed_spec.rb
+++ b/plugins/discourse-rss-polling/spec/jobs/poll_feed_spec.rb
@@ -144,15 +144,17 @@ RSpec.describe Jobs::DiscourseRssPolling::PollFeed do
}.not_to raise_error
end
- it "works with authentication in query parameters" do
+ it "sends API credentials as headers instead of query parameters" do
authenticated_url = "#{feed_url}?api_key=test123&api_username=testuser"
Discourse.redis.del("rss-polling-feed-polled:#{Digest::SHA1.hexdigest(authenticated_url)}")
- stub_request(:get, authenticated_url).to_return(
- status: 200,
- body: file_from_fixtures("feed.rss", "feed"),
- )
+ stub_request(:get, feed_url).with(
+ headers: {
+ "Api-Key" => "test123",
+ "Api-Username" => "testuser",
+ },
+ ).to_return(status: 200, body: file_from_fixtures("feed.rss", "feed"))
expect {
job.execute(feed_url: authenticated_url, author_username: author.username)