'Rails: During asset precompile throws error key must be 16 bytes

I am storing my secret key in environment and /config/environments/production.rb has config.require_master_key = true uncommented

config.require_master_key = true

When running

RAILS_ENV=production bundle exec rake assets:precompile

I get the error

/Users/something/Development/wwwroot/trivial/config/environment.rb:5:in `<main>'
/Users/something/.rbenv/versions/2.5.1/bin/bundle:23:in `load'
/Users/something/.rbenv/versions/2.5.1/bin/bundle:23:in `<main>'

Caused by:
ArgumentError: key must be 16 bytes
/Users/something/Development/wwwroot/trivial/config/environment.rb:5:in `<main>'
/Users/something/.rbenv/versions/2.5.1/bin/bundle:23:in `load'
/Users/something/.rbenv/versions/2.5.1/bin/bundle:23:in `<main>'
Tasks: TOP => environment

any ideas on how to fix this error? What else can I do?



Solution 1:[1]

Your problem is that the key you generated is longer of what rails expects https://github.com/rails/rails/issues/33528#issuecomment-412677795

Solution

You can recreate a new one by deleting your master.key and credentials.yml.enc and run

rails credentials:edit

Solution 2:[2]

I faced the same issue while setting up a Rails 6.0 application on Ubuntu in production.

I was using the figaro gem for my environment variables.

The issue was that I was copying the content of the secret_key_base instead of the master_key

Here's how I solved it

Delete the previous master.key and the credentials.yml.enc file

Recreate a new master.key and credentials.yml.enc:

rails credentials:edit

OR

EDITOR="code --wait" bin/rails credentials:edit  # If you want to use VS Code as your editor

Copy the contents of the master.key, which is of this format:

34d3cc7c5305dde06865acfa473716cd

Replace my RAILS_MASTER_KEY value with the master_key in production:

RAILS_MASTER_KEY: "34d3cc7c5305dde06865acfa473716cd"

And then save it.

Note: You could also experience this issue if you set/specify a wrong RAILS_MASTER_KEY environment variable in your .env files (.env, .env.development, .env.test, .env.production). Say you just want to use it as a placeholder temporarily. This may also throw an error key=': key must be 16 bytes (ArgumentError) if you try to generate new master.key and the credentials.yml.enc files using rails credentials:edit or EDITOR="code --wait" bin/rails credentials:edit

What you have to do is to either provide the right RAILS_MASTER_KEY environment variable in the .env file(s) or comment out the RAILS_MASTER_KEY environment variable if you are not using it.

That's it.

I hope this helps

Solution 3:[3]

For me I had to ensure I remove the quotes around the key in my .env file.

It seems my server(AWS ECS Fargate) was counting the "" as part of the key. Locally it was all fine.

Before

RAILS_MASTER_KEY="12345"

After

RAILS_MASTER_KEY=12345

Solution 4:[4]

You can run this in your terminal

heroku config:set RAILS_MASTER_KEY=`cat config/master.key`

You can follow the tutorial here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Javier Menéndez Rizo
Solution 2
Solution 3 Kaka Ruto
Solution 4 antoniolulee