posts: fix exception when viewing post with source Blog..

Fix a PublicSuffix::DomainNotAllowed exception raised with viewing or editing a post
with a source like `Blog.`.

This happened when parsing the post's source. `Danbooru::URL.parse("Blog.")` would
heuristically parse the source into `http://blog`. Calling any methods related to the
URL's hostname or domain would lead to calling `PublicSuffix.parse("blog")`, which
would fail with PublicSuffix::DomainNotAllowed.
This commit is contained in:
evazion
2022-03-21 03:12:17 -05:00
parent defea08084
commit 56f47c60e1
4 changed files with 28 additions and 3 deletions

View File

@@ -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

View File

@@ -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)

View File

@@ -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

View File

@@ -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