{"record":{"id":"9577efddfa6c2223","repo":"puppetlabs/puppet","slug":"the-given-string-in-encoding-enc-is-invalid","errorCode":null,"errorMessage":"The given string in encoding '%{enc}' is invalid. Cannot create a Binary UTF-8 representation","messagePattern":"The given string in encoding '%(.+?)' is invalid\\. Cannot create a Binary UTF-8 representation","errorType":"exception","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"lib/puppet/pops/types/p_binary_type.rb","lineNumber":77,"sourceCode":"    end\n\n    # Creates a new Binary from a String containing text/binary in its given encoding. If the string's encoding\n    # is not already UTF-8, the string is first transcoded to UTF-8.\n    # This means that this binary will have the UTF-8 byte representation of the original string.\n    # For this to be valid, the encoding used in the given string must be valid.\n    # The validity of the given string is therefore asserted.\n    #\n    # The given string will be frozen as a side effect if it is in ASCII-8BIT encoding. If this is not\n    # wanted, a copy should be given to this method.\n    #\n    # @param [String] A string with valid content in its given encoding\n    # @return [Puppet::Pops::Types::PBinaryType::Binary] with the UTF-8 representation of the UTF-8 transcoded string\n    # @api public\n    #\n    def self.from_string(encoded_string)\n      enc = encoded_string.encoding.name\n      unless encoded_string.valid_encoding?\n        raise ArgumentError, _(\"The given string in encoding '%{enc}' is invalid. Cannot create a Binary UTF-8 representation\") % { enc: enc }\n      end\n\n      # Convert to UTF-8 (if not already UTF-8), and then to binary\n      encoded_string = (enc == \"UTF-8\") ? encoded_string.dup : encoded_string.encode('UTF-8')\n      encoded_string.force_encoding(\"ASCII-8BIT\")\n      new(encoded_string)\n    end\n\n    # Creates a new Binary from a String containing raw binary data of unknown encoding. If the string's encoding\n    # is not already ASCII-8BIT, a copy of the string is forced to ASCII-8BIT (that is Ruby's \"binary\" format).\n    # This means that this binary will have the exact same content, but the string will considered\n    # to hold a sequence of bytes in the range 0 to 255.\n    #\n    # @param [String] A string with binary data\n    # @api private\n    #\n    def initialize(bin)\n      @binary_buffer = (bin.encoding.name == \"ASCII-8BIT\" ? bin : bin.b).freeze","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/puppetlabs/puppet/blob/e227c27540975c25aa22d533a52424a9d2fc886a/lib/puppet/pops/types/p_binary_type.rb#L59-L95","documentation":"PBinaryType::Binary.from_string builds a Binary by validating the input string in its declared encoding, transcoding to UTF-8, then forcing ASCII-8BIT. If the string contains byte sequences that are invalid in its own encoding, the validity assertion fails with this ArgumentError because no lossless conversion exists.","triggerScenarios":"Ruby: Puppet::Pops::Types::PBinaryType::Binary.from_string(str) where !str.valid_encoding?. Puppet DSL: Binary('...') with an argument carrying invalid bytes for its encoding — typically data that was force_encoding'd to UTF-8 upstream without being transcoded.","commonSituations":"Reading raw bytes from a socket/file opened without binary mode and routing them through Binary(); database or JSON strings already corrupted by a prior force_encoding; concatenating UTF-8 and binary fragments into one string.","solutions":["If the data is raw bytes of unknown/untrusted encoding, use Binary.from_binary_string(raw.dup) instead — it performs no transcoding.","Fix the upstream read: open files/sockets with 'rb' / set binmode so bytes keep ASCII-8BIT encoding from the start.","Only if losing bad bytes is acceptable, clean the string first: str.scrub!(\"?\") or re-encode from the true source encoding."],"exampleFix":"# before\nbin = Puppet::Pops::Types::PBinaryType::Binary.from_string(raw_bytes)\n\n# after\nbin = Puppet::Pops::Types::PBinaryType::Binary.from_binary_string(raw_bytes.dup)","handlingStrategy":"validation","validationCode":"# Ruby — choose the right constructor up front\nif raw.encoding == Encoding::ASCII_8BIT || !raw.valid_encoding?\n  bin = Puppet::Pops::Types::PBinaryType::Binary.from_binary_string(raw.dup)\nelse\n  bin = Puppet::Pops::Types::PBinaryType::Binary.from_string(raw)\nend","typeGuard":"def transcodable_string?(s)\n  s.is_a?(String) && s.valid_encoding?\nend","tryCatchPattern":"begin\n  Puppet::Pops::Types::PBinaryType::Binary.from_string(s)\nrescue ArgumentError\n  Puppet::Pops::Types::PBinaryType::Binary.from_binary_string(s.dup) # treat as raw bytes\nend","preventionTips":["Open files and sockets in binary mode when the payload is binary; never force_encoding to UTF-8 without transcoding.","Use from_binary_string for raw byte payloads; reserve from_string for valid encoded text."],"tags":["puppet","binary","encoding","string-validation","pcore"],"backgroundTag":"invalid-string-encoding","analyzedSha":"e227c27540975c25aa22d533a52424a9d2fc886a","analyzedAt":"2026-08-21T20:49:46.650Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}