mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FIX: Invalid URLs cause post not to save
This commit is contained in:
parent
6527862195
commit
dd8a06187a
@ -2,6 +2,9 @@ require 'uri'
|
|||||||
require_dependency 'slug'
|
require_dependency 'slug'
|
||||||
|
|
||||||
class TopicLink < ActiveRecord::Base
|
class TopicLink < ActiveRecord::Base
|
||||||
|
MAX_DOMAIN_LENGTH = 100 unless defined? MAX_DOMAIN_LENGTH
|
||||||
|
MAX_URL_LENGTH = 500 unless defined? MAX_URL_LENGTH
|
||||||
|
|
||||||
belongs_to :topic
|
belongs_to :topic
|
||||||
belongs_to :user
|
belongs_to :user
|
||||||
belongs_to :post
|
belongs_to :post
|
||||||
@ -126,6 +129,7 @@ class TopicLink < ActiveRecord::Base
|
|||||||
|
|
||||||
# Store the canonical URL
|
# Store the canonical URL
|
||||||
topic = Topic.find_by(id: topic_id)
|
topic = Topic.find_by(id: topic_id)
|
||||||
|
topic_id = nil unless topic
|
||||||
|
|
||||||
if topic.present?
|
if topic.present?
|
||||||
url = "#{Discourse.base_url}#{topic.relative_url}"
|
url = "#{Discourse.base_url}#{topic.relative_url}"
|
||||||
@ -142,6 +146,9 @@ class TopicLink < ActiveRecord::Base
|
|||||||
reflected_post = Post.find_by(topic_id: topic_id, post_number: post_number.to_i)
|
reflected_post = Post.find_by(topic_id: topic_id, post_number: post_number.to_i)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
next if url && url.length > MAX_URL_LENGTH
|
||||||
|
next if parsed && parsed.host && parsed.host.length > MAX_DOMAIN_LENGTH
|
||||||
|
|
||||||
added_urls << url
|
added_urls << url
|
||||||
TopicLink.create(post_id: post.id,
|
TopicLink.create(post_id: post.id,
|
||||||
user_id: post.user_id,
|
user_id: post.user_id,
|
||||||
|
@ -26,19 +26,25 @@ describe TopicLink do
|
|||||||
|
|
||||||
describe 'external links' do
|
describe 'external links' do
|
||||||
before do
|
before do
|
||||||
@post = Fabricate(:post_with_external_links, user: @user, topic: @topic)
|
@post = Fabricate(:post, raw: "
|
||||||
|
http://a.com/
|
||||||
|
http://b.com/b
|
||||||
|
http://#{'a'*200}.com/invalid
|
||||||
|
http://b.com/#{'a'*500}
|
||||||
|
", user: @user, topic: @topic)
|
||||||
|
|
||||||
TopicLink.extract_from(@post)
|
TopicLink.extract_from(@post)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'works' do
|
it 'works' do
|
||||||
# has the forum topic links
|
# has the forum topic links
|
||||||
@topic.topic_links.count.should == 4
|
@topic.topic_links.count.should == 2
|
||||||
|
|
||||||
# works with markdown links
|
# works with markdown links
|
||||||
@topic.topic_links.exists?(url: "http://forumwarz.com").should be_true
|
@topic.topic_links.exists?(url: "http://a.com/").should be_true
|
||||||
|
|
||||||
#works with markdown links followed by a period
|
#works with markdown links followed by a period
|
||||||
@topic.topic_links.exists?(url: "http://www.codinghorror.com/blog").should be_true
|
@topic.topic_links.exists?(url: "http://b.com/b").should be_true
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
@ -53,9 +59,10 @@ describe TopicLink do
|
|||||||
@other_post = @other_topic.posts.create(user: @user, raw: "some content for the second post")
|
@other_post = @other_topic.posts.create(user: @user, raw: "some content for the second post")
|
||||||
|
|
||||||
@url = "http://#{test_uri.host}/t/#{@other_topic.slug}/#{@other_topic.id}/#{@other_post.post_number}"
|
@url = "http://#{test_uri.host}/t/#{@other_topic.slug}/#{@other_topic.id}/#{@other_post.post_number}"
|
||||||
|
@invalid_url = "http://#{test_uri.host}/t/#{@other_topic.slug}/9999999999999999999999999999999"
|
||||||
|
|
||||||
@topic.posts.create(user: @user, raw: 'initial post')
|
@topic.posts.create(user: @user, raw: 'initial post')
|
||||||
@post = @topic.posts.create(user: @user, raw: "Link to another topic:\n\n#{@url}\n\n")
|
@post = @topic.posts.create(user: @user, raw: "Link to another topic:\n\n#{@url}\n\n#{@invalid_url}")
|
||||||
@post.reload
|
@post.reload
|
||||||
|
|
||||||
TopicLink.extract_from(@post)
|
TopicLink.extract_from(@post)
|
||||||
|
Loading…
Reference in New Issue
Block a user