diff --git a/app/logical/danbooru/url.rb b/app/logical/danbooru/url.rb index 5a0641b09..d421c7037 100644 --- a/app/logical/danbooru/url.rb +++ b/app/logical/danbooru/url.rb @@ -45,6 +45,7 @@ module Danbooru @url.path = nil if @url.path == "/" raise Error, "#{original_url} is not an http:// URL" if !@url.normalized_scheme.in?(["http", "https"]) + raise Error, "#{original_url} is not a valid hostname" if parsed_domain.nil? rescue Addressable::URI::InvalidURIError => e raise Error, e end @@ -113,6 +114,8 @@ module Danbooru # @return [PublicSuffix::Domain] def parsed_domain @parsed_domain ||= PublicSuffix.parse(host) + rescue PublicSuffix::DomainNotAllowed + nil end end end diff --git a/app/models/post.rb b/app/models/post.rb index 4eeddc8b1..7ea360ff4 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -308,11 +308,12 @@ class Post < ApplicationRecord end def normalized_source + return source unless web_source? Sources::Strategies.normalize_source(source) end def source_domain - return "" unless source =~ %r{\Ahttps?://}i + return "" unless web_source? Danbooru::URL.parse(normalized_source)&.domain.to_s end @@ -475,7 +476,7 @@ class Post < ApplicationRecord tags << "ugoira" end - if source.present? && source !~ %r{\Ahttps?://}i + if source.present? && !web_source? tags << "non-web_source" end @@ -645,6 +646,10 @@ class Post < ApplicationRecord end end + def web_source? + source.match?(%r{\Ahttps?://}i) + end + def has_tag?(tag) tag_string.match?(/(?:^| )(?:#{tag})(?:$| )/) end @@ -1149,6 +1154,7 @@ class Post < ApplicationRecord module PixivMethods def parse_pixiv_id self.pixiv_id = nil + return unless web_source? site = Sources::Strategies::Pixiv.new(source) if site.match? @@ -1255,7 +1261,7 @@ class Post < ApplicationRecord def has_artist_tag return if !new_record? - return if source !~ %r{\Ahttps?://} + return if !web_source? return if has_tag?("artist_request") || has_tag?("official_art") return if tags.any?(&:artist?) return if Sources::Strategies.find(source).is_a?(Sources::Strategies::Null) diff --git a/test/functional/posts_controller_test.rb b/test/functional/posts_controller_test.rb index f4a0c1645..563ccd6b1 100644 --- a/test/functional/posts_controller_test.rb +++ b/test/functional/posts_controller_test.rb @@ -646,6 +646,15 @@ class PostsControllerTest < ActionDispatch::IntegrationTest end end + context "with a non-web source" do + should "render" do + @post.update!(source: "Blog.") + get post_path(@post) + + assert_response :success + end + end + should "respect the disable tagged filenames option in the Download link" do @user.update!(disable_tagged_filenames: true) get_auth post_path(@post), @user diff --git a/test/unit/post_test.rb b/test/unit/post_test.rb index 98a025215..b678a048f 100644 --- a/test/unit/post_test.rb +++ b/test/unit/post_test.rb @@ -1368,6 +1368,13 @@ class PostTest < ActiveSupport::TestCase @post.pixiv_id = nil end end + + context "like 'Blog.'" do + should "not raise an exception" do + @post.update!(source: "Blog.") + assert_equal("Blog.", @post.source) + end + end end context "when validating tags" do