Skip to content

Commit d4afb8f

Browse files
authored
Add docs on troubleshooting web components CSP errors & update CSP terminal error message (#733)
1 parent ad19d57 commit d4afb8f

File tree

4 files changed

+77
-2
lines changed

4 files changed

+77
-2
lines changed

docs/assets/csp-message.webp

44.5 KB
Binary file not shown.
+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Troubleshooting
2+
3+
## Security policy
4+
5+
One of the most common issues when using web components is that you will need to relax the stringent security policy Mesop uses by default.
6+
7+
If you use the `mesop` command-line tool to run your app, you will see a detailed error message printed like this that will tell you how to fix the error:
8+
9+
<img src="/assets/csp-message.webp">
10+
11+
If you are using Colab or another tool to run your Mesop app, and you can't see the terminal messages, then you can use your browser developer tools to view the console error messages.
12+
13+
### Content security policy error messages
14+
15+
In your browser developer tools, you may see the following console error messages (the exact wording may differ):
16+
17+
#### script-src Error
18+
19+
???+ failure "script-src Console Error"
20+
Refused to load the script 'https://cdn.jsdelivr.net/gh/lit/dist@3/core/lit-core.min.js' because it violates the following Content Security Policy directive: "script-src 'self' 'nonce-X-_ZR64fycojGBCDQbjpLA'". Note that 'script-src-elem' was not explicitly set, so 'script-src' is used as a fallback.
21+
22+
If you see this error message, then you will need to update your page's [Security Policy](../api/page.md#mesop.security.security_policy.SecurityPolicy) `allowed_script_srcs` property. In this example, because the "script-src" directive was violated, you will need to add the script's URL to the Security Policy like this:
23+
24+
```py
25+
@me.page(
26+
security_policy=me.SecurityPolicy(
27+
allowed_script_srcs=["https://cdn.jsdelivr.net"]
28+
)
29+
)
30+
```
31+
32+
???+ tip "Allow-listing sites"
33+
You can allow-list the full URL including the path, but it's usually more convenient
34+
to allow-list the entire site. This depends on how trustworthy the site is.
35+
36+
#### connect-src Error
37+
38+
???+ failure "connect-src Console Error"
39+
zone.umd.js:2767 Refused to connect to 'https://identitytoolkit.googleapis.com/v1/projects' because it violates the following Content Security Policy directive: "default-src 'self'". Note that 'connect-src' was not explicitly set, so 'default-src' is used as a fallback.
40+
41+
42+
If you see this error message, then you will need to update your page's [Security Policy](../api/page.md#mesop.security.security_policy.SecurityPolicy) `allowed_connect_srcs` property. In this example, because the "connect-src" directive was violated, you will need to add the URL you are trying to connect to (e.g. XHR, fetch) to the Security Policy like this:
43+
44+
```py
45+
@me.page(
46+
security_policy=me.SecurityPolicy(
47+
allowed_connect_srcs=["https://*.googleapis.com"]
48+
)
49+
)
50+
```
51+
52+
???+ tip "Allow-listing domains using wildcard"
53+
You can wildcard all the subdomains for a site by using the wildcard character `*`.
54+
55+
#### Trusted Types Error
56+
57+
Trusted Types errors can come in various forms. If you see a console error message that contains TrustedHTML, TrustedScriptURL or some other variation, then you are likely hitting a trusted types error. Trusted Types is a powerful web security feature which prevents untrusted code from using sensitive browser APIs.
58+
59+
Unfortunately, many third-party libraries are incompatible with trusted types which means you need to disable this web security defense protection for the Mesop page which uses these libraries via web components.
60+
61+
???+ failure "TrustedHTML Console Error"
62+
63+
TypeError: Failed to set the 'innerHTML' property on 'Element': This document requires 'TrustedHTML' assignment.
64+
65+
You can fix this Trusted Types error by disabling Trusted Types in the security policy like this:
66+
67+
```py
68+
@me.page(
69+
security_policy=me.SecurityPolicy(
70+
dangerously_disable_trusted_types=True
71+
)
72+
)
73+
```

mesop/server/static_file_serving.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def csp_report():
135135
keyword_arg = "allowed_connect_srcs"
136136
elif violated_directive == "frame-ancestors":
137137
keyword_arg = "allowed_iframe_parents"
138-
elif violated_directive == "require-trusted-types-for":
138+
elif violated_directive in ("require-trusted-types-for", "trusted-types"):
139139
keyword_arg = "dangerously_disable_trusted_types"
140140
else:
141141
raise Exception("Unexpected CSP violation:", violated_directive, report)
@@ -148,7 +148,6 @@ def csp_report():
148148
f"""
149149
{tc.RED}⚠️ Content Security Policy Error ⚠️{tc.RESET}
150150
{tc.YELLOW}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━{tc.RESET}
151-
152151
{tc.CYAN}Directive:{tc.RESET} {tc.GREEN}{violated_directive}{tc.RESET}
153152
{tc.CYAN}Blocked URL:{tc.RESET} {tc.GREEN}{blocked_uri}{tc.RESET}
154153
{tc.CYAN}App path:{tc.RESET} {tc.GREEN}{path}{tc.RESET}
@@ -162,6 +161,8 @@ def csp_report():
162161
{tc.MAGENTA}){tc.RESET}
163162
{tc.MAGENTA}){tc.RESET}
164163
164+
{tc.YELLOW}For more info:
165+
{tc.CYAN}https://google.github.io/mesop/web-components/troubleshooting/{tc.RESET}
165166
{tc.YELLOW}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━{tc.RESET}
166167
""" # noqa: RUF001
167168
)

mkdocs.yml

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ nav:
3232
- Overview: web-components/index.md
3333
- Quickstart: web-components/quickstart.md
3434
- API: web-components/api.md
35+
- Troubleshooting: web-components/troubleshooting.md
3536
- High-level:
3637
- Chat: components/chat.md
3738
- Text to Text: components/text-to-text.md

0 commit comments

Comments
 (0)