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

propose fix for CORS issues with missing crossorigin tag #56

Merged
merged 19 commits into from
Apr 10, 2019
Merged

propose fix for CORS issues with missing crossorigin tag #56

merged 19 commits into from
Apr 10, 2019

Conversation

PhilETaylor
Copy link
Contributor

@PhilETaylor PhilETaylor commented Apr 1, 2019

This fixes #55 and comes with the unit tests too ;-)

updated:

This PR also allows the value for crossorigin attribute to be defined in the configuration yaml

@Lyrkan
Copy link
Contributor

Lyrkan commented Apr 1, 2019

Hi @PhilETaylor,

Indeed, that's an issue in a cross-domain context (which is not always the case).

I'm not against adding crossorigin="anonymous" but are we sure that other people with different setups won't want/need to use other (or no) values for that attribute?

I would 100% agree with using anonymous as the default value if there was also a way to override it, either by adding something to enableIntegrityHashes or directly when calling {{ encore_entry_script_tags }} (kinda related: #10)

@PhilETaylor
Copy link
Contributor Author

PhilETaylor commented Apr 1, 2019

Indeed, that's an issue in a cross-domain context (which is not always the case).

Its the case 100% of the time if you are using a CDN to serve your assets :)

I dont have any strong opinion either way, however when I upgraded and deployed to production (which uses a cdn) it broke my site.. hence I had to work out why, document it, and find a solution.. which I have proposed.

The current situation is that anyone that follow what I did, updates, enables integrity, and then deploys with assets from a cdn will break their site...

@PhilETaylor
Copy link
Contributor Author

PhilETaylor commented Apr 1, 2019

there are only two options really for the crossorigin attribute. https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_settings_attributes

anonymous and use-credentials

I've never seen anyone use the latter... but at the moment this feature is BROKEN because deploying assets on a CDN with the integrity attribute and no crossorigin attribute WILL BREAK THE PRODUCTION SITE! (break == asset loading will be blocked by Google Chrome)

I would say a quick fix would be the hard coding of crossorigin as proposed by this PR and then further discussion on making it configurable can be had later, it seems #10 has been open since last year with no progress and you cant expect to leave this feature broken for the majority while someone makes that decision

@PhilETaylor
Copy link
Contributor Author

ok with 768a820 I have now made the value of crossorigin attribute configurable :-)

if none is set in the config, then it still defaults to anonymous

PLEASE TEST!

@PhilETaylor
Copy link
Contributor Author

flex recipe also updated here symfony/recipes#562

src/DependencyInjection/Configuration.php Outdated Show resolved Hide resolved
src/DependencyInjection/WebpackEncoreExtension.php Outdated Show resolved Hide resolved
src/Resources/config/services.xml Outdated Show resolved Hide resolved
@PhilETaylor
Copy link
Contributor Author

@stof Thanks for the comments... coding on the sofa on laptop is not my best place to code :) also still learning some of the deep symfony stuff :) ... "it works" is not enough :) I'll go grab a coffee and update this PR with your comments - thanks for the feedback

@PhilETaylor
Copy link
Contributor Author

PhilETaylor commented Apr 1, 2019

@stof c304692 has changes based on your feedback, only thing Im not sure is if the services xml should be

<argument/>

or the repetition of the default value like

<argument>anonymous</argument>

I could not find any examples to learn from on this :)

@PhilETaylor
Copy link
Contributor Author

ok I have settled on

<argument /><!-- crossorigin -->

as I found that example in symfony/security-bundle like that :)

@PhilETaylor
Copy link
Contributor Author

This is ready for testing/feedback again now :)

src/DependencyInjection/Configuration.php Outdated Show resolved Hide resolved
src/Asset/TagRenderer.php Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
@PhilETaylor
Copy link
Contributor Author

@Lyrkan all your feedback has now been applied. Ready for testing/feedback/merging again now

@PhilETaylor
Copy link
Contributor Author

