Compare commits

...

2 Commits
2.4.0 ... csp

Author SHA1 Message Date
Aleks-Daniel Jakimenko-Aleksejev
eda34108e1 All CSP parameters should start with -
This is best described by the docs:
“Each argument name is preceded by a dash. Neither case nor order matters in
the argument list. -type, -Type, and -TYPE are all acceptable. In fact, only
the first argument needs to begin with a dash. If a dash is present in the
first argument, CGI.pm assumes dashes for the subsequent ones.”

We construct our own hash %headers and then pass that to $q->header. Since
hash values are not ordered in any way (in fact the order is pretty much
random), sometimes CSP things will be at the front of the list. Since there
was no leading dashes, these were interpreted as something else.
2015-11-02 21:27:41 +02:00
Aleks-Daniel Jakimenko-Aleksejev
39a59e257f Content Security Policy
For now, it is turned off by default, but we should try to turn it on as soon
as we can (this will require us to make sure that nothing in the codebase is
using inline javascript).

By default, everything from 'self' is allowed. This is good enough and I don't
think that it should be stricter.

style-src is set to * (anywhere!) because of the default stylesheet (which is
fetched from oddmuse.org) and also because of the “css=” query option.

img-src is set to * because linking to external images is a very common thing.
2015-11-01 04:35:37 +02:00

View File

@@ -101,6 +101,9 @@ our $EditPass //= ''; # Whitespace separated passwords.
our $PassHashFunction //= ''; # Name of the function to create hashes
our $PassSalt //= ''; # Salt will be added to any password before hashing
our $UseCsp = 0; # 1 = enable Content Security Policy # TODO should be enabled by default
our %CspDirectives = ('default-src' => ["'self'"], 'style-src' => ['*'], 'img-src' => ['*']); # CSP directives
our $BannedHosts = 'BannedHosts'; # Page for banned hosts
our $BannedCanRead = 1; # 1 = banned cannot edit, 0 = banned cannot read
our $BannedContent = 'BannedContent'; # Page for banned content (usually for link-ban)
@@ -2303,6 +2306,12 @@ sub GetHttpHeader {
$headers{-Content_Encoding} = $encoding if $encoding;
my $cookie = Cookie();
$headers{-cookie} = $cookie if $cookie;
if ($UseCsp) {
my $csp = join '; ', map { join ' ', $_, @{$CspDirectives{$_}} } sort keys %CspDirectives;
$headers{'-Content-Security-Policy'} = $csp;
$headers{'-X-Content-Security-Policy'} = $csp; # required for IE
$headers{'-X-Webkit-CSP'} = $csp; # required for UC browser
}
if ($q->request_method() eq 'HEAD') {
print $q->header(%headers), "\n\n"; # add newlines for FCGI because of exit()
exit; # total shortcut -- HEAD never expects anything other than the header!