diff options
author | Bob Van Landuyt <bob@gitlab.com> | 2018-09-24 17:46:29 +0300 |
---|---|---|
committer | Bob Van Landuyt <bob@vanlanduyt.co> | 2018-09-24 17:53:08 +0300 |
commit | 2ac1702119e36d0fa44188b4716523daba8fb359 (patch) | |
tree | f92da0b031dce6d480e736bf725c56330bbf92b6 /lib | |
parent | 17bd59adecf525e5e0a42101d241d05e29228338 (diff) |
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')
-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 |