1

With htaccess I am using:

<IfModule mod_headers.c>
    Header set Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-eval'; 
    style-src 'self' 'unsafe-inline'; frame-src 'self' *.youtube.com; img-src * data:; media-src * data:; 
    report-uri /logging"
</IfModule>

However, I need to exclude all pages that contain Paypal scripts because Paypal writes into the DOM and this gets blocked by the Content-Security-Policy.

Now with htaccess I try to exclude CSP using a condition, so Paypal is not blocked anymore.

How can I exclude URLs that start with /order and /paypal?

Examples of domains:

https://www.example.com/order/checkout
https://www.example.com/paypal
https://www.example.com/paypal/check

Can I use Files or FilesMatch inside the IfModule?

Maybe like this?

<IfModule mod_headers.c>
    <FilesMatch "\(order|paypal)">
        Header set Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; frame-src 'self' *.youtube.com; img-src * data:; media-src * data:; report-uri /logging"
    </FilesMatch>
</IfModule>

But I need the oppsite, if NOT FilesMatch.

Meanwhile I found this negative lookahead regex to exclude e.g. "order": ^((?!order).)*$ - and for "order" and "paypal" this regex should work: ^((?!order)(?!paypal).)*$

I tried it but it does not seem to work:

<IfModule mod_headers.c>
    <FilesMatch "^((?!order)(?!paypal).)*$">
        Header set Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; frame-src 'self' *.youtube.com; img-src * data:; media-src * data:; report-uri /logging"
    </FilesMatch>
 </IfModule>

I also tried ^(?!.*(order|paypal)$).*$ without success. It shows as valid in Regex101 but does not seem to work with Apache's htaccess.

Avatar
  • 14,622
  • 9
  • 119
  • 198
  • What are the URL(s) you are trying to match or exclude? Your regex is not quite right, but `FilesMatch` matches _filenames_ only, not paths and URLs. – MrWhite Jun 05 '23 at 17:07
  • 1
    Oh, that's a great hint! Thank you. URLs are "domain.com/order/..." or "domainc.om/paypal/..." -- Note that the CMS I am using rewrites the URLs to those "clean" URLs. – Avatar Jun 05 '23 at 17:08

1 Answers1

2

URLs are "domain.com/order/..." or "domainc.om/paypal/..."

As mentioned in comments, the FilesMatch directive matches against filenames only - this does not include filepaths or URLs. It would seem you are trying to exclude URLs (not files) that start with the order or paypal path segment.

You can do this using an Apache <If> expression. For example:

<If "%{REQUEST_URI} !~ m#^/(order|paypal)($|/)#">
    Header set .....
</If>

The !~ operator is a negated regex match. So, the contained Header directive is applied only when the requested URL does not match the regex.

However, whether this is applied successfully or not can still be dependent on other directives you might have in the config file. (eg. Rewriting requests to a front-controller?)

UPDATE:

You can also try matching against THE_REQUEST instead, which contains the first line of the request headers and does not change when the request is rewritten.

THE_REQUEST contains a string of the form:

GET /order/checkout HTTP/1.1

For example:

<If "%{THE_REQUEST} !~ m#^[A-Z]{3,7}\s/(order|paypal)($|/)#">
    Header set .....
</If>
MrWhite
  • 43,179
  • 8
  • 60
  • 84
  • Thanks for your help. I tried your directive but the CSP Header is still loaded. The regex seems fine. https://regex101.com/r/3ADzbj/1 ... The REQUEST_URI is the URI as I can see it in the browser. Sidenote: The CMS I am running rewrites URLs (index.php?q=123) to friendly URLs. But since we take the request URI it shouldn't be an issue, I think. – Avatar Jun 05 '23 at 17:23
  • @Avatar I've "fixed" the regex (it should be _anchored_) and added another solution that uses `THE_REQUEST` instead - this may be preferable if you are rewriting the URLs for a CMS. – MrWhite Jun 05 '23 at 17:27
  • 1
    You are a genius! This seems to work. Thanks a lot, that was a difficult task for me, even though I program for decades. – Avatar Jun 05 '23 at 17:32