class Puppet::SyntaxCheckers::Base64

Public Instance Methods

check(text, syntax, acceptor, source_pos) click to toggle source

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.

@param text [String] The text to check @param syntax [String] The syntax identifier in mime style (e.g. 'base64', 'text/xxx+base64') @param acceptor [#accept] A Diagnostic acceptor @param source_pos [Puppet::Pops::Adapters::SourcePosAdapter] A source pos adapter with location information @api public

   # File lib/puppet/syntax_checkers/base64.rb
17 def check(text, syntax, acceptor, source_pos)
18   raise ArgumentError.new(_("Base64 syntax checker: the text to check must be a String.")) unless text.is_a?(String)
19   raise ArgumentError.new(_("Base64 syntax checker: the syntax identifier must be a String, e.g. json, data+json")) unless syntax.is_a?(String)
20   raise ArgumentError.new(_("Base64 syntax checker: invalid Acceptor, got: '%{klass}'.") % { klass: acceptor.class.name }) unless acceptor.is_a?(Puppet::Pops::Validation::Acceptor)
21   cleaned_text = text.gsub(/[\r?\n[:blank:]]/, '')
22   begin
23     # Do a strict decode64 on text with all whitespace stripped since the non strict version
24     # simply skips all non base64 characters
25     Base64.strict_decode64(cleaned_text)
26   rescue
27     msg = if (cleaned_text.bytes.to_a.size * 8) % 6 != 0
28             _("Base64 syntax checker: Cannot parse invalid Base64 string - padding is not correct")
29           else
30             _("Base64 syntax checker: Cannot parse invalid Base64 string - contains letters outside strict base 64 range (or whitespace)")
31           end
32 
33     # TODO: improve the pops API to allow simpler diagnostic creation while still maintaining capabilities
34     # and the issue code. (In this case especially, where there is only a single error message being issued).
35     #
36     issue = Puppet::Pops::Issues::issue(:ILLEGAL_BASE64) { msg }
37     acceptor.accept(Puppet::Pops::Validation::Diagnostic.new(:error, issue, source_pos.file, source_pos, {}))
38   end
39 end