-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[Discover][Logs] Consistent doc content rendering #253210
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
Changes from all commits
8d9b353
6ab3429
1733890
4fd63b5
b7bf3f3
92f90ac
cff99de
a6b369d
e6a28be
0591abb
a3cf284
d848942
3d7e82c
e7a7bc3
4d903cb
6679000
187854a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| import { escapeAndPreserveHighlightTags } from './escape_preserve_highlight_tags'; | ||
|
|
||
| // Must match the html tags defined in @kbn/field-formats-plugin (html_tags.ts) | ||
| const PRE = '<mark class="ffSearch__highlight">'; | ||
| const POST = '</mark>'; | ||
|
|
||
| describe('escapeAndPreserveHighlightTags', () => { | ||
| it('escapes HTML when there are no highlight tags', () => { | ||
| expect(escapeAndPreserveHighlightTags('<hello>world</hello>')).toBe( | ||
| '<hello>world</hello>' | ||
| ); | ||
| }); | ||
|
|
||
| it('preserves highlight wrappers while escaping the content', () => { | ||
| expect(escapeAndPreserveHighlightTags(`${PRE}<hello>${POST}`)).toBe( | ||
| `${PRE}<hello>${POST}` | ||
| ); | ||
| }); | ||
|
|
||
| it('returns only escaped text when there are multiple highlight regions', () => { | ||
| expect(escapeAndPreserveHighlightTags(`${PRE}hello${POST} + ${PRE}world${POST}`)).toBe( | ||
| 'hello + world' | ||
| ); | ||
| }); | ||
|
|
||
| it('escapes plain <mark> tags that do not match the highlight format', () => { | ||
| expect(escapeAndPreserveHighlightTags('<mark><hello></mark>')).toBe( | ||
| '<mark><hello>' | ||
| ); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| import { escape } from 'lodash'; | ||
|
|
||
| // TODO: These constants are duplicated from @kbn/field-formats-plugin (html_tags.ts). | ||
| // They are kept locally because packages cannot depend on plugins. This is a temporary | ||
| // workaround until we reach an agreement on how to handle formatted/highlighted content | ||
| // across packages and plugins. | ||
| const HIGHLIGHT_PRE_TAG = '<mark class="ffSearch__highlight">'; | ||
| const HIGHLIGHT_POST_TAG = '</mark>'; | ||
| const HIGHLIGHT_TAGS_REGEX = new RegExp(`${HIGHLIGHT_PRE_TAG}|${HIGHLIGHT_POST_TAG}`, 'g'); | ||
|
Contributor
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. Do we consider ReDoS vulnerability here as a potential real threat? A potentially safer way would be to use
Contributor
Author
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. Really appreciate you taking the time to look into this! The code scanning flagged this in a previous version of this PR ( Also, this will be removed in the coming months once we implement a better approach for handling highlighting tags and rendering.
Contributor
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. Amazing, thanks for the explanation! |
||
|
|
||
| export function escapeAndPreserveHighlightTags(value: string): string { | ||
| const markTags: string[] = []; | ||
| const cleanText = value.replace(HIGHLIGHT_TAGS_REGEX, (match) => { | ||
| markTags.push(match); | ||
| return ''; | ||
| }); | ||
|
|
||
| const escapedText = escape(cleanText); | ||
|
|
||
| return markTags.length === 2 ? `${markTags[0]}${escapedText}${markTags[1]}` : escapedText; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.