sp-dev-docs icon indicating copy to clipboard operation
sp-dev-docs copied to clipboard

SPFx and Content Security Policy produce errors on each script source if server relative URL used

Open vlad-nvs opened this issue 1 year ago • 6 comments

Target SharePoint environment

SharePoint Online

What SharePoint development model, framework, SDK, or API is this about?

💥 SharePoint Framework

Developer environment

Windows

What browser(s) / client(s) have you tested

  • [ ] 💥 Internet Explorer
  • [x] 💥 Microsoft Edge
  • [x] 💥 Google Chrome
  • [x] 💥 FireFox
  • [ ] 💥 Safari
  • [ ] mobile (iOS/iPadOS)
  • [ ] mobile (Android)
  • [ ] not applicable
  • [ ] other (enter in the "Additional environment details" area below)

Additional environment details

  • SPFx version 1.18.2
  • Node.js version 18.17.1

Describe the bug/error

Content Security Policy (CSP) has now been introduced for SharePoint Online. A new tab is available in the SharePoint Admin Center under Advanced → Script sources.

Image

It contains all script URLs from the SPFx solutions, and everything seems to work well. However, when a server relative URL is used (scripts are on the same Tenant), the browser console shows errors such as:

Image

The source list for the Content Security Policy directive 'script-src' contains an invalid source: '/sites/..../.../..../'. It will be ignored.

For now, these are just console errors, and everything appears to be working correctly. However, if there are multiple SPFx solutions, it becomes difficult to identify real issues.

Furthermore, it identifies errors for Script sources that are hosted on different sites.

As far as I know, it is allowed to use relative URLs in SPFx, e.g., in cdnBasePath, and it is even possible to add such URLs to Script sources via the SharePoint Admin Center UI.

Can someone explain why these errors occur for relative URLs in script sources, and whether using relative URLs in SPFx solutions might have any impact in the future?

Steps to reproduce

  1. Create SPFx solution
  2. Use a relative URL for cdnBasePath
  3. Upload SPFx to App Catalog and install on any site

Expected behavior

I think server relative URLs should work properly with the Content Security Policy feature since it's allowed to use them in a SPFx solution from the beginning (and everything works), and it should not produce errors for such script sources

vlad-nvs avatar Aug 15 '25 15:08 vlad-nvs

Hello @vlad-nvs , Thank you for bringing this issue to our attention. We will look into it and get back to you shortly.

Ashlesha-MSFT avatar Aug 18 '25 04:08 Ashlesha-MSFT

@vlad-nvs, This behavior is expected and aligns with CSP standards — server-relative URLs (e.g., /sites/...) are not valid script sources under CSP. The official SPFx CSP documentation confirms that only absolute URLs or origins are supported. While SPFx allows relative paths in cdnBasePath, this is legacy behavior and not compatible with CSP.

This should be considered a feature request, not a bug. You can raise it at the https://feedbackportal.microsoft.com/feedback/forum/06735c62-321c-ec11-b6e7-0022481f8472

Ashlesha-MSFT avatar Aug 18 '25 10:08 Ashlesha-MSFT

Hi @Ashlesha-MSFT. Thx for the details.

Will it block loading scripts from such relative URLs? Because now it works with such URLs, and I can add such script source via UI in Admin Center.

And why does it show all invalid script sources, even those that are not related to the site where I try to load SPFx scripts?

For me, it looks more like a bug in a script in a browser that checks URLs and shows confusing errors from all invalid Script sources that should be available only for Admin use.

vlad-nvs avatar Aug 18 '25 11:08 vlad-nvs

Will it block loading scripts from such relative URLs? If the URL is not whitelisted, it will be blocked when the URL enforcement is applied. Currently we only write warnings without exact timeline to enforce the rules.

Because now it works with such URLs, and I can add such script source via UI in Admin Center. This is by design - currently we are not blocking any URLs yet.

And why does it show all invalid script sources, even those that are not related to the site where I try to load SPFx scripts? Can you @vlad-nvs provide a bit more input on this. What shows the invalid script sources and where? - Purview or in the Console or somewhere else. Pics are also welcome.

VesaJuvonen avatar Aug 26 '25 09:08 VesaJuvonen

Hi @VesaJuvonen.

Thx for the details.

To clarify our case:

  1. We have an SPFx package that users can install on different sites within a tenant (or even on different tenants).
  2. The SPFx solution uses server-relative URLs for scripts, and those scripts are stored in the site library where the package is installed.
  3. These script sources are automatically added to the whitelist (as expected), and we can see them in the SharePoint Admin Center → Advanced → Script sources. Since users can install the package on many sites, there can be hundreds of script sources per tenant—one per site where SPFx is deployed:
Image
  1. On any site where SPFx is installed, everything works correctly—so the server-relative URLs do load as expected. However, if we check the browser console, we see hundreds of CSP errors for script sources belonging to other sites in the tenant where the SPFx package was also installed. These errors appear even for users who do not have permission to those other sites:
Image
  1. As I understand it, the errors mean that server-relative URLs are not valid CSP sources and therefore get ignored.
  2. What’s confusing is that it’s still possible to add a server-relative URL as a trusted script source through the SharePoint Admin Center UI, and those sources do work on the site.

You mentioned that it's not blocked yet, so my questions are:

  1. Future compatibility:
  • Will SPFx solutions that use server-relative URLs continue to work once CSP starts fully blocking invalid entries?
  • Or will we need to switch to absolute URLs for script sources to avoid issues?
  1. Console errors:
  • If server-relative URLs are considered invalid, why does every user still see all of them listed in the browser console?
  • It doesn’t look like our SPFx code is attempting to load those sources—the errors seem to come from CSP validation logic itself, not from our solution.

Hope it's clear.

BR Vladyslav

vlad-nvs avatar Aug 27 '25 14:08 vlad-nvs

Hi @Ashlesha-MSFT, @VesaJuvonen.

Regarding the issue described in this ticket, I noticed that the errors have 'disappeared' for some tenants, while they are still present in the browser console for others.

However, since we have already switched to using absolute URLs in our SPFx solution and plan to adopt Automatic hosting of SPFx code in Microsoft 365 , which hosts scripts in the ClientSideAssets library, I will close this issue for now.

BR Vladyslav

vlad-nvs avatar Oct 28 '25 13:10 vlad-nvs