The process of building custom applications and tools that interact with Microsoft SharePoint, including SharePoint Online in Microsoft 365.
Hi Tim Hölsken
Thank you for reaching out to Microsoft Q&A forum
Based on your description, I have conducted some research and from my perspective view, your concerns are completely valid from a security perspective, and you are not overthinking it.
Technically, trusting localhost tenant-wide conceptually opens a small window where, if an attacker could somehow inject a reference to https://localhost:4321/malicious.js into your SharePoint environment, any user who happens to be running a compromised local server on that exact port could be vulnerable.
However, to answer your technical questions directly:
1. Is there a mechanism to enable this only in an SPFx context or for a specific site collection?
Unfortunately, NO, currently, the Content Security Policy (CSP) enforcement in SharePoint Online relies on the "Trusted Script Sources" list in the SharePoint Admin Center, which is strictly a tenant-wide setting. There is no granular control to scope trusted domains down to a specific site collection, hub site, or exclusively for the SPFx execution context.
2. Is there another approach to isolate the trustworthiness of localhost?
As far as I know, since SharePoint doesn't offer a built-in scoping mechanism for CSP, you have a few ways to approach this based on best practices:
- Use a Dedicated Developer Tenant: Microsoft's strong recommendation for SPFx development is to use a dedicated M365 Developer Tenant (which you can get for free via the Microsoft 365 Developer Program) rather than testing directly in your Production tenant. In a Dev tenant, adding
https://localhost:4321to the tenant-wide Trusted Script Sources is completely safe and is the intended workflow. You should ideally never need to testgulp servedirectly against your live Production tenant. - Remove it when not developing: If you are forced to do local development against a Production or shared Staging tenant, the only way to minimize the attack surface is operational: add
https://localhost:4321to the Trusted Script Sources when you begin a development session and actively remove it from the Admin Center when you are finished. - Use
ngrok/DevTunnels: Instead of trustinglocalhost, you can use a tunneling service likengrokor Microsoft Dev Tunnels to expose your local4321port to a secure, randomly generated public HTTPS URL. You can then add that specific URL to the Trusted Script Sources. When your development session ends, the tunnel is destroyed, and the URL becomes permanently useless, naturally isolating the trustworthiness. (Note: This does require slightly adjusting your SPFxwrite-manifests.jsonCDN path to point to the tunnel).
In the nutshell, while the theoretical risk of trusting localhost:4321 is relatively low, you are right to be cautious about tenant-wide security settings. The ultimate solution is restricting gulp serve workflows to dedicated Developer Tenants where tenant-wide settings do not impact production security.
You can read here for more information:
Support for Content Security Policy (CSP) in SharePoint Online
How SharePoint and OneDrive safeguard your data in the cloud
Hope my answer will help you.
If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.