diff options
author | Bob Van Landuyt <bob@gitlab.com> | 2018-09-24 18:43:51 +0300 |
---|---|---|
committer | Bob Van Landuyt <bob@vanlanduyt.co> | 2018-09-24 18:59:29 +0300 |
commit | aa76a86217e490b8acd1fa61d72b2a73ecc3d813 (patch) | |
tree | 26325a73c0d056617cde3dc754adc3710b49c0c5 /lib | |
parent | 06cbee3bf9dd960380390c0a5df9b67a52a85ba9 (diff) |
Merge branch 'security-security-2697-code-highlight-timeout-11-2' into 'security-11-2'
[11.2] Fix syntax highlight taking too long
See merge request gitlab/gitlabhq!2484
Diffstat (limited to 'lib')
-rw-r--r-- | lib/gitlab/highlight.rb | 14 |
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 |