flex recipe also updated here symfony/recipes#562

src/Asset/TagRenderer.php Outdated Show resolved Hide resolved
src/DependencyInjection/WebpackEncoreExtension.php Outdated Show resolved Hide resolved
src/Resources/config/services.xml Outdated Show resolved Hide resolved
Copy link
Contributor

@Lyrkan Lyrkan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me :)

Maybe we're just missing some test cases for the default and explicit false values?

@PhilETaylor
Copy link
Contributor Author

PhilETaylor commented Apr 2, 2019

In 7eeb941 I have reverted (and renamed) one of the tests so that it renders with no configured attributes, this is a quick win and covers the TagRenderer class only, I guess what is really needed is some coverage of the WebpackEncoreExtension final class to check if the configuration attributes resolve correctly but there was no existing test and im not in the mood for that challenge today, sorry :) :)

I have also notices that LOADS of DocBlocks are missing on methods in this bundle - any ideas if that is on purpose or is that another quick win we could make using phpStorm to generate these missing DocBlocks?

@Lyrkan
Copy link
Contributor

Lyrkan commented Apr 2, 2019

I guess what is really needed is some coverage of the WebpackEncoreExtension final class to check if the configuration attributes resolve correctly but there was no existing test

The extension is tested by the IntegrationTest:

$container->loadFromExtension('webpack_encore', [
'output_path' => __DIR__.'/fixtures/build',
'cache' => true,
'builds' => [
'different_build' => __DIR__.'/fixtures/different_build'
]
]);

I have also notices that LOADS of DocBlocks are missing on methods in this bundle - any ideas if that is on purpose or is that another quick win we could make using phpStorm to generate these missing DocBlocks?

Do you have an example of what you'd add? Most methods are already typed (params and return) so it wouldn't be really useful to add the same thing using PHPDoc.

Anyway, this should probably be in a separate PR to avoid mixing those changes with the ones here :)

@PhilETaylor
Copy link
Contributor Author

I'm just not used to seeing code without the additional PHPDoc for every method :-) I'll ignore that for now, phpStorm is great at reading the typehints so doesnt need the documentation to build its index anyway

I have added the default crossorigin = false in the IntegrationTest, the tests pass with it set to false, or not there at all.

I guess this is ready for merging ?

@weaverryan
Copy link
Member

Thanks for researching and fixing this Phil - excellent PR!

@weaverryan weaverryan merged commit ecad299 into symfony:master Apr 10, 2019
weaverryan added a commit that referenced this pull request Apr 10, 2019
…(PhilETaylor, weaverryan)

This PR was merged into the master branch.

Discussion
----------

propose fix for CORS issues with missing crossorigin tag

This fixes #55 and comes with the unit tests too ;-)

updated:

This PR also allows the value for crossorigin attribute to be defined in the configuration yaml

Commits
-------

ecad299 Merge branch 'master' into patch-1
04d8091 small docs tweak
95f7d27 making arg optional for BC
fee8b91 add default attribute set to false in tests
7eeb941 revert the default test so that it tests with no attribute array
a74dee0 tidy up service xml to remove keyed collection
fadd432 Apply codestyle from php-cs-fixer fix --config=.php_cs.dist
0b607f7 refactor based on @stof feedback
5eac7b3 correct documentation of configuration
2fb1d3d Fix unit tests
22c971c Allow for no attribute by default, and allow it to be configurable and future proofed array
d1866c5 <argument /><!-- crossorigin -->
c304692 refine as per feedback
2f83a66 remove additional spaces
4a6325e update documentation
768a820 Allow value for crossorigin attribute to be configurable, defaults to anonymous
9f1e101 fix tests
efdc276 fix tests for  crossorigin="anonymous"
99e8bce propose fix for CORS issues with missing crossorigin tag
@PhilETaylor
Copy link
Contributor Author

🎉

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

Successfully merging this pull request may close these issues.

CORS issues with new integrity feature.
4 participants