19

I am setting a Content-Security-Policy header in my .htaccess file, and it has grown to be an extremely long single line, which is a bother to manage. Is there some way to break up this line into more manageable substrings?

As a trivial example, say I am setting a header like

Header set Content-Security-Policy "deafult-src http://domainA.com; script-src http://domainB.com"

I can (without obvious breaking problems) accomplish my specific case with something like

Header append Content-Security-Policy "default-src http://domainA.com;"
Header append Content-Security-Policy "script-src http://domainB.com"

but that will insert commas into the string, so I was still curious if there was a better answer that could be applied generally, without adding the additional chars to the response.

What would be ideal is if there were some concatenation character that I could use to break the string into smaller parts, like

Header set Content-Security-Policy "default-src http://domainA.com;"
\" script-src http://domainB.com"

or

Header set Content-Security-Policy "default-src http://domainA.com;"^
" script-src http://domainB.com"

or

Header set Content-Security-Policy "default-src http://domainA.com;"
+" script-src http://domainB.com"

Alternatively, if I could set some sort of variable and just dump their contents to do something like

a="default-src http://domainA.com;"
b=" script-src http://domainB.com"
Header set Content-Security-Policy $a$b

that would also be much more managable.

There was a similar topic that came up for nginx and the conclusion was just to live with the long lines (they were dealing with a long regex, so the append solution wouldn't have worked); Is that going to be the case for Apache as well?

2 Answers2

22

The following should work:

 Header set Content-Security-Policy "default-src http://domainA.com; \
      script-src http://domainB.com"
10

Yes - the backslash works as a line-continuation. This is buried within the Apache 2.4 documentation on https://httpd.apache.org/docs/2.4/configuring.html#page-header

Important rules:

  1. Whitespace within a line is fine, ie. any number of tabs and spaces;
  2. The last character on all lines except the final one must be a backslash;<
  3. The final line must not terminate with a backslash;
  4. The Apache comment character (#) cannot be used to comment out a line.
  5. You can not break the [flags]

If these rules are not obeyed, the server will respond with an Error 500.

Note that when editing *.conf files you can use C:\Apache24\bin>httpd -t to check your syntax.

MeSo2
  • 294
Steve GS
  • 101