A platform for community discussion. Free, open, simple.
DEV: Don't forward cookies on onebox redirects (#39544)
Jarek Radosz596372a6f328
diff --git a/lib/onebox/helpers.rb b/lib/onebox/helpers.rbindex 034d7fc9cbc..453f94fe7ab 100644--- a/lib/onebox/helpers.rb+++ b/lib/onebox/helpers.rb@@ -99,14 +99,19 @@ module Oneboxsize_bytes = Onebox.options.max_download_kb * 1024http.request(request) do |response|- if cookie = response.get_fields("set-cookie")- headers["Cookie"] = cookie.join("; ") if allow_cross_domain_cookies- # HACK: If this breaks again in the future, use HTTP::CookieJar from gem 'http-cookie'- # See test: it "does not send cookies to the wrong domain"- redir_header = { "Cookie" => cookie.join("; ") }- end+ if set_cookies = response.get_fields("set-cookie")+ cookie_header =+ set_cookies.map { |c| c.split(";", 2).first.strip }.reject(&:empty?).join("; ")++ headers["Cookie"] = cookie_header if allow_cross_domain_cookies- redir_header = nil unless redir_header.is_a? Hash+ if redirect_location = response["location"]+ redirect_host = Addressable::URI.parse(redirect_location).host+ if redirect_host.nil? || redirect_host == uri.host+ redirect_header = { "Cookie" => cookie_header }+ end+ end+ endcode = response.code.to_iunless code === 200@@ -117,7 +122,7 @@ module Oneboxresponse["location"],redirect_limit: redirect_limit - 1,domain: "#{uri.scheme}://#{uri.host}",- headers: allow_cross_domain_cookies ? headers : redir_header,+ headers: allow_cross_domain_cookies ? headers : redirect_header,allow_cross_domain_cookies: allow_cross_domain_cookies,))diff --git a/spec/lib/onebox/helpers_spec.rb b/spec/lib/onebox/helpers_spec.rbindex fa29daaa513..3fa366e1196 100644--- a/spec/lib/onebox/helpers_spec.rb+++ b/spec/lib/onebox/helpers_spec.rb@@ -172,20 +172,20 @@ RSpec.describe Onebox::Helpers doenddescribe "cookie handling" do- it "naively forwards cookies to the next request" do+ it "forwards cookies on same-host redirects, stripping attributes" dostub_request(:get, "https://httpbin.org/cookies/set/a/b").to_return(status: 302,headers: {location: "/cookies",- "set-cookie": "a=b; Path=/",+ "set-cookie": ["a=b; Path=/; Secure", "c=d; HttpOnly; Max-Age=3600"],},)stub_request(:get, "https://httpbin.org/cookies").with(headers: {- cookie: "a=b; Path=/",+ cookie: "a=b; c=d",},- ).to_return(status: 200, body: "success, cookie readback not implemented")+ ).to_return(status: 200, body: "success")expect(described_class.fetch_response("https://httpbin.org/cookies/set/a/b")).to match("success",@@ -193,8 +193,6 @@ RSpec.describe Onebox::Helpers doendit "does not send cookies to the wrong domain" do- skip("unimplemented")-stub_request(:get, "https://httpbin.org/cookies/set/a/b").to_return(status: 302,headers: {@@ -203,13 +201,13 @@ RSpec.describe Onebox::Helpers do},)- stub_request(:get, "https://evil.com/show_cookies").with(- headers: {- cookie: nil,- },- ).to_return(status: 200, body: "success, cookie readback not implemented")+ stub_request(:get, "https://evil.com/show_cookies").to_return(status: 200, body: "ok")described_class.fetch_response("https://httpbin.org/cookies/set/a/b")++ expect(WebMock).to have_requested(:get, "https://evil.com/show_cookies").with { |req|+ !req.headers.key?("Cookie")+ }endendend