Class: Puppet::SyntaxCheckers::Base64

Inherits:
Plugins::SyntaxCheckers::SyntaxChecker show all
Defined in:
lib/puppet/syntax_checkers/base64.rb

Instance Method Summary collapse

Instance Method Details

#check(text, syntax, acceptor, source_pos) ⇒ Object

Checks the text for BASE64 syntax issues and reports them to the given acceptor. This checker allows the most relaxed form of Base64, including newlines and missing padding. It also accept URLsafe input.

Parameters:

  • text (String)

    The text to check

  • syntax (String)

    The syntax identifier in mime style (e.g. ‘base64’, ‘text/xxx+base64’)

  • acceptor (#accept)

    A Diagnostic acceptor

  • source_pos (Puppet::Pops::Adapters::SourcePosAdapter)

    A source pos adapter with location information

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/puppet/syntax_checkers/base64.rb', line 17

def check(text, syntax, acceptor, source_pos)
  raise ArgumentError.new(_("Base64 syntax checker: the text to check must be a String.")) unless text.is_a?(String)
  raise ArgumentError.new(_("Base64 syntax checker: the syntax identifier must be a String, e.g. json, data+json")) unless syntax.is_a?(String)
  raise ArgumentError.new(_("Base64 syntax checker: invalid Acceptor, got: '%{klass}'.") % { klass: acceptor.class.name }) unless acceptor.is_a?(Puppet::Pops::Validation::Acceptor)
  cleaned_text = text.gsub(/[\r?\n[:blank:]]/, '')
  begin
    # Do a strict decode64 on text with all whitespace stripped since the non strict version
    # simply skips all non base64 characters
    Base64.strict_decode64(cleaned_text)
  rescue => e
    if (cleaned_text.bytes.to_a.size * 8) % 6 != 0
      msg2 = _("padding is not correct")
    else
      msg2 = _("contains letters outside strict base 64 range (or whitespace)")
    end
    msg = _("Base64 syntax checker: Cannot parse invalid Base64 string - %{msg2}") % { msg2: msg2 }

    # TODO: improve the pops API to allow simpler diagnostic creation while still maintaining capabilities
    # and the issue code. (In this case especially, where there is only a single error message being issued).
    #
    issue = Puppet::Pops::Issues::issue(:ILLEGAL_BASE64) { msg }
    acceptor.accept(Puppet::Pops::Validation::Diagnostic.new(:error, issue, source_pos.file, source_pos, {}))
  end
end