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

fix(editor): Avoid sanitizing output to search node data #8126

Merged
merged 9 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions cypress/e2e/5-ndv.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -490,4 +490,31 @@ describe('NDV', () => {
ndv.getters.nodeVersion().should('have.text', 'Function node version 1 (Deprecated)');
ndv.actions.close();
});

it('Should render xml and html tags as strings and can search', () => {
cy.createFixtureWorkflow('Test_workflow_xml_output.json', `test`);

workflowPage.actions.executeWorkflow();

workflowPage.actions.openNode('Edit Fields');

ndv.getters.outputDisplayMode().find('[class*=active]').should('contain', 'Table');

ndv.getters.outputTableRow(1).should('include.text', '<?xml version="1.0" encoding="UTF-8"?> <library>');

cy.document().trigger('keyup', { key: '/' });
ndv.getters.searchInput().filter(':focus').type('<lib');

ndv.getters.outputTableRow(1).find('mark').should('have.text', '<lib')

ndv.getters.outputDisplayMode().find('label').eq(1).should('include.text', 'JSON');
ndv.getters.outputDisplayMode().find('label').eq(1).click();

ndv.getters.outputDataContainer().should('have.text', '[{"body": "<?xml version="1.0" encoding="UTF-8"?> <library> <book> <title>Introduction to XML</title> <author>John Doe</author> <publication_year>2020</publication_year> <isbn>1234567890</isbn> </book> <book> <title>Data Science Basics</title> <author>Jane Smith</author> <publication_year>2019</publication_year> <isbn>0987654321</isbn> </book> <book> <title>Programming in Python</title> <author>Bob Johnson</author> <publication_year>2021</publication_year> <isbn>5432109876</isbn> </book> </library>"}]');
ndv.getters.outputDataContainer().find('mark').should('have.text', '<lib')

ndv.getters.outputDisplayMode().find('label').eq(2).should('include.text', 'Schema');
ndv.getters.outputDisplayMode().find('label').eq(2).click({force: true});
ndv.getters.outputDataContainer().findChildByTestId('run-data-schema-item').find('> span').should('include.text', '<?xml version="1.0" encoding="UTF-8"?>');
});
});
53 changes: 53 additions & 0 deletions cypress/fixtures/Test_workflow_xml_output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"meta": {
"instanceId": "2d1cf27f75b18bb9e146336f791c37884f4fc7ddb97c2def27c0444d106778bf"
},
"nodes": [
{
"parameters": {},
"id": "8108d313-8b03-4aa4-963d-cd1c0fe8f85c",
"name": "When clicking \"Execute Workflow\"",
"type": "n8n-nodes-base.manualTrigger",
"typeVersion": 1,
"position": [
420,
220
]
},
{
"parameters": {
"fields": {
"values": [
{
"name": "body",
"stringValue": "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <library> <book> <title>Introduction to XML</title> <author>John Doe</author> <publication_year>2020</publication_year> <isbn>1234567890</isbn> </book> <book> <title>Data Science Basics</title> <author>Jane Smith</author> <publication_year>2019</publication_year> <isbn>0987654321</isbn> </book> <book> <title>Programming in Python</title> <author>Bob Johnson</author> <publication_year>2021</publication_year> <isbn>5432109876</isbn> </book> </library>"
}
]
},
"options": {}
},
"id": "45888152-7c5f-4d88-9039-660c594da084",
"name": "Edit Fields",
"type": "n8n-nodes-base.set",
"typeVersion": 3.2,
"position": [
640,
220
]
}
],
"connections": {
"When clicking \"Execute Workflow\"": {
"main": [
[
{
"node": "Edit Fields",
"type": "main",
"index": 0
}
]
]
}
},
"pinData": {}
}
22 changes: 13 additions & 9 deletions packages/editor-ui/src/components/RunDataJson.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
@update:selectedValue="selectedJsonPath = $event"
>
<template #renderNodeKey="{ node }">
<span
<TextWithHighlights
:content="getContent(node.key)"
:search="search"
data-target="mappable"
:data-value="getJsonParameterPath(node.path)"
:data-name="node.key"
Expand All @@ -43,13 +45,18 @@
[$style.mappable]: mappingEnabled,
[$style.dragged]: draggingPath === node.path,
}"
v-html="highlightSearchTerm(node.key)"
/>
</template>
<template #renderNodeValue="{ node }">
<span v-if="isNaN(node.index)" v-html="highlightSearchTerm(node.content)" />
<span
<TextWithHighlights
v-if="isNaN(node.index)"
:content="getContent(node.content)"
:search="search"
/>
<TextWithHighlights
v-else
:content="getContent(node.content)"
:search="search"
data-target="mappable"
:data-value="getJsonParameterPath(node.path)"
:data-name="getListItemName(node.path)"
Expand All @@ -60,7 +67,6 @@
[$style.dragged]: draggingPath === node.path,
}"
class="ph-no-capture"
v-html="highlightSearchTerm(node.content)"
/>
</template>
</vue-json-pretty>
Expand All @@ -76,7 +82,6 @@ import type { IDataObject, INodeExecutionData } from 'n8n-workflow';
import Draggable from '@/components/Draggable.vue';
import { executionDataToJson } from '@/utils/nodeTypesUtils';
import { isString } from '@/utils/typeGuards';
import { highlightText, sanitizeHtml } from '@/utils/htmlUtils';
import { shorten } from '@/utils/typesUtils';
import type { INodeUi } from '@/Interface';
import { mapStores } from 'pinia';
Expand All @@ -86,6 +91,7 @@ import { getMappedExpression } from '@/utils/mappingUtils';
import { useWorkflowsStore } from '@/stores/workflows.store';
import { nonExistingJsonPath } from '@/constants';
import { useExternalHooks } from '@/composables/useExternalHooks';
import TextWithHighlights from './TextWithHighlights.vue';

