summaryrefslogtreecommitdiff
path: root/lib/gitlab/highlight.rb
diff options
context:
space:
mode:
authorBob Van Landuyt <bob@gitlab.com>2018-09-24 14:46:29 +0000
committerBob Van Landuyt <bob@vanlanduyt.co>2018-09-24 16:53:08 +0200
commit2ac1702119e36d0fa44188b4716523daba8fb359 (patch)
treef92da0b031dce6d480e736bf725c56330bbf92b6 /lib/gitlab/highlight.rb
parent17bd59adecf525e5e0a42101d241d05e29228338 (diff)
downloadgitlab-ce-2ac1702119e36d0fa44188b4716523daba8fb359.tar.gz
Merge branch 'security-2697-code-highlight-timeout-11-3' into 'security-11-3'
[11.3] Fix syntax highlight taking too long See merge request gitlab/gitlabhq!2524
Diffstat (limited to 'lib/gitlab/highlight.rb')
-rw-r--r--lib/gitlab/highlight.rb14
1 files changed, 13 insertions, 1 deletions
diff --git a/lib/gitlab/highlight.rb b/lib/gitlab/highlight.rb
index 5408a1a6838..0b6cc893db1 100644
--- a/lib/gitlab/highlight.rb
+++ b/lib/gitlab/highlight.rb
@@ -1,5 +1,8 @@
module Gitlab
class Highlight
+ TIMEOUT_BACKGROUND = 30.seconds
+ TIMEOUT_FOREGROUND = 3.seconds
+
def self.highlight(blob_name, blob_content, repository: nil, plain: false)
new(blob_name, blob_content, repository: repository)
.highlight(blob_content, continue: false, plain: plain)
@@ -51,11 +54,20 @@ module Gitlab
end
def highlight_rich(text, continue: true)
- @formatter.format(lexer.lex(text, continue: continue), tag: lexer.tag).html_safe
+ tag = lexer.tag
+ tokens = lexer.lex(text, continue: continue)
+ Timeout.timeout(timeout_time) { @formatter.format(tokens, tag: tag).html_safe }
+ rescue Timeout::Error => e
+ Gitlab::Sentry.track_exception(e)
+ highlight_plain(text)
rescue
highlight_plain(text)
end
+ def timeout_time
+ Sidekiq.server? ? TIMEOUT_BACKGROUND : TIMEOUT_FOREGROUND
+ end
+
def link_dependencies(text, highlighted_text)
Gitlab::DependencyLinker.link(blob_name, text, highlighted_text)
end