Class: Gitlab::WikiPages::FrontMatterParser

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/wiki_pages/front_matter_parser.rb

Defined Under Namespace

Classes: Block, Result

Constant Summary collapse

FEATURE_FLAG =
:wiki_front_matter
MAX_SLUGS =

We limit the maximum length of text we are prepared to parse as YAML, to avoid exploitations and attempts to consume memory and CPU. We allow for:

- a title line
- a "slugs:" line
- and up to 50 slugs

This limit does not take comments into account.

50
SLUG_LINE_LENGTH =
(4 + Gitlab::WikiPages::MAX_DIRECTORY_BYTES + 1 + Gitlab::WikiPages::MAX_TITLE_BYTES)
MAX_FRONT_MATTER_LENGTH =
(8 + Gitlab::WikiPages::MAX_TITLE_BYTES) + 7 + (SLUG_LINE_LENGTH * MAX_SLUGS)
ParseError =
Class.new(StandardError)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wiki_content, feature_gate) ⇒ FrontMatterParser

Returns a new instance of FrontMatterParser.

Parameters:

  • wiki_content (String)
  • feature_gate (FeatureGate)

    The scope for feature availability



34
35
36
37
# File 'lib/gitlab/wiki_pages/front_matter_parser.rb', line 34

def initialize(wiki_content, feature_gate)
  @wiki_content = wiki_content
  @feature_gate = feature_gate
end

Class Method Details

.enabled?(gate = nil) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/gitlab/wiki_pages/front_matter_parser.rb', line 39

def self.enabled?(gate = nil)
  Feature.enabled?(FEATURE_FLAG, gate)
end

Instance Method Details

#parseObject



43
44
45
46
47
48
49
50
# File 'lib/gitlab/wiki_pages/front_matter_parser.rb', line 43

def parse
  return empty_result unless enabled? && wiki_content.present?
  return empty_result(block.error) unless block.valid?

  Result.new(front_matter: block.data, content: strip_front_matter_block)
rescue ParseError => error
  empty_result(:parse_error, error)
end