-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add latest changes from gitlab-org/security/gitlab@15-3-stable-ee
- Loading branch information
GitLab Bot
committed
Aug 26, 2022
1 parent
daf5ae5
commit 25ed7b6
Showing
15 changed files
with
156 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
module Banzai | ||
module Filter | ||
class PathologicalMarkdownFilter < HTML::Pipeline::TextFilter | ||
# It's not necessary for this to be precise - we just need to detect | ||
# when there are a non-trivial number of unclosed image links. | ||
# So we don't really care about code blocks, etc. | ||
# See https://gitlab.com/gitlab-org/gitlab/-/issues/370428 | ||
REGEX = /!\[(?:[^\]])+?!\[/.freeze | ||
DETECTION_MAX = 10 | ||
|
||
def call | ||
count = 0 | ||
|
||
@text.scan(REGEX) do |_match| | ||
count += 1 | ||
break if count > DETECTION_MAX | ||
end | ||
|
||
return @text if count <= DETECTION_MAX | ||
|
||
"_Unable to render markdown - too many unclosed markdown image links detected._" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
spec/lib/banzai/filter/pathological_markdown_filter_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
RSpec.describe Banzai::Filter::PathologicalMarkdownFilter do | ||
include FilterSpecHelper | ||
|
||
let_it_be(:short_text) { '![a' * 5 } | ||
let_it_be(:long_text) { ([short_text] * 10).join(' ') } | ||
let_it_be(:with_images_text) { "![One ![one](one.jpg) #{'and\n' * 200} ![two ![two](two.jpg)" } | ||
|
||
it 'detects a significat number of unclosed image links' do | ||
msg = <<~TEXT | ||
_Unable to render markdown - too many unclosed markdown image links detected._ | ||
TEXT | ||
|
||
expect(filter(long_text)).to eq(msg.strip) | ||
end | ||
|
||
it 'does nothing when there are only a few unclosed image links' do | ||
expect(filter(short_text)).to eq(short_text) | ||
end | ||
|
||
it 'does nothing when there are only a few unclosed image links and images' do | ||
expect(filter(with_images_text)).to eq(with_images_text) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.