Skip to content

Commit

Permalink
fix: Render valid urls only in custom attributes (#3921)
Browse files Browse the repository at this point in the history
  • Loading branch information
muhsin-k committed Feb 7, 2022
1 parent 047070a commit 9f37a6e
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 21 deletions.
7 changes: 5 additions & 2 deletions app/javascript/dashboard/components/CustomAttribute.vue
Expand Up @@ -63,7 +63,7 @@
rel="noopener noreferrer"
class="value"
>
{{ value || '---' }}
{{ urlValue }}
</a>
<p v-else class="value">
{{ displayValue || '---' }}
Expand Down Expand Up @@ -119,7 +119,7 @@ import format from 'date-fns/format';
import { required, url } from 'vuelidate/lib/validators';
import { BUS_EVENTS } from 'shared/constants/busEvents';
import MultiselectDropdown from 'shared/components/ui/MultiselectDropdown.vue';
import { isValidURL } from '../helper/URLHelper';
const DATE_FORMAT = 'yyyy-MM-dd';
export default {
Expand Down Expand Up @@ -184,6 +184,9 @@ export default {
isAttributeTypeDate() {
return this.attributeType === 'date';
},
urlValue() {
return isValidURL(this.value) ? this.value : '---';
},
notAttributeTypeCheckboxAndList() {
return !this.isAttributeTypeCheckbox && !this.isAttributeTypeList;
},
Expand Down
6 changes: 6 additions & 0 deletions app/javascript/dashboard/helper/URLHelper.js
Expand Up @@ -37,3 +37,9 @@ export const accountIdFromPathname = pathname => {
const accountId = isScoped ? Number(urlParam) : '';
return accountId;
};

export const isValidURL = value => {
/* eslint-disable no-useless-escape */
const URL_REGEX = /^https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)$/gm;
return URL_REGEX.test(value);
};
10 changes: 10 additions & 0 deletions app/javascript/dashboard/helper/specs/URLHelper.spec.js
Expand Up @@ -2,6 +2,7 @@ import {
frontendURL,
conversationUrl,
accountIdFromPathname,
isValidURL,
} from '../URLHelper';

describe('#URL Helpers', () => {
Expand Down Expand Up @@ -48,4 +49,13 @@ describe('#URL Helpers', () => {
expect(accountIdFromPathname('')).toBe('');
});
});

describe('isValidURL', () => {
it('should return true if valid url is passed', () => {
expect(isValidURL('https://chatwoot.com')).toBe(true);
});
it('should return false if invalid url is passed', () => {
expect(isValidURL('alert.window')).toBe(false);
});
});
});
9 changes: 2 additions & 7 deletions app/javascript/dashboard/mixins/attributeMixin.js
@@ -1,5 +1,5 @@
import { mapGetters } from 'vuex';

import { isValidURL } from '../helper/URLHelper';
export default {
computed: {
...mapGetters({
Expand Down Expand Up @@ -63,16 +63,11 @@ export default {
Number.isInteger(Number(attributeValue)) && Number(attributeValue) > 0
);
},
isAttributeLink(attributeValue) {
/* eslint-disable no-useless-escape */
const URL_REGEX = /^https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)$/gm;
return URL_REGEX.test(attributeValue);
},
attributeDisplayType(attributeValue) {
if (this.isAttributeNumber(attributeValue)) {
return 'number';
}
if (this.isAttributeLink(attributeValue)) {
if (isValidURL(attributeValue)) {
return 'link';
}
return 'text';
Expand Down
12 changes: 0 additions & 12 deletions app/javascript/dashboard/mixins/specs/attributeMixin.spec.js
Expand Up @@ -92,18 +92,6 @@ describe('attributeMixin', () => {
expect(wrapper.vm.attributeDisplayType(9988)).toBe('number');
});

it('return true if link is passed', () => {
const Component = {
render() {},
title: 'TestComponent',
mixins: [attributeMixin],
};
const wrapper = shallowMount(Component, { store, localVue });
expect(wrapper.vm.isAttributeLink('https://www.chatwoot.com/pricing')).toBe(
true
);
});

it('return true if number is passed', () => {
const Component = {
render() {},
Expand Down

0 comments on commit 9f37a6e

Please sign in to comment.