Commit 1227f34b authored by Jonne Haß's avatar Jonne Haß
Browse files

Pass normalized URI to OpenGraphReader

This ensures the hostname is downcase and thus subsequent third party library
assumptions hold, namely http-cookie (pulled through faraday-cookie_jar) doesn't
raise

closes #8021
parent 4a22f085
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@
## Refactor

## Bug fixes
* Improve handling of mixed case hostnames while fetching OpenGraph data [#8021](https://github.com/diaspora/diaspora/pull/8021)

## Features
* Add line mentioning diaspora\* on the splash page [#7966](https://github.com/diaspora/diaspora/pull/7966)
+3 −1
Original line number Diff line number Diff line
@@ -33,7 +33,9 @@ class OpenGraphCache < ApplicationRecord
  end

  def fetch_and_save_opengraph_data!
    object = OpenGraphReader.fetch!(self.url)
    uri = URI.parse(url.start_with?("http") ? url : "http://#{url}")
    uri.normalize!
    object = OpenGraphReader.fetch!(uri)

    return unless object

+19 −2
Original line number Diff line number Diff line
@@ -8,7 +8,7 @@ describe OpenGraphCache, type: :model do
  describe "fetch_and_save_opengraph_data!" do
    context "with an unsecure video url" do
      it "doesn't save the video url" do
        expect(OpenGraphReader).to receive(:fetch!).with("https://example.com/article/123").and_return(
        expect(OpenGraphReader).to receive(:fetch!).with(URI.parse("https://example.com/article/123")).and_return(
          double(
            og: double(
              description: "This is the article lead",
@@ -34,7 +34,7 @@ describe OpenGraphCache, type: :model do

    context "with a secure video url" do
      it "saves the video url" do
        expect(OpenGraphReader).to receive(:fetch!).with("https://example.com/article/123").and_return(
        expect(OpenGraphReader).to receive(:fetch!).with(URI.parse("https://example.com/article/123")).and_return(
          double(
            og: double(
              description: "This is the article lead",
@@ -57,5 +57,22 @@ describe OpenGraphCache, type: :model do
        expect(ogc.video_url).to eq("https://bandcamp.com/EmbeddedPlayer/v=2/track=12/size=small")
      end
    end

    context "a mixed case hostname" do
      it "downcases the hostname" do
        stub_request(:head, "http:///wetter.com")
          .with(headers: {
                  "Accept"     => "text/html",
                  "User-Agent" => "OpenGraphReader/0.6.2 (+https://github.com/jhass/open_graph_reader)"
                })
          .to_return(status: 200, body: "", headers:
            {"Set-Cookie" => "Dabgroup=A;path=/;Expires=Thu, 23 May 2019 16:12:01 GMT;httpOnly"})

        ogc = OpenGraphCache.new(url: "Wetter.com")
        expect {
          ogc.fetch_and_save_opengraph_data!
        }.to_not raise_error
      end
    end
  end
end