const RunDataJsonActions = defineAsyncComponent(
async () => import('@/components/RunDataJsonActions.vue'),
Expand All @@ -98,6 +104,7 @@ export default defineComponent({
Draggable,
RunDataJsonActions,
MappingPill,
TextWithHighlights,
},
props: {
editMode: {
Expand Down Expand Up @@ -202,9 +209,6 @@ export default defineComponent({
getListItemName(path: string): string {
return path.replace(/^(\["?\d"?]\.?)/g, '');
},
highlightSearchTerm(value: string): string {
return sanitizeHtml(highlightText(this.getContent(value), this.search));
},
},
});
</script>
Expand Down
21 changes: 13 additions & 8 deletions packages/editor-ui/src/components/RunDataSchemaItem.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<script lang="ts" setup>
import { computed } from 'vue';
import type { INodeUi, Schema } from '@/Interface';
import { highlightText, sanitizeHtml } from '@/utils/htmlUtils';
import { checkExhaustive } from '@/utils/typeGuards';
import { shorten } from '@/utils/typesUtils';
import { getMappedExpression } from '@/utils/mappingUtils';
import TextWithHighlights from './TextWithHighlights.vue';

type Props = {
schema: Schema;
Expand All @@ -30,12 +30,8 @@ const isFlat = computed(
props.schema.value.every((v) => !Array.isArray(v.value)),
);
const key = computed((): string | undefined => {
const highlightedKey = sanitizeHtml(highlightText(props.schema.key, props.search));
return isSchemaParentTypeArray.value ? `[${highlightedKey}]` : highlightedKey;
return isSchemaParentTypeArray.value ? `[${props.schema.key}]` : props.schema.key;
});
const parentKey = computed((): string | undefined =>
sanitizeHtml(highlightText(props.parent.key, props.search)),
);
const schemaName = computed(() =>
isSchemaParentTypeArray.value ? `${props.schema.type}[${props.schema.key}]` : props.schema.key,
);
Expand Down Expand Up @@ -99,8 +95,17 @@ const getIconBySchemaType = (type: Schema['type']): string => {
data-target="mappable"
>
<font-awesome-icon :icon="getIconBySchemaType(schema.type)" size="sm" />
<span v-if="isSchemaParentTypeArray" v-html="parentKey" />
<span v-if="key" :class="{ [$style.arrayIndex]: isSchemaParentTypeArray }" v-html="key" />
<TextWithHighlights
v-if="isSchemaParentTypeArray"
:content="props.parent?.key"
:search="props.search"
/>
<TextWithHighlights
v-if="key"
:class="{ [$style.arrayIndex]: isSchemaParentTypeArray }"
:content="key"
:search="props.search"
/>
</span>
</div>
<span v-if="text" :class="$style.text">{{ text }}</span>
Expand Down
19 changes: 9 additions & 10 deletions packages/editor-ui/src/components/RunDataTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
[$style.draggingHeader]: isDragging,
}"
>
<span v-html="highlightSearchTerm(column || '')" />
<TextWithHighlights :content="getValueToRender(column || '')" :search="search" />

Check failure on line 55 in packages/editor-ui/src/components/RunDataTable.vue

View workflow job for this annotation

GitHub Actions / Lint changes

Replace `·:content="getValueToRender(column·||·'')"·:search="search"·` with `⏎↹↹↹↹↹↹↹↹↹↹↹:content="getValueToRender(column·||·'')"⏎↹↹↹↹↹↹↹↹↹↹↹:search="search"⏎↹↹↹↹↹↹↹↹↹↹`
<div :class="$style.dragButton">
<font-awesome-icon icon="grip-vertical" />
</div>
Expand Down Expand Up @@ -117,10 +117,11 @@
@mouseleave="onMouseLeaveCell"
:class="hasJsonInColumn(index2) ? $style.minColWidth : $style.limitColWidth"
>
<span
<TextWithHighlights
v-if="isSimple(data)"
:content="getValueToRender(data)"
:search="search"
:class="{ [$style.value]: true, [$style.empty]: isEmpty(data) }"
v-html="highlightSearchTerm(data)"
/>
<n8n-tree :nodeClass="$style.nodeClass" v-else :value="data">
<template #label="{ label, path }">
Expand All @@ -141,9 +142,10 @@
>
</template>
<template #value="{ value }">
<span
<TextWithHighlights
:content="getValueToRender(value)"
:search="search"
:class="{ [$style.nestedValue]: true, [$style.empty]: isEmpty(value) }"
v-html="highlightSearchTerm(value)"
/>
</template>
</n8n-tree>
Expand All @@ -162,7 +164,6 @@
import { mapStores } from 'pinia';
import type { INodeUi, ITableData, NDVState } from '@/Interface';
import { shorten } from '@/utils/typesUtils';
import { highlightText, sanitizeHtml } from '@/utils/htmlUtils';
import { getPairedItemId } from '@/utils/pairedItemUtils';
import type { GenericValue, IDataObject, INodeExecutionData } from 'n8n-workflow';
import Draggable from './Draggable.vue';
Expand All @@ -171,14 +172,15 @@
import MappingPill from './MappingPill.vue';
import { getMappedExpression } from '@/utils/mappingUtils';
import { useExternalHooks } from '@/composables/useExternalHooks';
import TextWithHighlights from './TextWithHighlights.vue';

const MAX_COLUMNS_LIMIT = 40;

type DraggableRef = InstanceType<typeof Draggable>;

export default defineComponent({
name: 'run-data-table',
components: { Draggable, MappingPill },
components: { Draggable, MappingPill, TextWithHighlights },
props: {
node: {
type: Object as PropType<INodeUi>,
Expand Down Expand Up @@ -392,9 +394,6 @@
}
return value;
},
highlightSearchTerm(value: string): string {
return sanitizeHtml(highlightText(this.getValueToRender(value), this.search));
},
onDragStart() {
this.draggedColumn = true;
this.ndvStore.resetMappingTelemetry();
Expand Down
39 changes: 39 additions & 0 deletions packages/editor-ui/src/components/TextWithHighlights.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<script lang="ts" setup>
import type { PropType } from 'vue';
import { GenericValue } from 'n8n-workflow';

Check failure on line 3 in packages/editor-ui/src/components/TextWithHighlights.vue

View workflow job for this annotation

GitHub Actions / Lint changes

All imports in the declaration are only used as types. Use `import type`

const props = defineProps({
content: {
type: [Object, String, Number] as PropType<GenericValue>,
},
search: {
type: String,
},
});

const splitTextBySearch = (text = '', search = ''): { tag: 'span' | 'mark'; content: string }[] => {

Check failure on line 14 in packages/editor-ui/src/components/TextWithHighlights.vue

View workflow job for this annotation

GitHub Actions / Lint changes

Array type using 'T[]' is forbidden for non-simple types. Use 'Array<T>' instead
mutdmour marked this conversation as resolved.
Show resolved Hide resolved
if (!search) {
return [
{
tag: 'span',
content: text,
},
];
}

const pattern = new RegExp(`(${search})`, 'g');
mutdmour marked this conversation as resolved.
Show resolved Hide resolved
const splitText = text.split(new RegExp(pattern, 'gi'));

return splitText.map((t) => ({ tag: pattern.test(t) ? 'mark' : 'span', content: t }));
mutdmour marked this conversation as resolved.
Show resolved Hide resolved
mutdmour marked this conversation as resolved.
Show resolved Hide resolved
};
</script>

<template>
<span v-if="props.search && typeof props.content === 'string'">
Copy link
Contributor

Choose a reason for hiding this comment

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

What happens if the content is not a string? We are providing the search box but nothing will be highlighted?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it actually works with both boolean and number
Screenshot 2023-12-22 at 13 48 59

<template v-for="part in splitTextBySearch(props.content, props.search)">
mutdmour marked this conversation as resolved.
Show resolved Hide resolved
<mark v-if="part.tag === 'mark' && part.content">{{ part.content }}</mark>

Check failure on line 34 in packages/editor-ui/src/components/TextWithHighlights.vue

View workflow job for this annotation

GitHub Actions / Lint changes

Elements in iteration expect to have 'v-bind:key' directives
mutdmour marked this conversation as resolved.
Show resolved Hide resolved
<span v-else-if="part.content">{{ part.content }}</span>

Check failure on line 35 in packages/editor-ui/src/components/TextWithHighlights.vue

View workflow job for this annotation

GitHub Actions / Lint changes

Elements in iteration expect to have 'v-bind:key' directives
mutdmour marked this conversation as resolved.
Show resolved Hide resolved
</template>
</span>
<span v-else>{{ props.content }}</span>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { shallowMount } from '@vue/test-utils';
import TextWithHighlights from '@/components/TextWithHighlights.vue';

describe('TextWithHighlights', () => {
it('highlights the search text in the content', () => {
const wrapper = shallowMount(TextWithHighlights, {
props: {
content: 'Test content',
search: 'Test',
},
});

expect(wrapper.html()).toContain('<mark>Test</mark>');
expect(wrapper.html()).toContain('<span> content</span>');
});

it('renders correctly when search is not set', () => {
const wrapper = shallowMount(TextWithHighlights, {
props: {
content: 'Test content',
},
});

expect(wrapper.html()).toEqual('<span>Test content</span>');
expect(wrapper.html()).not.toContain('<mark>');
});

it('renders correctly numbers when search is not set', () => {
const wrapper = shallowMount(TextWithHighlights, {
props: {
content: 1,
},
});

expect(wrapper.html()).toEqual('<span>1</span>');
expect(wrapper.html()).not.toContain('<mark>');
});

it('renders correctly objects when search is not set', () => {
const wrapper = shallowMount(TextWithHighlights, {
props: {
content: { hello: 'world' },
},
});

expect(wrapper.html()).toEqual('<span>{\n "hello": "world"\n}</span>');
expect(wrapper.html()).not.toContain('<mark>');
});

it('renders correctly objects ignoring search', () => {
const wrapper = shallowMount(TextWithHighlights, {
props: {
content: { hello: 'world' },
search: 'yo',
},
});

expect(wrapper.html()).toEqual('<span>{\n "hello": "world"\n}</span>');
expect(wrapper.html()).not.toContain('<mark>');
});

it('highlights the search text in middle of the content', () => {
const wrapper = shallowMount(TextWithHighlights, {
props: {
content: 'Test content hello world',
search: 'con',
},
});

expect(wrapper.html()).toEqual(
'<span><span>Test </span><mark>con</mark><span>tent hello world</span></span>',
);
});
});
Loading
Loading