Skip to content

Frozen string literal mutation causes deprecation warning in s3.rb on Ruby 3.4 #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
kanejamison opened this issue Mar 4, 2025 · 0 comments

Comments

@kanejamison
Copy link

Hey there,

After upgrading to Ruby 3.4 and the 4.5 version of Froala we get the following frozen string literal warnings from /lib/froala-editor-sdk/s3.rb:

/home/runner/work/reponame/reponame/vendor/bundle/ruby/3.4.0/gems/froala-editor-sdk-4.5.0/lib/froala-editor-sdk/s3.rb:34: warning: literal string will be frozen in the future (run with --debug-frozen-string-literal for more information)

The OpenSSL line is the line in question:

    # Builds a HMAC-SHA256 digest using key and data
    # Params:
    # +key+:: Key to use for creating the digest
    # +data+:: Data to be used for creating the digest
    def self.sign(key, data)
      OpenSSL::HMAC.digest(OpenSSL::Digest::SHA256.new, key, data.force_encoding(Encoding::UTF_8))
    end

I don't feel qualified to submit a PR to fix it, but here's the Claude suggested fix in case it's helpful. I haven't tested it.

Here's why this warning appears:

In Ruby 3.4 (and fully implemented in future versions), all string literals will be automatically frozen by default.
In this line, OpenSSL::HMAC.digest() is being called with a string object that's being modified with force_encoding(Encoding::UTF_8).
The warning appears because the OpenSSL library might be attempting to modify the string that's passed to it, but in future Ruby versions, this string will be frozen (immutable), causing potential errors.

The most likely issue is that OpenSSL::HMAC.digest might be trying to modify the input string in place, but when strings are frozen, they can't be modified.
To fix this issue, you could:

Create a mutable copy of the string before passing it to the method:

OpenSSL::HMAC.digest(OpenSSL::Digest::SHA256.new, key, data.force_encoding(Encoding::UTF_8).dup)

Or more cleanly, make a copy and then modify it:

data_copy = data.dup
data_copy.force_encoding(Encoding::UTF_8)
OpenSSL::HMAC.digest(OpenSSL::Digest::SHA256.new, key, data_copy)

This warning is part of Ruby's transition toward making all string literals frozen by default for performance reasons, which has been a gradual process across several Ruby versions.

@kanejamison kanejamison changed the title Frozen String Literal Mutation in S3.rb Frozen string literal mutation causes deprecation warning in s3.rb on Ruby 3.4 Mar 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant