diff options
author | Diana Stanley <dstanley@gitlab.com> | 2018-09-25 00:11:35 +0000 |
---|---|---|
committer | Bob Van Landuyt <bob@vanlanduyt.co> | 2018-09-25 09:40:15 +0200 |
commit | ef057bab4344a3bf9c61e1d8d8a73ba098ce7a76 (patch) | |
tree | 7525e3f7a53bf4342191d3b292b2b337c60d9847 /lib | |
parent | 014912084ddfbafa35da6d52f1b1b46b601bfc62 (diff) | |
download | gitlab-ce-ef057bab4344a3bf9c61e1d8d8a73ba098ce7a76.tar.gz |
Merge branch 'security-security-2697-code-highlight-timeout-11-1' into 'security-11-1'
[11.1] Fix syntax highlight taking too long
See merge request gitlab/gitlabhq!2485
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 |