Search code examples
perlmojoliciousflash-message

perl mojolicious - using flash gives wierd error


The below code is giving the error:

Can't use an undefined value as an ARRAY reference at /usr/local/share/perl/5.22.1/Mojolicious/Controller.pm line 286.

and I am not particularly sure how i'd go about handling it. Especially as it quotes an error in the source code that seems to be related to encryption of cookies. And my application contains no cookies or encryption so that is suprising.

 sub remove {
    my $self = shift;
    my $host_id = $self->stash('host_id');

    $self->hosts->remove($self->stash('host')->{host_id});

    $self->flash(message => 'User created successfully!');
    $self->redirect_to('hosts');
}

Solution

  • This most likely happens when you have undef as your secrets. The error comes from the following line:

    my $checksum = Mojo::Util::hmac_sha1_sum($value, $self->app->secrets->[0]);
    

    Normally, the secret is pre-generated which is insecure and needs to be defined by you in the config of the app. An example of it being used is in the Mojo::Pg example application:

    {
        pg      => 'postgresql://tester:testing@/test',
        secrets => ['s3cret']
    }
    

    Which is then consumed by the application itself

    $self->secrets($self->config('secrets'));
    

    If your application sets the secrets from a config file but the config file doesn't declare a secrets, or the key has been misspelt, the error you write will be given back whenever you try to set a cookie, such as using the flash.