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

[@kbn/handlebars] Add support for partials #150151

Merged
merged 3 commits into from
Feb 2, 2023

Conversation

watson
Copy link
Contributor

@watson watson commented Feb 2, 2023

Add support for partials to our own implementation of the handlebars template engine. The most important reason for this change is support for inline partials, which is a prerequisite for later changing default value of csp.disableUnsafeEval from false to true.

Closes #139068

Release notes

Added support for "inline partials" in handlebars templates when csp.disableUnsafeEval is set to true. This allows users who rely on inline partials to remove the unsafe-eval source expression in the Kibana Content Security Policy (CSP).

@watson watson added release_note:enhancement backport:all-open Backport to all branches that could still receive a release labels Feb 2, 2023
@watson watson requested a review from a team as a code owner February 2, 2023 10:44
@watson watson self-assigned this Feb 2, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Feb 2, 2023

Documentation preview:

@watson watson added backport:prev-minor Backport to (8.x) the previous minor version (i.e. one version back from main) and removed backport:all-open Backport to all branches that could still receive a release labels Feb 2, 2023
@watson
Copy link
Contributor Author

watson commented Feb 2, 2023

Current CI failures are unrelated to this PR as main is currently not passing CI.

Copy link
Member

@azasypkin azasypkin left a comment

Choose a reason for hiding this comment

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

Code LGMT! I couldn't confidently review all intricacies of the Handlebars logic due to lack of the specific knowledge, but as I said elsewhere, for this specific fork it feels reasonable to rely on the extensive test coverage that you provided.

packages/kbn-handlebars/index.ts Outdated Show resolved Hide resolved
packages/kbn-handlebars/index.ts Outdated Show resolved Hide resolved
@@ -747,6 +875,9 @@ class ElasticHandlebarsVisitor extends Handlebars.Visitor {
);
}

// inherit partials from parent program
runtimeOptions.partials = runtimeOptions.partials || this.runtimeOptions!.partials;
Copy link
Member

Choose a reason for hiding this comment

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

optional nit: I see we have multiple places in this function where we rely on !, wondering if it'd make sense to move this.runtimeOptions! to a local variable and use it instead? Unless I'm missing something...

      // It should be defined because ...
      const currentRuntimeOptions = this.runtimeOptions!;
runtimeOptions.data = runtimeOptions.data || currentRuntimeOptions.data;
      if (runtimeOptions.blockParams) {
        runtimeOptions.blockParams = runtimeOptions.blockParams.concat(
          currentRuntimeOptions.blockParams
        );
      }

      // inherit partials from parent program
      runtimeOptions.partials = runtimeOptions.partials || currentRuntimeOptions.partials;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, it's because runtimeOptions isn't set in the constructor, but in render, which technically means it's possible to get to this point in the code before it's set. But only theoretically as render is ALWAYS called before anything else, so we can just treat it as if it is always set.

We use this.runtimeOptions! all over the code, not only in this function, so I would prefer an approach that satisfied the entire class than to simply add a local variable in each function. Otherwise I prefer the ! approach.

@@ -82,6 +83,18 @@ class HandlebarsTestBench {
return this;
}

withPartial(name: string | number, partial: Handlebars.Template) {
Copy link
Member

Choose a reason for hiding this comment

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

question: Is there a valid use case for name to be a number?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

})
.withHelper('partial', () => 'missing')
.withPartial('dude', '{{name}} ({{url}}) ')
.toThrow('The partial missing could not be found'); // TODO: Is there a way we can test that the error is of type `Handlebars.Exception`?
Copy link
Member

Choose a reason for hiding this comment

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

question: Cannot Jest's toThrow (inside test_bench) perform instanceof check? https://jestjs.io/docs/expect#tothrowerror

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately only if you give up checking the message. You can either do:

.toThrow('The partial missing could not be found')

Or:

.toThrow(Handlebars.Exception)

You could technically do:

.toThrow(new Handlebars.Exception('The partial missing could not be found'))

But that is according to my research technically equivalent to just checking the string value as it doesn't seem to perform an instanceof check.

But maybe I'm missing a completely other option. However, it's not super important.

Copy link
Contributor Author

@watson watson Feb 2, 2023

Choose a reason for hiding this comment

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

