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 server option to whitelist IPs for all session cookies #2681

Merged
merged 1 commit into from
Nov 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions lib/ProductOpener/Config2_sample.pm
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ $robotoff_url = '';
cookie_domain => "openfoodfacts.dev", # if not set, default to $server _domain
private_products => 1, # Make products visible only to the owner
export_servers => { public => "off", experiment => "off-exp" },
ip_whitelist_session_cookie => [ "172.19.0.1" ],
);


Expand Down
23 changes: 21 additions & 2 deletions lib/ProductOpener/Users.pm
Original file line number Diff line number Diff line change
Expand Up @@ -624,8 +624,7 @@ sub init_user()
if ((not defined $user_ref->{'user_sessions'})
or (not defined $user_session)
or (not defined $user_ref->{'user_sessions'}{$user_session})
or (not defined $user_ref->{'user_sessions'}{$user_session}{'ip'})
or (($short_ip->($user_ref->{'user_sessions'}{$user_session}{'ip'}) ne ($short_ip->(remote_addr()))) ))
or (not is_ip_known_or_whitelisted($user_ref, $user_session, remote_addr(), $short_ip)))
{
$log->debug("no matching session for user") if $log->is_debug();
$user_id = undef;
Expand Down Expand Up @@ -692,6 +691,26 @@ sub init_user()
return 0;
}

sub is_ip_known_or_whitelisted {
my ($user_ref, $user_session, $ip, $shorten_ip) = @_;

my $short_ip = $shorten_ip->($ip);

if ((defined $user_ref->{'user_sessions'}{$user_session}{'ip'})
and ($shorten_ip->($user_ref->{'user_sessions'}{$user_session}{'ip'}) eq $short_ip)) {
return 1;
}

if (defined $server_options{ip_whitelist_session_cookie}) {
foreach (@{$server_options{ip_whitelist_session_cookie}}) {
if ($_ eq $ip) {
return 1;
}
}
}

return 0;
}

sub check_session($$) {

Expand Down