module
Ruby::Marshal
Overview
Minimal reader and writer for Ruby's Marshal format, version 4.8.
This exists for one reason: Rails fingerprints an ActiveStorage variation with
OpenSSL::Digest::SHA1.base64digest(Marshal.dump(transformations)), and that fingerprint is the
variation_digest column a variant record is looked up by. Reproducing the fingerprint means
reproducing the dump byte for byte, so only the types a variation can hold are supported.
Rails calls deep_symbolize_keys on the transformations before dumping them, so every hash key
is written as a Ruby Symbol regardless of how it arrived here.
https://github.com/rails/rails/blob/v8.1.3/activestorage/app/models/active_storage/variation.rb
https://github.com/ruby/ruby/blob/master/marshal.c
Defined in:
ruby/marshal.crConstant Summary
-
ENCODING_IVAR =
"E" -
Name of the instance variable carrying a string's encoding.
-
MAJOR_VERSION =
4_u8 -
MINOR_VERSION =
8_u8 -
TYPE_ARRAY =
91_u8 -
TYPE_FALSE =
70_u8 -
TYPE_FIXNUM =
105_u8 -
TYPE_FLOAT =
102_u8 -
TYPE_HASH =
123_u8 -
TYPE_NIL =
48_u8 -
Type tags, as written by
marshal.c. -
TYPE_STRING =
34_u8 -
TYPE_SYMBOL =
58_u8 -
TYPE_SYMBOL_LINK =
59_u8 -
TYPE_TRUE =
84_u8 -
TYPE_WITH_INSTANCES =
73_u8
Class Method Summary
-
.dump(value : String) : Bytes
Returns value as a
Marshalpayload. -
.dump_symbolized(value : JSON::Any, symbolize_string_values : Bool = false) : Bytes
Returns the
Marshal.dumpbytes of the passed JSON value, with every hash key written as a RubySymbol. -
.load(data : Bytes) : String
Returns the
Stringcarried by the dataMarshalpayload. -
.load(data : String) : String
Returns the
Stringcarried by the dataMarshalpayload.
Class Method Detail
Returns value as a Marshal payload.
Rails pins Marshal as the serializer of its encrypted files, so the plaintext inside a
config/credentials.yml.enc envelope is Marshal.dump(yaml) rather than the YAML text:
# activesupport/lib/active_support/encrypted_file.rb, Rails v8.1.3
ActiveSupport::MessageEncryptor.new([key].pack("H*"), cipher: CIPHER, serializer: Marshal)
The string is dumped as UTF-8, which is what Marshal.dump emits for any Ruby string in that
encoding: a " object wrapped in an I instance-variable record carrying :E => true. Ruby
restores it as a UTF-8 String, so the YAML parser on the other side sees text rather than
bytes.
This is not #dump_symbolized: that one fingerprints a variation and only writes the types a
variation can hold. A String is the only type this pair handles, which covers every file
Rails encrypts, since EncryptedFile passes the contents around as a string.
Returns the Marshal.dump bytes of the passed JSON value, with every hash key written as a
Ruby Symbol.
With symbolize_string_values, string values are written as symbols too. A variant spec like
format: :jpg carries symbol values in the app, but JSON has no symbols, so the same
transformations arrive as strings after travelling through a signed URL key — and Rails
digests whichever dump it received. Both forms are offered where a variant record may be
looked up, see ActiveStorage::Variation#digest_candidates.
Returns the String carried by the data Marshal payload.
Both string forms Ruby emits are accepted: the bare " object a binary (ASCII-8BIT) string
dumps to, and the I-wrapped form a UTF-8 or US-ASCII string dumps to. Rails writes the
former whenever it re-encrypts a file it read back from the editor through File#binread, and
the latter when a generator writes the initial template, so a credentials file in the wild can
be either.
Raises Error when the payload is not a dumped string or when the restored bytes are not
valid UTF-8, rather than substituting replacement characters for them.
Returns the String carried by the data Marshal payload.
Both string forms Ruby emits are accepted: the bare " object a binary (ASCII-8BIT) string
dumps to, and the I-wrapped form a UTF-8 or US-ASCII string dumps to. Rails writes the
former whenever it re-encrypts a file it read back from the editor through File#binread, and
the latter when a generator writes the initial template, so a credentials file in the wild can
be either.
Raises Error when the payload is not a dumped string or when the restored bytes are not
valid UTF-8, rather than substituting replacement characters for them.