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

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