diff options
author | Thibaut Girka <thib@sitedethib.com> | 2020-10-13 12:47:38 +0200 |
---|---|---|
committer | ThibG <thib@sitedethib.com> | 2020-10-13 14:35:07 +0200 |
commit | 311f457430e4863f3c74fe6e222a96f62fc0148d (patch) | |
tree | 587ad3d1ce347a3bfe4a68e83c60e23188df8325 | |
parent | cd861c051ce5500df49d2fc41b2a6084faa34620 (diff) |
Fix crash when autolinking an invalid URI in Markdown
-rw-r--r-- | app/lib/formatter.rb | 2 | ||||
-rw-r--r-- | spec/lib/formatter_spec.rb | 16 |
2 files changed, 16 insertions, 2 deletions
diff --git a/app/lib/formatter.rb b/app/lib/formatter.rb index c0f7866bf..e7bb0743d 100644 --- a/app/lib/formatter.rb +++ b/app/lib/formatter.rb @@ -11,6 +11,8 @@ class HTMLRenderer < Redcarpet::Render::HTML def autolink(link, link_type) return link if link_type == :email Formatter.instance.link_url(link) + rescue Addressable::URI::InvalidURIError, IDN::Idna::IdnaError + encode(link) end private diff --git a/spec/lib/formatter_spec.rb b/spec/lib/formatter_spec.rb index 633d59c2a..efefb8f00 100644 --- a/spec/lib/formatter_spec.rb +++ b/spec/lib/formatter_spec.rb @@ -336,11 +336,22 @@ RSpec.describe Formatter do end context do + let(:content_type) { 'text/plain' } + subject do - status = Fabricate(:status, text: text, uri: nil) + status = Fabricate(:status, text: text, content_type: content_type, uri: nil) Formatter.instance.format(status) end + context 'given an invalid URL (invalid port)' do + let(:text) { 'https://foo.bar:X/' } + let(:content_type) { 'text/markdown' } + + it 'outputs the raw URL' do + is_expected.to eq '<p>https://foo.bar:X/</p>' + end + end + include_examples 'encode and link URLs' end @@ -464,7 +475,8 @@ RSpec.describe Formatter do subject { Formatter.instance.plaintext(status) } context 'given a post with local status' do - let(:status) { Fabricate(:status, text: '<p>a text by a nerd who uses an HTML tag in text</p>', uri: nil) } + let(:status) { Fabricate(:status, text: '<p>a text by a nerd who uses an HTML tag in text</p>', content_type: content_type, uri: nil) } + let(:content_type) { 'text/plain' } it 'returns the raw text' do is_expected.to eq '<p>a text by a nerd who uses an HTML tag in text</p>' |