From f029e2eaf6fa344fca38ff4f8ff33f818d99819d Mon Sep 17 00:00:00 2001 From: Penar Musaraj Date: Thu, 6 Feb 2020 10:32:42 -0500 Subject: [PATCH] FEATURE: Add site setting for specific hosts using custom user agent when oneboxing Followup to #00c406 --- config/locales/server.en.yml | 15 ++++++++------- config/site_settings.yml | 4 +++- lib/oneboxer.rb | 2 +- spec/components/oneboxer_spec.rb | 14 ++++++++++++++ 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index bb5e6c4acd9..3a35fd6933e 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -386,13 +386,13 @@ en: time_must_be_provided: "time must be provided for all reminders except '%{reminder_type}'" reminders: - at_desktop: "Next time I'm at my desktop" - later_today: "Later today
{{date}}" - next_business_day: "Next business day
{{date}}" - tomorrow: "Tomorrow
{{date}}" - next_week: "Next week
{{date}}" - next_month: "Next month
{{date}}" - custom: "Custom date and time" + at_desktop: "Next time I'm at my desktop" + later_today: "Later today
{{date}}" + next_business_day: "Next business day
{{date}}" + tomorrow: "Tomorrow
{{date}}" + next_week: "Next week
{{date}}" + next_month: "Next month
{{date}}" + custom: "Custom date and time" groups: success: @@ -1451,6 +1451,7 @@ en: onebox_domains_blacklist: "A list of domains that will never be oneboxed." inline_onebox_domains_whitelist: "A list of domains that will be oneboxed in miniature form if linked without a title" enable_inline_onebox_on_all_domains: "Ignore inline_onebox_domain_whitelist site setting and allow inline onebox on all domains." + force_custom_user_agent_hosts: "Hosts for which to use the custom onebox user agent on all requests. (Especially useful for hosts that limit access by user agent)." max_oneboxes_per_post: "Maximum number of oneboxes in a post." logo: "The logo image at the top left of your site. Use a wide rectangular image with a height of 120 and an aspect ratio greater than 3:1. If left blank, the site title text will be shown." diff --git a/config/site_settings.yml b/config/site_settings.yml index 40ae62f3f4e..0a05a2d6c9b 100644 --- a/config/site_settings.yml +++ b/config/site_settings.yml @@ -1436,7 +1436,9 @@ onebox: list_type: compact enable_inline_onebox_on_all_domains: default: false - + force_custom_user_agent_hosts: + default: "http://codepen.io" + type: list spam: add_rel_nofollow_to_user_content: true hide_post_sensitivity: diff --git a/lib/oneboxer.rb b/lib/oneboxer.rb index 9c323f0c391..0c2e74f6995 100644 --- a/lib/oneboxer.rb +++ b/lib/oneboxer.rb @@ -31,7 +31,7 @@ module Oneboxer end def self.force_custom_user_agent_hosts - @force_custom_user_agent_hosts ||= ['http://codepen.io'] + @force_custom_user_agent_hosts ||= SiteSetting.force_custom_user_agent_hosts.split('|') end def self.allowed_post_types diff --git a/spec/components/oneboxer_spec.rb b/spec/components/oneboxer_spec.rb index c04287ecb9d..520825d0f1d 100644 --- a/spec/components/oneboxer_spec.rb +++ b/spec/components/oneboxer_spec.rb @@ -3,6 +3,9 @@ require 'rails_helper' describe Oneboxer do + before do + SiteSetting.force_custom_user_agent_hosts = "http://codepen.io|https://video.discourse.org/" + end it "returns blank string for an invalid onebox" do stub_request(:head, "http://boom.com") @@ -169,4 +172,15 @@ describe Oneboxer do expect(Oneboxer.external_onebox(url)[:onebox]).to be_present end + + it "uses the Onebox custom user agent on specified hosts" do + url = 'https://video.discourse.org/presentation.mp4' + + stub_request(:head, url).to_return(status: 403, body: "", headers: {}) + stub_request(:get, url).to_return(status: 403, body: "", headers: {}) + stub_request(:head, url).with(headers: { "User-Agent" => Onebox.options.user_agent }).to_return(status: 200, body: "", headers: {}) + stub_request(:get, url).with(headers: { "User-Agent" => Onebox.options.user_agent }).to_return(status: 200, body: "", headers: {}) + + expect(Oneboxer.preview(url, invalidate_oneboxes: true)).to be_present + end end