Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FF 117.0b4: CSP blocks live example with img.src="data:image/svg+xml,base64,"+... #9463

Closed
kkm000 opened this issue Aug 9, 2023 · 4 comments · Fixed by #9771
Closed

FF 117.0b4: CSP blocks live example with img.src="data:image/svg+xml,base64,"+... #9463

kkm000 opened this issue Aug 9, 2023 · 4 comments · Fixed by #9771
Labels
CSP Problems caused by Content-Security-Policy has PR Issues that already have a PR live-samples issues related to live samples (EmbedLiveSample macro) p2 We want to address this but may have other higher priority items. plus:playground regression

Comments

@kkm000
Copy link

kkm000 commented Aug 9, 2023

MDN URL

https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-color-scheme

What specific section or headline is this issue about?

Color scheme inheritance

What information was incorrect, unhelpful, or incomplete?

The example's in-page JS creates an SVG image with three colored circles and assigns it to img.src property as a data: URI; specifically, in this line of the example:

  img.src="data:image/svg+xml,base64," + btoa("<svg> ... </svg>")

The image fails to load. Instead, the output box shows alt-text only:

Circle
Circle
Circle

An error is logged in the browser's log:

Content-Security-Policy: The page’s settings blocked the loading of a resource at data:image/svg+xml;base64,CiAgICAgIDxzdm… (“default-src”).

Addendum (23-08-11): MS Edge's (v115.0.1901.200) error message is more specific:

Refused to load the image 'data:image/svg+xml;base64,CiAgIC[…]ogICAg' because it violates the following Content Security Policy directive: default-src 'self' https:. Note that img-src was not explicitly set, so default-src is used as a fallback.

The full log of warnings and errors; all but the message above are warnings:

Content-Security-Policy: Ignoring ‘unsafe-eval’ or ‘wasm-unsafe-eval’ inside “script-src-elem”. runner.html
Partitioned cookie or storage access was provided to “https://live.mdnplay.dev/en-US/docs/Web/CSS/@media/prefers-color-scheme/runner.html?id=detecting_a_dark_or_light_theme” because it is loaded in the third-party context and dynamic state partitioning is enabled.
Content-Security-Policy: Ignoring ‘unsafe-eval’ or ‘wasm-unsafe-eval’ inside “script-src-elem”. runner.html
Content-Security-Policy: The page’s settings blocked the loading of a resource at data:image/svg+xml;base64,CiAgICAgIDxzdm… (“default-src”). 3 runner.html:6:6
Partitioned cookie or storage access was provided to “https://live.mdnplay.dev/en-US/docs/Web/CSS/@media/prefers-color-scheme/runner.html?id=color_scheme_inheritance” because it is loaded in the third-party context and dynamic state partitioning is enabled.

I'm a scientist/SWE mainly familiar with HPC C++, with little UI experience, barely able to code a bit of simple HTML and CSS. Fixing this error is way above my pay grade, sorry.

What did you expect to see?

Three colored circles.

Do you have any supporting links, references, or citations?

No response

Do you have anything more you want to share?

Browser: Firefox 117.0b3 (64-bit), beta channel, on Windows 11 v10.0.22621.1992.

MDN metadata

Page report details
@kkm000 kkm000 added the needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. label Aug 9, 2023
@kkm000 kkm000 changed the title FF 117.0b4: CSP blocks live example with "img.src="data:image/svg+xml,base64,"+... FF 117.0b4: CSP blocks live example with img.src="data:image/svg+xml,base64,"+... Aug 9, 2023
@bsmth bsmth transferred this issue from mdn/content Aug 9, 2023
@bsmth bsmth added the CSP Problems caused by Content-Security-Policy label Aug 9, 2023
@bsmth
Copy link
Member

bsmth commented Aug 9, 2023

Thanks for reporting @kkm000 - we'll take a look, but you're right about the CSP issue. I wonder if there's a better way to write the example that doesn't B64 encode the images like this.

@kkm000
Copy link
Author

kkm000 commented Aug 11, 2023

@bsmth, Thank you for your quick response! Yes, I vaguely remember that the <svg> element can be used in HTML5 directly, though I'm not betting my arm an the leg on that being true and universally supported by browsers. If true, tho, it feels a better practice to me. An analogy is short-cutting the processing of a chunk of data in memory by dumping it to a temporary file and reusing existing machinery to process it. I was reading the document to figure out how to respect user's dark/bright theme preference properly, so that doesn't apply to my little task. Nevertheless, using the data: URI and base64 encoding struck me as a kinda round-about way of embedding a piece of SVG graphics into the page. In any case, MDN seems the most authoritative guide on the practical use of Web technologies out there, much more thorough, deep and complete than e.g. w3school, so it better remain top-notch.

OTOH, Web programming sometimes looks less than straightforward IRL. I've seen comments like “Don't use X here, it breaks Safari under iOS on small screens.” That's one of these random moments of relief when I feel happy that I'm not writing public Web pages, only simple ones for internal visualisation. But then, I immediately recalled, w.r.t. my own craft, comments in C++ code like “// Don't yank these 'typedef's! They unbreak compilation with MSVC prior to v17.” When you have experience, you know what specs are borked and know the tricks to work around the borkage. This is why I didn't dare trying to fix (or, rather, “fix”) the example. :-)

@spookylukey
Copy link

The same issue is breaking the example code on this page: https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsDataURL - the images do not appear.

In this case it is pretty much impossible to avoid using src="data:...", as that's the whole point of the page.

Tested with both Firefox and Chrome.

@bsmth
Copy link
Member

bsmth commented Aug 22, 2023

OK thanks for reporting, so either way we need a CSP update (img-src 'self' data:?) to fix this - someone from the Yari team will take a look shortly.

@caugner caugner added live-samples issues related to live samples (EmbedLiveSample macro) regression plus:playground and removed needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. labels Oct 2, 2023
@caugner caugner added p2 We want to address this but may have other higher priority items. has PR Issues that already have a PR labels Oct 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CSP Problems caused by Content-Security-Policy has PR Issues that already have a PR live-samples issues related to live samples (EmbedLiveSample macro) p2 We want to address this but may have other higher priority items. plus:playground regression
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants