Commit 395e1724 authored by Dheeraj Joshi's avatar Dheeraj Joshi

Remove v-html from NoteForm Component

This is a refactor for removing potentially
dangerous v-html from the codebase. This switches
to using secure alternative: GlSprintf and GlLink

Thanks to @Toumash for their contributions
parent a0d10fed
<script> <script>
import { GlButton } from '@gitlab/ui'; import { GlButton, GlSprintf, GlLink } from '@gitlab/ui';
import { mapGetters, mapActions, mapState } from 'vuex'; import { mapGetters, mapActions, mapState } from 'vuex';
import { getDraft, updateDraft } from '~/lib/utils/autosave'; import { getDraft, updateDraft } from '~/lib/utils/autosave';
import { mergeUrlParams } from '~/lib/utils/url_utility'; import { mergeUrlParams } from '~/lib/utils/url_utility';
import { __, sprintf } from '~/locale'; import { __ } from '~/locale';
import markdownField from '~/vue_shared/components/markdown/field.vue'; import markdownField from '~/vue_shared/components/markdown/field.vue';
import glFeatureFlagsMixin from '~/vue_shared/mixins/gl_feature_flags_mixin'; import glFeatureFlagsMixin from '~/vue_shared/mixins/gl_feature_flags_mixin';
import eventHub from '../event_hub'; import eventHub from '../event_hub';
...@@ -17,6 +17,8 @@ export default { ...@@ -17,6 +17,8 @@ export default {
markdownField, markdownField,
CommentFieldLayout, CommentFieldLayout,
GlButton, GlButton,
GlSprintf,
GlLink,
}, },
mixins: [glFeatureFlagsMixin(), issuableStateMixin, resolvable], mixins: [glFeatureFlagsMixin(), issuableStateMixin, resolvable],
props: { props: {
...@@ -203,16 +205,12 @@ export default { ...@@ -203,16 +205,12 @@ export default {
); );
}, },
changedCommentText() { changedCommentText() {
return sprintf( return {
__( text: __(
'This comment changed after you started editing it. Review the %{startTag}updated comment%{endTag} to ensure information is not lost.', 'This comment changed after you started editing it. Review the %{startTag}updated comment%{endTag} to ensure information is not lost.',
), ),
{ placeholder: { link: ['startTag', 'endTag'] },
startTag: `<a href="${this.noteHash}" target="_blank" rel="noopener noreferrer">`, };
endTag: '</a>',
},
false,
);
}, },
}, },
watch: { watch: {
...@@ -318,11 +316,13 @@ export default { ...@@ -318,11 +316,13 @@ export default {
<template> <template>
<div ref="editNoteForm" class="note-edit-form current-note-edit-form js-discussion-note-form"> <div ref="editNoteForm" class="note-edit-form current-note-edit-form js-discussion-note-form">
<div <div v-if="conflictWhileEditing" class="js-conflict-edit-warning alert alert-danger">
v-if="conflictWhileEditing" <gl-sprintf :message="changedCommentText.text" :placeholders="changedCommentText.placeholder">
class="js-conflict-edit-warning alert alert-danger" <template #link="{ content }">
v-html="changedCommentText /* eslint-disable-line vue/no-v-html */" <gl-link :href="noteHash" target="_blank">{{ content }}</gl-link>
></div> </template>
</gl-sprintf>
</div>
<div class="flash-container timeline-content"></div> <div class="flash-container timeline-content"></div>
<form :data-line-code="lineCode" class="edit-note common-note-form js-quick-submit gfm-form"> <form :data-line-code="lineCode" class="edit-note common-note-form js-quick-submit gfm-form">
<comment-field-layout <comment-field-layout
......
import { GlLink } from '@gitlab/ui';
import { mount } from '@vue/test-utils'; import { mount } from '@vue/test-utils';
import { nextTick } from 'vue'; import { nextTick } from 'vue';
import batchComments from '~/batch_comments/stores/modules/batch_comments'; import batchComments from '~/batch_comments/stores/modules/batch_comments';
...@@ -91,6 +92,7 @@ describe('issue_note_form component', () => { ...@@ -91,6 +92,7 @@ describe('issue_note_form component', () => {
expect(conflictWarning.exists()).toBe(true); expect(conflictWarning.exists()).toBe(true);
expect(conflictWarning.text().replace(/\s+/g, ' ').trim()).toBe(message); expect(conflictWarning.text().replace(/\s+/g, ' ').trim()).toBe(message);
expect(conflictWarning.find(GlLink).attributes('href')).toBe('#note_545');
}); });
}); });
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment