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

Error: Subresource Integrity: The resource styles.css has an integrity attribute... #1436

Closed
jhm-ciberman opened this issue May 1, 2024 · 2 comments

Comments

@jhm-ciberman
Copy link

Horizon Version

v5.24.3

Laravel Version

11.x

PHP Version

8.3

Redis Driver

PhpRedis

Redis Version

Database Driver & Version

Description

Brief

If you configure your ASSET_URL: (https://laravel.com/docs/11.x/vite#custom-base-urls)

ASSET_URL=https://cdn.example.com

Then the Horizon CSS won't load at all:

Chrome Dev Console:

Subresource Integrity: The resource 'https://cdn.example.com/vendor/horizon/styles.css' has an integrity attribute, but the resource requires the request to be CORS enabled to check the integrity, and it is not. The resource has been blocked because the integrity cannot be enforced.
Subresource Integrity: The resource 'https://cdn.example.com/vendor/horizon/styles-dark.css' has an integrity attribute, but the resource requires the request to be CORS enabled to check the integrity, and it is not. The resource has been blocked because the integrity cannot be enforced.

Description of the bug

The problem relies in that Horizon uses a new instance of \Illuminate\Foundation\Vite for injecting the CSS and javascript:

$viteDataSchemeLight = new ViteFoundation();
$viteDataSchemeLight->useHotFile($nonExistentFileName);
$viteDataSchemeLight->useStyleTagAttributes([
'data-scheme' => 'light',
]);

The problem, is that this generates a tag like this:

<link rel="preload" as="style" href="https://cdn.example.com/vendor/horizon/styles.css" integrity="sha384-4HOmv1E51xgqbUYzCYXaRXPRja5nEho6eq/L/CKs0LlidzTGNTk81VtCAHqLiYSC" />
<link rel="stylesheet" href="https://cdn.example.com/vendor/horizon/styles.css" integrity="sha384-4HOmv1E51xgqbUYzCYXaRXPRja5nEho6eq/L/CKs0LlidzTGNTk81VtCAHqLiYSC" data-scheme="light" />

In order for the browser to check the data integrity and use the integrity tag:

  1. The CDN (or Url where the CSS file is hosted) must send the CORS headers (Access-Control-Allow-Origin: *). ✅
  2. The crossorigin attribute must be defined in the <link> tag and set to anonymous. ❌

How To Fix

Simply add 'crossorigin' => 'anonymous' to both tags:

$viteDataSchemeDark = new ViteFoundation();
$viteDataSchemeDark->useHotFile($nonExistentFileName);
$viteDataSchemeDark->useStyleTagAttributes([
'data-scheme' => 'dark',
+ 'crossorigin' => 'anonymous',
]);

Steps To Reproduce

  1. Configure your ASSET_URL to point to a CDN:
ASSET_URL=https://cdn.example.com

Ensure the CDN is sending the proper CORS headers.

  1. Open Horizon. The CSS won't load.
@jhm-ciberman
Copy link
Author

Looks like a duplicate of #1425

Closing and waiting for the next release.

@jhm-ciberman
Copy link
Author

jhm-ciberman commented May 2, 2024

@driesvints is this fixed in master? Currently the Horizon UI is unusable for me in production.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant