Skip to content

Commit ee062af

Browse files
authored
Merge pull request #1488 from metacpan/oalders/cookie-domain
Make cookie domain settable via config
2 parents 94ce2a7 + 1522bf2 commit ee062af

4 files changed

Lines changed: 69 additions & 1 deletion

File tree

lib/MetaCPAN/Server/Controller/Login.pm

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,18 @@ sub auto : Private {
1717
# Store params in a temporary cookie so we can keep track of them.
1818
# This should include `client_id` (metacpan env) and `choice` (provider).
1919
if ( $c->req->params->{client_id} ) {
20+
21+
# The OAuth handshake can start on one *.metacpan.org host and finish
22+
# on another (e.g. the provider callback host), so a host-only cookie
23+
# would be lost in transit. `oauth_cookie_domain` (config) scopes it to
24+
# the shared parent domain in production; an empty value keeps it
25+
# host-only for local dev.
26+
my $domain = $c->config->{oauth_cookie_domain};
2027
$c->res->cookies->{oauth_tmp} = {
2128
value => encode_json( $c->req->parameters ),
2229
path => '/',
23-
expires => '+7d'
30+
expires => '+7d',
31+
( $domain ? ( domain => $domain ) : () ),
2432
};
2533
}
2634

metacpan_server.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,10 @@ oauth:
3333
secret: seekrit
3434

3535
front_end_url: http://0.0.0.0:5001
36+
37+
# Domain for the temporary OAuth cookie (oauth_tmp), scoped to the shared
38+
# parent domain so the login handshake can start and finish on different hosts
39+
# (e.g. api.metacpan.org and the provider callback host). For local development
40+
# override this in metacpan_server_local.yaml (an empty value keeps the cookie
41+
# host-only).
42+
oauth_cookie_domain: .metacpan.org

metacpan_server_testing.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,6 @@ oauth:
3535
secret: seekrit
3636

3737
front_end_url: http://0.0.0.0:5001
38+
39+
# See metacpan_server.yaml.
40+
oauth_cookie_domain: .metacpan.org
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
use strict;
2+
use warnings;
3+
use lib 't/lib';
4+
5+
use MetaCPAN::Server::Test qw( app GET test_psgi );
6+
use Test::More;
7+
8+
# The `oauth_tmp` cookie carries the OAuth params from the leg that starts the
9+
# flow to the leg that finishes it (the provider callback). When those legs land
10+
# on different *.metacpan.org hosts a host-only cookie is lost and login
11+
# silently fails, so the cookie domain is configurable via `oauth_cookie_domain`
12+
# (set to the shared parent domain in production). We ship a sane default and
13+
# allow it to be overridden -- or disabled (host-only) with an empty value.
14+
15+
test_psgi app, sub {
16+
my $cb = shift;
17+
18+
subtest 'applies the cookie domain from config' => sub {
19+
my $domain = MetaCPAN::Server->config->{oauth_cookie_domain};
20+
ok defined $domain && length $domain,
21+
'a default oauth_cookie_domain is configured';
22+
23+
my $res = $cb->( GET '/login/github?client_id=metacpan.dev' );
24+
like $res->header('Set-Cookie'), qr/oauth_tmp=/,
25+
'oauth_tmp cookie set';
26+
like $res->header('Set-Cookie'), qr/domain=\Q$domain\E/i,
27+
"Set-Cookie carries the configured domain ($domain)";
28+
};
29+
30+
subtest 'config override is honored' => sub {
31+
my $config = MetaCPAN::Server->config;
32+
local $config->{oauth_cookie_domain} = '.example.test';
33+
34+
my $res = $cb->( GET '/login/github?client_id=metacpan.dev' );
35+
like $res->header('Set-Cookie'), qr/domain=\.example\.test/i,
36+
'overridden domain applied';
37+
};
38+
39+
subtest 'empty oauth_cookie_domain disables the domain (host-only)' =>
40+
sub {
41+
my $config = MetaCPAN::Server->config;
42+
local $config->{oauth_cookie_domain} = q{};
43+
44+
my $res = $cb->( GET '/login/github?client_id=metacpan.dev' );
45+
unlike $res->header('Set-Cookie'), qr/domain=/i,
46+
'no Domain attribute when explicitly disabled';
47+
};
48+
};
49+
50+
done_testing;

0 commit comments

Comments
 (0)