Skip to content
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

Add MFA protection to several pages of the PAUSE #455

Draft
wants to merge 19 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cpanfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
requires 'Apache::Session::Counted';
requires 'Auth::GoogleAuth', '1.05';
requires 'BSD::Resource';
requires 'CPAN::Checksums', '1.050';
requires 'CPAN::DistnameInfo';
Expand All @@ -22,6 +23,7 @@ requires 'HTML::Entities';
requires 'HTTP::Date';
requires 'HTTP::Status';
requires 'HTTP::Tiny', '0.059';
requires 'Imager::QRCode';
requires 'IO::Socket::SSL', '1.56';
requires 'IPC::Run3';
requires 'JSON';
Expand Down
2 changes: 2 additions & 0 deletions doc/authen_pause.schema.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ CREATE TABLE usertable (
`changed` int(11) DEFAULT NULL,
changedby char(10) DEFAULT NULL,
lastvisit datetime DEFAULT NULL,
mfa_secret32 varchar(16) DEFAULT NULL,
mfa_recovery_codes text DEFAULT NULL,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like I'm probably going to ask a lot of questions. I hope it's useful and not just annoying.

  1. Does mfa mean "MFA is enabled"? If so, is there any value to having that and mfa_secret32? I would expect that you can't temporarily turn off MFA and turn it back on with the same secret, so no extra field is needed.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to store mfa_last_verified_at with the time window in which it was verified. If a OTP can be used more than once, it isn't a OTP! Better practice is something like:

  • TOTP tokens change every 30s
  • we accept the TOTP token for now, 30s ago, and 30s from now
  • we store the time seed of any verified TOTP
  • we never accept a TOTP with a seed less than or equal to the last one

Otherwise, the token used at 12:00:00 has 89 seconds to be exfiltrated and used again.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does mfa mean "MFA is enabled"? If so, is there any value to having that and mfa_secret32

Maybe we don't need mfa column anymore.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed mfa column with e1549a5

PRIMARY KEY (`user`),
KEY usertable_password (`password`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 PACK_KEYS=1;
Expand Down
2 changes: 2 additions & 0 deletions doc/schemas/authen_pause.schema.sqlite
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ CREATE TABLE usertable (
changed int(11) DEFAULT NULL,
changedby char(10) DEFAULT NULL,
lastvisit datetime DEFAULT NULL,
mfa_secret32 varchar(16) DEFAULT NULL,
mfa_recovery_codes text DEFAULT NULL,
PRIMARY KEY (user)
);

Expand Down
1 change: 1 addition & 0 deletions lib/PAUSE.pm
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ $PAUSE::Config ||=
HTTP_ERRORLOG => '/var/log/nginx/error.log', # harmless use in cron-daily
INCOMING => 'file://data/pause/incoming/',
INCOMING_LOC => '/data/pause/incoming',
INCOMING_TMP => '/data/pause/tmp/',
MAIL_MAILER => ["sendmail"],
MAXRETRIES => 16,
MIRRORCONFIG => '/usr/local/mirror/mymirror.config',
Expand Down
5 changes: 4 additions & 1 deletion lib/pause_2017/PAUSE/Web.pm
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ sub startup {

# Load plugins to modify path/set stash values/provide helper methods
$app->plugin("WithCSRFProtection");
$app->plugin("PAUSE::Web::Plugin::WithMFAProtection");
$app->plugin("PAUSE::Web::Plugin::ConfigPerRequest");
$app->plugin("PAUSE::Web::Plugin::IsPauseClosed");
$app->plugin("PAUSE::Web::Plugin::GetActiveUserRecord");
Expand Down Expand Up @@ -80,7 +81,9 @@ sub startup {
my $action = $app->pause->config->action($name);
for my $method (qw/get post/) {
my $route = $private->$method("/$name");
$route->with_csrf_protection if $method eq "post" and $action->{x_csrf_protection};
$route->with_csrf_protection if $method eq "post" and $action->{x_csrf_protection};
$route->with_mfa_protection if $method eq "post" and $action->{x_mfa_protection};
$route->with_csrf_and_mfa_protection if $method eq "post" and $action->{x_csrf_and_mfa_protection};
$route->to($action->{x_mojo_to});
}
}
Expand Down
23 changes: 21 additions & 2 deletions lib/pause_2017/PAUSE/Web/Config.pm
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ our %Actions = (
cat => "User/01Files/01up",
desc => "This is the heart of the <b>Upload Server</b>, the page most heavily used on PAUSE.",
method => 'POST',
x_mfa_protection => 1,
x_form => {
HIDDENNAME => {form_type => "hidden_field"},
CAN_MULTIPART => {form_type => "hidden_field"},
Expand Down Expand Up @@ -427,7 +428,7 @@ our %Actions = (
cat => "User/06Account/02",
desc => "Change your password any time you want.",
method => 'POST',
x_csrf_protection => 1,
x_csrf_and_mfa_protection => 1,
x_form => {
HIDDENNAME => {form_type => "hidden_field"},
ABRA => {form_type => "hidden_field"},
Expand All @@ -443,7 +444,7 @@ our %Actions = (
cat => "User/06Account/01",
desc => "Edit your user name, your email addresses (both public and secret one), change the URL of your homepage.",
method => 'POST',
x_csrf_protection => 1,
x_csrf_and_mfa_protection => 1,
x_form => {
HIDDENNAME => {form_type => "hidden_field"},
pause99_edit_cred_fullname => {form_type => "text_field"},
Expand All @@ -456,6 +457,21 @@ our %Actions = (
pause99_edit_cred_sub => {form_type => "submit_button"},
},
},
mfa => {
x_mojo_to => "user-mfa#edit",
verb => "Multifactor Auth",
priv => "user",
cat => "User/06Account/03",
desc => "Multifactor Authentication.",
method => 'POST',
x_csrf_protection => 1,
x_form => {
HIDDENNAME => {form_type => "hidden_field"},
pause99_mfa_code => {form_type => "text_field"},
pause99_mfa_reset => {form_type => "hidden_field"},
pause99_mfa_sub => {form_type => "submit_button"},
},
},
pause_logout => {
x_mojo_to => "user#pause_logout",
verb => "About Logging Out",
Expand Down Expand Up @@ -493,6 +509,7 @@ our %Actions = (
cat => "01usr/01add",
desc => "Admins can add users or mailinglists.",
method => 'POST',
x_mfa_protection => 1,
x_form => {
SUBMIT_pause99_add_user_Soundex => {form_type => "submit_button"},
SUBMIT_pause99_add_user_Metaphone => {form_type => "submit_button"},
Expand Down Expand Up @@ -520,6 +537,7 @@ our %Actions = (
cat => "01usr/02",
desc => "Admins and mailing list representatives can change the name, address and description of a mailing list.",
method => 'POST',
x_mfa_protection => 1,
x_form => {
HIDDENNAME => {form_type => "hidden_field"},
pause99_edit_ml_3 => {form_type => "select_field"}, # mailing lists
Expand All @@ -544,6 +562,7 @@ our %Actions = (
cat => "01usr/03",
desc => "Admins can access PAUSE as-if they were somebody else. Here they select a user/action pair.",
method => 'POST',
x_mfa_protection => 1,
x_form => {
HIDDENNAME => {form_type => "select_field"},
ACTIONREQ => {form_type => "select_field"},
Expand Down
12 changes: 12 additions & 0 deletions lib/pause_2017/PAUSE/Web/Context.pm
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use Email::MIME;
use Data::Dumper;
use PAUSE::Web::Config;
use PAUSE::Web::Exception;
use Auth::GoogleAuth;

our $VERSION = "1072";

Expand Down Expand Up @@ -40,6 +41,17 @@ sub version {
$version;
}

sub authenticator_for {
my ($self, $user) = @_;
my $userid = lc($user->{userid});
my $secret32 = $user->{mfa_secret32};
return Auth::GoogleAuth->new({
secret32 => $secret32,
issuer => $PAUSE::Config->{MFA_ISSUER} || 'PAUSE',
key_id => $userid,
});
}

sub hostname {
my $self = shift;
$PAUSE::Config->{SERVER_NAME} || Sys::Hostname::hostname();
Expand Down
95 changes: 95 additions & 0 deletions lib/pause_2017/PAUSE/Web/Controller/User/Mfa.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package PAUSE::Web::Controller::User::Mfa;

use Mojo::Base "Mojolicious::Controller";
use Auth::GoogleAuth;
use PAUSE::Crypt;
use Crypt::URandom qw(urandom);
use Convert::Base32 qw(encode_base32);
use Imager::QRCode qw(plot_qrcode);
use URI;

sub edit {
my $c = shift;
my $pause = $c->stash(".pause");
my $mgr = $c->app->pause;
my $req = $c->req;
my $u = $c->active_user_record;

my $auth = $c->app->pause->authenticator_for($u);
$pause->{mfa_qrcode} = _generate_qrcode($auth);
if (!$u->{mfa_secret32}) {
my $dbh = $mgr->authen_connect;
my $tbl = $PAUSE::Config->{AUTHEN_USER_TABLE};
my $sql = "UPDATE $tbl SET mfa_secret32 = ?, changed = ?, changedby = ? WHERE user = ?";
$dbh->do($sql, undef, $auth->secret32, time, $pause->{User}{userid}, $u->{userid})
or push @{$pause->{ERROR}}, sprintf(qq{Could not enter the data into the database: <i>%s</i>.},$dbh->errstr);
}

if (uc $req->method eq 'POST' and $req->param("pause99_mfa_sub")) {
Copy link
Collaborator

@wolfsage wolfsage Apr 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block of code says:

  1. The user is updating their mfa (pause99_mfa_sub has a value)
  2. Did they provided a 6 digit code in pause99_mfa_code? Does it verify? No? Fail
  3. Did they provide a backup code in pause99_mfa_code? Does it verify? No? Fail
  4. Success. Update/disable the mfa

This means that you can supply an empty string in pause99_mfa_code, or any string that doesn't match the two regexps, and successfully disable mfa

It would probably be better to invert the logic and start with my $success = 0; and then only set $success to true under very explicit circumstances. Then, we only take action if $success is true. This can help avoid these kind of logic bugs

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inverted the logic with 684f2f0

my $code = $req->param("pause99_mfa_code");
$req->param("pause99_mfa_code", undef);
my $verified;
if ($code =~ /\A[0-9]{6}\z/ && $auth->verify($code)) {
$verified = 1;
} elsif ($code =~ /\A[a-z0-9]{5}\-[a-z0-9]{5}\z/ && $u->{mfa_recovery_codes} && $req->param("pause99_mfa_reset")) {
my @recovery_codes = split / /, $u->{mfa_recovery_codes} // '';
if (grep { PAUSE::Crypt::password_verify($code, $_) } @recovery_codes) {
$verified = 1;
}
}
unless ($verified) {
$pause->{error}{invalid_code} = 1;
return;
}
my ($secret32, $recovery_codes);
if ($req->param("pause99_mfa_reset")) {
$secret32 = undef;
$recovery_codes = undef;
$c->flash(mfa_disabled => 1);
} else {
$secret32 = $auth->secret32;
$c->flash(mfa_enabled => 1);
my @codes = _generate_recovery_codes();
$c->flash(recovery_codes => \@codes);
$recovery_codes = join " ", map { PAUSE::Crypt::hash_password($_) } @codes;
}
my $dbh = $mgr->authen_connect;
my $tbl = $PAUSE::Config->{AUTHEN_USER_TABLE};
my $sql = "UPDATE $tbl SET mfa_secret32 = ?, mfa_recovery_codes = ?, changed = ?, changedby = ? WHERE user = ?";
if ($dbh->do($sql, undef, $secret32, $recovery_codes, time, $pause->{User}{userid}, $u->{userid})) {
my $mailblurb = $c->render_to_string("email/user/mfa/edit", format => "email");
my $header = {Subject => "User update for $u->{userid}"};
my @to = $u->{secretemail};
$mgr->send_mail_multi(\@to, $header, $mailblurb);
} else {
push @{$pause->{ERROR}}, sprintf(qq{Could not enter the data
into the database: <i>%s</i>.},$dbh->errstr);
}
$c->redirect_to('/authenquery?ACTION=mfa');
}
}

sub _generate_recovery_codes {
my @codes;
for (1 .. 8) {
my $code = encode_base32(urandom(6));
$code =~ tr/lo/89/;
$code =~ s/^(.{5})/$1-/;
push @codes, $code;
}
@codes;
}

# using $auth->qr_code directly is handy but insecure
sub _generate_qrcode {
my $auth = shift;
my $otpauth = $auth->qr_code(undef, undef, undef, 1);
my $img = plot_qrcode($otpauth, { casesensitive => 1, size => 4, margin => 4, version => 1, level => 'M' });
$img->write(data => \my $qr_png, type => 'png') or die "Failed to write image: " . $img->errstr;
my $data = URI->new("data:");
$data->data($qr_png);
$data->media_type('image/png');
$data;
}

1;
40 changes: 34 additions & 6 deletions lib/pause_2017/PAUSE/Web/Controller/User/Uri.pm
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ sub add {
my $req = $c->req;

$PAUSE::Config->{INCOMING_LOC} =~ s|/$||;
$PAUSE::Config->{INCOMING_TMP} =~ s|/$||;

my $u = $c->active_user_record;
die PAUSE::Web::Exception
Expand All @@ -26,11 +27,20 @@ sub add {

if ($req->param("SUBMIT_pause99_add_uri_HTTPUPLOAD")
|| $req->param("SUBMIT_pause99_add_uri_httpupload")) {
my $upl = $req->upload('pause99_add_uri_httpupload');
unless ($upl->size) {
warn "Warning: maybe they hit RETURN, no upload size, not doing HTTPUPLOAD";
$req->param("SUBMIT_pause99_add_uri_HTTPUPLOAD","");
$req->param("SUBMIT_pause99_add_uri_httpupload","");
if (my $stashed = $req->param("pause99_add_uri_httpupload_stashed")) {
my $stashed_file = "$PAUSE::Config->{INCOMING_TMP}/$stashed";
if (!-e $stashed_file) {
warn "Warning: maybe their files are already gone, not doing HTTPUPLOAD";
$req->param("SUBMIT_pause99_add_uri_HTTPUPLOAD","");
$req->param("SUBMIT_pause99_add_uri_httpupload","");
}
} else {
my $upl = $req->upload('pause99_add_uri_httpupload');
unless ($upl->size) {
warn "Warning: maybe they hit RETURN, no upload size, not doing HTTPUPLOAD";
$req->param("SUBMIT_pause99_add_uri_HTTPUPLOAD","");
$req->param("SUBMIT_pause99_add_uri_httpupload","");
}
}
}
if (! $req->param("SUBMIT_pause99_add_uri_HTTPUPLOAD")
Expand All @@ -54,7 +64,25 @@ sub add {
) {
{ # $pause->{UseModuleSet} eq "ApReq"
my $upl;
if (
if (my $filename = $req->param("pause99_add_uri_httpupload_stashed")) {
my $stashed_file = "$PAUSE::Config->{INCOMING_TMP}/$filename";
my $to = "$PAUSE::Config->{INCOMING_LOC}/$filename";
rename $stashed_file => $to or die PAUSE::Web::Exception
->new(ERROR => "Couldn't copy file '$filename' to '$to': $!");
$pause->{successfully_copied_to} = $to;
warn "h1[File successfully copied to '$to']filename[$filename]";
if ($req->param("pause99_add_uri_httpupload_renamed_from")) {
require Dumpvalue;
my $dv = Dumpvalue->new;
$req->param("pause99_add_uri_httpupload",$filename);
$pause->{upload_is_renamed} = {
from => $dv->stringify($req->param("pause99_add_uri_httpupload_renamed_from")),
to => $dv->stringify($filename),
};
}
$uri = $filename;
}
elsif (
$upl = $req->upload("pause99_add_uri_httpupload") or # from 990806
$upl = $req->upload("HTTPUPLOAD")
) {
Expand Down
Loading
Loading