I guess I could make some custom logic that after the toThrow check, does a expect(e.message).toEqual(...) check 🤷 (but that quickly becomes overly complicated, which is probably why I didn't do it)

@watson watson enabled auto-merge (squash) February 2, 2023 18:56
@watson watson merged commit 2b82cb7 into elastic:main Feb 2, 2023
@kibana-ci
Copy link
Collaborator

💚 Build Succeeded

Metrics [docs]

Public APIs missing comments

Total count of every public API that lacks a comment. Target amount is 0. Run node scripts/build_api_docs --plugin [yourplugin] --stats comments for more detailed information.

id before after diff
@kbn/handlebars 12 14 +2

Async chunks

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

id before after diff
canvas 1022.3KB 1023.9KB +1.7KB
uiActionsEnhanced 135.1KB 136.8KB +1.7KB
visTypeTimeseries 506.2KB 507.9KB +1.7KB
total +5.1KB
Unknown metric groups

API count

id before after diff
@kbn/handlebars 19 21 +2

ESLint disabled line counts

id before after diff
@kbn/handlebars 4 5 +1

Total ESLint disabled count

id before after diff
@kbn/handlebars 8 9 +1

History

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

cc @watson

kibanamachine pushed a commit to kibanamachine/kibana that referenced this pull request Feb 2, 2023
Add support for [partials](https://handlebarsjs.com/guide/partials.html)
to our own implementation of the [handlebars](https://handlebarsjs.com)
template engine.

Closes elastic#139068

(cherry picked from commit 2b82cb7)
@kibanamachine
Copy link
Contributor

💚 All backports created successfully

Status Branch Result
8.6

Note: Successful backport PRs will be merged automatically after passing CI.

Questions ?

Please refer to the Backport tool documentation

@watson watson deleted the handlebars-partials branch February 2, 2023 20:39
kibanamachine added a commit that referenced this pull request Feb 2, 2023
# Backport

This will backport the following commits from `main` to `8.6`:
- [[@kbn/handlebars] Add support for partials
(#150151)](#150151)

<!--- Backport version: 8.9.7 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Thomas
Watson","email":"[email protected]"},"sourceCommit":{"committedDate":"2023-02-02T19:59:09Z","message":"[@kbn/handlebars]
Add support for partials (#150151)\n\nAdd support for
[partials](https://handlebarsjs.com/guide/partials.html)\r\nto our own
implementation of the [handlebars](https://handlebarsjs.com)\r\ntemplate
engine.\r\n\r\nCloses
#139068","sha":"2b82cb7fa24e019a3717b60abbe0f814b5ddcd5a","branchLabelMapping":{"^v8.7.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:enhancement","backport:prev-minor","v8.7.0"],"number":150151,"url":"https://github.com/elastic/kibana/pull/150151","mergeCommit":{"message":"[@kbn/handlebars]
Add support for partials (#150151)\n\nAdd support for
[partials](https://handlebarsjs.com/guide/partials.html)\r\nto our own
implementation of the [handlebars](https://handlebarsjs.com)\r\ntemplate
engine.\r\n\r\nCloses
#139068","sha":"2b82cb7fa24e019a3717b60abbe0f814b5ddcd5a"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v8.7.0","labelRegex":"^v8.7.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/150151","number":150151,"mergeCommit":{"message":"[@kbn/handlebars]
Add support for partials (#150151)\n\nAdd support for
[partials](https://handlebarsjs.com/guide/partials.html)\r\nto our own
implementation of the [handlebars](https://handlebarsjs.com)\r\ntemplate
engine.\r\n\r\nCloses
#139068","sha":"2b82cb7fa24e019a3717b60abbe0f814b5ddcd5a"}}]}]
BACKPORT-->

Co-authored-by: Thomas Watson <[email protected]>
@KOTungseth KOTungseth added the Team:Security Team focused on: Auth, Users, Roles, Spaces, Audit Logging, and more! label Feb 13, 2023
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-security (Team:Security)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:prev-minor Backport to (8.x) the previous minor version (i.e. one version back from main) release_note:enhancement Team:Security Team focused on: Auth, Users, Roles, Spaces, Audit Logging, and more! v8.6.2 v8.7.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support inline partials in @kbn/handlebars
6 participants