class method self.new

Ruby on Rails 7.2.3

Since v2.3.18

Available in: v2.3.18 v3.0.20 v3.1.12 v3.2.22.5 v4.0.13 v4.1.16 v4.2.9 v5.2.8.1 v6.0.6 v6.1.7.10 v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

self.new(secret, **options)

Initialize a new MessageVerifier with a secret for the signature.

Options

:digest

Digest used for signing. The default is "SHA1". See OpenSSL::Digest for alternatives.

:serializer

The serializer used to serialize message data. You can specify any object that responds to dump and load, or you can choose from several preconfigured serializers: :marshal, :json_allow_marshal, :json, :message_pack_allow_marshal, :message_pack.

The preconfigured serializers include a fallback mechanism to support multiple deserialization formats. For example, the :marshal serializer will serialize using Marshal, but can deserialize using Marshal, ActiveSupport::JSON, or ActiveSupport::MessagePack. This makes it easy to migrate between serializers.

The :marshal, :json_allow_marshal, and :message_pack_allow_marshal serializers support deserializing using Marshal, but the others do not. Beware that Marshal is a potential vector for deserialization attacks in cases where a message signing secret has been leaked. If possible, choose a serializer that does not support Marshal.

The :message_pack and :message_pack_allow_marshal serializers use ActiveSupport::MessagePack, which can roundtrip some Ruby types that are not supported by JSON, and may provide improved performance. However, these require the msgpack gem.

When using Rails, the default depends on config.active_support.message_serializer. Otherwise, the default is :marshal.

:url_safe

By default, MessageVerifier generates RFC 4648 compliant strings which are not URL-safe. In other words, they can contain “+” and “/”. If you want to generate URL-safe strings (in compliance with “Base 64 Encoding with URL and Filename Safe Alphabet” in RFC 4648), you can pass true. Note that MessageVerifier will always accept both URL-safe and URL-unsafe encoded messages, to allow a smooth transition between the two settings.

:force_legacy_metadata_serializer

Whether to use the legacy metadata serializer, which serializes the message first, then wraps it in an envelope which is also serialized. This was the default in Rails 7.0 and below.

If you don’t pass a truthy value, the default is set using config.active_support.use_message_serializer_for_metadata.

Parameters

secret req
options keyrest
Source
# File activesupport/lib/active_support/message_verifier.rb, line 167
    def initialize(secret, **options)
      raise ArgumentError, "Secret should not be nil." unless secret
      super(**options)
      @secret = secret
      @digest = options[:digest]&.to_s || "SHA1"
    end

Defined in activesupport/lib/active_support/message_verifier.rb line 167 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveSupport::MessageVerifier

Type at least 2 characters to search.

↑↓ navigate · open · esc close