Jack Moore

Email: jack(at)jmoore53.com
Project Updates

Rails ActiveSupport::MessageVerifier::InvalidSignature

13 Apr 2020 » rails, encryption, ruby, config, error, caching, database

Gist of the issue was in fact that Rails was trying to load encrypted cached variables from the database, but was unable to decrpyt them on the fly.

I tried to disable the cache on the database with uncached, but that ended up not working.

The fix I applied was to catch the InvalidSignature and return nil. This is actually terrible, but it works for the time being and allows me to load the application and the console without running into any errors. The good news with the fix and the data returning nil is that it is able to easily be tested if the database is filled with any data.

I can only assume the keys are correct and the yml file isn’t broken, which isn’t too difficult to ensure.

    def self.last_base_url(n=0)
        n = n
        if self.order(id: :desc).limit(1).offset(n).first.base_url != nil
            return self.order(id: :desc).limit(1).offset(n).first.base_url
        else
            while self.order(id: :desc).limit(1).offset(n).first.base_url == nil
                n = n + 1
                if self.order(id: :desc).limit(1).offset(n).first.base_url != nil
                    return self.order(id: :desc).limit(1).offset(n).first.base_url
                end
            end
        end
        return nil
    rescue NoMethodError
        puts "Reached end of loop and tried to pull last base url where it does not exist, returning nil"
        return nil

    # Fix was to catch the InvalidSignature
    # This is really bad, but works
    # Currently looking into better solutions
    rescue ActiveSupport::MessageVerifier::InvalidSignature
        puts "Invalid Signature, Likely due to rails attempting to load these from cache"
        puts "Likely a big time issue if you are passing the wrong keys in.."
        puts "Beware of silent errors."
        return nil
    end

© Jack Moore