Replies: 1 comment 1 reply
-
Thanks for this great opening to add CSP to NiceGUI. Here are my thoughts on the raised major topics: Inline scripts and styles I very much prefer to include a Vue.js template compiler None of the three options you suggested are very appealing in my opinion. We mainly need the Vue.js template compiler for
Also, we use some libraries which have Maybe we need to look deeper into the Trusted Types as our only option? Security Documentation Yes! We should have something like that. I guess that should be something we should do in parallel with the development of the two obstacles we need to tackle first. Right? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I am working on securing a NiceGUI web app deployment against OWASP Top Ten Web Application Security Risks. I am currently implementing a Content Security Policy (CSP) to help secure the app against cross-site scripting (XSS) attacks and I have run into some obstacles that make it difficult to balance security with functionality in NiceGUI. Below is some example code to help guide the discussion followed by a list of obstacles I have encountered in the implementation and some ideas about how these could be overcome. If anyone has good ideas about how to overcome any of these obstacles in the current version of NiceGUI, please share your thoughts. I am also interested in thoughts on how NiceGUI could be updated to facilitate implementing this type of security without impacting features and functionality.
Below is an example middleware implementation that uses the secure.py Python library to set a CSP header consistent with OWASP recommendations on all responses in NiceGUI (or FastAPI or Starlette). This is a strict CSP that would essentially prevent an app from loading cross-origin resources as well as inline scripts and styles with unknown origins. Implementing a strict CSP like this one would likely break most, if not all, NiceGUI deployments. I am not suggesting that a CSP needs to be this strict in order to be secure but just illustrating a simple implementation.
Reasonable adjustments to the policy above could include allowing trusted locations as script sources, using nonce directives, etc.
I think the following NiceGUI implementation details present the most complexity when attempting to modify a CSP in order to secure a NiceGUI web app deployment (I understand that listed potential solutions may not be trivial or practical to implement but hopefully they are good discussion starters).
Inline scripts and styles
Obstacle:
NiceGUI renders inline scripts and styles that require CSP to include
'unsafe-inline'
values in thescript-src
andstyle-src
directives for a NiceGUI page to render properly. This opens up a broad XSS attack vector.Potential solution 1:
Change NiceGUI page render code to render inline script and style elements to script and style elements with
src
attributes pointing to files with same-site origins, which would satisfy the'self'
value in thescript-src
andstyle-src
CSP directives.Potential solution 2:
Change NiceGUI page render code to include a
nonce
attribute on all script and style elements with a per response server-generated, difficult to guess random value. It is important to use some kind of templating to insert nonces (since programmatically inserting them on all script tags may inadvertently include them in attacker injected scripts). Also requires implementing a function to retrieve the generated nonces so they can be included in the CSP header.Vue.js template compiler
Obstacle:
I have not looked at this in great detail, so I apologize if I am off the mark here. I think that NiceGUI is implementing the Vue.js template compiler to dynamically generate components. The template compiler utilizes
new Function()
to convert templates into executable JavaScript render functions, which creates security vulnerabilities similar to those associated with theeval()
API. This requires the CSP to include'unsafe-eval'
in thescript-src
directive for a NiceGUI page to render properly and opens up a broad XSS attack vector.Potential solution 1: Pre-compile templates and use the runtime-only build of Vue.js which excludes the template compiler. This may not be practical depending on NiceGUI implementation details.
Potential solution 2: Define components using render functions instead of templates to avoid runtime compilation. This may not be trivial to implement.
Potential solution 3: Look at incorporating the Trusted Types API with polyfill to mitigate XSS vulnerabilities and enable use of more secure CSP directives. I don't have experience with this approach yet and can't guess on implementation difficulty.
In the near term, it would be generally helpful to those deploying NiceGUI apps in a secure context if security best practices were included in the documentation, something like Vue.js security best practices. This would certainly help with implementing CSP if it made clear whether or not there are ways to use NiceGUI without the vulnerabilities exposed by inline script and style injection or the Vue.js template compiler (for example, perhaps it is only a subset of components that require these NiceGUI implementation details).
Looking forward to the discussion on this topic and hope it is helpful in the continued development of NiceGUI.
Beta Was this translation helpful? Give feedback.
All reactions