diff options
author | Bob Van Landuyt <bob@gitlab.com> | 2018-10-01 16:48:00 +0000 |
---|---|---|
committer | Bob Van Landuyt <bob@gitlab.com> | 2018-10-01 16:48:00 +0000 |
commit | 7cb9957a33d37394cd884106865e4aedef519e97 (patch) | |
tree | f7eb746219c254d0bd0e139a0a382d5baf9aed2c /lib | |
parent | bfd3062cd3479beedb327e8fed04767f52c5c135 (diff) | |
parent | 3bfc08be9130fef1f07462d543d559f5c7f533a6 (diff) | |
download | gitlab-ce-7cb9957a33d37394cd884106865e4aedef519e97.tar.gz |
Merge branch 'security-2697-code-highlight-timeout' into 'master'
[master] Fix syntax highlight taking too long
Closes #2697
See merge request gitlab/gitlabhq!2467
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 |