The mentioned files must not be altered. They are part of the server core and are fine as they are.
You are writing an app that needs a looser csp setting than normal (pure NC) apps need. So, read the documentation. There is an example on allowing all domains. I would highly suggest to whitelist individual domains to minimize the security impact.
Did you check your web server doesn’t have CSP policies itself (.htaccess, .conf…). You can check by accessing through a page that isn’t generated by Nextcloud (eg test.php).
I would say there are parts obsolete in your solution:
The first two lines of creating a ContentSecurityPolicy objectis obsolete.
This sounds like the actual solution:
The last step is obsolete again:
This is due to the fact that you take the CSP object from the response (which will be initialized eventually as a by-product) and get that as a reference from the response object. By changing the CSP object (addAllowedWorkerSrcDomain), this is affecting the response already.
I am although not sure if this is acually solving the original poster’s question, as there was an issue about script-src-elem while this modifies the worker domain. A bit more digging might be needed there. This comment above was just about the pure PHP part.