Module: Olyx::Guardrails::Text::Base64Decoder

Defined in:
lib/olyx/guardrails/text/base64_decoder.rb

Overview

Decodes one bounded layer of standalone Base64-looking runs. A run is only substituted when it both decodes cleanly and produces printable text, so ordinary alphanumeric text (hex hashes, identifiers) is never corrupted by a coincidental decode.

Constant Summary collapse

RUN =
%r{(?<![A-Za-z0-9+/=])[A-Za-z0-9+/]{16,}={0,2}(?![A-Za-z0-9+/=])}
PADDING_FOR_REMAINDER =

Padding needed to reach a multiple of 4, keyed by length % 4. Unpadded base64 can only end on remainder 0 (none needed), 2, or 3; remainder 1 is not a valid unpadded length and maps to no padding rather than guessing, so decode simply fails for it below.

{ 0 => '', 2 => '==', 3 => '=' }.freeze

Class Method Summary collapse

Class Method Details

.call(value) ⇒ Object



23
24
25
# File 'lib/olyx/guardrails/text/base64_decoder.rb', line 23

def call(value)
  value.gsub(RUN) { |run| decode(run) || run }
end