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

Use @emotion/server for server-side security prompts #142662

Merged
merged 4 commits into from
Oct 4, 2022

Conversation

cee-chen
Copy link
Member

@cee-chen cee-chen commented Oct 4, 2022

Summary

closes #124490

In #134919, EUI updated Security's prompt_page template to correctly support the CSS of Emotion-converted components. At the time, styling was working and Emotion was outputting its CSS styles into <body> tags. At some point between then (June) and now, Emotion styles stopped rendering correctly / as expected.

The solution is to this is to utilize @emotion/server for server-side rendering. In addition to fixing the now-broken/missing Emotion styles, this also has the added benefits of:

  • Ensuring Emotion styles are in the <head> and not the <body>, reducing potential CSS specificity issues
  • Gets rid of server-side warnings/logs complaining about :first-child/:nth-child selectors
  • Only loads critical CSS necessary for that page, which is a nice microperf bonus

This PR also incidentally updates all Kibana's various @emotion dependencies to the latest minors.

Before

After

@cee-chen cee-chen added release_note:skip Skip the PR/issue when compiling release notes v8.5.0 labels Oct 4, 2022
@cee-chen cee-chen requested a review from a team as a code owner October 4, 2022 19:30
Comment on lines +96 to +97
{/* eslint-disable-next-line react/no-danger */}
<style dangerouslySetInnerHTML={{ __html: `</style>${emotionStyles}` }} />
Copy link
Member Author

@cee-chen cee-chen Oct 4, 2022

Choose a reason for hiding this comment

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

A quick explanation on why this creates a blank <style></style> tag:

  1. Since Emotion is returning its styles as one giant HTML string (including specific classes/attributes on its <style> tags that we likely should not ignore), we need to use dangerouslySetInnerHTML

  2. The number of children allowed within the <head> tag is limited, e.g. no <div>s or otherwise handy 'grouping' type tag. If you try to use a tag that normally does not have children, e.g. the <meta> tag, Kibana/React will error with [ERROR][http.server.Kibana] Error: meta is a void element tag and must neither have `children` nor use `dangerouslySetInnerHTML`.

  3. style was therefore the tag that made the most sense, but causes weird/unnecessary nesting behavior, hence the prepended </style>. See this StackOverflow answer for how this works. This ends up creating a blank <style></style> tag, but that really isn't the worst thing in the world, and is an unfortunate necessary evil until React supports dangerouslySetInnerHTML on fragments.

@cee-chen
Copy link
Member Author

cee-chen commented Oct 4, 2022

@kc13greiner Does this need to be backported to 8.4? Not sure if Ops would have any issue with that considering the extra dependency/dependency upgrades, but in general they shouldn't be risky.

Copy link
Contributor

@kc13greiner kc13greiner left a comment

Choose a reason for hiding this comment

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

LGTM!

@legrego
Copy link
Member

legrego commented Oct 4, 2022

Does this need to be backported to 8.4?

No, 8.5 is sufficient here. The regression was introduced via #140323, which wasn't backported to 8.4 as far as I can tell.

Thanks for working on this fix for us, @constancecchen!

@cee-chen cee-chen enabled auto-merge (squash) October 4, 2022 21:28
@cee-chen cee-chen merged commit 899081a into elastic:main Oct 4, 2022
@kibana-ci
Copy link
Collaborator

💚 Build Succeeded

Metrics [docs]

Module Count

Fewer modules leads to a faster build time

id before after diff
dashboard 385 390 +5
dataViewManagement 176 181 +5
discover 529 534 +5
expressionGauge 81 86 +5
expressionHeatmap 113 118 +5
expressionPartitionVis 95 100 +5
expressionXY 145 150 +5
kibanaOverview 101 106 +5
lens 922 927 +5
maps 996 1001 +5
visualizations 352 357 +5
total +55

Async chunks

Total size of all lazy-loaded chunks that will be downloaded as the user navigates the app

id before after diff
dashboard 418.3KB 418.7KB +406.0B
dataViewManagement 148.3KB 148.6KB +391.0B
discover 478.2KB 478.2KB +19.0B
expressionGauge 18.0KB 18.3KB +376.0B
expressionHeatmap 90.7KB 91.1KB +376.0B
expressionPartitionVis 50.6KB 51.0KB +376.0B
expressionXY 118.0KB 118.4KB +376.0B
kibanaOverview 46.3KB 46.7KB +396.0B
lens 1.3MB 1.3MB +411.0B
maps 2.6MB 2.6MB +406.0B
visualizations 239.4KB 239.8KB +406.0B
total +3.8KB

Page load bundle

Size of the bundles that are downloaded on every page load. Target size is below 100kb

id before after diff
charts 45.3KB 45.3KB +3.0B
discover 26.7KB 27.0KB +376.0B
kbnUiSharedDeps-npmDll 5.0MB 5.0MB +1.1KB
total +1.5KB
Unknown metric groups

ESLint disabled line counts

id before after diff
security 20 21 +1

Total ESLint disabled count

id before after diff
security 22 23 +1

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

@cee-chen cee-chen deleted the eui-emotion-ssr branch October 4, 2022 22:15
@kibanamachine
Copy link
Contributor

💔 All backports failed

Status Branch Result
8.5 Backport failed because of merge conflicts

Manual backport

To create the backport manually run:

node scripts/backport --pr 142662

Questions ?

Please refer to the Backport tool documentation

@cee-chen
Copy link
Member Author

cee-chen commented Oct 4, 2022

@legrego @kc13greiner I think I lost track of my FF releases 🙈 Is 8.5 the one that just went into FF and 8.6 is latest main? If so, do we need to backport this manually to 8.5?

edit: backported #142689

cee-chen pushed a commit that referenced this pull request Oct 5, 2022
…2689)

* Update all `@emotion` dependencies to latest

* Install `@emotion/server`

* Use Emotion server-side rendering for security prompt pages

* snapshots

(cherry picked from commit 899081a)
@legrego
Copy link
Member

legrego commented Oct 5, 2022

I think I lost track of my FF releases 🙈 Is 8.5 the one that just went into FF and 8.6 is latest main? If so, do we need to backport this manually to 8.5?

It looks like you figured this out already, but to close the loop: Yes, 8.5 just went into FF, so main represents 8.6.

WafaaNasr pushed a commit to WafaaNasr/kibana that referenced this pull request Oct 11, 2022
* Update all `@emotion` dependencies to latest

* Install `@emotion/server`

* Use Emotion server-side rendering for security prompt pages

* snapshots
WafaaNasr pushed a commit to WafaaNasr/kibana that referenced this pull request Oct 14, 2022
* Update all `@emotion` dependencies to latest

* Install `@emotion/server`

* Use Emotion server-side rendering for security prompt pages

* snapshots
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release_note:skip Skip the PR/issue when compiling release notes v8.5.0 v8.6.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Styles for "You do not have permission" screen are broken
5 participants