While turning this policy off completely is not recommended it can be beneficial to adjust the policy to be less restrictive, allowing the user of external reports without compromising security.
Although I described modifying the CSP in an earlier post, I didn't realise at the time of publishing that the method I mentioned was only temporarily, and as soon as Jenkins was restarted, the defaults were reapplied. This post both covers that original method, and how to do it permanently, serving as a stand-alone reference.
The contents of the CSP headers are defined by the
hudson.model.DirectoryBrowserSupport.CSP setting and supports
a wide range of values - the CSP specification is very flexible
and allows you to control how pretty much any type of resource
The specific configuration values are beyond the scope of this post, but you can learn more about CSP settings at a reference site.
To change the CSP, open the Script Console in Jenkins administration section, and run the following command
Important! Changing the policy via the Script Console is not permanent and will revert back to the default values when Jenkins is restarted.
If you run Jenkins via the command line, e.g. by calling
java.exe, you can add an extra argument to set the value of
the CSP setting.
The Java documentation states you can use the
to set a "system property value", allowing us to add the
following to the command line
Remember to add double quotes around the CSP value, otherwise it will not be parsed correctly
There's one important caveat though - the ordering of the parameters is important. I run Jenkins as a Windows service (more on that in the next section) and the command line I use looks similar to the following
Initially I added the new argument to the end of that string,
but this never had any effect - each time Jenkins started the
value was missing. When checking the System Information view
I noted the parameter wasn't listed in the main System
Properties table but was instead appended to the value of
another property -
sun.java.command. That was the point I
realized ordering mattered and moved the argument to before the
--http argument. After that, the property setting was
If you run Jenkins as a Windows Service, the command parameters
are read from
jenkins.xml, which is located in your main
Jenkins installation. If you open this file in the text editor
of your choice, you should see XML similar to the following
Simply add your new argument to the
arguments element (with
the same caveat that parameter ordering is important), save the
file and restart Jenkins.
Note: The contents of the
argumentselement must be a single line, if line breaks are present the service will terminate on startup
An easy way to check the CSP policy (regardless of if you set it via the command line or the Script Console) is to use Jenkins' System Information view. This view includes a System Properties table, if a custom CSP is in use, this will be displayed in the table.
- 2017-02-03 - First published
- 2020-11-21 - Updated formatting