-
Notifications
You must be signed in to change notification settings - Fork 582
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate and persist a 256 bit session secret by default
* Add `urandom_bytes` and `urandom_urlsafe` to `Mojo::Util` for generating secure random bits from the system csprng. * Don't use the hard coded moniker as the default secret * Generate and store a strong secret if not exists in `$ENV{MOJO_HOME}/mojo.secrets`, overridable with `$ENV{MOJO_SECRETS_FILE}` when app->secrets is called * Only load secrets from `mojo.secrets` that are over 22 chars * Use `urandom_urlsafe` when generating CSRF tokens * Use `urandom_urlsafe` when in `mojo generate app` * Add `mojo generate secret` * Tests: - Add misc tests for generating and loading mojo.secrets in `t/mojolicious/secret/` and for `mojo generate secret`. - Add a default secret in `t/mojolicious/mojo.secrets` so other session checks work
- Loading branch information
Showing
16 changed files
with
277 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package Mojolicious::Command::Author::generate::secret; | ||
use Mojo::Base 'Mojolicious::Command'; | ||
use Mojo::File qw(path); | ||
use Mojo::Util qw(urandom_urlsafe); | ||
|
||
has description => 'Generate secret'; | ||
has usage => sub { shift->extract_usage }; | ||
|
||
sub run { | ||
my ($self, $secret_file) = (shift, shift); | ||
|
||
$secret_file //= $self->app->secrets_file; | ||
|
||
my $token = urandom_urlsafe(); | ||
|
||
print "Writing @{[ length($token) ]} byte to $secret_file\n"; | ||
|
||
path($secret_file)->touch->chmod(0600)->spew($token); | ||
} | ||
|
||
1; | ||
|
||
=encoding utf8 | ||
=head1 NAME | ||
Mojolicious::Command::Author::generate::secret - Secret generator command | ||
=head1 SYNOPSIS | ||
Usage: APPLICATION generate secret [PATH] | ||
mojo generate secret | ||
mojo generate secret /path/to/secret | ||
Options: | ||
-h, --help Show this summary of available options | ||
=head1 DESCRIPTION | ||
L<Mojolicious::Command::Author::generate::secret> generates a secret token for protecting session cookies | ||
This is a core command, that means it is always enabled and its code a good example for learning to build new commands, | ||
you're welcome to fork it. | ||
See L<Mojolicious::Commands/"COMMANDS"> for a list of commands that are available by default. | ||
=head1 ATTRIBUTES | ||
L<Mojolicious::Command::Author::generate::secret> inherits all attributes from L<Mojolicious::Command> and implements | ||
the following new ones. | ||
=head2 description | ||
my $description = $app->description; | ||
$app = $app->description('Foo'); | ||
Short description of this command, used for the command list. | ||
=head2 usage | ||
my $usage = $app->usage; | ||
$app = $app->usage('Foo'); | ||
Usage information for this command, used for the help screen. | ||
=head1 METHODS | ||
L<Mojolicious::Command::Author::generate::secret> inherits all methods from L<Mojolicious::Command> and implements | ||
the following new ones. | ||
=head2 run | ||
$app->run(@ARGV); | ||
Run this command. | ||
=head1 SEE ALSO | ||
L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>. | ||
=cut |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
NeverGonnaGiveYouUpNeverGonnaLetYouDown |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
NeverGonnaMakeYouCryNeverGonnaSayGoodbye | ||
skip-me | ||
NeverGonnaTellALieAndHurtYou |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use Mojo::Base -strict; | ||
|
||
use Mojo::File qw(tempdir path); | ||
use Test::Mojo; | ||
use Test::More; | ||
use Mojolicious::Lite; | ||
|
||
|
||
my $tmpdir = tempdir; | ||
my $file = $tmpdir->child("mojo.secrets"); | ||
$ENV{MOJO_SECRETS_FILE} = $file; | ||
|
||
like app->secrets->[0], qr/^[-A-Za-z0-9_]{43}$/, 'secret was generated, and matches expected urandom_urlsafe format'; | ||
is app->secrets->[0], $file->slurp, 'secret stored at $ENV{MOJO_SECRETS_FILE} is the same as app->secrets->[0]'; | ||
|
||
done_testing(); |
Oops, something went wrong.