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

set CSP header in FastBoot #113

Merged
merged 5 commits into from
Oct 8, 2019

Conversation

jelhan
Copy link
Collaborator

@jelhan jelhan commented Sep 16, 2019

Let me give a quick overview of the architecture. Especially cause I'm not that used to FastBoot development and there might be problems that I have missed.

  • Addon provides part of it's build-time configuration (reportOnly and the calculated policy string) as run-time configuration by returning it from config hook. Therefore the calculation was moved from included hook to config hook.
  • It provides an instance-initializer, which runs only in FastBoot. That one reads the run-time configuration and sets the CSP header using the fastboot service.
  • If running ember serve not only FastBoot but also the registered express middleware in serverMiddleware hook sets CSP header. Test is using fastboot-app-server to prevent false-positives.

There are some potential improvements, which would reduce bundle size:

  • Run-time configuration should only be provided if addon has ember-cli-fastboot as a dependency.
  • There is no need to provide the policy string through run-time configuration if it's already available as CSP meta tag.

Documented that ones as todos in code. To be honest I don't think that few bytes would make a real impact on performance. 😇

This lays the foundation for manipulation of the policy per request as required by #67 and jelhan/ember-style-modifier#11.

Open questions:

  • Should there be an option to disable setting CSP headers in FastBoot?

@jelhan jelhan mentioned this pull request Sep 16, 2019
10 tasks
@jelhan jelhan force-pushed the fastboot-support branch 2 times, most recently from 8f4db5a to 0b9ee61 Compare September 16, 2019 20:36
@jelhan jelhan changed the title set CSP header in FastBoot WIP: set CSP header in FastBoot Sep 16, 2019
@jelhan
Copy link
Collaborator Author

jelhan commented Sep 16, 2019

Need to check what is going on with the tests. They seem to be flickering. Added WIP. Sorry for the noise.


// reads addon config stored in meta element
function readAddonConfig(appInstance) {
let config = appInstance.resolveRegistration('config:environment');
Copy link
Member

Choose a reason for hiding this comment

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

Doesn't this read the config from the "legacy" location (in config/environment.js)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

You are right that it reads the run-time configuration, which is normally provided by consumer via config/environment.js. But in this case the configuration is meant to be provided by addon's config hook only. It's a subset of addons configuration. It only includes reportOnly option and the build policy string. Both are required at run-time for FastBoot support.

addon/instance-initializers/content-security-policy.js Outdated Show resolved Hide resolved
@@ -19,11 +19,12 @@
"start": "ember serve",
"test": "ember test",
"test:all": "ember try:each",
"test:node": "mocha node-tests/**"
"test:node": "for i in node-tests/*/*; do mocha $i; done"
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

There seems to be strange issues with ember-cli-addon-tests triggered only if multiple tests are executed at the same time and also depending on the order in which the tests are executed. While this is definitely not a good solution it gets us unblocked here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Reported this upstream: tomdale/ember-cli-addon-tests#215

Copy link
Member

Choose a reason for hiding this comment

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

Eeck, seems fine but that sounds like it was pretty gnarly to track down.

@jelhan jelhan changed the title WIP: set CSP header in FastBoot set CSP header in FastBoot Sep 28, 2019
@jelhan
Copy link
Collaborator Author

jelhan commented Sep 28, 2019

This is ready to be merged if we accept the ugly hack for CI. There is one question still open, which I would love to get some feedback.

Should there be an option to disable setting CSP headers in FastBoot?

I'm tending towards no, cause we have the enabled and delievery options. Both could be used to not set headers in FastBoot. I'm not sure if I miss a scenario in which development server should set CSP headers but FastBoot should not.

@rwjblue @sandstrom Do you have any input on that one?

@sandstrom
Copy link
Collaborator

sandstrom commented Sep 30, 2019

@jelhan I don't think it makes sense with dedicated config for disabling CSP under Fastboot. If that need arises, one could simply use the existing enabled option to disable.

I know too little about fastboot to review the code though, sorry about that!

@jelhan
Copy link
Collaborator Author

jelhan commented Oct 2, 2019

@rwjblue Any objections merging this one?

Copy link
Member

@rwjblue rwjblue left a comment

Choose a reason for hiding this comment

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

Sorry for the review delay here, seems good generally (left only a couple of inline comments).

I'd also like to see some information added to the README.md RE: how this is to be used in practice.

fastboot/instance-initializers/content-security-policy.js Outdated Show resolved Hide resolved
@@ -19,11 +19,12 @@
"start": "ember serve",
"test": "ember test",
"test:all": "ember try:each",
"test:node": "mocha node-tests/**"
"test:node": "for i in node-tests/*/*; do mocha $i; done"
Copy link
Member

Choose a reason for hiding this comment

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

Eeck, seems fine but that sounds like it was pretty gnarly to track down.

@jelhan
Copy link
Collaborator Author

jelhan commented Oct 6, 2019

Added some documentation in 42b1e83. There isn't much to say about the default usage as this should just work.

index.js Show resolved Hide resolved
index.js Show resolved Hide resolved
DELIVERY_HEADER and DELIVERY_META const wasn't used consistently. Also they
increased the code length and didn't improved readability.
@rwjblue rwjblue merged commit d70da99 into adopted-ember-addons:master Oct 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants