-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
Allow action types to perform their own mustache variable escaping in parameter templates #83919
Merged
pmuellr
merged 6 commits into
elastic:master
from
pmuellr:actions/per-type-mustache-escaping-2
Dec 15, 2020
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4081c28
[alerts] allow action types to escape their own mustache templates
pmuellr 651eefc
address PR comment with better function test
pmuellr da7b387
merge master and fix merge conflicts
pmuellr 50a80cd
Merge branch 'master' into actions/per-type-mustache-escaping-2
kibanamachine 9e14968
add more tests, and code to fix those newly broken tests
pmuellr 589c5d8
Merge branch 'master' into actions/per-type-mustache-escaping-2
kibanamachine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ import { portSchema } from './lib/schemas'; | |
import { Logger } from '../../../../../src/core/server'; | ||
import { ActionType, ActionTypeExecutorOptions, ActionTypeExecutorResult } from '../types'; | ||
import { ActionsConfigurationUtilities } from '../actions_config'; | ||
import { renderMustacheString, renderMustacheObject } from '../lib/mustache_renderer'; | ||
|
||
export type EmailActionType = ActionType< | ||
ActionTypeConfigType, | ||
|
@@ -140,10 +141,23 @@ export function getActionType(params: GetActionTypeParams): EmailActionType { | |
secrets: SecretsSchema, | ||
params: ParamsSchema, | ||
}, | ||
renderParameterTemplates, | ||
executor: curry(executor)({ logger }), | ||
}; | ||
} | ||
|
||
function renderParameterTemplates( | ||
params: ActionParamsType, | ||
variables: Record<string, unknown> | ||
): ActionParamsType { | ||
return { | ||
// most of the params need no escaping | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good example of how to do the traditional no-escape templating first, then customize the escaping for specific parameters |
||
...renderMustacheObject(params, variables), | ||
// message however, needs to escaped as markdown | ||
message: renderMustacheString(params.message, variables, 'markdown'), | ||
}; | ||
} | ||
|
||
// action executor | ||
|
||
async function executor( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
170 changes: 170 additions & 0 deletions
170
x-pack/plugins/actions/server/lib/mustache_renderer.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { renderMustacheString, renderMustacheObject } from './mustache_renderer'; | ||
|
||
const variables = { | ||
a: 1, | ||
b: '2', | ||
c: false, | ||
d: null, | ||
e: undefined, | ||
f: { | ||
g: 3, | ||
}, | ||
lt: '<', | ||
gt: '>', | ||
amp: '&', | ||
nl: '\n', | ||
dq: '"', | ||
bt: '`', | ||
bs: '\\', | ||
st: '*', | ||
ul: '_', | ||
st_lt: '*<', | ||
}; | ||
|
||
describe('mustache_renderer', () => { | ||
describe('renderMustacheString()', () => { | ||
it('handles basic templating that does not need escaping', () => { | ||
expect(renderMustacheString('', variables, 'none')).toBe(''); | ||
expect(renderMustacheString('{{a}}', variables, 'none')).toBe('1'); | ||
expect(renderMustacheString('{{b}}', variables, 'none')).toBe('2'); | ||
expect(renderMustacheString('{{c}}', variables, 'none')).toBe('false'); | ||
expect(renderMustacheString('{{d}}', variables, 'none')).toBe(''); | ||
expect(renderMustacheString('{{e}}', variables, 'none')).toBe(''); | ||
expect(renderMustacheString('{{f.g}}', variables, 'none')).toBe('3'); | ||
}); | ||
|
||
it('handles escape:none with commonly escaped strings', () => { | ||
expect(renderMustacheString('{{lt}}', variables, 'none')).toBe(variables.lt); | ||
expect(renderMustacheString('{{gt}}', variables, 'none')).toBe(variables.gt); | ||
expect(renderMustacheString('{{amp}}', variables, 'none')).toBe(variables.amp); | ||
expect(renderMustacheString('{{nl}}', variables, 'none')).toBe(variables.nl); | ||
expect(renderMustacheString('{{dq}}', variables, 'none')).toBe(variables.dq); | ||
expect(renderMustacheString('{{bt}}', variables, 'none')).toBe(variables.bt); | ||
expect(renderMustacheString('{{bs}}', variables, 'none')).toBe(variables.bs); | ||
expect(renderMustacheString('{{st}}', variables, 'none')).toBe(variables.st); | ||
expect(renderMustacheString('{{ul}}', variables, 'none')).toBe(variables.ul); | ||
}); | ||
|
||
it('handles escape:markdown with commonly escaped strings', () => { | ||
expect(renderMustacheString('{{lt}}', variables, 'markdown')).toBe(variables.lt); | ||
expect(renderMustacheString('{{gt}}', variables, 'markdown')).toBe(variables.gt); | ||
expect(renderMustacheString('{{amp}}', variables, 'markdown')).toBe(variables.amp); | ||
expect(renderMustacheString('{{nl}}', variables, 'markdown')).toBe(variables.nl); | ||
expect(renderMustacheString('{{dq}}', variables, 'markdown')).toBe(variables.dq); | ||
expect(renderMustacheString('{{bt}}', variables, 'markdown')).toBe('\\' + variables.bt); | ||
expect(renderMustacheString('{{bs}}', variables, 'markdown')).toBe('\\' + variables.bs); | ||
expect(renderMustacheString('{{st}}', variables, 'markdown')).toBe('\\' + variables.st); | ||
expect(renderMustacheString('{{ul}}', variables, 'markdown')).toBe('\\' + variables.ul); | ||
}); | ||
|
||
it('handles triple escapes', () => { | ||
expect(renderMustacheString('{{{bt}}}', variables, 'markdown')).toBe(variables.bt); | ||
expect(renderMustacheString('{{{bs}}}', variables, 'markdown')).toBe(variables.bs); | ||
expect(renderMustacheString('{{{st}}}', variables, 'markdown')).toBe(variables.st); | ||
expect(renderMustacheString('{{{ul}}}', variables, 'markdown')).toBe(variables.ul); | ||
}); | ||
|
||
it('handles escape:slack with commonly escaped strings', () => { | ||
expect(renderMustacheString('{{lt}}', variables, 'slack')).toBe('<'); | ||
expect(renderMustacheString('{{gt}}', variables, 'slack')).toBe('>'); | ||
expect(renderMustacheString('{{amp}}', variables, 'slack')).toBe('&'); | ||
expect(renderMustacheString('{{nl}}', variables, 'slack')).toBe(variables.nl); | ||
expect(renderMustacheString('{{dq}}', variables, 'slack')).toBe(variables.dq); | ||
expect(renderMustacheString('{{bt}}', variables, 'slack')).toBe(`'`); | ||
expect(renderMustacheString('{{bs}}', variables, 'slack')).toBe(variables.bs); | ||
expect(renderMustacheString('{{st}}', variables, 'slack')).toBe('`*`'); | ||
expect(renderMustacheString('{{ul}}', variables, 'slack')).toBe('`_`'); | ||
// html escapes not needed when using backtic escaping | ||
expect(renderMustacheString('{{st_lt}}', variables, 'slack')).toBe('`*<`'); | ||
}); | ||
|
||
it('handles escape:json with commonly escaped strings', () => { | ||
expect(renderMustacheString('{{lt}}', variables, 'json')).toBe(variables.lt); | ||
expect(renderMustacheString('{{gt}}', variables, 'json')).toBe(variables.gt); | ||
expect(renderMustacheString('{{amp}}', variables, 'json')).toBe(variables.amp); | ||
expect(renderMustacheString('{{nl}}', variables, 'json')).toBe('\\n'); | ||
expect(renderMustacheString('{{dq}}', variables, 'json')).toBe('\\"'); | ||
expect(renderMustacheString('{{bt}}', variables, 'json')).toBe(variables.bt); | ||
expect(renderMustacheString('{{bs}}', variables, 'json')).toBe('\\\\'); | ||
expect(renderMustacheString('{{st}}', variables, 'json')).toBe(variables.st); | ||
expect(renderMustacheString('{{ul}}', variables, 'json')).toBe(variables.ul); | ||
}); | ||
|
||
it('handles errors', () => { | ||
expect(renderMustacheString('{{a}', variables, 'none')).toMatchInlineSnapshot( | ||
`"error rendering mustache template \\"{{a}\\": Unclosed tag at 4"` | ||
); | ||
}); | ||
}); | ||
|
||
const object = { | ||
literal: 0, | ||
literals: { | ||
a: 1, | ||
b: '2', | ||
c: true, | ||
d: null, | ||
e: undefined, | ||
eval: '{{lt}}{{b}}{{gt}}', | ||
}, | ||
list: ['{{a}}', '{{bt}}{{st}}{{bt}}'], | ||
object: { | ||
a: ['{{a}}', '{{bt}}{{st}}{{bt}}'], | ||
}, | ||
}; | ||
|
||
describe('renderMustacheObject()', () => { | ||
it('handles deep objects', () => { | ||
expect(renderMustacheObject(object, variables)).toMatchInlineSnapshot(` | ||
Object { | ||
"list": Array [ | ||
"1", | ||
"\`*\`", | ||
], | ||
"literal": 0, | ||
"literals": Object { | ||
"a": 1, | ||
"b": "2", | ||
"c": true, | ||
"d": null, | ||
"e": undefined, | ||
"eval": "<2>", | ||
}, | ||
"object": Object { | ||
"a": Array [ | ||
"1", | ||
"\`*\`", | ||
], | ||
}, | ||
} | ||
`); | ||
}); | ||
|
||
it('handles primitive objects', () => { | ||
expect(renderMustacheObject(undefined, variables)).toMatchInlineSnapshot(`undefined`); | ||
expect(renderMustacheObject(null, variables)).toMatchInlineSnapshot(`null`); | ||
expect(renderMustacheObject(0, variables)).toMatchInlineSnapshot(`0`); | ||
expect(renderMustacheObject(true, variables)).toMatchInlineSnapshot(`true`); | ||
expect(renderMustacheObject('{{a}}', variables)).toMatchInlineSnapshot(`"1"`); | ||
expect(renderMustacheObject(['{{a}}'], variables)).toMatchInlineSnapshot(` | ||
Array [ | ||
"1", | ||
] | ||
`); | ||
}); | ||
|
||
it('handles errors', () => { | ||
expect(renderMustacheObject({ a: '{{a}' }, variables)).toMatchInlineSnapshot(` | ||
Object { | ||
"a": "error rendering mustache template \\"{{a}\\": Unclosed tag at 4", | ||
} | ||
`); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the first of a new set of tests for action types with optional
renderParameterTemplates()
methods. They don't exhaustively test the escaping, as that is done in the render-specific code - it only tests that the escaping is basically happening as expected.