Open jeran-urban opened 4 years ago
Interesting that no one else has run into this in 4 years. While going through our SOC2 security process we implemented CSP and so far NSwag is the only thing I can't figure out how to fix. We're using nonces so we aren't able to allow inline scripts or styles. To fix it for us, I believe we would just need to pass our CSP nonce value to NSwag and then it can add the nonce attribute to any loaded JS and CSS file tags. I'm willing to put in a pull request if you like this idea.
Maybe a better approach would be to create an option similar to this code:
c.CustomAsset("index", containingAssembly, "YourWebApiProject.SwaggerExtensions.index.html");
It could allow a func returning a custom string which could be dynamically generated instead of requiring a static embedded resource?
c.CustomAsset("index", () => "<html><script src='' nonce=''>...</html>");
Using this technique I could then just pull out the embedded NSwag index.html string, use HtmlAgilityPack to add a placeholder for the nonces on the Githubissues.
Hello,
I have been researching getting rid of the errors that come up using NSWAG with content-security-policies in place, without just allowing all.
I have narrowed the issues unresolvable by hashes and secure settings down to the following 2:
1) if I change the code in the index.html: 32 by removing the style tag:
and change the swagger-ui.css to include that inline style mentioned above:
That takes care of one of the errors and leaves the page looking the same.
2) The second error I resolved by doing the following: prettifying the swagger-ui-bundle.js and on line 3739: commenting out the following lines:
With these two changes, I am able to use simple hashes to apply CSP rules without errors. I have been unable to determine exactly how that function I commented out works, but in all variations to the api and swagger xml annotations, I have not seen any difference to the swagger ui page without that code. I know that function triggers when you click the get/put/delete etc routes, but could not determine more than that. Could you share the unminified version of that code or explain what it does? If it is not needed, could these 2 changes be made to the code so we can stop having the CSP errors when using NSWAG in production?
If these changes are not possible to the core code, is there a way to make these 2 changes locally and easily?
Thank you.