Skip to content

Commit 7ea3c26

Browse files
committed
Add an option to add headers to only SSL request responses.
The option is `extra_ssl_headers` in the `conf/webwork2.mojolicious.yml` file. It works just like the `extra_headers` option, except that the headers are only added to responses to secure requests. This is to address a need to add the `Strict-Transport-Security` header to SSL request responses that was brought up in the forums. See https://forums.openwebwork.org/mod/forum/discuss.php?d=8782#p22468. That header should not be added to non SSL requests.
1 parent 1fedbc8 commit 7ea3c26

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

conf/webwork2.mojolicious.dist.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ JSON_ERROR_LOG: 0
7272
# /pg_files:
7373
# Access-Control-Allow-Origin: '*'
7474

75+
# The extra_ssl_headers option is much like the extra_headers option above,
76+
# except that these headers are only added to responses to secure SSL requests.
77+
# The example below adds the Strict-Transport-Security header to responses to
78+
# all SSL requests. Note that like the extra_headers option above, headers can
79+
# be added to only specific paths as well (but only if the request is secure).
80+
81+
#extra_ssl_headers:
82+
# '*':
83+
# Strict-Transport-Security: 'max-age=31536000; includeSubDomains; preload'
84+
7585
# The user and group to run the server as. These are only used when the
7686
# webwork2 app is in production mode and run as the root user. This means that
7787
# these settings are not used when proxying via another web server like apache2

lib/Mojolicious/WeBWorK.pm

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,24 @@ sub startup ($app) {
100100
);
101101

102102
# Add a hook to add extra headers if set in the config file.
103-
if (ref $config->{extra_headers} eq 'HASH') {
103+
if (ref $config->{extra_headers} eq 'HASH' || ref $config->{extra_ssl_headers} eq 'HASH') {
104+
my $extraHeaders = ref $config->{extra_headers} eq 'HASH' ? $config->{extra_headers} : {};
105+
my $extraSSLHeaders = ref $config->{extra_ssl_headers} eq 'HASH' ? $config->{extra_ssl_headers} : {};
104106
$app->hook(
105107
before_dispatch => sub ($c) {
106-
for my $path (keys %{ $config->{extra_headers} }) {
108+
for my $path (keys %$extraHeaders) {
107109
if ($c->req->url->path =~ /^$path/) {
108-
for (keys %{ $config->{extra_headers}{$path} }) {
109-
$c->res->headers->header($_ => $config->{extra_headers}{$path}{$_});
110+
for (keys %{ $extraHeaders->{$path} }) {
111+
$c->res->headers->header($_ => $extraHeaders->{$path}{$_});
112+
}
113+
}
114+
}
115+
if ($c->req->is_secure) {
116+
for my $path (keys %$extraSSLHeaders) {
117+
if ($c->req->url->path =~ /^$path/) {
118+
for (keys %{ $$extraSSLHeaders->{$path} }) {
119+
$c->res->headers->header($_ => $extraSSLHeaders->{$path}{$_});
120+
}
110121
}
111122
}
112123
}

0 commit comments

Comments
 (0)