@webitel/ui-chats 26.12.4 → 26.12.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webitel/ui-chats",
3
- "version": "26.12.4",
3
+ "version": "26.12.5",
4
4
  "description": "Reusable Webitel Frontend Code for Chats UI",
5
5
  "workspaces": [
6
6
  "../../",
@@ -0,0 +1,58 @@
1
+ import { describe, expect, it } from 'vitest';
2
+
3
+ import { mapMessageToChatMessage } from '../toChatMessage';
4
+
5
+ const UNSAFE_URL = 'javascript:alert(1)';
6
+
7
+ describe('toChatMessage attachment urls', () => {
8
+ it('drops an unsafe image url', () => {
9
+ const result = mapMessageToChatMessage({
10
+ id: '1',
11
+ images: [
12
+ {
13
+ id: 'i1',
14
+ url: UNSAFE_URL,
15
+ mime: 'image/png',
16
+ },
17
+ ],
18
+ } as never);
19
+
20
+ expect(result.file?.url).toBeUndefined();
21
+ });
22
+
23
+ it('drops an unsafe document url', () => {
24
+ const result = mapMessageToChatMessage({
25
+ id: '1',
26
+ documents: [
27
+ {
28
+ id: 'd1',
29
+ url: UNSAFE_URL,
30
+ name: 'r.pdf',
31
+ mime: 'application/pdf',
32
+ },
33
+ ],
34
+ } as never);
35
+
36
+ expect(result.file?.url).toBeUndefined();
37
+ // the name survives, so the UI can still list the attachment
38
+ expect(result.file?.name).toBe('r.pdf');
39
+ });
40
+
41
+ it('keeps a storage url', () => {
42
+ const result = mapMessageToChatMessage({
43
+ id: '1',
44
+ documents: [
45
+ {
46
+ id: 'd1',
47
+ url: 'https://example.test/storage/file/1/download',
48
+ name: 'r.pdf',
49
+ mime: 'application/pdf',
50
+ },
51
+ ],
52
+ } as never);
53
+
54
+ expect(result.file?.url).toBe(
55
+ 'https://example.test/storage/file/1/download',
56
+ );
57
+ });
58
+ });
@@ -1,5 +1,6 @@
1
1
  import type { IMessage } from '@webitel/chat-web-sdk';
2
2
 
3
+ import { toSafeAttachmentUrl } from '../../shared/scripts/isSafeAttachmentUrl';
3
4
  import type { ChatMember, ChatMessageFile, ChatMessageType } from '../../types';
4
5
 
5
6
  /**
@@ -35,7 +36,7 @@ const toTimestamp = (value: string | undefined): number => {
35
36
  const mapImage = (image: SdkImage): ChatMessageFile => ({
36
37
  id: image.id,
37
38
  mime: image.mime,
38
- url: image.url,
39
+ url: toSafeAttachmentUrl(image.url),
39
40
  });
40
41
 
41
42
  const mapDocument = (document: SdkDocument): ChatMessageFile => ({
@@ -43,7 +44,7 @@ const mapDocument = (document: SdkDocument): ChatMessageFile => ({
43
44
  name: document.name,
44
45
  size: document.size,
45
46
  mime: document.mime,
46
- url: document.url,
47
+ url: toSafeAttachmentUrl(document.url),
47
48
  });
48
49
 
49
50
  /**
@@ -0,0 +1,13 @@
1
+ import { describe, expect, it } from 'vitest';
2
+
3
+ import { isPlayableAttachmentUrl } from '../isSafeAttachmentUrl';
4
+
5
+ // scheme cases live in `@webitel/ui-sdk`; the sinks' own specs cover the wiring
6
+ describe('isPlayableAttachmentUrl', () => {
7
+ it.each([
8
+ 'https://example.test/a.mpd',
9
+ 'https://example.test/a.M3U8?token=1',
10
+ ])('refuses the stream manifest %s', (url) => {
11
+ expect(isPlayableAttachmentUrl(url)).toBe(false);
12
+ });
13
+ });
@@ -0,0 +1,18 @@
1
+ import { isSafeUrlScheme } from '@webitel/ui-sdk/scripts';
2
+
3
+ // the sender picks the URL, so `javascript:` and `data:` must not reach an href or src
4
+ export const isSafeAttachmentUrl = (url?: string | null): url is string =>
5
+ isSafeUrlScheme(url);
6
+
7
+ export const toSafeAttachmentUrl = (url?: string | null): string | undefined =>
8
+ isSafeAttachmentUrl(url) ? url : undefined;
9
+
10
+ // Vidstack picks HLS / DASH by this extension whatever the MIME and loads the library from a CDN
11
+ const STREAM_MANIFEST_URL = /\.(m3u8|mpd)($|\?)/i;
12
+
13
+ export const isPlayableAttachmentUrl = (url?: string | null): url is string =>
14
+ isSafeAttachmentUrl(url) && !STREAM_MANIFEST_URL.test(url);
15
+
16
+ export const toPlayableAttachmentUrl = (
17
+ url?: string | null,
18
+ ): string | undefined => (isPlayableAttachmentUrl(url) ? url : undefined);
@@ -0,0 +1,28 @@
1
+ import { mount } from '@vue/test-utils';
2
+ import { describe, expect, it } from 'vitest';
3
+
4
+ import ImageViewer from '../image-viewer.vue';
5
+
6
+ const imgSrc = (url: string) =>
7
+ mount(ImageViewer, {
8
+ props: {
9
+ media: {
10
+ id: 'i1',
11
+ url,
12
+ },
13
+ },
14
+ })
15
+ .find('img')
16
+ .attributes('src');
17
+
18
+ describe('ImageViewer', () => {
19
+ it('shows a safe url', () => {
20
+ expect(imgSrc('https://example.test/1.png')).toBe(
21
+ 'https://example.test/1.png',
22
+ );
23
+ });
24
+
25
+ it('leaves the image without a src for an unsafe url', () => {
26
+ expect(imgSrc('javascript:alert(1)')).toBeUndefined();
27
+ });
28
+ });
@@ -8,7 +8,7 @@
8
8
  <div class="media-viewer__content-wrapper">
9
9
  <img
10
10
  class="media-viewer__content-img"
11
- :src="media.url"
11
+ :src="safeSrc"
12
12
  :alt="media.name"
13
13
  >
14
14
  </div>
@@ -16,12 +16,17 @@
16
16
  </template>
17
17
 
18
18
  <script setup lang="ts">
19
+ import { computed } from 'vue';
20
+
21
+ import { toSafeAttachmentUrl } from '../../shared/scripts/isSafeAttachmentUrl';
19
22
  import type { ChatMessageFile } from '../messaging/types/ChatMessage.types';
20
23
 
21
- defineProps<{
24
+ const props = defineProps<{
22
25
  media: ChatMessageFile | null;
23
26
  }>();
24
27
 
28
+ const safeSrc = computed(() => toSafeAttachmentUrl(props.media?.url));
29
+
25
30
  const emit = defineEmits<{
26
31
  close: [];
27
32
  }>();
@@ -0,0 +1,130 @@
1
+ import { mount } from '@vue/test-utils';
2
+ import { afterEach, describe, expect, it, vi } from 'vitest';
3
+
4
+ import ChatMessageDocument from '../chat-message-document.vue';
5
+ import ChatMessageImage from '../chat-message-image.vue';
6
+ import ChatMessagePlayer from '../chat-message-player.vue';
7
+
8
+ vi.mock('@webitel/ui-sdk/components', async (importOriginal) => ({
9
+ ...(await importOriginal<typeof import('@webitel/ui-sdk/components')>()),
10
+ WtPlayer: {
11
+ name: 'WtPlayer',
12
+ props: [
13
+ 'src',
14
+ ],
15
+ template: '<div class="player-stub" />',
16
+ },
17
+ WtVidstackPlayer: {
18
+ name: 'WtVidstackPlayer',
19
+ props: [
20
+ 'src',
21
+ ],
22
+ template: '<div class="player-stub" />',
23
+ },
24
+ }));
25
+
26
+ const SAFE_URL = 'https://example.test/storage/file/1/download';
27
+ const UNSAFE_URL = 'javascript:alert(1)';
28
+
29
+ describe('ChatMessageDocument', () => {
30
+ const clicked: HTMLAnchorElement[] = [];
31
+
32
+ afterEach(() => {
33
+ clicked.length = 0;
34
+ vi.restoreAllMocks();
35
+ });
36
+
37
+ const clickDocument = async (url: string) => {
38
+ vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(function (
39
+ this: HTMLAnchorElement,
40
+ ) {
41
+ clicked.push(this);
42
+ });
43
+ const wrapper = mount(ChatMessageDocument, {
44
+ props: {
45
+ file: {
46
+ id: 'd1',
47
+ name: 'report.pdf',
48
+ url,
49
+ },
50
+ },
51
+ global: {
52
+ stubs: {
53
+ WtIcon: true,
54
+ },
55
+ },
56
+ });
57
+ await wrapper.trigger('click');
58
+ };
59
+
60
+ it('downloads a safe url', async () => {
61
+ await clickDocument(SAFE_URL);
62
+
63
+ expect(clicked).toHaveLength(1);
64
+ expect(clicked[0].href).toBe(SAFE_URL);
65
+ expect(clicked[0].rel).toBe('noopener noreferrer');
66
+ expect(clicked[0].download).toBe('report.pdf');
67
+ });
68
+
69
+ it('does nothing for an unsafe url', async () => {
70
+ await clickDocument(UNSAFE_URL);
71
+
72
+ expect(clicked).toEqual([]);
73
+ });
74
+ });
75
+
76
+ describe('ChatMessageImage', () => {
77
+ const imgSrc = (url: string) =>
78
+ mount(ChatMessageImage, {
79
+ props: {
80
+ file: {
81
+ id: 'i1',
82
+ url,
83
+ },
84
+ },
85
+ })
86
+ .find('img')
87
+ .attributes('src');
88
+
89
+ it('shows a safe url', () => {
90
+ expect(imgSrc(SAFE_URL)).toBe(SAFE_URL);
91
+ });
92
+
93
+ it('leaves the image without a src for an unsafe url', () => {
94
+ expect(imgSrc(UNSAFE_URL)).toBeUndefined();
95
+ });
96
+ });
97
+
98
+ describe('ChatMessagePlayer', () => {
99
+ const playerSrc = (url: string) =>
100
+ mount(ChatMessagePlayer, {
101
+ props: {
102
+ file: {
103
+ id: 'a1',
104
+ url,
105
+ },
106
+ type: 'audio/mpeg',
107
+ },
108
+ })
109
+ .findComponent({
110
+ name: 'WtPlayer',
111
+ })
112
+ .props('src');
113
+
114
+ it('plays a safe url with its type', () => {
115
+ expect(playerSrc(SAFE_URL)).toEqual({
116
+ src: SAFE_URL,
117
+ type: 'audio/mpeg',
118
+ });
119
+ });
120
+
121
+ it.each([
122
+ UNSAFE_URL,
123
+ 'https://example.test/live.m3u8',
124
+ ])('gives the player an empty source for %s', (url) => {
125
+ expect(playerSrc(url)).toEqual({
126
+ src: '',
127
+ type: 'audio/mpeg',
128
+ });
129
+ });
130
+ });
@@ -21,6 +21,7 @@
21
21
  <script setup lang="ts">
22
22
  import { prettifyFileSize } from '@webitel/ui-sdk/scripts';
23
23
  import { computed } from 'vue';
24
+ import { isSafeAttachmentUrl } from '../../../../../../shared/scripts/isSafeAttachmentUrl';
24
25
  import type { ChatMessageFile } from '../../../../types/ChatMessage.types';
25
26
 
26
27
  const props = withDefaults(
@@ -38,10 +39,14 @@ const documentSize = computed(() => {
38
39
  });
39
40
 
40
41
  function downloadDocument() {
41
- if (!props.file?.url) return;
42
+ const url = props.file?.url;
43
+
44
+ if (!isSafeAttachmentUrl(url)) return;
45
+
42
46
  const a = document.createElement('a');
43
- a.href = props.file.url;
47
+ a.href = url;
44
48
  a.target = '_blank';
49
+ a.rel = 'noopener noreferrer';
45
50
  // empty `download` lets the browser derive the filename from the URL
46
51
  a.download = props.file.name ?? '';
47
52
  a.click();
@@ -5,7 +5,7 @@
5
5
  >
6
6
  <img
7
7
  class="chat-message-image__img typo-body-1"
8
- :src="props.file.url"
8
+ :src="safeSrc"
9
9
  :alt="props.file.name"
10
10
  draggable="false"
11
11
  >
@@ -17,11 +17,15 @@
17
17
  setup
18
18
  lang="ts"
19
19
  >
20
+ import { computed } from 'vue';
21
+ import { toSafeAttachmentUrl } from '../../../../../../shared/scripts/isSafeAttachmentUrl';
20
22
  import type { ChatMessageFile } from '../../../../types/ChatMessage.types';
21
23
 
22
24
  const props = defineProps<{
23
25
  file: ChatMessageFile;
24
26
  }>();
27
+
28
+ const safeSrc = computed(() => toSafeAttachmentUrl(props.file.url));
25
29
  const emit = defineEmits<{
26
30
  open: [
27
31
  ChatMessageFile,
@@ -31,6 +31,7 @@
31
31
  import { WtPlayer, WtVidstackPlayer } from '@webitel/ui-sdk/components';
32
32
  import { ComponentSize } from '@webitel/ui-sdk/enums';
33
33
  import { computed } from 'vue';
34
+ import { toPlayableAttachmentUrl } from '../../../../../../shared/scripts/isSafeAttachmentUrl';
34
35
  import type { ChatMessageFile } from '../../../../types/ChatMessage.types';
35
36
 
36
37
  const props = defineProps<{
@@ -52,7 +53,8 @@ const isVideo = computed(() => {
52
53
  });
53
54
  const mediaSrc = computed(() => {
54
55
  return {
55
- src: props.file.streamUrl || props.file.url || '',
56
+ // an empty source, so the player has nothing to put in its download link
57
+ src: toPlayableAttachmentUrl(props.file.streamUrl || props.file.url) ?? '',
56
58
  type: props.type ?? '',
57
59
  };
58
60
  });
@@ -166,3 +166,74 @@ describe('collectGalleryImages', () => {
166
166
  ]);
167
167
  });
168
168
  });
169
+
170
+ describe('attachment url allow-list', () => {
171
+ const UNSAFE_URL = 'javascript:alert(1)';
172
+
173
+ const unsafeImage = () =>
174
+ message({
175
+ images: [
176
+ {
177
+ id: 'i1',
178
+ url: UNSAFE_URL,
179
+ mime: 'image/png',
180
+ },
181
+ ],
182
+ });
183
+
184
+ it('keeps an image with an unsafe url out of the gallery', () => {
185
+ const msg = unsafeImage();
186
+
187
+ expect(classifyAttachments(msg).images).toEqual([]);
188
+ expect(
189
+ collectGalleryImages([
190
+ msg,
191
+ ]),
192
+ ).toEqual([]);
193
+ });
194
+
195
+ it('shows an image with an unsafe url as a file card with no url', () => {
196
+ const result = classifyAttachments(unsafeImage());
197
+
198
+ expect(result.files).toHaveLength(1);
199
+ expect(result.files[0].url).toBeUndefined();
200
+ });
201
+
202
+ it('never selects a player for an unsafe url', () => {
203
+ const result = classifyAttachments(
204
+ message({
205
+ documents: [
206
+ {
207
+ id: 'd1',
208
+ url: UNSAFE_URL,
209
+ mime: 'audio/mpeg',
210
+ name: 'a.mp3',
211
+ },
212
+ ],
213
+ }),
214
+ );
215
+
216
+ expect(result.media).toEqual([]);
217
+ expect(result.files).toHaveLength(1);
218
+ expect(result.files[0].url).toBeUndefined();
219
+ // the card still names the attachment
220
+ expect(result.files[0].name).toBe('a.mp3');
221
+ });
222
+
223
+ it('still renders an image with a safe url', () => {
224
+ const result = classifyAttachments(
225
+ message({
226
+ images: [
227
+ {
228
+ id: 'i1',
229
+ url: '/api/storage/file/1/download',
230
+ mime: 'image/png',
231
+ },
232
+ ],
233
+ }),
234
+ );
235
+
236
+ expect(result.images).toHaveLength(1);
237
+ expect(result.files).toEqual([]);
238
+ });
239
+ });
@@ -1,3 +1,8 @@
1
+ import {
2
+ isPlayableAttachmentUrl,
3
+ isSafeAttachmentUrl,
4
+ toSafeAttachmentUrl,
5
+ } from '../../shared/scripts/isSafeAttachmentUrl';
1
6
  import type { ChatDocument, ChatImage, MessageModel } from '../types';
2
7
 
3
8
  export interface ChatFileCard {
@@ -42,7 +47,8 @@ const documentKey = (
42
47
  const UNDRAWABLE_IMAGE_MIME = /hei[cf]/i;
43
48
 
44
49
  export const isRenderableImage = (image: ChatImage): boolean =>
45
- !!image.url && !UNDRAWABLE_IMAGE_MIME.test(image.mime ?? '');
50
+ isSafeAttachmentUrl(image.url) &&
51
+ !UNDRAWABLE_IMAGE_MIME.test(image.mime ?? '');
46
52
 
47
53
  const mediaKind = (mime: string | undefined): 'audio' | 'video' | null => {
48
54
  const value = mime?.toLowerCase() ?? '';
@@ -96,14 +102,17 @@ export const classifyAttachments = (
96
102
  key,
97
103
  name: imageFileName(image),
98
104
  mime: image.mime,
99
- url: image.url,
105
+ // the card keeps its name; only the unsafe link is dropped
106
+ url: toSafeAttachmentUrl(image.url),
100
107
  });
101
108
  }
102
109
  });
103
110
 
104
111
  (message.documents ?? []).forEach((document, index) => {
105
112
  const key = documentKey(message, document, index);
106
- const kind = document.url ? mediaKind(document.mime) : null;
113
+ const kind = isPlayableAttachmentUrl(document.url)
114
+ ? mediaKind(document.mime)
115
+ : null;
107
116
  if (kind) {
108
117
  result.media.push({
109
118
  key,
@@ -116,7 +125,7 @@ export const classifyAttachments = (
116
125
  name: document.name ?? '',
117
126
  size: document.size,
118
127
  mime: document.mime,
119
- url: document.url,
128
+ url: toSafeAttachmentUrl(document.url),
120
129
  });
121
130
  }
122
131
  });
@@ -281,6 +281,37 @@ describe('ChatHistory', () => {
281
281
  expect(wrapper.find('.galleria-stub').exists()).toBe(true);
282
282
  });
283
283
 
284
+ it('opens a gallery download in a new tab with no opener or referrer', async () => {
285
+ const open = vi.spyOn(window, 'open').mockReturnValue(null);
286
+ const wrapper = setup({
287
+ messages: [
288
+ message({
289
+ body: undefined,
290
+ images: [
291
+ {
292
+ id: 'i1',
293
+ url: 'https://x/1.png',
294
+ },
295
+ ],
296
+ }),
297
+ ],
298
+ });
299
+
300
+ await wrapper.find('.message-attachments__image').trigger('click');
301
+ wrapper
302
+ .findComponent({
303
+ name: 'WtGalleria',
304
+ })
305
+ .vm.$emit('download', 0);
306
+
307
+ expect(open).toHaveBeenCalledWith(
308
+ 'https://x/1.png',
309
+ '_blank',
310
+ 'noopener,noreferrer',
311
+ );
312
+ open.mockRestore();
313
+ });
314
+
284
315
  // the topmost entry anchors the scroll position while older items are prepended;
285
316
  // a divider must not, as a same-day page keeps its element in place
286
317
  it('marks only message and notice rows as scroll anchors', () => {
@@ -89,6 +89,7 @@ import { computed, provide, ref, useTemplateRef, watch } from 'vue';
89
89
 
90
90
  import { useChatScroll } from '../../../shared/composables/useChatScroll';
91
91
  import { useObserveHeightUntilStable } from '../../../shared/composables/useObserveHeightUntilStable';
92
+ import { isSafeAttachmentUrl } from '../../../shared/scripts/isSafeAttachmentUrl';
92
93
  import { useChatsV2I18n } from '../../locale/useChatsV2I18n';
93
94
  import { collectGalleryImages } from '../../scripts/attachments';
94
95
  import { isSelfMessage } from '../../scripts/isSelfMessage';
@@ -211,7 +212,10 @@ provide(ChatGalleryKey, {
211
212
 
212
213
  function downloadGalleryImage(index: number) {
213
214
  const image = galleryImages.value[index];
214
- if (image) window.open(image.src, '_blank', 'noopener');
215
+ // collectGalleryImages already filters; re-checked on purpose
216
+ if (image && isSafeAttachmentUrl(image.src)) {
217
+ window.open(image.src, '_blank', 'noopener,noreferrer');
218
+ }
215
219
  }
216
220
 
217
221
  watch(
@@ -0,0 +1,73 @@
1
+ import { describe, expect, it, vi } from 'vitest';
2
+
3
+ import { message } from '../../../../scripts/__tests__/fixtures';
4
+ import type { ChatDocument } from '../../../../types';
5
+ import { mountWithChats } from '../../../__tests__/mountWithChats';
6
+ import MessageAttachments from '../message-attachments.vue';
7
+
8
+ vi.mock('@webitel/ui-sdk/components', async (importOriginal) => ({
9
+ ...(await importOriginal<typeof import('@webitel/ui-sdk/components')>()),
10
+ WtPlayer: {
11
+ name: 'WtPlayer',
12
+ props: [
13
+ 'src',
14
+ ],
15
+ template: '<div class="player-stub" />',
16
+ },
17
+ WtVidstackPlayer: {
18
+ name: 'WtVidstackPlayer',
19
+ props: [
20
+ 'src',
21
+ ],
22
+ template: '<div class="player-stub" />',
23
+ },
24
+ }));
25
+
26
+ const setup = (document: ChatDocument) =>
27
+ mountWithChats(MessageAttachments, {
28
+ props: {
29
+ message: message({
30
+ documents: [
31
+ document,
32
+ ],
33
+ }),
34
+ },
35
+ });
36
+
37
+ describe('MessageAttachments', () => {
38
+ it('gives the audio player a safe url with its type', () => {
39
+ const url = 'https://example.test/a.mp3';
40
+ const wrapper = setup({
41
+ id: 'd1',
42
+ url,
43
+ mime: 'audio/mpeg',
44
+ name: 'a.mp3',
45
+ });
46
+
47
+ expect(
48
+ wrapper
49
+ .findComponent({
50
+ name: 'WtPlayer',
51
+ })
52
+ .props('src'),
53
+ ).toEqual({
54
+ src: url,
55
+ type: 'audio/mpeg',
56
+ });
57
+ });
58
+
59
+ it('shows a stream manifest as a download link, not a player', () => {
60
+ const url = 'https://example.test/live.m3u8';
61
+ const wrapper = setup({
62
+ id: 'd1',
63
+ url,
64
+ mime: 'audio/mpeg',
65
+ name: 'live.m3u8',
66
+ });
67
+
68
+ expect(wrapper.find('.player-stub').exists()).toBe(false);
69
+ expect(wrapper.find('.message-attachments__file').attributes('href')).toBe(
70
+ url,
71
+ );
72
+ });
73
+ });